| 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 #import("dart:io"); |
| 6 #import("dart:isolate"); |
| 7 #import("dart:uri"); |
| 8 #import("../../../tools/testing/dart/test_suite.dart"); |
| 9 #import("../../../tools/testing/dart/test_runner.dart"); |
| 10 #import("../../../tools/testing/dart/test_options.dart"); |
| 11 #import("../../../tools/testing/dart/status_file_parser.dart"); |
| 12 |
| 13 class FsUtils { |
| 14 Directory tempDir; |
| 15 File testJs; |
| 16 File testJsDeps; |
| 17 File testDart; |
| 18 File testSnapshot; |
| 19 |
| 20 FsUtils({bool createJs, |
| 21 bool createJsDeps, |
| 22 bool createDart, |
| 23 bool createSnapshot}) { |
| 24 tempDir = new Directory('').createTempSync(); |
| 25 if (createJs) { |
| 26 testJs = _createFile(testJsFilePath); |
| 27 _writeToFile(testJs, "test.js content"); |
| 28 } |
| 29 if (createSnapshot) { |
| 30 testSnapshot = _createFile(testSnapshotFilePath); |
| 31 _writeToFile(testSnapshot, "dart2js snapshot"); |
| 32 } |
| 33 if (createDart) { |
| 34 testDart = _createFile(testDartFilePath); |
| 35 _writeToFile(testDart, "dart code"); |
| 36 } |
| 37 if (createJsDeps) { |
| 38 testJsDeps = _createFile(testJsDepsFilePath); |
| 39 var path = TestUtils.absolutePath(new Path.fromNative(tempDir.path)) |
| 40 .append("test.dart"); |
| 41 _writeToFile(testJsDeps, "file://$path"); |
| 42 } |
| 43 } |
| 44 |
| 45 void cleanup() { |
| 46 if (testJs != null) testJs.deleteSync(); |
| 47 if (testJsDeps != null) testJsDeps.deleteSync(); |
| 48 if (testDart != null) testDart.deleteSync(); |
| 49 if (testSnapshot != null) testSnapshot.deleteSync(); |
| 50 |
| 51 // if the script did run, it created this file, so we need to delete it |
| 52 File file = new File(scriptOutputPath.toNativePath()); |
| 53 if (file.existsSync()) { |
| 54 file.deleteSync(); |
| 55 } |
| 56 |
| 57 tempDir.deleteSync(); |
| 58 } |
| 59 |
| 60 Path get scriptOutputPath { |
| 61 return TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 62 .append('created_if_command_did_run.txt')); |
| 63 } |
| 64 |
| 65 Path get testDartFilePath { |
| 66 return TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 67 .append('test.dart')); |
| 68 } |
| 69 |
| 70 Path get testJsFilePath { |
| 71 return TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 72 .append('test.js')); |
| 73 } |
| 74 |
| 75 Path get testJsDepsFilePath { |
| 76 return TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 77 .append('test.js.deps')); |
| 78 } |
| 79 |
| 80 Path get testSnapshotFilePath { |
| 81 return TestUtils.absolutePath(new Path.fromNative(tempDir.path) |
| 82 .append('test_dart2js.snapshot')); |
| 83 } |
| 84 |
| 85 void touchFile(File file) { |
| 86 _writeToFile(file, _readFile(file)); |
| 87 } |
| 88 |
| 89 void _writeToFile(File file, String content) { |
| 90 if (content != null) { |
| 91 var fd = new File(file.fullPathSync()).openSync(FileMode.WRITE); |
| 92 fd.writeStringSync(content); |
| 93 fd.closeSync(); |
| 94 } |
| 95 } |
| 96 |
| 97 String _readFile(File file) { |
| 98 return file.readAsStringSync(); |
| 99 } |
| 100 |
| 101 File _createFile(Path path) { |
| 102 var file = new File(path.toNativePath()); |
| 103 file.createSync(); |
| 104 return file; |
| 105 } |
| 106 } |
| 107 |
| 108 class TestCompletedHandler { |
| 109 FsUtils fsUtils; |
| 110 Date _expectedTimestamp; |
| 111 bool _shouldHaveRun; |
| 112 |
| 113 TestCompletedHandler(FsUtils this.fsUtils, bool this._shouldHaveRun); |
| 114 |
| 115 void processCompletedTest(TestCase testCase) { |
| 116 var output = testCase.lastCommandOutput; |
| 117 |
| 118 Expect.isFalse(output.unexpectedOutput); |
| 119 Expect.isTrue(output.stderr.length == 0); |
| 120 if (_shouldHaveRun) { |
| 121 Expect.isTrue(output.stdout.length == 0); |
| 122 Expect.isTrue(new File(fsUtils.scriptOutputPath.toNativePath()) |
| 123 .existsSync()); |
| 124 } else { |
| 125 Expect.isFalse(new File(fsUtils.scriptOutputPath.toNativePath()) |
| 126 .existsSync()); |
| 127 } |
| 128 fsUtils.cleanup(); |
| 129 } |
| 130 } |
| 131 |
| 132 TestCase MakeTestCase(String testName, TestCompletedHandler completedHandler) { |
| 133 var fsUtils = completedHandler.fsUtils; |
| 134 var config = new TestOptionsParser().parse(['--timeout', '2'])[0]; |
| 135 var scriptDirPath = new Path.fromNative(new Options().script).directoryPath; |
| 136 var createFileScript = scriptDirPath. |
| 137 append('skipping_dart2js_compilations_helper.dart').toNativePath(); |
| 138 var executable = new Options().executable; |
| 139 var arguments = [createFileScript, fsUtils.scriptOutputPath.toNativePath()]; |
| 140 var bootstrapDeps = [ |
| 141 new Uri("file://${fsUtils.testSnapshotFilePath}")]; |
| 142 var commands = [new Dart2JsCommand(fsUtils.testJsFilePath.toNativePath(), |
| 143 false, bootstrapDeps, executable, arguments)]; |
| 144 return new TestCase(testName, commands, config, |
| 145 completedHandler.processCompletedTest, new Set<String>.from([PASS])); |
| 146 } |
| 147 |
| 148 void main() { |
| 149 var fs_noTestJs = new FsUtils(createJs: false, |
| 150 createJsDeps: true, |
| 151 createDart: true, |
| 152 createSnapshot: true); |
| 153 var fs_noTestJsDeps = new FsUtils(createJs: true, |
| 154 createJsDeps: false, |
| 155 createDart: true, |
| 156 createSnapshot: true); |
| 157 var fs_noTestDart = new FsUtils(createJs: true, |
| 158 createJsDeps: true, |
| 159 createDart: false, |
| 160 createSnapshot: true); |
| 161 var fs_noTestSnapshot = new FsUtils(createJs: true, |
| 162 createJsDeps: true, |
| 163 createDart: true, |
| 164 createSnapshot: false); |
| 165 var fs_notUpToDate_snapshot = new FsUtils(createJs: true, |
| 166 createJsDeps: true, |
| 167 createDart: true, |
| 168 createSnapshot: true); |
| 169 var fs_notUpToDate_dart = new FsUtils(createJs: true, |
| 170 createJsDeps: true, |
| 171 createDart: true, |
| 172 createSnapshot: true); |
| 173 var fs_upToDate = new FsUtils(createJs: true, |
| 174 createJsDeps: true, |
| 175 createDart: true, |
| 176 createSnapshot: true); |
| 177 |
| 178 void touchFilesAndRunTests(Timer unused) { |
| 179 fs_notUpToDate_snapshot.touchFile(fs_notUpToDate_snapshot.testSnapshot); |
| 180 fs_notUpToDate_dart.touchFile(fs_notUpToDate_dart.testDart); |
| 181 fs_upToDate.touchFile(fs_upToDate.testJs); |
| 182 |
| 183 void runTest(String name, FsUtils fsUtils, bool shouldRun) { |
| 184 new RunningProcess(MakeTestCase(name, |
| 185 new TestCompletedHandler(fsUtils, shouldRun))).start(); |
| 186 } |
| 187 runTest("fs_noTestJs", fs_noTestJs, true); |
| 188 runTest("fs_noTestJsDeps", fs_noTestJsDeps, true); |
| 189 runTest("fs_noTestDart", fs_noTestDart, true); |
| 190 runTest("fs_noTestSnapshot", fs_noTestSnapshot, true); |
| 191 runTest("fs_notUpToDate_snapshot", fs_notUpToDate_snapshot, true); |
| 192 runTest("fs_notUpToDate_dart", fs_notUpToDate_dart, true); |
| 193 // This is the only test where all dependencies are present and the test.js |
| 194 // file is newer than all the others. So we pass 'false' for shouldRun. |
| 195 runTest("fs_upToDate", fs_upToDate, false); |
| 196 } |
| 197 // We need to wait some time to make sure that the files we 'touch' get a |
| 198 // bigger timestamp than the old ones |
| 199 new Timer(1000, touchFilesAndRunTests); |
| 200 } |
| OLD | NEW |