Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Stop and finalize build on exec termination #332

Merged
merged 5 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions src/commands/exec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {flags} from '@oclif/command'
import { flags } from '@oclif/command'
import * as spawn from 'cross-spawn'
import { DEFAULT_CONFIGURATION } from '../configuration/configuration'
import ConfigurationService from '../services/configuration-service'
Expand Down Expand Up @@ -32,12 +32,13 @@ export default class Exec extends PercyCommand {
}),
}

// helps prevent exiting before the agent service has stopped
private exiting = false

async run() {
await super.run()

const {argv} = this.parse(Exec)
const {flags} = this.parse(Exec)

const { argv, flags } = this.parse(Exec)
const command = argv.shift()

if (!command) {
Expand All @@ -54,14 +55,23 @@ export default class Exec extends PercyCommand {
}

// Even if Percy will not run, continue to run the subprocess
const spawnedProcess = spawn(command, argv, {stdio: 'inherit'})
const spawnedProcess = spawn(command, argv, { stdio: 'inherit' })
spawnedProcess.on('exit', (code) => this.stop(code))

// Receiving any of these events should stop the agent and exit
process.on('SIGHUP', () => this.stop())
process.on('SIGINT', () => this.stop())
process.on('SIGTERM', () => this.stop())
}

spawnedProcess.on('exit', async (code: any) => {
if (this.percyWillRun()) {
await this.agentService.stop()
}
private async stop(exitCode?: number | null) {
if (this.exiting) { return }
this.exiting = true

if (this.percyWillRun()) {
await this.agentService.stop()
}

process.exit(code)
})
process.exit(exitCode || 0)
}
}
2 changes: 1 addition & 1 deletion src/commands/percy-command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class PercyCommand extends Command {
}

async run() {
if (this.percyEnabled && !this.percyTokenPresent()) {
if (this.percyEnabled() && !this.percyTokenPresent()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great catch here 👍

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch!

this.warn('Skipping visual tests. PERCY_TOKEN was not provided.')
}
}
Expand Down
10 changes: 4 additions & 6 deletions test/commands/percy-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,13 @@ describe('percy-command', () => {
.stub(process, 'env', {PERCY_ENABLE: '0', PERCY_TOKEN: ''})
.stderr()
.command(['percy-command'])
.do((output) => expect(output.stderr).to.contain(
'Warning: Skipping visual tests. PERCY_TOKEN was not provided.',
))
.it('warns about PERCY_TOKEN to be set')
.do((output) => expect(output.stderr).to.eql(''))
.it('outputs no warnings when PERCY_ENABLED is 0')

test
.stub(process, 'env', {PERCY_ENABLE: '0', PERCY_TOKEN: 'ABC'})
.stub(process, 'env', {PERCY_TOKEN: 'ABC'})
.stderr()
.command(['percy-command'])
.do((output) => expect(output.stderr).to.eql(''))
.it('outputs no errors')
.it('outputs no errors when PERCY_TOKEN is set')
})