Skip to content

Commit

Permalink
✅ Add tests for glob parsing util
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Oct 28, 2019
1 parent f64725b commit 2dfa572
Showing 1 changed file with 96 additions and 72 deletions.
168 changes: 96 additions & 72 deletions test/utils/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,122 @@ 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'
import config, { explorer, parseGlobs } 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[]
describe('configuration utils', () => {
describe('config', () => {
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)
}
// 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 = []
})
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)
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('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='
`)
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=',
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',
},
})
})
})

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.*"
}
}`)
describe('parseGlobs', () => {
it('splits glob strings with commas', () => {
expect(parseGlobs('*/**.html,foo/bar'))
.to.deep.equal(['*/**.html', 'foo/bar'])
})

it('does not split on commas within glob "or" lists', () => {
expect(parseGlobs('*/**.html,*.{png,jpg},foo'))
.to.deep.equal(['*/**.html', '*.{png,jpg}', 'foo'])
})

const flags = { 'snapshot-files': '**/*.snapshot.html' }
const args = { snapshotDirectory: '_site/' }
it('trims whitespace from globs', () => {
expect(parseGlobs('*/**.html, *.{png,jpg} , foo'))
.to.deep.equal(['*/**.html', '*.{png,jpg}', 'foo'])
})

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',
},
it('filters empty globs', () => {
expect(parseGlobs('*/**.html,,foo,'))
.to.deep.equal(['*/**.html', 'foo'])
})
})
})

0 comments on commit 2dfa572

Please sign in to comment.