-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
199 additions
and
51 deletions.
There are no files selected for viewing
98 changes: 77 additions & 21 deletions
98
tools/nx-internal/src/executors/publish-devcontainer-feature/executor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 76 additions & 21 deletions
97
tools/nx-internal/src/executors/test-devcontainer-feature/executor.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters