| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 |
| 5 /* |
| 6 * This test makes sure that the "skipping Dart2Js compilations if the output is |
| 7 * already up to date" feature does work as it should. |
| 8 * Therefore this test ensures that compilations are only skipped if the last |
| 9 * modified date of the output of a dart2js compilation is newer than |
| 10 * - the the dart application to compile (including it's dependencies) |
| 11 * - the dart2js snapshot |
| 12 * Furtheremore it ensure that a compilations is not skipped if any of the |
| 13 * necessary files could not be found (dart2js snapshots, previous dart2js |
| 14 * output (+deps file), dart application) |
| 15 */ |
| 16 |
| 17 import 'dart:io'; |
| 18 import 'dart:isolate'; |
| 19 import 'dart:uri'; |
| 20 import '../../../tools/testing/dart/test_suite.dart' as suite; |
| 21 import '../../../tools/testing/dart/test_runner.dart' as runner; |
| 22 import '../../../tools/testing/dart/test_options.dart' as options; |
| 23 import '../../../tools/testing/dart/status_file_parser.dart' as status; |
| 24 |
| 25 /** |
| 26 * This class is reponsible for setting up the files necessary for this test |
| 27 * as well as touching a file. |
| 28 */ |
| 29 class FileUtils { |
| 30 Directory tempDir; |
| 31 File testJs; |
| 32 File testJsDeps; |
| 33 File testDart; |
| 34 File testSnapshot; |
| 35 |
| 36 FileUtils({bool createJs, |
| 37 bool createJsDeps, |
| 38 bool createDart, |
| 39 bool createSnapshot}) { |
| 40 tempDir = new Directory('').createTempSync(); |
| 41 if (createJs) { |
| 42 testJs = _createFile(testJsFilePath); |
| 43 _writeToFile(testJs, "test.js content"); |
| 44 } |
| 45 if (createSnapshot) { |
| 46 testSnapshot = _createFile(testSnapshotFilePath); |
| 47 _writeToFile(testSnapshot, "dart2js snapshot"); |
| 48 } |
| 49 if (createDart) { |
| 50 testDart = _createFile(testDartFilePath); |
| 51 _writeToFile(testDart, "dart code"); |
| 52 } |
| 53 if (createJsDeps) { |
| 54 testJsDeps = _createFile(testJsDepsFilePath); |
| 55 var path = suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path)) |
| 56 .append("test.dart"); |
| 57 _writeToFile(testJsDeps, "file://$path"); |
| 58 } |
| 59 } |
| 60 |
| 61 void cleanup() { |
| 62 if (testJs != null) testJs.deleteSync(); |
| 63 if (testJsDeps != null) testJsDeps.deleteSync(); |
| 64 if (testDart != null) testDart.deleteSync(); |
| 65 if (testSnapshot != null) testSnapshot.deleteSync(); |
| 66 |
| 67 // if the script did run, it created this file, so we need to delete it |
| 68 File file = new File(scriptOutputPath.toNativePath()); |
| 69 if (file.existsSync()) { |
| 70 file.deleteSync(); |
| 71 } |
| 72 |
| 73 tempDir.deleteSync(); |
| 74 } |
| 75 |
| 76 Path get scriptOutputPath { |
| 77 return suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 78 .append('created_if_command_did_run.txt')); |
| 79 } |
| 80 |
| 81 Path get testDartFilePath { |
| 82 return suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 83 .append('test.dart')); |
| 84 } |
| 85 |
| 86 Path get testJsFilePath { |
| 87 return suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 88 .append('test.js')); |
| 89 } |
| 90 |
| 91 Path get testJsDepsFilePath { |
| 92 return suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 93 .append('test.js.deps')); |
| 94 } |
| 95 |
| 96 Path get testSnapshotFilePath { |
| 97 return suite.TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 98 .append('test_dart2js.snapshot')); |
| 99 } |
| 100 |
| 101 void touchFile(File file) { |
| 102 _writeToFile(file, _readFile(file)); |
| 103 } |
| 104 |
| 105 void _writeToFile(File file, String content) { |
| 106 if (content != null) { |
| 107 var fd = new File(file.fullPathSync()).openSync(FileMode.WRITE); |
| 108 fd.writeStringSync(content); |
| 109 fd.closeSync(); |
| 110 } |
| 111 } |
| 112 |
| 113 String _readFile(File file) { |
| 114 return file.readAsStringSync(); |
| 115 } |
| 116 |
| 117 File _createFile(Path path) { |
| 118 var file = new File(path.toNativePath()); |
| 119 file.createSync(); |
| 120 return file; |
| 121 } |
| 122 } |
| 123 |
| 124 class TestCompletedHandler { |
| 125 FileUtils fileUtils; |
| 126 Date _expectedTimestamp; |
| 127 bool _shouldHaveRun; |
| 128 |
| 129 TestCompletedHandler(FileUtils this.fileUtils, bool this._shouldHaveRun); |
| 130 |
| 131 void processCompletedTest(runner.TestCase testCase) { |
| 132 var output = testCase.lastCommandOutput; |
| 133 |
| 134 Expect.isFalse(output.unexpectedOutput); |
| 135 Expect.isTrue(output.stderr.length == 0); |
| 136 if (_shouldHaveRun) { |
| 137 Expect.isTrue(output.stdout.length == 0); |
| 138 Expect.isTrue(new File(fileUtils.scriptOutputPath.toNativePath()) |
| 139 .existsSync()); |
| 140 } else { |
| 141 Expect.isFalse(new File(fileUtils.scriptOutputPath.toNativePath()) |
| 142 .existsSync()); |
| 143 } |
| 144 fileUtils.cleanup(); |
| 145 } |
| 146 } |
| 147 |
| 148 runner.TestCase makeTestCase(String testName, |
| 149 TestCompletedHandler completedHandler) { |
| 150 var fileUtils = completedHandler.fileUtils; |
| 151 var config = new options.TestOptionsParser().parse(['--timeout', '2'])[0]; |
| 152 var scriptDirPath = new Path.fromNative(new Options().script).directoryPath; |
| 153 var createFileScript = scriptDirPath. |
| 154 append('skipping_dart2js_compilations_helper.dart').toNativePath(); |
| 155 var executable = new Options().executable; |
| 156 var arguments = [createFileScript, fileUtils.scriptOutputPath.toNativePath()]; |
| 157 var bootstrapDeps = [ |
| 158 new Uri("file://${fileUtils.testSnapshotFilePath}")]; |
| 159 var commands = [new runner.Dart2JsCommand( |
| 160 fileUtils.testJsFilePath.toNativePath(), |
| 161 false, |
| 162 bootstrapDeps, |
| 163 executable, |
| 164 arguments)]; |
| 165 return new runner.TestCase( |
| 166 testName, |
| 167 commands, |
| 168 config, |
| 169 completedHandler.processCompletedTest, |
| 170 new Set<String>.from([status.PASS])); |
| 171 } |
| 172 |
| 173 void main() { |
| 174 var fs_noTestJs = new FileUtils(createJs: false, |
| 175 createJsDeps: true, |
| 176 createDart: true, |
| 177 createSnapshot: true); |
| 178 var fs_noTestJsDeps = new FileUtils(createJs: true, |
| 179 createJsDeps: false, |
| 180 createDart: true, |
| 181 createSnapshot: true); |
| 182 var fs_noTestDart = new FileUtils(createJs: true, |
| 183 createJsDeps: true, |
| 184 createDart: false, |
| 185 createSnapshot: true); |
| 186 var fs_noTestSnapshot = new FileUtils(createJs: true, |
| 187 createJsDeps: true, |
| 188 createDart: true, |
| 189 createSnapshot: false); |
| 190 var fs_notUpToDate_snapshot = new FileUtils(createJs: true, |
| 191 createJsDeps: true, |
| 192 createDart: true, |
| 193 createSnapshot: true); |
| 194 var fs_notUpToDate_dart = new FileUtils(createJs: true, |
| 195 createJsDeps: true, |
| 196 createDart: true, |
| 197 createSnapshot: true); |
| 198 var fs_upToDate = new FileUtils(createJs: true, |
| 199 createJsDeps: true, |
| 200 createDart: true, |
| 201 createSnapshot: true); |
| 202 |
| 203 void touchFilesAndRunTests(Timer unused) { |
| 204 fs_notUpToDate_snapshot.touchFile(fs_notUpToDate_snapshot.testSnapshot); |
| 205 fs_notUpToDate_dart.touchFile(fs_notUpToDate_dart.testDart); |
| 206 fs_upToDate.touchFile(fs_upToDate.testJs); |
| 207 |
| 208 void runTest(String name, FileUtils fileUtils, bool shouldRun) { |
| 209 new runner.RunningProcess(makeTestCase(name, |
| 210 new TestCompletedHandler(fileUtils, shouldRun))).start(); |
| 211 } |
| 212 runTest("fs_noTestJs", fs_noTestJs, true); |
| 213 runTest("fs_noTestJsDeps", fs_noTestJsDeps, true); |
| 214 runTest("fs_noTestDart", fs_noTestDart, true); |
| 215 runTest("fs_noTestSnapshot", fs_noTestSnapshot, true); |
| 216 runTest("fs_notUpToDate_snapshot", fs_notUpToDate_snapshot, true); |
| 217 runTest("fs_notUpToDate_dart", fs_notUpToDate_dart, true); |
| 218 // This is the only test where all dependencies are present and the test.js |
| 219 // file is newer than all the others. So we pass 'false' for shouldRun. |
| 220 runTest("fs_upToDate", fs_upToDate, false); |
| 221 } |
| 222 // We need to wait some time to make sure that the files we 'touch' get a |
| 223 // bigger timestamp than the old ones |
| 224 new Timer(1000, touchFilesAndRunTests); |
| 225 } |
| OLD | NEW |