Skip to content

Commit d94b6e4

Browse files
committed
Fix incorrect building commands for devserver
1 parent 362de6a commit d94b6e4

File tree

2 files changed

+47
-31
lines changed

2 files changed

+47
-31
lines changed

.github/actions/src/load-artifacts/__tests__/artifact.test.ts

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ArtifactNotFoundError } from '@actions/artifact';
12
import * as core from '@actions/core';
23
import * as exec from '@actions/exec';
34
import * as manifest from '@sourceacademy/modules-repotools/manifest';
@@ -10,10 +11,10 @@ vi.mock(import('@actions/core'), async importOriginal => {
1011
...original,
1112
// Mock these functions to remove stdout output
1213
error: vi.fn(),
13-
info: () => {},
14-
startGroup: () => {},
14+
info: () => { },
15+
startGroup: () => { },
1516
setFailed: vi.fn(),
16-
endGroup: () => {}
17+
endGroup: () => { }
1718
};
1819
});
1920
const mockedResolveAllTabs = vi.spyOn(manifest, 'resolveAllTabs');
@@ -41,6 +42,7 @@ test('tab resolution errors cause setFailed to be called', async () => {
4142

4243
await main();
4344

45+
expect(mockedResolveAllTabs).toHaveBeenCalledOnce();
4446
expect(core.error).toHaveBeenCalledExactlyOnceWith('error1');
4547
expect(core.setFailed).toHaveBeenCalledExactlyOnceWith('Tab resolution failed with errors');
4648
});
@@ -68,24 +70,28 @@ test('tabs that can\'t be found are built', async () => {
6870
if (name === 'Tab0-tab') {
6971
return { artifact: { id: 0 } };
7072
}
71-
throw new Error();
73+
throw new ArtifactNotFoundError();
7274
});
7375

7476
await main();
7577

7678
expect(mockedGetArtifact).toHaveBeenCalledTimes(2);
79+
expect(mockedResolveAllTabs).toHaveBeenCalledOnce();
80+
7781
const [[artifactCall0], [artifactCall1]] = mockedGetArtifact.mock.calls;
7882
expect(artifactCall0).toEqual('Tab0-tab');
7983
expect(artifactCall1).toEqual('Tab1-tab');
8084

8185
expect(exec.exec).toHaveBeenCalledTimes(2);
82-
const [[,execCall0], [,execCall1]] = vi.mocked(exec.exec).mock.calls;
83-
console.log('args are', execCall0);
86+
const [[execCmd0, execCall0], [execCmd1, execCall1]] = vi.mocked(exec.exec).mock.calls;
87+
88+
expect(execCmd0).toEqual('yarn workspaces focus');
8489
expect(execCall0).toContain('@sourceacademy/tab-Tab1');
8590
expect(execCall0).not.toContain('@sourceacademy/tab-Tab0');
8691

87-
expect(execCall1).toContain('@sourceacademy/tab-Tab1');
88-
expect(execCall1).not.toContain('@sourceacademy/tab-Tab0');
92+
expect(execCmd1).toEqual('yarn workspaces foreach -pA');
93+
expect(execCall1).toContain('--include @sourceacademy/tab-Tab1');
94+
expect(execCall1).not.toContain('--include @sourceacademy/tab-Tab0');
8995
});
9096

9197
test('install failure means build doesn\'t happen', async () => {
@@ -101,11 +107,12 @@ test('install failure means build doesn\'t happen', async () => {
101107
}
102108
});
103109

104-
mockedGetArtifact.mockRejectedValueOnce(new Error());
105-
110+
mockedGetArtifact.mockRejectedValueOnce(new ArtifactNotFoundError());
106111
mockedExec.mockResolvedValueOnce(1);
112+
107113
await main();
108114

115+
expect(mockedGetArtifact).toHaveBeenCalledOnce();
109116
expect(exec.exec).toHaveBeenCalledOnce();
110-
expect(core.setFailed).toHaveBeenCalledExactlyOnceWith('yarn workspace focus failed for Tab0');
117+
expect(core.setFailed).toHaveBeenCalledExactlyOnceWith('yarn workspace focus failed');
111118
});
Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pathlib from 'path';
2-
import { DefaultArtifactClient } from '@actions/artifact';
2+
import { ArtifactNotFoundError, DefaultArtifactClient } from '@actions/artifact';
33
import * as core from '@actions/core';
44
import { exec } from '@actions/exec';
55
import { bundlesDir, outDir, tabsDir } from '@sourceacademy/modules-repotools/getGitRoot';
@@ -19,40 +19,49 @@ export async function main() {
1919
return;
2020
}
2121

22-
const tabPromises = Object.keys(tabsResult.tabs).map(async tabName => {
22+
const tabPromises = Object.keys(tabsResult.tabs).map(async (tabName): Promise<string | null> => {
2323
try {
2424
const { artifact: { id } } = await artifact.getArtifact(`${tabName}-tab`);
2525
await artifact.downloadArtifact(id, { path: pathlib.join(outDir, 'tabs') });
2626
core.info(`Downloaded artifact for ${tabName}`);
27-
return;
27+
return null;
2828
} catch (error) {
29-
core.error(`Error retrieving artifact for ${tabName}, will try building`);
30-
core.error(error as Error);
29+
if (!(error instanceof ArtifactNotFoundError)) {
30+
throw error;
31+
}
32+
core.error(`Error retrieving artifact for ${tabName}, need to try building`);
33+
return tabName;
3134
}
35+
});
3236

33-
// Artifact could not be found, we probably need to build it
34-
const focusExitCode = await exec('yarn', ['workspaces', 'focus', `@sourceacademy/tab-${tabName}`]);
35-
if (focusExitCode !== 0) {
36-
core.setFailed(`yarn workspace focus failed for ${tabName}`);
37-
return;
38-
}
37+
// Artifacts could not be found, we probably need to build it
38+
const tabsToBuild = (await Promise.all(tabPromises)).filter(x => x !== null);
39+
if (tabsToBuild.length === 0) return;
3940

40-
const buildExitCode = await exec('yarn', ['workspaces', 'foreach', '-A', '--include', `@sourceacademy/tab-${tabName}`, 'run', 'build']);
41-
if (buildExitCode !== 0) {
42-
core.setFailed(`Building ${tabName} failed`);
43-
return;
44-
}
45-
core.info(`Built ${tabName}`);
46-
});
41+
// focus all at once
42+
const workspaces = tabsToBuild.map(each => `@sourceacademy/tab-${each}`);
43+
const focusExitCode = await exec('yarn workspaces focus', workspaces, { silent: false });
44+
if (focusExitCode !== 0) {
45+
core.setFailed('yarn workspace focus failed');
46+
return;
47+
}
4748

48-
await Promise.all(tabPromises);
49+
const workspaceBuildArgs = workspaces.map(each => `--include ${each}`);
50+
const buildExitCode = await exec(
51+
'yarn workspaces foreach -pA',
52+
[...workspaceBuildArgs, 'run', 'build'],
53+
{ silent: false }
54+
);
55+
if (buildExitCode !== 0) {
56+
core.setFailed('Building tabs failed');
57+
}
4958
}
5059

5160
if (process.env.GITHUB_ACTIONS) {
5261
// Only automatically execute when running in github actions
5362
try {
5463
await main();
5564
} catch (error: any) {
56-
core.setFailed(error.message);
65+
core.setFailed(error);
5766
}
5867
}

0 commit comments

Comments
 (0)