Skip to content

Commit

Permalink
fix: 🐛 Default config overrides (#394)
Browse files Browse the repository at this point in the history
* ✅ Add configuration utiliy tests

* fix: 🐛 Remove requirement for static snapshot and upload arg

This arg is allowed to be specified via a config file. It seems it was always
meant to be this way as there are defaults specified for these args.

* fix: 🐛 Remove oclif defaults

Percy defaults are handled seperately from oclif due to allowing specifying some
flags and args within a configuration file.
  • Loading branch information
Wil Wilsman authored Oct 7, 2019
1 parent 1bb72bd commit ca4ecd1
Show file tree
Hide file tree
Showing 8 changed files with 163 additions and 33 deletions.
12 changes: 8 additions & 4 deletions src/commands/exec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ export default class Exec extends PercyCommand {
}),
'network-idle-timeout': flags.integer({
char: 't',
default: DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout'],
description: 'asset discovery network idle timeout (in milliseconds)',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout']}]`,
'Asset discovery network idle timeout (in milliseconds)',
].join(' '),
}),
'port': flags.integer({
char: 'p',
default: DEFAULT_CONFIGURATION.agent.port,
description: 'port',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent.port}]`,
'Port',
].join(' '),
}),
'config': flags.string({
char: 'c',
Expand Down
6 changes: 4 additions & 2 deletions src/commands/health-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export default class HealthCheck extends Command {
static flags = {
port: flags.integer({
char: 'p',
default: DEFAULT_CONFIGURATION.agent.port,
description: 'port',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent.port}]`,
'Port',
].join(' '),
}),
}

Expand Down
40 changes: 26 additions & 14 deletions src/commands/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ export default class Snapshot extends PercyCommand {

static args = [{
name: 'snapshotDirectory',
description: 'A path to the directory you would like to snapshot',
required: true,
description: [
`[default: ${DEFAULT_CONFIGURATION['static-snapshots'].path}]`,
'A path to the directory you would like to snapshot',
].join(' '),
}]

static examples = [
Expand All @@ -25,35 +27,45 @@ export default class Snapshot extends PercyCommand {
static flags = {
'snapshot-files': flags.string({
char: 's',
description: 'Glob or comma-seperated string of globs for matching the files and directories to snapshot.',
default: DEFAULT_CONFIGURATION['static-snapshots']['snapshot-files'],
description: [
`[default: ${DEFAULT_CONFIGURATION['static-snapshots']['snapshot-files']}]`,
'Glob or comma-seperated string of globs for matching the files and directories to snapshot.',
].join(' '),
}),
'ignore-files': flags.string({
char: 'i',
description: 'Glob or comma-seperated string of globs for matching the files and directories to ignore.',
default: DEFAULT_CONFIGURATION['static-snapshots']['ignore-files'],
description: [
`[default: ${DEFAULT_CONFIGURATION['static-snapshots']['ignore-files']}]`,
'Glob or comma-seperated string of globs for matching the files and directories to ignore.',
].join(' '),
}),
'base-url': flags.string({
char: 'b',
description: 'If your static files will be hosted in a subdirectory, instead \n' +
'of the webserver\'s root path, set that subdirectory with this flag.',
default: DEFAULT_CONFIGURATION['static-snapshots']['base-url'],
description: [
`[default: ${DEFAULT_CONFIGURATION['static-snapshots']['base-url']}]`,
'If your static files will be hosted in a subdirectory, instead',
'of the webserver\'s root path, set that subdirectory with this flag.',
].join(' '),
}),
// from exec command. needed to start the agent service.
'allowed-hostname': flags.string({
char: 'h',
description: 'Allowable hostname(s) to capture assets from',
multiple: true,
}),
// from exec command. needed to start the agent service.
'network-idle-timeout': flags.integer({
char: 't',
default: DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout'],
description: 'Asset discovery network idle timeout (in milliseconds)',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout']}]`,
'Asset discovery network idle timeout (in milliseconds)',
].join(' '),
}),
'port': flags.integer({
char: 'p',
default: DEFAULT_CONFIGURATION.agent.port,
description: 'Port',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent.port}]`,
'Port',
].join(' '),
}),
'config': flags.string({
char: 'c',
Expand Down
12 changes: 8 additions & 4 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,17 @@ export default class Start extends PercyCommand {
}),
'network-idle-timeout': flags.integer({
char: 't',
default: DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout'],
description: 'asset discovery network idle timeout (in milliseconds)',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent['asset-discovery']['network-idle-timeout']}]`,
'Asset discovery network idle timeout (in milliseconds)',
].join(' '),
}),
'port': flags.integer({
char: 'p',
default: DEFAULT_CONFIGURATION.agent.port,
description: 'port',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent.port}]`,
'Port',
].join(' '),
}),
'config': flags.string({
char: 'c',
Expand Down
6 changes: 4 additions & 2 deletions src/commands/stop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@ export default class Stop extends PercyCommand {
static flags = {
port: flags.integer({
char: 'p',
default: DEFAULT_CONFIGURATION.agent.port,
description: 'port',
description: [
`[default: ${DEFAULT_CONFIGURATION.agent.port}]`,
'Port',
].join(' '),
}),
}

Expand Down
18 changes: 12 additions & 6 deletions src/commands/upload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ export default class Upload extends Command {

static args = [{
name: 'uploadDirectory',
description: 'A path to the directory containing static snapshot images',
required: true,
description: [
`[default: ${DEFAULT_CONFIGURATION['static-snapshots'].path}]`,
'A path to the directory containing static snapshot images',
].join(' '),
}]

static examples = [
Expand All @@ -21,13 +23,17 @@ export default class Upload extends Command {
static flags = {
files: flags.string({
char: 'f',
description: 'Glob or comma-seperated string of globs for matching the files and directories to snapshot.',
default: DEFAULT_CONFIGURATION['image-snapshots'].files,
description: [
`[default: ${DEFAULT_CONFIGURATION['image-snapshots'].files}]`,
'Glob or comma-seperated string of globs for matching the files and directories to snapshot.',
].join(' '),
}),
ignore: flags.string({
char: 'i',
description: 'Glob or comma-seperated string of globs for matching the files and directories to ignore.',
default: DEFAULT_CONFIGURATION['image-snapshots'].ignore,
description: [
`[default: ${DEFAULT_CONFIGURATION['image-snapshots'].ignore}]`,
'Glob or comma-seperated string of globs for matching the files and directories to ignore.',
].join(' '),
}),
config: flags.string({
char: 'c',
Expand Down
3 changes: 2 additions & 1 deletion src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import logger from './logger'

const { isArray } = Array
const { assign, keys } = Object
const explorer = cosmiconfig('percy', {

export const explorer = cosmiconfig('percy', {
searchPlaces: [
'package.json',
'.percyrc',
Expand Down
99 changes: 99 additions & 0 deletions test/utils/configuration.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import { expect } from 'chai'
import * as fs from 'fs'
import * as path from 'path'
import { DEFAULT_CONFIGURATION } from '../../src/configuration/configuration'
import config, { explorer } from '../../src/utils/configuration'

function dedent(str: string) {
const indent = str.match(/ +/g)![0].length
return str.replace(new RegExp(`\n {${indent}}`, 'g'), '\n').trim()
}

describe('configuration', () => {
let configfiles: string[]

// helper to create a config files and cleanup on `afterEach`
function mkconfig(filename: string, contents: string) {
const filepath = path.join(process.cwd(), filename)
fs.writeFileSync(filepath, dedent(contents))
configfiles.push(filepath)
}

beforeEach(() => {
// clear caches for creating & removing files during testing
explorer.clearCaches()
configfiles = []
})

afterEach(() => {
// clean up any created config files
configfiles.forEach((file) => {
fs.unlinkSync(file)
})
})

it('returns the default configuration', () => {
expect(config({})).to.deep.equal(DEFAULT_CONFIGURATION)
})

it('automatically loads overrides from a `.percy.yml` config file', () => {
mkconfig('.percy.yml', `
version: 1
snapshot:
widths: [320, 1200]
enable-javascript: true
agent:
asset-discovery:
request-headers:
Authorization: 'Basic abc123='
`)

expect(config({})).to.deep.equal({
...DEFAULT_CONFIGURATION,
snapshot: {
...DEFAULT_CONFIGURATION.snapshot,
'widths': [320, 1200],
'enable-javascript': true,
},
agent: {
...DEFAULT_CONFIGURATION.agent,
'asset-discovery': {
...DEFAULT_CONFIGURATION.agent['asset-discovery'],
'request-headers': {
Authorization: 'Basic abc123=',
},
},
},
})
})

it('overrides defaults and config file options with flags and args', () => {
mkconfig('.percy.json', `{
"version": 1,
"snapshot": {
"widths": [800]
},
"static-snapshots": {
"path": "_wrong/",
"ignore-files": "**/*.ignore.*"
}
}`)

const flags = { 'snapshot-files': '**/*.snapshot.html' }
const args = { snapshotDirectory: '_site/' }

expect(config(flags, args)).to.deep.equal({
...DEFAULT_CONFIGURATION,
'snapshot': {
...DEFAULT_CONFIGURATION.snapshot,
widths: [800],
},
'static-snapshots': {
...DEFAULT_CONFIGURATION['static-snapshots'],
'path': '_site/',
'ignore-files': '**/*.ignore.*',
'snapshot-files': '**/*.snapshot.html',
},
})
})
})

0 comments on commit ca4ecd1

Please sign in to comment.