Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: a script to update baselines #100

Merged
merged 2 commits into from
Nov 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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"
Expand Down
100 changes: 100 additions & 0 deletions typescript/tools/update-baselines.ts
Original file line number Diff line number Diff line change
@@ -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);