Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #library("multitest"); | 5 #library("multitest"); |
| 6 | 6 |
| 7 #import("dart:io"); | 7 #import("dart:io"); |
| 8 #import("test_suite.dart"); | 8 #import("test_suite.dart"); |
| 9 | 9 |
| 10 // Multitests are Dart test scripts containing lines of the form | 10 // Multitests are Dart test scripts containing lines of the form |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 44 // aaa | 44 // aaa |
| 45 // ddd /// 07: static type warning | 45 // ddd /// 07: static type warning |
| 46 // eee | 46 // eee |
| 47 // | 47 // |
| 48 // Note that it is possible to indicate more than one acceptable outcome | 48 // Note that it is possible to indicate more than one acceptable outcome |
| 49 // in the case of dynamic and static type warnings | 49 // in the case of dynamic and static type warnings |
| 50 // aaa | 50 // aaa |
| 51 // ddd /// 07: static type warning, dynamic type error | 51 // ddd /// 07: static type warning, dynamic type error |
| 52 // eee | 52 // eee |
| 53 | 53 |
| 54 void ExtractTestsFromMultitest(String filename, | 54 void ExtractTestsFromMultitest(Path filePath, |
| 55 Map<String, String> tests, | 55 Map<String, String> tests, |
| 56 Map<String, Set<String>> outcomes) { | 56 Map<String, Set<String>> outcomes) { |
| 57 // Read the entire file into a byte buffer and transform it to a | 57 // Read the entire file into a byte buffer and transform it to a |
| 58 // String. This will treat the file as ascii but the only parts | 58 // String. This will treat the file as ascii but the only parts |
| 59 // we are interested in will be ascii in any case. | 59 // we are interested in will be ascii in any case. |
| 60 RandomAccessFile file = (new File(filename)).openSync(FileMode.READ); | 60 RandomAccessFile file = new File.fromPath(filePath).openSync(FileMode.READ); |
| 61 List chars = new List(file.lengthSync()); | 61 List chars = new List(file.lengthSync()); |
| 62 int offset = 0; | 62 int offset = 0; |
| 63 while (offset != chars.length) { | 63 while (offset != chars.length) { |
| 64 offset += file.readListSync(chars, offset, chars.length - offset); | 64 offset += file.readListSync(chars, offset, chars.length - offset); |
| 65 } | 65 } |
| 66 file.closeSync(); | 66 file.closeSync(); |
| 67 String contents = new String.fromCharCodes(chars); | 67 String contents = new String.fromCharCodes(chars); |
| 68 chars = null; | 68 chars = null; |
| 69 int first_newline = contents.indexOf('\n'); | 69 int first_newline = contents.indexOf('\n'); |
| 70 final String line_separator = | 70 final String line_separator = |
| 71 (first_newline == 0 || contents[first_newline - 1] != '\r') | 71 (first_newline == 0 || contents[first_newline - 1] != '\r') |
| 72 ? '\n' | 72 ? '\n' |
| 73 : '\r\n'; | 73 : '\r\n'; |
| 74 List<String> lines = contents.split(line_separator); | 74 List<String> lines = contents.split(line_separator); |
| 75 if (lines.last() == '') lines.removeLast(); | 75 if (lines.last() == '') lines.removeLast(); |
| 76 contents = null; | 76 contents = null; |
| 77 Set<String> validMultitestOutcomes = new Set<String>.from( | 77 Set<String> validMultitestOutcomes = new Set<String>.from( |
| 78 ['compile-time error', 'runtime error', | 78 ['compile-time error', 'runtime error', |
| 79 'static type warning', 'dynamic type error']); | 79 'static type warning', 'dynamic type error']); |
| 80 | 80 |
| 81 List<String> testTemplate = new List<String>(); | 81 List<String> testTemplate = new List<String>(); |
| 82 testTemplate.add('// Test created from multitest named $filename.'); | 82 testTemplate.add( |
| 83 '// Test created from multitest named ${filePath.toNativePath()}.'); | |
| 83 // Create the set of multitests, which will have a new test added each | 84 // Create the set of multitests, which will have a new test added each |
| 84 // time we see a multitest line with a new key. | 85 // time we see a multitest line with a new key. |
| 85 Map<String, List<String>> testsAsLines = new Map<String, List<String>>(); | 86 Map<String, List<String>> testsAsLines = new Map<String, List<String>>(); |
| 86 | 87 |
| 87 int lineCount = 0; | 88 int lineCount = 0; |
| 88 for (String line in lines) { | 89 for (String line in lines) { |
| 89 lineCount++; | 90 lineCount++; |
| 90 var annotation = new _Annotation.from(line); | 91 var annotation = new _Annotation.from(line); |
| 91 if (annotation != null) { | 92 if (annotation != null) { |
| 92 testsAsLines.putIfAbsent(annotation.key, | 93 testsAsLines.putIfAbsent(annotation.key, |
| 93 () => new List<String>.from(testTemplate)).add(line); | 94 () => new List<String>.from(testTemplate)).add(line); |
| 94 outcomes.putIfAbsent(annotation.key, | 95 outcomes.putIfAbsent(annotation.key, |
| 95 () => new Set<String>()); | 96 () => new Set<String>()); |
| 96 if (annotation.rest == 'continued') { | 97 if (annotation.rest == 'continued') { |
| 97 continue; | 98 continue; |
| 98 } else { | 99 } else { |
| 99 for (String nextOutcome in annotation.outcomesList) { | 100 for (String nextOutcome in annotation.outcomesList) { |
| 100 outcomes[annotation.key].add(nextOutcome); | 101 outcomes[annotation.key].add(nextOutcome); |
| 101 if (!validMultitestOutcomes.contains(nextOutcome)) { | 102 if (!validMultitestOutcomes.contains(nextOutcome)) { |
| 102 // TODO(zundel): fix long line | |
| 103 Expect.fail( | 103 Expect.fail( |
| 104 "Invalid test directive '$nextOutcome' on line ${lineCount}: ${ann otation.rest} "); | 104 "Invalid test directive '$nextOutcome' on line ${lineCount}:\n" |
|
Mads Ager (google)
2012/06/27 16:08:18
Is the addition of a newline character on purpose
Bill Hesse
2012/06/28 15:31:22
Yes. If it can't fit on a line in the source, why
| |
| 105 "${annotation.rest} "); | |
| 105 } | 106 } |
| 106 } | 107 } |
| 107 } | 108 } |
| 108 } else { | 109 } else { |
| 109 testTemplate.add(line); | 110 testTemplate.add(line); |
| 110 for (var test in testsAsLines.getValues()) test.add(line); | 111 for (var test in testsAsLines.getValues()) test.add(line); |
| 111 } | 112 } |
| 112 } | 113 } |
| 113 | 114 |
| 114 // Check that every key (other than the none case) has at least one outcome | 115 // Check that every key (other than the none case) has at least one outcome |
| 115 for (var outcomeKey in outcomes.getKeys()) { | 116 for (var outcomeKey in outcomes.getKeys()) { |
| 116 if (outcomeKey != 'none' && outcomes[outcomeKey].isEmpty()) { | 117 if (outcomeKey != 'none' && outcomes[outcomeKey].isEmpty()) { |
| 117 // TODO(zundel): fix long line | 118 Expect.fail("Test ${outcomeKey} has no valid annotated outcomes.\n" |
|
Mads Ager (google)
2012/06/27 16:08:18
Ditto.
| |
| 118 Expect.fail("Test ${outcomeKey} has no valid annotated outcomes. Expected one of: ${validMultitestOutcomes.toString()}"); | 119 "Expected one of: ${validMultitestOutcomes.toString()}"); |
| 119 } | 120 } |
| 120 } | 121 } |
| 121 | 122 |
| 122 // Add the template, with no multitest lines, as a test with key 'none'. | 123 // Add the template, with no multitest lines, as a test with key 'none'. |
| 123 testsAsLines['none'] = testTemplate; | 124 testsAsLines['none'] = testTemplate; |
| 124 outcomes['none'] = new Set<String>(); | 125 outcomes['none'] = new Set<String>(); |
| 125 | 126 |
| 126 // Copy all the tests into the output map tests, as multiline strings. | 127 // Copy all the tests into the output map tests, as multiline strings. |
| 127 for (String key in testsAsLines.getKeys()) { | 128 for (String key in testsAsLines.getKeys()) { |
| 128 tests[key] = | 129 tests[key] = |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 145 annotation.key = parts[0]; | 146 annotation.key = parts[0]; |
| 146 annotation.rest = parts[1]; | 147 annotation.rest = parts[1]; |
| 147 annotation.outcomesList = annotation.rest.split(',') | 148 annotation.outcomesList = annotation.rest.split(',') |
| 148 .map((s) => s.trim()); | 149 .map((s) => s.trim()); |
| 149 return annotation; | 150 return annotation; |
| 150 } | 151 } |
| 151 } | 152 } |
| 152 | 153 |
| 153 // Find all relative imports and copy them into the dir that contains | 154 // Find all relative imports and copy them into the dir that contains |
| 154 // the generated tests. | 155 // the generated tests. |
| 155 Set<String> _findAllRelativeImports(String topLibrary) { | 156 Set<Path> _findAllRelativeImports(Path topLibrary) { |
| 156 Set<String> toSearch = new Set<String>.from([topLibrary]); | 157 Set<String> toSearch = new Set<Path>.from([topLibrary]); |
| 157 Set<String> foundImports = new HashSet<String>(); | 158 Set<String> foundImports = new HashSet<Path>(); |
| 158 String pathSep = Platform.pathSeparator; | 159 Path libraryDir = topLibrary.directoryPath; |
| 159 int end = topLibrary.lastIndexOf(pathSep); | |
| 160 String libraryDir = topLibrary.substring(0, end); | |
| 161 | |
| 162 // Matches #import( or #source( followed by " or ' followed by anything | 160 // Matches #import( or #source( followed by " or ' followed by anything |
| 163 // except dart: or /, at the beginning of a line. | 161 // except dart: or /, at the beginning of a line. |
| 164 RegExp relativeImportRegExp = | 162 RegExp relativeImportRegExp = |
| 165 const RegExp('^#(import|source)[(]["\'](?!(dart:|/))([^"\']*)["\']'); | 163 const RegExp('^#(import|source)[(]["\'](?!(dart:|/))([^"\']*)["\']'); |
| 166 while (!toSearch.isEmpty()) { | 164 while (!toSearch.isEmpty()) { |
| 167 var thisPass = toSearch; | 165 var thisPass = toSearch; |
| 168 toSearch = new HashSet<String>(); | 166 toSearch = new HashSet<Path>(); |
| 169 for (String filename in thisPass) { | 167 for (Path filename in thisPass) { |
| 170 File f = new File(filename); | 168 File f = new File.fromPath(filename); |
| 171 for (String line in f.readAsLinesSync()) { | 169 for (String line in f.readAsLinesSync()) { |
| 172 Match match = relativeImportRegExp.firstMatch(line); | 170 Match match = relativeImportRegExp.firstMatch(line); |
| 173 if (match != null) { | 171 if (match != null) { |
| 174 String relativePath = match.group(3); | 172 Path relativePath = new Path(match.group(3)); |
| 175 if (foundImports.contains(relativePath)) { | 173 if (foundImports.contains(relativePath)) { |
| 176 continue; | 174 continue; |
| 177 } | 175 } |
| 178 if (relativePath.contains(@'\.\.')) { | 176 if (relativePath.toString().contains('..')) { |
| 179 // This is just for safety reasons, we don't want | 177 // This is just for safety reasons, we don't want |
| 180 // to unintentionally clobber files relative to the destination | 178 // to unintentionally clobber files relative to the destination |
| 181 // dir when copying them ove. | 179 // dir when copying them ove. |
| 182 Expect.fail("relative paths containing .. are not allowed."); | 180 Expect.fail("relative paths containing .. are not allowed."); |
| 183 } | 181 } |
| 184 foundImports.add(relativePath); | 182 foundImports.add(relativePath); |
| 185 toSearch.add('$libraryDir/$relativePath'); | 183 toSearch.add(libraryDir.join(relativePath)); |
| 186 } | 184 } |
| 187 } | 185 } |
| 188 } | 186 } |
| 189 } | 187 } |
| 190 return foundImports; | 188 return foundImports; |
| 191 } | 189 } |
| 192 | 190 |
| 193 void DoMultitest(String filename, | 191 void DoMultitest(Path filePath, |
| 194 String outputDir, | 192 String outputDir, |
| 195 String testDir, | 193 Path suiteDir, |
| 196 // TODO(zundel): Are the boolean flags now redundant | 194 // TODO(zundel): Are the boolean flags now redundant |
| 197 // with the 'multitestOutcome' field? | 195 // with the 'multitestOutcome' field? |
| 198 Function doTest(String filename, | 196 Function doTest(Path filePath, |
| 199 bool isNegative, | 197 bool isNegative, |
| 200 [bool isNegativeIfChecked, | 198 [bool isNegativeIfChecked, |
| 201 bool hasFatalTypeErrors, | 199 bool hasFatalTypeErrors, |
| 202 bool hasRuntimeErrors, | 200 bool hasRuntimeErrors, |
| 203 Set<String> multitestOutcome]), | 201 Set<String> multitestOutcome]), |
| 204 Function multitestDone) { | 202 Function multitestDone) { |
| 205 // Each new test is a single String value in the Map tests. | 203 // Each new test is a single String value in the Map tests. |
| 206 Map<String, String> tests = new Map<String, String>(); | 204 Map<String, String> tests = new Map<String, String>(); |
| 207 Map<String, Set<String>> outcomes = new Map<String, Set<String>>(); | 205 Map<String, Set<String>> outcomes = new Map<String, Set<String>>(); |
| 208 ExtractTestsFromMultitest(filename, tests, outcomes); | 206 ExtractTestsFromMultitest(filePath, tests, outcomes); |
| 209 | 207 |
| 210 String directory = CreateMultitestDirectory(outputDir, testDir); | 208 Path sourceDir = filePath.directoryPath; |
| 211 Expect.isNotNull(directory); | 209 Path targetDir = CreateMultitestDirectory(outputDir, suiteDir); |
| 212 String pathSep = Platform.pathSeparator; | 210 Expect.isNotNull(targetDir); |
| 213 int start = filename.lastIndexOf(pathSep) + 1; | 211 |
| 214 int end = filename.indexOf('.dart', start); | 212 // Copy all the relative imports of the multitest. |
| 215 String baseFilename = filename.substring(start, end); | 213 Set<Path> importsToCopy = _findAllRelativeImports(filePath); |
| 216 String sourceDirectory = filename.substring(0, start - 1); | 214 List<Future> futureCopies = []; |
| 217 Set<String> importsToCopy = _findAllRelativeImports(filename); | 215 for (Path importPath in importsToCopy) { |
| 218 Directory destDir = new Directory("directory"); | 216 // Make sure the target directory exists. |
| 219 for (String import in importsToCopy) { | 217 Path importDir = importPath.directoryPath; |
| 220 File source = new File('$sourceDirectory/$import'); | 218 TestUtils.mkdirRecursive(targetDir, importDir); |
| 221 var dest = new File('$directory/$import'); | 219 // Copy file. |
| 222 var basenameStart = import.lastIndexOf('/'); | 220 futureCopies.add(TestUtils.copyFile(sourceDir.join(importPath), |
| 223 if (basenameStart > 0) { | 221 targetDir.join(importPath))); |
| 224 // make sure we have a dir for it | 222 } |
| 225 var importDir = import.substring(0, basenameStart); | 223 |
| 226 TestUtils.mkdirRecursive(directory, importDir); | 224 // Wait until all imports are copied before scheduling test cases. |
| 225 Futures.wait(futureCopies).then((ignored) { | |
| 226 String baseFilename = filePath.filenameWithoutExtension; | |
| 227 for (String key in tests.getKeys()) { | |
| 228 final Path multitestFilename = | |
| 229 targetDir.append('${baseFilename}_$key.dart'); | |
| 230 final File file = new File.fromPath(multitestFilename); | |
| 231 | |
| 232 file.createSync(); | |
| 233 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | |
| 234 var bytes = tests[key].charCodes(); | |
| 235 openedFile.writeListSync(bytes, 0, bytes.length); | |
| 236 openedFile.closeSync(); | |
| 237 Set<String> outcome = outcomes[key]; | |
| 238 bool enableFatalTypeErrors = outcome.contains('static type warning'); | |
| 239 bool hasRuntimeErrors = outcome.contains('runtime error'); | |
| 240 bool isNegative = hasRuntimeErrors | |
| 241 || outcome.contains('compile-time error'); | |
| 242 bool isNegativeIfChecked = outcome.contains('dynamic type error'); | |
| 243 doTest(multitestFilename, | |
| 244 isNegative, | |
| 245 isNegativeIfChecked, | |
| 246 enableFatalTypeErrors, | |
| 247 hasRuntimeErrors, | |
| 248 outcome); | |
| 227 } | 249 } |
| 228 TestUtils.copyFile(source, dest); | 250 multitestDone(); |
| 229 } | 251 }); |
| 230 for (String key in tests.getKeys()) { | |
| 231 final String multitestFilename = '$directory/${baseFilename}_$key.dart'; | |
| 232 final File file = new File(multitestFilename); | |
| 233 | |
| 234 file.createSync(); | |
| 235 RandomAccessFile openedFile = file.openSync(FileMode.WRITE); | |
| 236 var bytes = tests[key].charCodes(); | |
| 237 openedFile.writeListSync(bytes, 0, bytes.length); | |
| 238 openedFile.closeSync(); | |
| 239 Set<String> outcome = outcomes[key]; | |
| 240 bool enableFatalTypeErrors = outcome.contains('static type warning'); | |
| 241 bool hasRuntimeErrors = outcome.contains('runtime error'); | |
| 242 bool isNegative = hasRuntimeErrors | |
| 243 || outcome.contains('compile-time error'); | |
| 244 bool isNegativeIfChecked = outcome.contains('dynamic type error'); | |
| 245 doTest(multitestFilename, | |
| 246 isNegative, | |
| 247 isNegativeIfChecked, | |
| 248 enableFatalTypeErrors, | |
| 249 hasRuntimeErrors, | |
| 250 outcome); | |
| 251 } | |
| 252 multitestDone(); | |
| 253 } | 252 } |
| 254 | 253 |
| 255 | 254 |
| 256 String CreateMultitestDirectory(String outputDir, String testDir) { | 255 Path CreateMultitestDirectory(String outputDir, Path suiteDir) { |
| 257 final String generatedTestDirectory = 'generated_tests'; | 256 final String generatedTestDirectory = 'generated_tests'; |
| 258 Directory generatedTestDir = new Directory('$outputDir/generated_tests'); | 257 Directory generatedTestDir = new Directory('$outputDir/generated_tests'); |
| 259 if (!new Directory(outputDir).existsSync()) { | 258 if (!new Directory(outputDir).existsSync()) { |
| 260 new Directory(outputDir).createSync(); | 259 new Directory(outputDir).createSync(); |
| 261 } | 260 } |
| 262 if (!generatedTestDir.existsSync()) { | 261 if (!generatedTestDir.existsSync()) { |
| 263 generatedTestDir.createSync(); | 262 generatedTestDir.createSync(); |
| 264 } | 263 } |
| 265 var split = testDir.split('/'); | 264 var split = suiteDir.segments(); |
| 266 if (split.last() == 'src') { | 265 if (split.last() == 'src') { |
| 267 // TODO(sigmund): remove this once all tests are migrated to use | 266 // TODO(sigmund): remove this once all tests are migrated to use |
| 268 // TestSuite.forDirectory. | 267 // TestSuite.forDirectory. |
| 269 split.removeLast(); | 268 split.removeLast(); |
| 270 } | 269 } |
| 271 String path = '${generatedTestDir.path}/${split.last()}'; | 270 String path = '${generatedTestDir.path}/${split.last()}'; |
| 272 Directory dir = new Directory(path); | 271 Directory dir = new Directory(path); |
| 273 if (!dir.existsSync()) { | 272 if (!dir.existsSync()) { |
| 274 dir.createSync(); | 273 dir.createSync(); |
| 275 } | 274 } |
| 276 return path; | 275 return new Path.fromNative(new File(path).fullPathSync()); |
| 277 } | 276 } |
| OLD | NEW |