Skip to content

Commit 4aa230b

Browse files
committed
test: update test case
1 parent 9df4533 commit 4aa230b

File tree

4 files changed

+199
-51
lines changed

4 files changed

+199
-51
lines changed
Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,83 @@
1-
import { ExecutorContext } from '@nx/devkit';
2-
3-
import { PublishDevcontainerFeatureExecutorSchema } from './schema';
41
import executor from './executor';
2+
import { ExecutorContext, logger } from '@nx/devkit';
3+
import { execFileSync } from 'child_process';
54

6-
const options: PublishDevcontainerFeatureExecutorSchema = {};
7-
const context: ExecutorContext = {
8-
root: '',
9-
cwd: process.cwd(),
10-
isVerbose: false,
11-
projectGraph: {
12-
nodes: {},
13-
dependencies: {},
14-
},
15-
projectsConfigurations: {
16-
projects: {},
17-
version: 2,
5+
jest.mock('@nx/devkit', () => ({
6+
logger: {
7+
fatal: jest.fn(),
8+
error: jest.fn(),
189
},
19-
nxJsonConfiguration: {},
20-
};
10+
}));
11+
12+
jest.mock('child_process', () => ({
13+
execFileSync: jest.fn(),
14+
}));
15+
16+
describe('runExecutor', () => {
17+
const context: ExecutorContext = {
18+
root: '/workspace-root',
19+
cwd: '',
20+
isVerbose: false,
21+
projectName: 'test-project',
22+
projectGraph: {
23+
nodes: {},
24+
dependencies: {},
25+
},
26+
projectsConfigurations: {
27+
projects: {
28+
'test-project': {
29+
root: '/workspace-root/project-root',
30+
targets: {},
31+
},
32+
},
33+
version: 2,
34+
},
35+
nxJsonConfiguration: {},
36+
};
37+
38+
afterEach(() => {
39+
jest.resetAllMocks();
40+
});
41+
42+
it('should execute the publish command successfully', async () => {
43+
(execFileSync as jest.Mock).mockImplementation(() => {});
44+
45+
const result = await executor({}, context);
46+
47+
expect(result).toEqual({ success: true });
48+
expect(execFileSync).toHaveBeenCalledWith(
49+
'npx',
50+
[
51+
'devcontainer',
52+
'features',
53+
'publish',
54+
'--registry',
55+
'ghcr.io',
56+
'--namespace',
57+
'ebizbase/devcontainer-features',
58+
'/workspace-root/project-root/src/test-project',
59+
],
60+
{ cwd: '/workspace-root', stdio: 'inherit' }
61+
);
62+
});
63+
64+
it('should log an error and return failure when ProjectUtils throws', async () => {
65+
const result = await executor({}, { ...context, projectName: undefined });
66+
expect(result).toEqual({ success: false });
67+
expect(logger.fatal).toHaveBeenCalledWith('No project name provided', expect.any(Error));
68+
expect(execFileSync).not.toHaveBeenCalled();
69+
});
70+
71+
it('should handle errors from execFileSync gracefully', async () => {
72+
(execFileSync as jest.Mock).mockImplementation(() => {
73+
throw new Error('Command failed');
74+
});
2175

22-
describe('PublishDevcontainerFeature Executor', () => {
23-
it('can run', async () => {
24-
const output = await executor(options, context);
25-
expect(output.success).toBe(true);
76+
const result = await executor({}, context);
77+
expect(result).toEqual({ success: false });
78+
expect(logger.fatal).toHaveBeenCalledWith(
79+
'Error while executing the publish command',
80+
expect.any(Error)
81+
);
2682
});
2783
});

tools/nx-internal/src/executors/publish-devcontainer-feature/executor.ts

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,36 @@ import { ProjectUtils } from '@ebizbase/nx-devkit';
33
import { execFileSync } from 'child_process';
44
import { PublishDevcontainerFeatureExecutorSchema } from './schema';
55

6-
const runExecutor: PromiseExecutor<PublishDevcontainerFeatureExecutorSchema> = async (_options, context) => {
6+
const runExecutor: PromiseExecutor<PublishDevcontainerFeatureExecutorSchema> = async (
7+
_options,
8+
context
9+
) => {
710
let projectUtils;
811
try {
912
projectUtils = new ProjectUtils(context);
1013
} catch (error: unknown) {
1114
logger.fatal('No project name provided', error);
1215
return { success: false };
1316
}
14-
const commands = ['npx', 'devcontainer', 'features', 'publish', '--registry', 'ghcr.io', '--namespace', 'ebizbase/devcontainer-features', projectUtils.getProjectRoot() + '/src/' + projectUtils.getProjectName()];
17+
const commands = [
18+
'npx',
19+
'devcontainer',
20+
'features',
21+
'publish',
22+
'--registry',
23+
'ghcr.io',
24+
'--namespace',
25+
'ebizbase/devcontainer-features',
26+
projectUtils.getProjectRoot() + '/src/' + projectUtils.getProjectName(),
27+
];
1528

16-
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
17-
18-
return { success: true };
29+
try {
30+
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
31+
return { success: true };
32+
} catch (error) {
33+
logger.fatal('Error while executing the publish command', error);
34+
return { success: false };
35+
}
1936
};
2037

2138
export default runExecutor;
Lines changed: 76 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,82 @@
1-
import { ExecutorContext } from '@nx/devkit';
2-
3-
import { TestDevcontainerFeatureExecutorSchema } from './schema';
41
import executor from './executor';
2+
import { ExecutorContext, logger } from '@nx/devkit';
3+
import { execFileSync } from 'child_process';
54

6-
const options: TestDevcontainerFeatureExecutorSchema = {};
7-
const context: ExecutorContext = {
8-
root: '',
9-
cwd: process.cwd(),
10-
isVerbose: false,
11-
projectGraph: {
12-
nodes: {},
13-
dependencies: {},
14-
},
15-
projectsConfigurations: {
16-
projects: {},
17-
version: 2,
5+
jest.mock('@nx/devkit', () => ({
6+
logger: {
7+
fatal: jest.fn(),
8+
error: jest.fn(),
189
},
19-
nxJsonConfiguration: {},
20-
};
10+
}));
11+
12+
jest.mock('child_process', () => ({
13+
execFileSync: jest.fn(),
14+
}));
15+
16+
describe('runExecutor', () => {
17+
const context: ExecutorContext = {
18+
root: '/workspace-root',
19+
cwd: '',
20+
isVerbose: false,
21+
projectName: 'test-project',
22+
projectGraph: {
23+
nodes: {},
24+
dependencies: {},
25+
},
26+
projectsConfigurations: {
27+
projects: {
28+
'test-project': {
29+
root: '/workspace-root/project-root',
30+
targets: {},
31+
},
32+
},
33+
version: 2,
34+
},
35+
nxJsonConfiguration: {},
36+
};
37+
38+
afterEach(() => {
39+
jest.resetAllMocks();
40+
});
41+
42+
it('should execute the publish command successfully', async () => {
43+
(execFileSync as jest.Mock).mockImplementation(() => {});
44+
45+
const result = await executor({}, context);
46+
47+
expect(result).toEqual({ success: true });
48+
expect(execFileSync).toHaveBeenCalledWith(
49+
'npx',
50+
[
51+
'devcontainer',
52+
'features',
53+
'test',
54+
'--skip-autogenerated',
55+
'-p',
56+
'/workspace-root/project-root',
57+
'-f',
58+
'test-project',
59+
'-i',
60+
'debian:bookworm-slim',
61+
],
62+
{ cwd: '/workspace-root', stdio: 'inherit' }
63+
);
64+
});
65+
66+
it('should log an error and return failure when ProjectUtils throws', async () => {
67+
const result = await executor({}, { ...context, projectName: undefined });
68+
expect(result).toEqual({ success: false });
69+
expect(logger.fatal).toHaveBeenCalledWith('No project name provided', expect.any(Error));
70+
expect(execFileSync).not.toHaveBeenCalled();
71+
});
72+
73+
it('should handle errors from execFileSync gracefully', async () => {
74+
(execFileSync as jest.Mock).mockImplementation(() => {
75+
throw new Error('Command failed');
76+
});
2177

22-
describe('TestDevcontainerFeature Executor', () => {
23-
it('can run', async () => {
24-
const output = await executor(options, context);
25-
expect(output.success).toBe(true);
78+
const result = await executor({}, context);
79+
expect(result).toEqual({ success: false });
80+
expect(logger.fatal).toHaveBeenCalledWith('Error running devcontainer test', expect.any(Error));
2681
});
2782
});

tools/nx-internal/src/executors/test-devcontainer-feature/executor.ts

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,41 @@ import { TestDevcontainerFeatureExecutorSchema } from './schema';
33
import { ProjectUtils } from '@ebizbase/nx-devkit';
44
import { execFileSync } from 'child_process';
55

6-
const runExecutor: PromiseExecutor<TestDevcontainerFeatureExecutorSchema> = async (options, context) => {
6+
const runExecutor: PromiseExecutor<TestDevcontainerFeatureExecutorSchema> = async (
7+
options,
8+
context
9+
) => {
710
let projectUtils;
811
try {
912
projectUtils = new ProjectUtils(context);
1013
} catch (error: unknown) {
1114
logger.fatal('No project name provided', error);
1215
return { success: false };
1316
}
14-
const commands = ['npx', 'devcontainer', 'features', 'test','--skip-autogenerated', '-p', projectUtils.getProjectRoot(), '-f', projectUtils.getProjectName(), '-i', 'debian:bookworm-slim'];
17+
const commands = [
18+
'npx',
19+
'devcontainer',
20+
'features',
21+
'test',
22+
'--skip-autogenerated',
23+
'-p',
24+
projectUtils.getProjectRoot(),
25+
'-f',
26+
projectUtils.getProjectName(),
27+
'-i',
28+
'debian:bookworm-slim',
29+
];
1530
if (options.filter) {
1631
commands.push('--filter', options.filter);
1732
}
18-
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
1933

20-
return { success: true };
34+
try {
35+
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
36+
return { success: true };
37+
} catch (error) {
38+
logger.fatal('Error running devcontainer test', error);
39+
return { success: false };
40+
}
2141
};
2242

2343
export default runExecutor;

0 commit comments

Comments
 (0)