From 35d192200607f3b15f506a0aca155de35d3c48a2 Mon Sep 17 00:00:00 2001 From: Georgios Kalpakas Date: Mon, 15 May 2017 15:43:08 +0300 Subject: [PATCH] ci(aio): log the full PWA testing results This will help diagnose errors or regressions in PWA scores. --- aio/scripts/deploy-preview.sh | 2 +- aio/scripts/deploy-staging.sh | 2 +- aio/scripts/test-pwa-score.js | 40 +++++++++++++++++++++-------------- scripts/ci/env.sh | 3 +++ 4 files changed, 29 insertions(+), 18 deletions(-) diff --git a/aio/scripts/deploy-preview.sh b/aio/scripts/deploy-preview.sh index a6380f6369..b1ff1e7617 100755 --- a/aio/scripts/deploy-preview.sh +++ b/aio/scripts/deploy-preview.sh @@ -69,5 +69,5 @@ readonly relevantChangedFilesCount=$(git diff --name-only $TRAVIS_COMMIT_RANGE | fi # Run PWA-score tests - yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE" + yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE" "$PWA_RESULTS_LOG" ) diff --git a/aio/scripts/deploy-staging.sh b/aio/scripts/deploy-staging.sh index c7e8eea59d..15943643ad 100755 --- a/aio/scripts/deploy-staging.sh +++ b/aio/scripts/deploy-staging.sh @@ -20,5 +20,5 @@ DEPLOYED_URL=https://$FIREBASE_PROJECT_ID.firebaseapp.com # Run PWA-score tests # TODO(gkalpak): Figure out why this fails and re-enable. sleep 10 - yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE" || true + yarn test-pwa-score -- "$DEPLOYED_URL" "$MIN_PWA_SCORE" "$PWA_RESULTS_LOG" || true ) diff --git a/aio/scripts/test-pwa-score.js b/aio/scripts/test-pwa-score.js index d4d4005774..505ebc96c5 100644 --- a/aio/scripts/test-pwa-score.js +++ b/aio/scripts/test-pwa-score.js @@ -2,11 +2,10 @@ /** * Usage: - * node scripts/test-pwa-score [ []] + * node scripts/test-pwa-score [] * - * Defaults: - * url: http://localhost:4200 - * minScore: 90 + * Fails if the score is below ``. + * If `` is defined, the full results will be logged there. * * (Ignores HTTPS-related audits, when run for HTTP URL.) */ @@ -18,7 +17,7 @@ const Printer = require('lighthouse/lighthouse-cli/printer'); const config = require('lighthouse/lighthouse-core/config/default.json'); // Constants -const FLAGS = {output: 'json'}; +const VIEWER_URL = 'https://googlechrome.github.io/lighthouse/viewer/'; // Work-around traceviewer-js bug. global.atob = str => new Buffer(str, 'base64').toString('binary'); @@ -34,7 +33,7 @@ _main(process.argv.slice(2)); // Functions - Definitions function _main(args) { - const {url, minScore} = parseInput(args); + const {url, minScore, logFile} = parseInput(args); const isOnHttp = /^http:/.test(url); console.log(`Running PWA audit for '${url}'...`); @@ -43,8 +42,8 @@ function _main(args) { ignoreHttpsAudits(config.aggregations); } - launchChromeAndRunLighthouse(url, FLAGS, config). - then(getScore). + launchChromeAndRunLighthouse(url, {}, config). + then(results => processResults(results, logFile)). then(score => evaluateScore(minScore, score)). catch(onError); } @@ -59,13 +58,6 @@ function evaluateScore(expectedScore, actualScore) { } } -function getScore(results) { - const scoredAggregations = results.aggregations.filter(a => a.scored); - const total = scoredAggregations.reduce((sum, a) => sum + a.total, 0); - - return Math.round((total / scoredAggregations.length) * 100); -} - function ignoreHttpsAudits(aggregations) { const httpsAudits = [ 'is-on-https', @@ -99,6 +91,7 @@ function onError(err) { function parseInput(args) { const url = args[0]; const minScore = Number(args[1]); + const logFile = args[2]; if (!url) { onError('Invalid arguments: not specified.'); @@ -106,5 +99,20 @@ function parseInput(args) { onError('Invalid arguments: not specified or not a number.'); } - return {url, minScore}; + return {url, minScore, logFile}; +} + +function processResults(results, logFile) { + if (logFile) { + console.log(`Saving results in '${logFile}'...`); + console.log(`(LightHouse viewer: ${VIEWER_URL})`); + + results.artifacts = undefined; // Avoid circular dependency errors. + Printer.write(results, 'json', logFile); + } + + const scoredAggregations = results.aggregations.filter(a => a.scored); + const total = scoredAggregations.reduce((sum, a) => sum + a.total, 0); + + return Math.round((total / scoredAggregations.length) * 100); } diff --git a/scripts/ci/env.sh b/scripts/ci/env.sh index 336cb86fab..cb42cd04b4 100755 --- a/scripts/ci/env.sh +++ b/scripts/ci/env.sh @@ -120,6 +120,9 @@ setEnvVar PATH $HOME/.yarn/bin:$PATH setEnvVar NODE_PATH ${NODE_PATH:-}:${PROJECT_ROOT}/dist/all:${PROJECT_ROOT}/dist/tools setEnvVar LOGS_DIR /tmp/angular-build/logs +# Log file for full PWA testing results +setEnvVar PWA_RESULTS_LOG $LOGS_DIR/pwa-results.json + # strip leading "/home/travis/build/angular/angular/" or "./" path. Could this be done in one shot? CURRENT_SHELL_SOURCE_FILE=${BASH_SOURCE#${PROJECT_ROOT}/} export CURRENT_SHELL_SOURCE_FILE=${CURRENT_SHELL_SOURCE_FILE#./}