Skip to content

Commit

Permalink
✨ Handle own core config migration logic
Browse files Browse the repository at this point in the history
  • Loading branch information
wwilsman committed Feb 2, 2021
1 parent b2b1c3e commit f060966
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 4 deletions.
25 changes: 25 additions & 0 deletions packages/core/src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,28 @@ export const schema = {
}
}
};

// Migration function
export function migration(input, set) {
/* eslint-disable curly */
if (input.version < 2) {
// previous snapshot options map 1:1
if (input.snapshot != null)
set('snapshot', input.snapshot);
// request-headers option moved
if (input.agent?.assetDiscovery?.requestHeaders != null)
set('snapshot.requestHeaders', input.agent.assetDiscovery.requestHeaders);
// allowed-hostnames moved
if (input.agent?.assetDiscovery?.allowedHostnames != null)
set('discovery.allowedHostnames', input.agent.assetDiscovery.allowedHostnames);
// network-idle-timeout moved
if (input.agent?.assetDiscovery?.networkIdleTimeout != null)
set('discovery.networkIdleTimeout', input.agent.assetDiscovery.networkIdleTimeout);
// page pooling was rewritten to be a concurrent task queue
if (input.agent?.assetDiscovery?.pagePoolSizeMax != null)
set('discovery.concurrency', input.agent.assetDiscovery.pagePoolSizeMax);
// cache-responses was renamed to match the CLI flag
if (input.agent?.assetDiscovery?.cacheResponses != null)
set('discovery.disableCache', !input.agent.assetDiscovery.cacheResponses);
}
}
8 changes: 8 additions & 0 deletions packages/core/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
// Register core config options
const { default: PercyConfig } = require('@percy/config');
const CoreConfig = require('./config');

PercyConfig.addSchema(CoreConfig.schema);
PercyConfig.addMigration(CoreConfig.migration);

// Export the Percy class with commonjs compatibility
module.exports = require('./percy').default;
4 changes: 0 additions & 4 deletions packages/core/src/percy.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { promises as fs } from 'fs';
import PercyClient from '@percy/client';
import PercyConfig from '@percy/config';
import logger from '@percy/logger';
import { schema } from './config';
import Queue from './queue';
import Discoverer from './discovery/discoverer';
import createPercyServer from './server';
Expand All @@ -12,9 +11,6 @@ import injectPercyCSS from './utils/percy-css';
import { createRootResource, createLogResource } from './utils/resources';
import { normalizeURL } from './utils/url';

// Register core config options
PercyConfig.addSchema(schema);

// A Percy instance will create a new build when started, handle snapshot
// creation, asset discovery, and resource uploads, and will finalize the build
// when stopped. Snapshots are processed concurrently and the build is not
Expand Down
63 changes: 63 additions & 0 deletions packages/core/test/unit/config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import expect from 'expect';
import { migration } from '../../src/config';

describe('Unit / Config', () => {
let migrate;
let set = (key, value) => (migrate[key] = value);

beforeEach(() => {
migrate = {};
});

it('migrates v1 config', () => {
migration({
version: 1,
snapshot: {
widths: [1000]
},
agent: {
assetDiscovery: {
requestHeaders: { foo: 'bar' },
allowedHostnames: ['allowed'],
networkIdleTimeout: 150,
pagePoolSizeMin: 1,
pagePoolSizeMax: 5,
cacheResponses: false
}
}
}, set);

expect(migrate).toEqual({
snapshot: { widths: [1000] },
'snapshot.requestHeaders': { foo: 'bar' },
'discovery.allowedHostnames': ['allowed'],
'discovery.networkIdleTimeout': 150,
'discovery.concurrency': 5,
'discovery.disableCache': true
});
});

it('only migrates own config options', () => {
migration({
version: 1,
otherOptions: {
baseUrl: 'base-url',
snapshotFiles: '*.html',
ignoreFiles: '*.htm'
}
}, set);

expect(migrate).toEqual({});
});

it('does not migrate when not needed', () => {
migration({
version: 2,
discovery: {
allowedHostnames: ['allowed']
}
}, set);

expect(migrate).toEqual({});
});
});

0 comments on commit f060966

Please sign in to comment.