Skip to content

Commit

Permalink
Run visual tests for prod and canary configs (#10732)
Browse files Browse the repository at this point in the history
  • Loading branch information
rsimha committed Aug 1, 2017
1 parent 0e83eab commit b4fbfb8
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 22 deletions.
41 changes: 32 additions & 9 deletions build-system/tasks/prepend-global/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,27 @@ var util = require('gulp-util');


/**
* Checks that only 1 AMP_CONFIG should exist after append.
* Returns the number of AMP_CONFIG matches in the given config string.
*
* @param {string} str
* @return {boolean}
* @return {number}
*/
function sanityCheck(str) {
function numConfigs(str) {
var re = /\/\*AMP_CONFIG\*\//g;
// There must be one and exactly 1 match.
var matches = str.match(re)
return matches != null && matches.length == 1;
return matches == null ? 0 : matches.length;
}

/**
* Checks that only 1 AMP_CONFIG should exist after append.
*
* @param {string} str
*/
function sanityCheck(str) {
if (numConfigs(str) != 1) {
throw new Error(
'Found ' + numMatches + ' AMP_CONFIG(s) before write. Aborting!');
}
}

/**
Expand Down Expand Up @@ -105,6 +116,18 @@ function main() {
return;
}

if (argv.remove) {
return fs.readFileAsync(target)
.then(file => {
var contents = file.toString();
sanityCheck(contents);
var config =
/self\.AMP_CONFIG\|\|\(self\.AMP_CONFIG=.*?\/\*AMP_CONFIG\*\//;
contents = contents.replace(config, '');
return writeTarget(target, contents, argv.dryrun);
});
}

if (!(argv.prod || argv.canary)) {
util.log(util.colors.red('One of --prod or --canary should be provided.'));
return;
Expand Down Expand Up @@ -141,10 +164,7 @@ function main() {
return prependConfig(configFile, targetFile);
})
.then(fileString => {
if (!sanityCheck(fileString)) {
throw new Error('Found 0 or > 1 AMP_CONFIG(s) before write. ' +
'aborting');
}
sanityCheck(fileString);
return writeTarget(target, fileString, argv.dryrun);
});
}
Expand All @@ -159,6 +179,8 @@ gulp.task('prepend-global', 'Prepends a json config to a target file', main, {
'branch': ' Switch to a git branch to get config source from. ' +
'Uses master by default.',
'local': ' Don\'t switch branches and use local config',
'remove': ' Removes previously prepended json config from the target ' +
'file (if present).',
}
});

Expand All @@ -167,3 +189,4 @@ exports.prependConfig = prependConfig;
exports.writeTarget = writeTarget;
exports.valueOrDefault = valueOrDefault;
exports.sanityCheck = sanityCheck;
exports.numConfigs = numConfigs;
6 changes: 3 additions & 3 deletions build-system/tasks/prepend-global/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ test('sync - sanityCheck', t => {
var goodStr = 'self.AMP_CONFIG||(self.AMP_CONFIG={"hello":"world"})' +
'/*AMP_CONFIG*/' +
'var x = 1 + 1;';
t.false(m.sanityCheck(badStr));
t.true(m.sanityCheck(goodStr));
t.false(m.sanityCheck(badStr2));
t.false(m.numConfigs(badStr) == 1);
t.true(m.numConfigs(goodStr) == 1);
t.false(m.numConfigs(badStr2) == 1);
});
29 changes: 19 additions & 10 deletions build-system/tasks/visual-diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,14 @@
DEFAULT_WIDTHS = [375, 411, 1440]
HOST = 'localhost'
PORT = '8000'
CONFIGS = ['prod', 'canary']
AMP_RUNTIME_FILE = 'dist/amp.js'


# Colorize logs.
def red(text); "\e[31m#{text}\e[0m"; end
def cyan(text); "\e[36m#{text}\e[0m"; end
def green(text); "\e[32m#{text}\e[0m"; end


# Launches a background AMP webserver for unminified js using gulp.
Expand Down Expand Up @@ -126,16 +129,22 @@ def generateSnapshots(pagesToSnapshot)
if ARGV.include? '--master'
Percy::Capybara.snapshot(page, name: 'Blank page')
end
webpages.each do |webpage|
url = webpage["url"]
name = webpage["name"]
forbidden_css = webpage["forbidden_css"]
loading_incomplete_css = webpage["loading_incomplete_css"]
loading_complete_css = webpage["loading_complete_css"]
page.visit(url)
verifyCssElements(
page, forbidden_css, loading_incomplete_css, loading_complete_css)
Percy::Capybara.snapshot(page, name: name)
for config in CONFIGS
puts green('Generating snapshots using the ') + cyan("#{config}") +
green(' AMP config')
system("gulp prepend-global --target #{AMP_RUNTIME_FILE} --#{config}")
webpages.each do |webpage|
url = webpage["url"]
name = "#{webpage["name"]} (#{config})"
forbidden_css = webpage["forbidden_css"]
loading_incomplete_css = webpage["loading_incomplete_css"]
loading_complete_css = webpage["loading_complete_css"]
page.visit(url)
verifyCssElements(
page, forbidden_css, loading_incomplete_css, loading_complete_css)
Percy::Capybara.snapshot(page, name: name)
end
system("gulp prepend-global --target #{AMP_RUNTIME_FILE} --remove")
end
end
end
Expand Down

0 comments on commit b4fbfb8

Please sign in to comment.