Skip to content

Commit

Permalink
test: update test case
Browse files Browse the repository at this point in the history
  • Loading branch information
johnitvn committed Nov 23, 2024
1 parent 9df4533 commit 4aa230b
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -1,27 +1,83 @@
import { ExecutorContext } from '@nx/devkit';

import { PublishDevcontainerFeatureExecutorSchema } from './schema';
import executor from './executor';
import { ExecutorContext, logger } from '@nx/devkit';
import { execFileSync } from 'child_process';

const options: PublishDevcontainerFeatureExecutorSchema = {};
const context: ExecutorContext = {
root: '',
cwd: process.cwd(),
isVerbose: false,
projectGraph: {
nodes: {},
dependencies: {},
},
projectsConfigurations: {
projects: {},
version: 2,
jest.mock('@nx/devkit', () => ({
logger: {
fatal: jest.fn(),
error: jest.fn(),
},
nxJsonConfiguration: {},
};
}));

jest.mock('child_process', () => ({
execFileSync: jest.fn(),
}));

describe('runExecutor', () => {
const context: ExecutorContext = {
root: '/workspace-root',
cwd: '',
isVerbose: false,
projectName: 'test-project',
projectGraph: {
nodes: {},
dependencies: {},
},
projectsConfigurations: {
projects: {
'test-project': {
root: '/workspace-root/project-root',
targets: {},
},
},
version: 2,
},
nxJsonConfiguration: {},
};

afterEach(() => {
jest.resetAllMocks();
});

it('should execute the publish command successfully', async () => {
(execFileSync as jest.Mock).mockImplementation(() => {});

const result = await executor({}, context);

expect(result).toEqual({ success: true });
expect(execFileSync).toHaveBeenCalledWith(
'npx',
[
'devcontainer',
'features',
'publish',
'--registry',
'ghcr.io',
'--namespace',
'ebizbase/devcontainer-features',
'/workspace-root/project-root/src/test-project',
],
{ cwd: '/workspace-root', stdio: 'inherit' }
);
});

it('should log an error and return failure when ProjectUtils throws', async () => {
const result = await executor({}, { ...context, projectName: undefined });
expect(result).toEqual({ success: false });
expect(logger.fatal).toHaveBeenCalledWith('No project name provided', expect.any(Error));
expect(execFileSync).not.toHaveBeenCalled();
});

it('should handle errors from execFileSync gracefully', async () => {
(execFileSync as jest.Mock).mockImplementation(() => {
throw new Error('Command failed');
});

describe('PublishDevcontainerFeature Executor', () => {
it('can run', async () => {
const output = await executor(options, context);
expect(output.success).toBe(true);
const result = await executor({}, context);
expect(result).toEqual({ success: false });
expect(logger.fatal).toHaveBeenCalledWith(
'Error while executing the publish command',
expect.any(Error)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,36 @@ import { ProjectUtils } from '@ebizbase/nx-devkit';
import { execFileSync } from 'child_process';
import { PublishDevcontainerFeatureExecutorSchema } from './schema';

const runExecutor: PromiseExecutor<PublishDevcontainerFeatureExecutorSchema> = async (_options, context) => {
const runExecutor: PromiseExecutor<PublishDevcontainerFeatureExecutorSchema> = async (
_options,
context
) => {
let projectUtils;
try {
projectUtils = new ProjectUtils(context);
} catch (error: unknown) {
logger.fatal('No project name provided', error);
return { success: false };
}
const commands = ['npx', 'devcontainer', 'features', 'publish', '--registry', 'ghcr.io', '--namespace', 'ebizbase/devcontainer-features', projectUtils.getProjectRoot() + '/src/' + projectUtils.getProjectName()];
const commands = [
'npx',
'devcontainer',
'features',
'publish',
'--registry',
'ghcr.io',
'--namespace',
'ebizbase/devcontainer-features',
projectUtils.getProjectRoot() + '/src/' + projectUtils.getProjectName(),
];

execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });

return { success: true };
try {
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
return { success: true };
} catch (error) {
logger.fatal('Error while executing the publish command', error);
return { success: false };
}
};

export default runExecutor;
Original file line number Diff line number Diff line change
@@ -1,27 +1,82 @@
import { ExecutorContext } from '@nx/devkit';

import { TestDevcontainerFeatureExecutorSchema } from './schema';
import executor from './executor';
import { ExecutorContext, logger } from '@nx/devkit';
import { execFileSync } from 'child_process';

const options: TestDevcontainerFeatureExecutorSchema = {};
const context: ExecutorContext = {
root: '',
cwd: process.cwd(),
isVerbose: false,
projectGraph: {
nodes: {},
dependencies: {},
},
projectsConfigurations: {
projects: {},
version: 2,
jest.mock('@nx/devkit', () => ({
logger: {
fatal: jest.fn(),
error: jest.fn(),
},
nxJsonConfiguration: {},
};
}));

jest.mock('child_process', () => ({
execFileSync: jest.fn(),
}));

describe('runExecutor', () => {
const context: ExecutorContext = {
root: '/workspace-root',
cwd: '',
isVerbose: false,
projectName: 'test-project',
projectGraph: {
nodes: {},
dependencies: {},
},
projectsConfigurations: {
projects: {
'test-project': {
root: '/workspace-root/project-root',
targets: {},
},
},
version: 2,
},
nxJsonConfiguration: {},
};

afterEach(() => {
jest.resetAllMocks();
});

it('should execute the publish command successfully', async () => {
(execFileSync as jest.Mock).mockImplementation(() => {});

const result = await executor({}, context);

expect(result).toEqual({ success: true });
expect(execFileSync).toHaveBeenCalledWith(
'npx',
[
'devcontainer',
'features',
'test',
'--skip-autogenerated',
'-p',
'/workspace-root/project-root',
'-f',
'test-project',
'-i',
'debian:bookworm-slim',
],
{ cwd: '/workspace-root', stdio: 'inherit' }
);
});

it('should log an error and return failure when ProjectUtils throws', async () => {
const result = await executor({}, { ...context, projectName: undefined });
expect(result).toEqual({ success: false });
expect(logger.fatal).toHaveBeenCalledWith('No project name provided', expect.any(Error));
expect(execFileSync).not.toHaveBeenCalled();
});

it('should handle errors from execFileSync gracefully', async () => {
(execFileSync as jest.Mock).mockImplementation(() => {
throw new Error('Command failed');
});

describe('TestDevcontainerFeature Executor', () => {
it('can run', async () => {
const output = await executor(options, context);
expect(output.success).toBe(true);
const result = await executor({}, context);
expect(result).toEqual({ success: false });
expect(logger.fatal).toHaveBeenCalledWith('Error running devcontainer test', expect.any(Error));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,41 @@ import { TestDevcontainerFeatureExecutorSchema } from './schema';
import { ProjectUtils } from '@ebizbase/nx-devkit';
import { execFileSync } from 'child_process';

const runExecutor: PromiseExecutor<TestDevcontainerFeatureExecutorSchema> = async (options, context) => {
const runExecutor: PromiseExecutor<TestDevcontainerFeatureExecutorSchema> = async (
options,
context
) => {
let projectUtils;
try {
projectUtils = new ProjectUtils(context);
} catch (error: unknown) {
logger.fatal('No project name provided', error);
return { success: false };
}
const commands = ['npx', 'devcontainer', 'features', 'test','--skip-autogenerated', '-p', projectUtils.getProjectRoot(), '-f', projectUtils.getProjectName(), '-i', 'debian:bookworm-slim'];
const commands = [
'npx',
'devcontainer',
'features',
'test',
'--skip-autogenerated',
'-p',
projectUtils.getProjectRoot(),
'-f',
projectUtils.getProjectName(),
'-i',
'debian:bookworm-slim',
];
if (options.filter) {
commands.push('--filter', options.filter);
}
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });

return { success: true };
try {
execFileSync(commands[0], commands.slice(1), { cwd: context.root, stdio: 'inherit' });
return { success: true };
} catch (error) {
logger.fatal('Error running devcontainer test', error);
return { success: false };
}
};

export default runExecutor;

0 comments on commit 4aa230b

Please sign in to comment.