Skip to content

Commit

Permalink
fix: Exit nicely if the passed directory doesn't exist (#344)
Browse files Browse the repository at this point in the history
* fix: Exit nicely if the passed directory doesn't exist

* Pass a real dir in tests
  • Loading branch information
Robdel12 committed Sep 12, 2019
1 parent d40f0db commit f65acaf
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
11 changes: 9 additions & 2 deletions src/commands/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {flags} from '@oclif/command'
import { flags } from '@oclif/command'
import { existsSync } from 'fs'
import { DEFAULT_CONFIGURATION } from '../configuration/configuration'
import ConfigurationService from '../services/configuration-service'
import StaticSnapshotService from '../services/static-snapshot-service'
Expand Down Expand Up @@ -59,7 +60,7 @@ export default class Snapshot extends PercyCommand {
async run() {
await super.run()

const {args, flags} = this.parse(Snapshot)
const { args, flags } = this.parse(Snapshot)

const configurationService = new ConfigurationService()
configurationService.applyFlags(flags)
Expand All @@ -70,13 +71,19 @@ export default class Snapshot extends PercyCommand {
if (!this.percyWillRun()) { this.exit(0) }

const baseUrl = configuration['static-snapshots']['base-url']
const snapshotPath = configuration['static-snapshots'].path

// check that base url starts with a slash and exit if it is missing
if (baseUrl[0] !== '/') {
logger.warn('The base-url flag must begin with a slash.')
this.exit(1)
}

if (!existsSync(snapshotPath)) {
logger.warn(`Exiting. The passed directory (${snapshotPath}) is empty.`)
this.exit(1)
}

await this.agentService.start(configuration)
this.logStart()

Expand Down
2 changes: 1 addition & 1 deletion test/commands/snapshot.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ describe('snapshot', () => {
const staticSnapshotServiceStub = StaticSnapshotServiceStub()

const stdout = await captureStdOut(async () => {
await Snapshot.run(['./dummy-test-dir'])
await Snapshot.run(['./'])
})

chai.expect(agentServiceStub.start).to.be.calledWith(DEFAULT_CONFIGURATION)
Expand Down

0 comments on commit f65acaf

Please sign in to comment.