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
24 changes: 17 additions & 7 deletions src/commands/snapshot.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {flags} from '@oclif/command'
import configuration from '../configuration/configuration'
import {StaticSnapshotsConfiguration} from '../configuration/static-snapshots-configuration'
import Constants from '../services/constants'
import {StaticSnapshotOptions} from '../services/static-snapshot-options'
import StaticSnapshotService from '../services/static-snapshot-service'
Expand Down Expand Up @@ -60,18 +62,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-snapshots'] || {}) as StaticSnapshotsConfiguration
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
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
import * as fs from 'fs'
import * as yaml from 'js-yaml'
import * as path from 'path'

export interface SnapshotConfiguration {
widths?: [number],
'min-height'?: number,
}
import { SnapshotConfiguration } from './snapshot-configuration'
import { StaticSnapshotsConfiguration } from './static-snapshots-configuration'

export interface Configuration {
version: number,
snapshot: SnapshotConfiguration
'static-snapshots': StaticSnapshotsConfiguration
}

const configuration = (relativePath = '.percy.yml'): Configuration => {
Expand All @@ -22,11 +20,12 @@ 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,
'snapshot': {},
'static-snapshots': {},
}

return defaultConfiguration

}
}

Expand Down
4 changes: 4 additions & 0 deletions src/configuration/snapshot-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface SnapshotConfiguration {
widths?: [number],
'min-height'?: number,
}
5 changes: 5 additions & 0 deletions src/configuration/static-snapshots-configuration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
export interface StaticSnapshotsConfiguration {
'base-url'?: string,
'snapshot-files'?: string,
'ignore-files'?: string,
}
5 changes: 3 additions & 2 deletions src/services/agent-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ import * as bodyParser from 'body-parser'
import * as cors from 'cors'
import * as express from 'express'
import {Server} from 'http'
import { SnapshotOptions } from '../percy-agent-client/snapshot-options'
import configuration, {SnapshotConfiguration} from '../utils/configuration'
import configuration from '../configuration/configuration'
import {SnapshotConfiguration} from '../configuration/snapshot-configuration'
import {SnapshotOptions} from '../percy-agent-client/snapshot-options'
import logger, {profile} from '../utils/logger'
import {AgentOptions} from './agent-options'
import BuildService from './build-service'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect} from 'chai'
import configuration from '../../src/utils/configuration'
import configuration from '../../src/configuration/configuration'

describe('Configuration', () => {
it('parses valid configuration', () => {
Expand All @@ -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-snapshots']['base-url']).to.eql('/blog')
expect(subject['static-snapshots']['snapshot-files']).to.eql('**/*.html')
expect(subject['static-snapshots']['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-snapshots': {}})
})
})
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-snapshots:
base-url: /blog
djones marked this conversation as resolved.
Show resolved Hide resolved
snapshot-files: '**/*.html'
ignore-files: '**/*.htm'