-
-
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.
feat(task): added shell task support
fix #5
- Loading branch information
Murat
committed
Mar 23, 2024
1 parent
45470f5
commit 8e838b9
Showing
10 changed files
with
584 additions
and
15 deletions.
There are no files selected for viewing
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 |
---|---|---|
@@ -0,0 +1,199 @@ | ||
require('../../mocks/mockAll'); | ||
const mockSpawn = jest.spyOn(require('child_process'), 'spawn'); | ||
|
||
import { shellTask } from '../../../tasks/shellTask'; | ||
import { ShellTaskType } from '../../../types/mod.types'; | ||
import { variables } from '../../../variables'; | ||
import { mockPrompter } from '../../mocks/mockPrompter'; | ||
|
||
describe('shellTask', () => { | ||
it('should run command', async () => { | ||
mockSpawn.mockImplementationOnce(() => ({ | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb(0); | ||
}, | ||
stdout: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stdout'); | ||
}, | ||
}, | ||
stderr: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stderr'); | ||
}, | ||
}, | ||
})); | ||
|
||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
name: 'test', | ||
command: 'test', | ||
}, | ||
], | ||
}; | ||
|
||
await shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(mockSpawn).toHaveBeenCalled(); | ||
expect(variables.get('test.output')).toBe('stdoutstderr'); | ||
|
||
mockSpawn.mockReset(); | ||
}); | ||
it('should run command with args', async () => { | ||
mockSpawn.mockImplementationOnce(() => ({ | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb(0); | ||
}, | ||
stdout: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stdout'); | ||
}, | ||
}, | ||
stderr: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stderr'); | ||
}, | ||
}, | ||
})); | ||
|
||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
name: 'test', | ||
command: 'test', | ||
args: ['arg1', 'arg2 arg3'], | ||
}, | ||
], | ||
}; | ||
|
||
await shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(mockSpawn).toHaveBeenCalledWith('test', ['arg1', 'arg2 arg3']); | ||
|
||
mockSpawn.mockReset(); | ||
}); | ||
it('should handle unexpected error', async () => { | ||
mockSpawn.mockImplementationOnce(() => { | ||
throw new Error('unexpected error'); | ||
}); | ||
|
||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
name: 'test', | ||
command: 'test', | ||
}, | ||
], | ||
}; | ||
|
||
await expect( | ||
shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}) | ||
).rejects.toThrowError('unexpected error'); | ||
|
||
expect(variables.get('test')).toEqual('error'); | ||
|
||
mockSpawn.mockReset(); | ||
}); | ||
it('should handle non zero exit code', async () => { | ||
mockSpawn.mockImplementationOnce(() => ({ | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb(1); | ||
}, | ||
stdout: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stdout'); | ||
}, | ||
}, | ||
stderr: { | ||
on: (_event: string, cb: CallableFunction) => { | ||
cb('stderr'); | ||
}, | ||
}, | ||
})); | ||
|
||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
name: 'test', | ||
command: 'test', | ||
}, | ||
], | ||
}; | ||
|
||
await expect( | ||
shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}) | ||
).rejects.toThrowError('non zero exit code'); | ||
|
||
expect(variables.get('test')).toEqual('error'); | ||
|
||
mockSpawn.mockReset(); | ||
}); | ||
it('should skip when condition does not meet', async () => { | ||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
when: { random: false }, | ||
name: 'test', | ||
command: 'test', | ||
}, | ||
], | ||
}; | ||
|
||
await shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(mockSpawn).not.toHaveBeenCalled(); | ||
|
||
mockSpawn.mockReset(); | ||
}); | ||
it('should skip when execution not allowed', async () => { | ||
mockPrompter.confirm.mockClear(); | ||
mockPrompter.confirm.mockReturnValueOnce(false); | ||
|
||
const task: ShellTaskType = { | ||
type: 'shell', | ||
actions: [ | ||
{ | ||
name: 'test', | ||
command: 'test', | ||
}, | ||
], | ||
}; | ||
|
||
await shellTask({ | ||
configPath: 'path/to/config', | ||
task: task, | ||
packageName: 'test-package', | ||
}); | ||
|
||
expect(mockSpawn).not.toHaveBeenCalled(); | ||
|
||
mockSpawn.mockReset(); | ||
mockPrompter.confirm.mockReset(); | ||
}); | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* eslint-disable @typescript-eslint/no-unsafe-call */ | ||
|
||
require('../../mocks/mockAll'); | ||
|
||
import { parseArgs } from '../../../utils/parseArgs'; | ||
|
||
describe('parseArgs', () => { | ||
it('should parse args correctly', () => { | ||
const args = parseArgs('arg1 "arg2 arg3" arg4'); | ||
expect(args).toEqual(['arg1', 'arg2 arg3', 'arg4']); | ||
}); | ||
it('should parse escaped args correctly', () => { | ||
const args = parseArgs('arg1 "arg\\"2 a\\"rg3"'); | ||
expect(args).toEqual(['arg1', 'arg"2 a"rg3']); | ||
}); | ||
}); |
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
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
Oops, something went wrong.