From 6e7db32a250444aa0013a491a8e4249dc2d9704f Mon Sep 17 00:00:00 2001 From: Patrick Hulce Date: Thu, 19 Jul 2018 10:15:27 -0700 Subject: [PATCH] misc(i18n): add assertion script (#5686) --- .travis.yml | 1 + .../scripts/i18n/assert-strings-collected.sh | 50 +++++++++++++++++++ .../scripts/i18n/collect-strings.js | 2 +- package.json | 2 + 4 files changed, 54 insertions(+), 1 deletion(-) create mode 100755 lighthouse-core/scripts/i18n/assert-strings-collected.sh diff --git a/.travis.yml b/.travis.yml index d8870d064f69..210f03e6ee8a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -41,6 +41,7 @@ script: - yarn test-extension - yarn test-viewer - yarn test-lantern + - yarn i18n:checks # _JAVA_OPTIONS is breaking parsing of compiler output. See #3338. - unset _JAVA_OPTIONS # FIXME(paulirish): re-enable after a roll of LH->CDT diff --git a/lighthouse-core/scripts/i18n/assert-strings-collected.sh b/lighthouse-core/scripts/i18n/assert-strings-collected.sh new file mode 100755 index 000000000000..c5d6a9ae6167 --- /dev/null +++ b/lighthouse-core/scripts/i18n/assert-strings-collected.sh @@ -0,0 +1,50 @@ +#!/usr/bin/env bash + +## +# @license Copyright 2018 Google Inc. All Rights Reserved. +# Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 +# Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +## + +pwd="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" +lhroot_path="$pwd/../../.." +lh_tmp_path="$lhroot_path/.tmp" +mkdir -p "$lh_tmp_path" + +purple='\033[1;35m' +red='\033[1;31m' +green='\033[1;32m' +colorText() { + printf "\\n$2$1%b\\n" '\033[0m' +} + +collectedstringsPath="$lhroot_path/lighthouse-core/lib/locales/en-US.json"; +currentstringsPath="$lh_tmp_path/current_strings.json"; +freshstringsPath="$lh_tmp_path/fresh_strings.json"; + +# always run this before exiting +function teardown { rm -f "$currentstringsPath" "$freshstringsPath"; } +trap teardown EXIT + +cp "$collectedstringsPath" "$currentstringsPath" + +colorText "Collecting strings..." "$purple" +set -x +node "$lhroot_path/lighthouse-core/scripts/i18n/collect-strings.js" +set +x + +cp "$collectedstringsPath" "$freshstringsPath" + +colorText "Diff'ing golden strings against the fresh strings" "$purple" +git --no-pager diff --color=always --no-index "$currentstringsPath" "$freshstringsPath" + +# Use the return value from last command +retVal=$? + +if [ $retVal -eq 0 ]; then + colorText "✅ PASS. All strings have been collected." "$green" +else + colorText "❌ FAIL. Strings have changed." "$red" + echo "Commit the changes to lighthouse-core/lib/locales/ update the strings." +fi +exit $retVal diff --git a/lighthouse-core/scripts/i18n/collect-strings.js b/lighthouse-core/scripts/i18n/collect-strings.js index 72bbae1c29f1..582d8197175d 100644 --- a/lighthouse-core/scripts/i18n/collect-strings.js +++ b/lighthouse-core/scripts/i18n/collect-strings.js @@ -91,7 +91,7 @@ function writeStringsToLocaleFormat(locale, strings) { output[key] = {message}; } - fs.writeFileSync(fullPath, JSON.stringify(output, null, 2)); + fs.writeFileSync(fullPath, JSON.stringify(output, null, 2) + '\n'); } const strings = collectAllStringsInDir(path.join(LH_ROOT, 'lighthouse-core')); diff --git a/package.json b/package.json index f15e5ab79d77..d19015d413c8 100644 --- a/package.json +++ b/package.json @@ -62,6 +62,8 @@ "plots-smoke": "bash plots/test/smoke.sh", "changelog": "conventional-changelog --config ./build/changelog-generator/index.js --infile changelog.md --same-file", "type-check": "tsc -p . && cd ./lighthouse-viewer && yarn type-check", + "i18n:checks": "./lighthouse-core/scripts/i18n/assert-strings-collected.sh", + "i18n:collect-strings": "node lighthouse-core/scripts/i18n/collect-strings.js", "update:sample-artifacts": "node lighthouse-core/scripts/update-report-fixtures.js -G", "update:sample-json": "node ./lighthouse-cli -A=./lighthouse-core/test/results/artifacts --throttling-method=devtools --output=json --output-path=./lighthouse-core/test/results/sample_v2.json && node lighthouse-core/scripts/cleanup-LHR-for-diff.js ./lighthouse-core/test/results/sample_v2.json --only-remove-timing", "diff:sample-json": "bash lighthouse-core/scripts/assert-golden-lhr-unchanged.sh",