blob: a9508a4b6fdb13390e43b58bcff6c7649ce34cc4 [file] [log] [blame]
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Test that cps_ir/update_all.dart and every cps_ir/*_test.dart file are up to
/// date. There should be a line in update_all and a file in the cps_ir folder
/// for each test file in the `input/` folder.
library tests.compiler.dart2js.cps_ir.up_to_date_test;
import 'dart:io';
main(args) {
bool update = args.length > 0 && args[0] == 'update';
var inputDir = new Directory.fromUri(Platform.script.resolve('input'));
bool errorsFound = false;
// Note: we create a test file per input file because invoking dart2js many
// times on a single test often makes test.py timeout.
//
// We tried using multi-tests for this, but it is unfortunately brittle. For
// example, multi-tests can't import code from one folder above the current
// directory, which prevents us from putting generated tests under a different
// folder than the rest of the helpers (like memory_compiler.dart)
var files = inputDir.listSync().map((f) => f.uri.pathSegments.last).toList();
files.sort();
for (var file in files) {
var testFilename = file.replaceAll('.dart', '_test.dart');
var contents = generateTestFile(file);
if (checkAndMaybeUpdate(testFilename, contents, update)) errorsFound = true;
}
var updateAllContents = generateUpdateAllFile(files);
if (checkAndMaybeUpdate('update_all.dart', updateAllContents, update)) {
errorsFound = true;
}
if (errorsFound) {
print(regenerateMessage);
exit(1);
}
}
/// Checks and if [update] is true, updates an autogenerated test file.
///
/// This doesn't update an actual test expectation, that's done by executing the
/// files that we generate here.
bool checkAndMaybeUpdate(String filename, String contents,
bool update) {
var testFile = new File.fromUri(Platform.script.resolve(filename));
bool exists = testFile.existsSync();
var isUpToDate = exists && testFile.readAsStringSync() == contents;
if (isUpToDate) {
print('PASS: ${filename} is up to date.');
} else if (update) {
testFile.writeAsStringSync(contents);
print('INFO: ${filename} was updated.');
} else {
print("FAILED: ${filename} is ${exists ? 'out of date' : 'missing'}");
return true;
}
return false;
}
String generateUpdateAllFile(List<String> files) {
var lines = files.map((f) => "runTest('$f', update: true);");
return '''
// ---- AUTO-GENERATED -------------------
// This file was autogenerated by running:
//
// dart path/to/${Platform.script.pathSegments.last} update
//
// Do not edit this file by hand.
// ---------------------------------------
library tests.compiler.dart2js.cps_ir.update_all;
import 'runner.dart';
main(args) {
${lines.join('\n ')}
}
''';
}
String generateTestFile(String file) => '''
// ---- AUTO-GENERATED -------------------
// This file was autogenerated by running:
//
// dart path/to/${Platform.script.pathSegments.last} update
//
// Do not edit this file by hand.
// ---------------------------------------
library tests.compiler.dart2js.cps_ir.$file;
import 'runner.dart';
main(args) {
runTest("$file", update: args.length > 0 && args[0] == "update");
}
''';
String get regenerateMessage {
var flags = Platform.packageRoot == null
? '' : '--package-root=${Platform.packageRoot} ';
return '''
To regenerate the test files, please run:
dart $flags${Platform.script} update''';
}