From 07cabdc993b734459699ecddf08f0d05e5b81336 Mon Sep 17 00:00:00 2001 From: Alexander Fenster Date: Mon, 4 Nov 2019 13:31:19 -0800 Subject: [PATCH] feat: a script to update baselines (#100) --- package.json | 2 + typescript/tools/update-baselines.ts | 100 +++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 typescript/tools/update-baselines.ts diff --git a/package.json b/package.json index d4784f895..09ebda403 100644 --- a/package.json +++ b/package.json @@ -39,6 +39,7 @@ "@types/fs-extra": "^8.0.1", "@types/get-stdin": "^5.0.1", "@types/mocha": "^5.2.5", + "@types/ncp": "^2.0.3", "@types/node": "^11.13.22", "@types/nunjucks": "^3.1.0", "@types/object-hash": "^1.3.0", @@ -51,6 +52,7 @@ "gts": "^1.0.0", "intelli-espower-loader": "^1.0.1", "mocha": "^6.2.1", + "ncp": "^2.0.0", "power-assert": "^1.6.0", "rimraf": "^3.0.0", "typescript": "~3.6.0" diff --git a/typescript/tools/update-baselines.ts b/typescript/tools/update-baselines.ts new file mode 100644 index 000000000..a30d4287f --- /dev/null +++ b/typescript/tools/update-baselines.ts @@ -0,0 +1,100 @@ +#!/usr/bin/env node + +// Copyright 2019 Google LLC +// +// 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. + +// Updates baselines. Useful if many templates were changed and the changes +// needs to be propagated to all baselines. +// Usage: node build/tools/update-baselines.js + +import { exec } from 'child_process'; +import * as path from 'path'; +import * as rimraf from 'rimraf'; +import { promisify } from 'util'; +import { readdir, stat, mkdir, existsSync } from 'fs'; +import * as ncp from 'ncp'; + +const rmrf = promisify(rimraf); +const readdirp = promisify(readdir); +const statp = promisify(stat); +const mkdirp = promisify(mkdir); +const execp = promisify(exec); +const ncpp = promisify(ncp); + +const root = path.resolve(__dirname, '..', '..'); +const resultPrefix = /^\.test-out-(.*)$/; + +function getBaselineDirectory(library: string): string { + return path.join(root, 'typescript', 'test', 'testdata', library); +} + +function getBaselineFilename(library: string, filename: string): string { + return path.join(getBaselineDirectory(library), `${filename}.baseline`); +} + +async function copyBaseline(library: string, root: string, directory = '.') { + const start = path.join(root, directory); + const targetDirectory = path.join(getBaselineDirectory(library), directory); + if (!existsSync(targetDirectory)) { + await mkdirp(targetDirectory); + } + const files = await readdirp(start); + for (const file of files) { + const relativePath = `${directory}${path.sep}${file}`; + const absolutePath = path.join(start, file); + const stat = await statp(absolutePath); + if (stat.isDirectory()) { + await copyBaseline(library, root, relativePath); + } else if (stat.isFile()) { + const baseline = getBaselineFilename(library, relativePath); + await ncpp(absolutePath, baseline); + console.log(` - ${relativePath}`); + } + } +} + +async function main() { + // generate test output + try { + console.log(`Running npm test...`); + await execp('npm test'); + console.log('Tests passed! No need to update baselines.'); + return; + } catch (err) { + console.log(`Tests failed - that's OK, will update baselines.`); + } + + // get a list of baselines + const files = await readdirp(root); + const outDirs = files.filter(file => file.match(resultPrefix)); + + // update baselines for all libraries + for (const dir of outDirs) { + const match = dir.match(resultPrefix); + if (!match) { + throw new Error(`Cannot extract library name from ${dir}`); + } + const library = match[1]; + const baselineDir = getBaselineDirectory(library); + + console.log(`Updating baseline for ${library}...`); + console.log(` - rm -rf "${baselineDir}"...`); + await rmrf(baselineDir); + console.log(` - copying files from ${dir}...`); + await copyBaseline(library, path.join(root, dir)); + console.log('done!'); + } +} + +main().catch(console.error);