-
-
Notifications
You must be signed in to change notification settings - Fork 232
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
1 parent
581b984
commit 950132d
Showing
8 changed files
with
237 additions
and
18 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
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
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
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,90 @@ | ||
import createMockInstance from 'jest-create-mock-instance'; | ||
|
||
import { createFakeProcess, FakeCommand } from '../fixtures/fake-command'; | ||
import { Logger } from '../logger'; | ||
import { getSpawnOpts } from '../spawn'; | ||
import { Teardown } from './teardown'; | ||
|
||
let spawn: jest.Mock; | ||
let logger: Logger; | ||
const commands = [new FakeCommand()]; | ||
const teardown = 'cowsay bye'; | ||
|
||
beforeEach(() => { | ||
logger = createMockInstance(Logger); | ||
spawn = jest.fn(() => createFakeProcess(1)); | ||
}); | ||
|
||
const create = (teardown: string[]) => | ||
new Teardown({ | ||
spawn, | ||
logger, | ||
commands: teardown, | ||
}); | ||
|
||
it('returns commands unchanged', () => { | ||
const { commands: actual } = create([]).handle(commands); | ||
expect(actual).toBe(commands); | ||
}); | ||
|
||
describe('onFinish callback', () => { | ||
it('does not spawn nothing if there are no teardown commands', () => { | ||
create([]).handle(commands).onFinish(); | ||
expect(spawn).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('runs teardown command', () => { | ||
create([teardown]).handle(commands).onFinish(); | ||
expect(spawn).toHaveBeenCalledWith(teardown, getSpawnOpts({ stdio: 'raw' })); | ||
}); | ||
|
||
it('waits for teardown command to close', async () => { | ||
const child = createFakeProcess(1); | ||
spawn.mockReturnValue(child); | ||
|
||
const result = create([teardown]).handle(commands).onFinish(); | ||
child.emit('close', 1, null); | ||
await expect(result).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('rejects if teardown command errors', async () => { | ||
const child = createFakeProcess(1); | ||
spawn.mockReturnValue(child); | ||
|
||
const result = create([teardown]).handle(commands).onFinish(); | ||
child.emit('error', 'fail'); | ||
await expect(result).rejects.toBeUndefined(); | ||
}); | ||
|
||
it('runs multiple teardown commands in sequence', async () => { | ||
const child1 = createFakeProcess(1); | ||
const child2 = createFakeProcess(2); | ||
spawn.mockReturnValueOnce(child1).mockReturnValueOnce(child2); | ||
|
||
const result = create(['foo', 'bar']).handle(commands).onFinish(); | ||
|
||
expect(spawn).toHaveBeenCalledTimes(1); | ||
expect(spawn).toHaveBeenLastCalledWith('foo', getSpawnOpts({ stdio: 'raw' })); | ||
|
||
child1.emit('close', 1, null); | ||
await new Promise((resolve) => setTimeout(resolve)); | ||
|
||
expect(spawn).toHaveBeenCalledTimes(2); | ||
expect(spawn).toHaveBeenLastCalledWith('bar', getSpawnOpts({ stdio: 'raw' })); | ||
|
||
child2.emit('close', 0, null); | ||
await expect(result).resolves.toBeUndefined(); | ||
}); | ||
|
||
it('stops running teardown commands on SIGINT', async () => { | ||
const child = createFakeProcess(1); | ||
spawn.mockReturnValue(child); | ||
|
||
const result = create(['foo', 'bar']).handle(commands).onFinish(); | ||
child.emit('close', null, 'SIGINT'); | ||
await result; | ||
|
||
expect(spawn).toHaveBeenCalledTimes(1); | ||
expect(spawn).toHaveBeenLastCalledWith('foo', expect.anything()); | ||
}); | ||
}); |
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,73 @@ | ||
import * as Rx from 'rxjs'; | ||
|
||
import { Command, SpawnCommand } from '../command'; | ||
import { Logger } from '../logger'; | ||
import { getSpawnOpts, spawn as baseSpawn } from '../spawn'; | ||
import { FlowController } from './flow-controller'; | ||
|
||
export class Teardown implements FlowController { | ||
private readonly logger: Logger; | ||
private readonly spawn: SpawnCommand; | ||
private readonly teardown: readonly string[]; | ||
|
||
constructor({ | ||
logger, | ||
spawn, | ||
commands, | ||
}: { | ||
logger: Logger; | ||
/** | ||
* Which function to use to spawn commands. | ||
* Defaults to the same used by the rest of concurrently. | ||
*/ | ||
spawn?: SpawnCommand; | ||
commands: readonly string[]; | ||
}) { | ||
this.logger = logger; | ||
this.spawn = spawn || baseSpawn; | ||
this.teardown = commands; | ||
} | ||
|
||
handle(commands: Command[]): { commands: Command[]; onFinish: () => Promise<void> } { | ||
const { logger, teardown, spawn } = this; | ||
const onFinish = async () => { | ||
if (!teardown.length) { | ||
return; | ||
} | ||
|
||
for (const command of teardown) { | ||
logger.logGlobalEvent(`Running teardown command "${command}"`); | ||
|
||
const child = spawn(command, getSpawnOpts({ stdio: 'raw' })); | ||
const error = Rx.fromEvent(child, 'error'); | ||
const close = Rx.fromEvent(child, 'close'); | ||
|
||
try { | ||
const [exitCode, signal] = await Promise.race([ | ||
Rx.firstValueFrom(error).then((event) => { | ||
throw event; | ||
}), | ||
Rx.firstValueFrom(close).then( | ||
(event) => event as [number | null, NodeJS.Signals | null], | ||
), | ||
]); | ||
|
||
logger.logGlobalEvent( | ||
`Teardown command "${command}" exited with code ${exitCode ?? signal}`, | ||
); | ||
|
||
if (signal === 'SIGINT') { | ||
break; | ||
} | ||
} catch (error) { | ||
const errorText = String(error instanceof Error ? error.stack || error : error); | ||
logger.logGlobalEvent(`Teardown command "${command}" errored:`); | ||
logger.logGlobalEvent(errorText); | ||
return Promise.reject(); | ||
} | ||
} | ||
}; | ||
|
||
return { commands, onFinish }; | ||
} | ||
} |
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