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

feat: Support static snapshots in .percy.yml configuration file #159

Merged
merged 11 commits into from
Jun 6, 2019
23 changes: 16 additions & 7 deletions src/commands/snapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {flags} from '@oclif/command'
import Constants from '../services/constants'
import {StaticSnapshotOptions} from '../services/static-snapshot-options'
import StaticSnapshotService from '../services/static-snapshot-service'
import configuration, {StaticSiteSnapshotConfiguration} from '../utils/configuration'
import logger from '../utils/logger'
import PercyCommand from './percy-command'

Expand Down Expand Up @@ -60,18 +61,26 @@ export default class Snapshot extends PercyCommand {
const port = flags.port as number
const staticServerPort = port + 1
const networkIdleTimeout = flags['network-idle-timeout'] as number
const baseUrl = flags['base-url'] as string
const rawIgnoreGlob = flags['ignore-files'] as string
const rawSnapshotGlob = flags['snapshot-files'] as string

const snapshotGlobs = rawSnapshotGlob.split(',')

const ignoreGlobs = rawIgnoreGlob ? rawIgnoreGlob.split(',') : []
const baseUrlFlag = flags['base-url'] as string
const rawIgnoreGlobFlag = flags['ignore-files'] as string
const rawSnapshotGlobFlag = flags['snapshot-files'] as string

// exit gracefully if percy will not run
if (!this.percyWillRun()) { this.exit(0) }

// check that the base url passed in starts with a slash and exit if it is missing
// read configurations from the percy.yml file
const staticSiteConfiguration = (configuration().static_site || {}) as StaticSiteSnapshotConfiguration
const baseUrl = staticSiteConfiguration['base-url'] || baseUrlFlag
const rawSnapshotGlobs = staticSiteConfiguration['snapshot-files'] || rawSnapshotGlobFlag
const rawIgnoreGlobs = staticSiteConfiguration['ignore-files'] || rawIgnoreGlobFlag

const snapshotGlobs = rawSnapshotGlobs.split(',')

// if it is an empty string then convert it to an empty array instead of an array of an empty string
const ignoreGlobs = rawIgnoreGlobs ? rawIgnoreGlobs.split(',') : []

// 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)
Expand Down
11 changes: 10 additions & 1 deletion src/utils/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ export interface SnapshotConfiguration {
'min-height'?: number,
}

export interface StaticSiteSnapshotConfiguration {
djones marked this conversation as resolved.
Show resolved Hide resolved
'base-url'?: string,
'snapshot-files'?: string,
djones marked this conversation as resolved.
Show resolved Hide resolved
'ignore-files'?: string,
}

export interface Configuration {
version: number,
snapshot: SnapshotConfiguration
static_site: StaticSiteSnapshotConfiguration
}

const configuration = (relativePath = '.percy.yml'): Configuration => {
Expand All @@ -22,7 +29,9 @@ const configuration = (relativePath = '.percy.yml'): Configuration => {
// in a chain. snapshot specific options -> agent configuration -> default values

const defaultConfiguration: Configuration = {
version: 1.0, snapshot: {},
version: 1.0,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Should this version be updated since it introduces static_site? It is still 100% compatible with the existing format

Copy link
Contributor

Choose a reason for hiding this comment

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

I think so, 1.1?

snapshot: {},
static_site: {},
}

return defaultConfiguration
Expand Down
4 changes: 4 additions & 0 deletions test/support/.percy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@ version: 1
snapshot:
widths: [375, 1280]
min-height: 1024
Copy link
Contributor

Choose a reason for hiding this comment

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

Pre-existing, but interesting this is not camel cased.

static_site:
base-url: /blog
djones marked this conversation as resolved.
Show resolved Hide resolved
snapshot-files: '**/*.html'
ignore-files: '**/*.htm'
5 changes: 4 additions & 1 deletion test/utils/configuration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ describe('Configuration', () => {
expect(subject.version).to.eql(1)
expect(subject.snapshot.widths).to.eql([375, 1280])
expect(subject.snapshot['min-height']).to.eql(1024)
expect(subject.static_site['base-url']).to.eql('/blog')
expect(subject.static_site['snapshot-files']).to.eql('**/*.html')
expect(subject.static_site['ignore-files']).to.eql('**/*.htm')
})

it('gracefully handles a missing file', () => {
const subject = configuration('test/support/.file-does-not-exist.yml')

expect(subject).to.eql({version: 1.0, snapshot: {}})
expect(subject).to.eql({version: 1.0, snapshot: {}, static_site: {}})
})
})