Index: tools/testing/dart/utils.dart |
diff --git a/tools/testing/dart/utils.dart b/tools/testing/dart/utils.dart |
index f1abfaebcee77af0a9fdff2de10be1487432f240..97081135de152775035d4d6ebfbc3f720c95bea3 100644 |
--- a/tools/testing/dart/utils.dart |
+++ b/tools/testing/dart/utils.dart |
@@ -2,9 +2,12 @@ |
// 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:async'; |
import 'dart:io'; |
import 'dart:convert'; |
+import 'dart:math' as math; |
+import 'configuration.dart'; |
import 'path.dart'; |
// This is the maximum time we expect stdout/stderr of subprocesses to deliver |
@@ -255,3 +258,272 @@ class UniqueObject { |
UniqueObject() : _hashCode = ++_nextId; |
} |
+ |
+class LastModifiedCache { |
+ Map<String, DateTime> _cache = <String, DateTime>{}; |
+ |
+ /** |
+ * Returns the last modified date of the given [uri]. |
+ * |
+ * The return value will be cached for future queries. If [uri] is a local |
+ * file, it's last modified [Date] will be returned. If the file does not |
+ * exist, null will be returned instead. |
+ * In case [uri] is not a local file, this method will always return |
+ * the current date. |
+ */ |
+ DateTime getLastModified(Uri uri) { |
+ if (uri.scheme == "file") { |
+ if (_cache.containsKey(uri.path)) { |
+ return _cache[uri.path]; |
+ } |
+ var file = new File(new Path(uri.path).toNativePath()); |
+ _cache[uri.path] = file.existsSync() ? file.lastModifiedSync() : null; |
+ return _cache[uri.path]; |
+ } |
+ return new DateTime.now(); |
+ } |
+} |
+ |
+class ExistsCache { |
+ Map<String, bool> _cache = <String, bool>{}; |
+ |
+ /** |
+ * Returns true if the file in [path] exists, false otherwise. |
+ * |
+ * The information will be cached. |
+ */ |
+ bool doesFileExist(String path) { |
+ if (!_cache.containsKey(path)) { |
+ _cache[path] = new File(path).existsSync(); |
+ } |
+ return _cache[path]; |
+ } |
+} |
+ |
+class TestUtils { |
+ /** |
+ * Any script using TestUtils must set dartDirUri to a file:// URI |
+ * pointing to the root of the Dart checkout. |
+ */ |
+ static void setDartDirUri(Uri uri) { |
+ dartDirUri = uri; |
+ dartDir = new Path(uri.toFilePath()); |
+ } |
+ |
+ static math.Random rand = new math.Random.secure(); |
+ static Uri dartDirUri; |
+ static Path dartDir; |
+ static LastModifiedCache lastModifiedCache = new LastModifiedCache(); |
+ static ExistsCache existsCache = new ExistsCache(); |
+ static Path currentWorkingDirectory = new Path(Directory.current.path); |
+ |
+ /** |
+ * Generates a random number. |
+ */ |
+ static int getRandomNumber() { |
+ return rand.nextInt(0xffffffff); |
+ } |
+ |
+ /** |
+ * Creates a directory using a [relativePath] to an existing |
+ * [base] directory if that [relativePath] does not already exist. |
+ */ |
+ static Directory mkdirRecursive(Path base, Path relativePath) { |
+ if (relativePath.isAbsolute) { |
+ base = new Path('/'); |
+ } |
+ Directory dir = new Directory(base.toNativePath()); |
+ assert(dir.existsSync()); |
+ var segments = relativePath.segments(); |
+ for (String segment in segments) { |
+ base = base.append(segment); |
+ if (base.toString() == "/$segment" && |
+ segment.length == 2 && |
+ segment.endsWith(':')) { |
+ // Skip the directory creation for a path like "/E:". |
+ continue; |
+ } |
+ dir = new Directory(base.toNativePath()); |
+ if (!dir.existsSync()) { |
+ dir.createSync(); |
+ } |
+ assert(dir.existsSync()); |
+ } |
+ return dir; |
+ } |
+ |
+ /** |
+ * Copy a [source] file to a new place. |
+ * Assumes that the directory for [dest] already exists. |
+ */ |
+ static Future copyFile(Path source, Path dest) { |
+ return new File(source.toNativePath()) |
+ .openRead() |
+ .pipe(new File(dest.toNativePath()).openWrite()); |
+ } |
+ |
+ static Future copyDirectory(String source, String dest) { |
+ source = new Path(source).toNativePath(); |
+ dest = new Path(dest).toNativePath(); |
+ |
+ var executable = 'cp'; |
+ var args = ['-Rp', source, dest]; |
+ if (Platform.operatingSystem == 'windows') { |
+ executable = 'xcopy'; |
+ args = [source, dest, '/e', '/i']; |
+ } |
+ return Process.run(executable, args).then((ProcessResult result) { |
+ if (result.exitCode != 0) { |
+ throw new Exception("Failed to execute '$executable " |
+ "${args.join(' ')}'."); |
+ } |
+ }); |
+ } |
+ |
+ static Future deleteDirectory(String path) { |
+ // We are seeing issues with long path names on windows when |
+ // deleting them. Use the system tools to delete our long paths. |
+ // See issue 16264. |
+ if (Platform.operatingSystem == 'windows') { |
+ var native_path = new Path(path).toNativePath(); |
+ // Running this in a shell sucks, but rmdir is not part of the standard |
+ // path. |
+ return Process |
+ .run('rmdir', ['/s', '/q', native_path], runInShell: true) |
+ .then((ProcessResult result) { |
+ if (result.exitCode != 0) { |
+ throw new Exception('Can\'t delete path $native_path. ' |
+ 'This path might be too long'); |
+ } |
+ }); |
+ } else { |
+ var dir = new Directory(path); |
+ return dir.delete(recursive: true); |
+ } |
+ } |
+ |
+ static void deleteTempSnapshotDirectory(Configuration configuration) { |
+ if (configuration.compiler == Compiler.appJit || |
+ configuration.compiler == Compiler.precompiler) { |
+ var checked = configuration.isChecked ? '-checked' : ''; |
+ var strong = configuration.isStrong ? '-strong' : ''; |
+ var minified = configuration.isMinified ? '-minified' : ''; |
+ var csp = configuration.isCsp ? '-csp' : ''; |
+ var sdk = configuration.useSdk ? '-sdk' : ''; |
+ var dirName = "${configuration.compiler.name}" |
+ "$checked$strong$minified$csp$sdk"; |
+ var generatedPath = |
+ configuration.buildDirectory + "/generated_compilations/$dirName"; |
+ TestUtils.deleteDirectory(generatedPath); |
+ } |
+ } |
+ |
+ static final debugLogFilePath = new Path(".debug.log"); |
+ |
+ /// If a flaky test did fail, infos about it (i.e. test name, stdin, stdout) |
+ /// will be written to this file. |
+ /// |
+ /// This is useful for debugging flaky tests. When running on a buildbot, the |
+ /// file can be made visible in the waterfall UI. |
+ static const flakyFileName = ".flaky.log"; |
+ |
+ /// If test.py was invoked with '--write-test-outcome-log it will write |
+ /// test outcomes to this file. |
+ static const testOutcomeFileName = ".test-outcome.log"; |
+ |
+ static void ensureExists(String filename, Configuration configuration) { |
+ if (!configuration.listTests && !existsCache.doesFileExist(filename)) { |
+ throw "'$filename' does not exist"; |
+ } |
+ } |
+ |
+ static Path absolutePath(Path path) { |
+ if (!path.isAbsolute) { |
+ return currentWorkingDirectory.join(path); |
+ } |
+ return path; |
+ } |
+ |
+ static int shortNameCounter = 0; // Make unique short file names on Windows. |
+ |
+ static String getShortName(String path) { |
+ final PATH_REPLACEMENTS = const { |
+ "pkg_polymer_e2e_test_bad_import_test": "polymer_bi", |
+ "pkg_polymer_e2e_test_canonicalization_test": "polymer_c16n", |
+ "pkg_polymer_e2e_test_experimental_boot_test": "polymer_boot", |
+ "pkg_polymer_e2e_test_good_import_test": "polymer_gi", |
+ "tests_co19_src_Language_12_Expressions_14_Function_Invocation_": |
+ "co19_fn_invoke_", |
+ "tests_co19_src_LayoutTests_fast_css_getComputedStyle_getComputedStyle-": |
+ "co19_css_getComputedStyle_", |
+ "tests_co19_src_LayoutTests_fast_dom_Document_CaretRangeFromPoint_" |
+ "caretRangeFromPoint-": "co19_caretrangefrompoint_", |
+ "tests_co19_src_LayoutTests_fast_dom_Document_CaretRangeFromPoint_" |
+ "hittest-relative-to-viewport_": "co19_caretrange_hittest_", |
+ "tests_co19_src_LayoutTests_fast_dom_HTMLLinkElement_link-onerror-" |
+ "stylesheet-with-": "co19_dom_link-", |
+ "tests_co19_src_LayoutTests_fast_dom_": "co19_dom", |
+ "tests_co19_src_LayoutTests_fast_canvas_webgl": "co19_canvas_webgl", |
+ "tests_co19_src_LibTest_core_AbstractClassInstantiationError_" |
+ "AbstractClassInstantiationError_": "co19_abstract_class_", |
+ "tests_co19_src_LibTest_core_IntegerDivisionByZeroException_" |
+ "IntegerDivisionByZeroException_": "co19_division_by_zero", |
+ "tests_co19_src_WebPlatformTest_html_dom_documents_dom-tree-accessors_": |
+ "co19_dom_accessors_", |
+ "tests_co19_src_WebPlatformTest_html_semantics_embedded-content_" |
+ "media-elements_": "co19_media_elements", |
+ "tests_co19_src_WebPlatformTest_html_semantics_": "co19_semantics_", |
+ "tests_co19_src_WebPlatformTest_html-templates_additions-to-" |
+ "the-steps-to-clone-a-node_": "co19_htmltemplates_clone_", |
+ "tests_co19_src_WebPlatformTest_html-templates_definitions_" |
+ "template-contents-owner": "co19_htmltemplates_contents", |
+ "tests_co19_src_WebPlatformTest_html-templates_parsing-html-" |
+ "templates_additions-to-": "co19_htmltemplates_add_", |
+ "tests_co19_src_WebPlatformTest_html-templates_parsing-html-" |
+ "templates_appending-to-a-template_": "co19_htmltemplates_append_", |
+ "tests_co19_src_WebPlatformTest_html-templates_parsing-html-" |
+ "templates_clearing-the-stack-back-to-a-given-context_": |
+ "co19_htmltemplates_clearstack_", |
+ "tests_co19_src_WebPlatformTest_html-templates_parsing-html-" |
+ "templates_creating-an-element-for-the-token_": |
+ "co19_htmltemplates_create_", |
+ "tests_co19_src_WebPlatformTest_html-templates_template-element" |
+ "_template-": "co19_htmltemplates_element-", |
+ "tests_co19_src_WebPlatformTest_html-templates_": "co19_htmltemplate_", |
+ "tests_co19_src_WebPlatformTest_shadow-dom_shadow-trees_": |
+ "co19_shadow-trees_", |
+ "tests_co19_src_WebPlatformTest_shadow-dom_elements-and-dom-objects_": |
+ "co19_shadowdom_", |
+ "tests_co19_src_WebPlatformTest_shadow-dom_html-elements-in-" |
+ "shadow-trees_": "co19_shadow_html_", |
+ "tests_co19_src_WebPlatformTest_html_webappapis_system-state-and-" |
+ "capabilities_the-navigator-object": "co19_webappapis_navigator_", |
+ "tests_co19_src_WebPlatformTest_DOMEvents_approved_": "co19_dom_approved_" |
+ }; |
+ |
+ // Some tests are already in [build_dir]/generated_tests. |
+ String GEN_TESTS = 'generated_tests/'; |
+ if (path.contains(GEN_TESTS)) { |
+ int index = path.indexOf(GEN_TESTS) + GEN_TESTS.length; |
+ path = 'multitest/${path.substring(index)}'; |
+ } |
+ path = path.replaceAll('/', '_'); |
+ final int WINDOWS_SHORTEN_PATH_LIMIT = 58; |
+ final int WINDOWS_PATH_END_LENGTH = 30; |
+ if (Platform.operatingSystem == 'windows' && |
+ path.length > WINDOWS_SHORTEN_PATH_LIMIT) { |
+ for (var key in PATH_REPLACEMENTS.keys) { |
+ if (path.startsWith(key)) { |
+ path = path.replaceFirst(key, PATH_REPLACEMENTS[key]); |
+ break; |
+ } |
+ } |
+ if (path.length > WINDOWS_SHORTEN_PATH_LIMIT) { |
+ ++shortNameCounter; |
+ var pathEnd = path.substring(path.length - WINDOWS_PATH_END_LENGTH); |
+ path = "short${shortNameCounter}_$pathEnd"; |
+ } |
+ } |
+ return path; |
+ } |
+} |