Files
sdk/pkg/kernel/test/baseline_tester.dart
T
Asger Feldthaus 8bfc4b47c0 Implement canonical name scheme in kernel.
This adds a class CanonicalName that can represent a library, class,
or member.  All references now go through a Reference object, which is
linked to both the AST node and its CanonicalName, so either can be
created first.

dartk now accepts multiple input files:
- If multiple dart files are given, they are all compiled.
- If multiple binaries are given, they are linked together.
Mixed dart and binary input is not supported by dartk.

dartk now has a flag --include-sdk which includes the entire SDK in
the output.  This is so the SDK can be compiled alone and then linked.

Example of compiling separately and then linking:
  dartk foo.dart -o foo.dill
  dartk main.dart -o main.dill
  dartk --include-sdk -o sdk.dill
  dartk main.dill foo.dill sdk.dill --target=vm --link -o program.dill

dartk still has incredibly slow cold start due to the analyzer loading
the dart sdk, so this does not actually speed things up at the moment.

BUG=
R=ahe@google.com, kmillikin@google.com, kustermann@google.com, sigmund@google.com

Review-Url: https://codereview.chromium.org/2665723002 .
2017-02-23 14:12:10 +01:00

90 lines
3.6 KiB
Dart

// 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.
import 'dart:io';
import 'package:kernel/analyzer/loader.dart';
import 'package:kernel/application_root.dart';
import 'package:kernel/kernel.dart';
import 'package:kernel/target/targets.dart';
import 'package:kernel/text/ast_to_text.dart';
import 'package:kernel/verifier.dart';
import 'package:path/path.dart' as pathlib;
import 'package:test/test.dart';
final String testcaseDirectory = 'pkg/kernel/testcases';
final String inputDirectory = 'pkg/kernel/testcases/input';
final String sdkDirectory = 'sdk';
/// A target to be used for testing.
///
/// To simplify testing dependencies, we avoid transformations that rely on
/// a patched SDK or any SDK changes that have not landed in the main SDK.
abstract class TestTarget extends Target {
/// Annotations to apply on the textual output.
Annotator get annotator => null;
// Return a list of strings so that we can accumulate errors.
List<String> performModularTransformations(Program program);
List<String> performGlobalTransformations(Program program);
}
void runBaselineTests(String folderName, TestTarget target) {
String outputDirectory = '$testcaseDirectory/$folderName';
var batch = new DartLoaderBatch();
Directory directory = new Directory(inputDirectory);
var applicationRoot = new ApplicationRoot(directory.absolute.path);
for (FileSystemEntity file in directory.listSync()) {
if (file is File && file.path.endsWith('.dart')) {
String name = pathlib.basename(file.path);
test(name, () async {
Uri dartPath =
new Uri(scheme: 'file', path: pathlib.absolute(file.path));
String shortName = pathlib.withoutExtension(name);
String filenameOfBaseline = '$outputDirectory/$shortName.baseline.txt';
String filenameOfCurrent = '$outputDirectory/$shortName.current.txt';
var program = new Program();
var loader = await batch.getLoader(
program,
new DartOptions(
strongMode: target.strongMode,
sdk: sdkDirectory,
declaredVariables: target.extraDeclaredVariables,
applicationRoot: applicationRoot));
loader.loadProgram(dartPath, target: target);
verifyProgram(program);
var errors = <String>[];
errors.addAll(target.performModularTransformations(program));
verifyProgram(program);
errors.addAll(target.performGlobalTransformations(program));
verifyProgram(program);
var buffer = new StringBuffer();
for (var error in errors) {
buffer.writeln('// $error');
}
new Printer(buffer, annotator: target.annotator)
.writeLibraryFile(program.mainMethod.enclosingLibrary);
String current = '$buffer';
new File(filenameOfCurrent).writeAsStringSync(current);
var baselineFile = new File(filenameOfBaseline);
if (!baselineFile.existsSync()) {
new File(filenameOfBaseline).writeAsStringSync(current);
} else {
var baseline = baselineFile.readAsStringSync();
if (baseline != current) {
fail('Output of `$name` changed for $folderName.\n'
'Command to reset the baseline:\n'
' rm $filenameOfBaseline\n'
'Command to see the diff:\n'
' diff -cd $outputDirectory/$shortName.{baseline,current}.txt'
'\n');
}
}
});
}
}
}