OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013, 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 * Helper library to run tests in content_shell |
| 7 */ |
| 8 library polymer.testing.end2end; |
| 9 |
| 10 import 'dart:io'; |
| 11 import 'package:args/args.dart'; |
| 12 import 'package:path/path.dart' as path; |
| 13 import 'package:unittest/unittest.dart'; |
| 14 import 'package:polymer/dwc.dart' as dwc; |
| 15 |
| 16 |
| 17 /** |
| 18 * Compiles [testFile] with the web-ui compiler, and then runs the output as a |
| 19 * unit test in content_shell. |
| 20 */ |
| 21 void endToEndTests(String inputDir, String outDir, {List<String> arguments}) { |
| 22 _testHelper(new _TestOptions(inputDir, inputDir, null, outDir, |
| 23 arguments: arguments)); |
| 24 } |
| 25 |
| 26 /** |
| 27 * Compiles [testFile] with the web-ui compiler, and then runs the output as a |
| 28 * render test in content_shell. |
| 29 */ |
| 30 void renderTests(String baseDir, String inputDir, String expectedDir, |
| 31 String outDir, {List<String> arguments, String script, String pattern, |
| 32 bool deleteDir: true}) { |
| 33 _testHelper(new _TestOptions(baseDir, inputDir, expectedDir, outDir, |
| 34 arguments: arguments, script: script, pattern: pattern, |
| 35 deleteDir: deleteDir)); |
| 36 } |
| 37 |
| 38 void _testHelper(_TestOptions options) { |
| 39 expect(options, isNotNull); |
| 40 |
| 41 var paths = new Directory(options.inputDir).listSync() |
| 42 .where((f) => f is File).map((f) => f.path) |
| 43 .where((p) => p.endsWith('_test.html') && options.pattern.hasMatch(p)); |
| 44 |
| 45 if (paths.isEmpty) return; |
| 46 |
| 47 // First clear the output folder. Otherwise we can miss bugs when we fail to |
| 48 // generate a file. |
| 49 var dir = new Directory(options.outDir); |
| 50 if (dir.existsSync() && options.deleteDir) { |
| 51 print('Cleaning old output for ${path.normalize(options.outDir)}'); |
| 52 dir.deleteSync(recursive: true); |
| 53 } |
| 54 dir.createSync(); |
| 55 |
| 56 for (var filePath in paths) { |
| 57 var filename = path.basename(filePath); |
| 58 test('compile $filename', () { |
| 59 var testArgs = ['-o', options.outDir, '--basedir', options.baseDir, |
| 60 '--deploy'] |
| 61 ..addAll(options.compilerArgs) |
| 62 ..add(filePath); |
| 63 expect(dwc.run(testArgs, printTime: false).then((res) { |
| 64 expect(res.messages.length, 0, reason: res.messages.join('\n')); |
| 65 }), completes); |
| 66 }); |
| 67 } |
| 68 |
| 69 var filenames = paths.map(path.basename).toList(); |
| 70 // Sort files to match the order in which run.sh runs diff. |
| 71 filenames.sort(); |
| 72 |
| 73 // Get the path from "input" relative to "baseDir" |
| 74 var relativeToBase = path.relative(options.inputDir, from: options.baseDir); |
| 75 var finalOutDir = path.join(options.outDir, relativeToBase); |
| 76 |
| 77 runTests(String search) { |
| 78 var output; |
| 79 |
| 80 for (var filename in filenames) { |
| 81 test('content_shell run $filename$search', () { |
| 82 var args = ['--dump-render-tree', |
| 83 'file://$finalOutDir/$filename$search']; |
| 84 var env = {'DART_FLAGS': '--checked'}; |
| 85 expect(Process.run('content_shell', args, environment: env).then((res) { |
| 86 expect(res.exitCode, 0, reason: 'content_shell exit code: ' |
| 87 '${res.exitCode}. Contents of stderr: \n${res.stderr}'); |
| 88 var outs = res.stdout.split('#EOF\n') |
| 89 .where((s) => !s.trim().isEmpty).toList(); |
| 90 expect(outs.length, 1); |
| 91 output = outs.first; |
| 92 }), completes); |
| 93 }); |
| 94 |
| 95 test('verify $filename $search', () { |
| 96 expect(output, isNotNull, reason: |
| 97 'Output not available, maybe content_shell failed to run.'); |
| 98 var outPath = path.join(options.outDir, '$filename.txt'); |
| 99 new File(outPath).writeAsStringSync(output); |
| 100 if (options.isRenderTest) { |
| 101 var expectedPath = path.join(options.expectedDir, '$filename.txt'); |
| 102 var expected = new File(expectedPath).readAsStringSync(); |
| 103 expect(output, expected, reason: 'unexpected output for <$filename>'); |
| 104 } else { |
| 105 bool passes = matches( |
| 106 new RegExp('All .* tests passed')).matches(output, {}); |
| 107 expect(passes, true, reason: 'unit test failed:\n$output'); |
| 108 } |
| 109 }); |
| 110 } |
| 111 } |
| 112 |
| 113 bool compiled = false; |
| 114 ensureCompileToJs() { |
| 115 if (compiled) return; |
| 116 compiled = true; |
| 117 |
| 118 for (var filename in filenames) { |
| 119 test('dart2js $filename', () { |
| 120 // TODO(jmesserly): this depends on DWC's output scheme. |
| 121 // Alternatively we could use html5lib to find the script tag. |
| 122 var inPath = '${filename}_bootstrap.dart'; |
| 123 var outPath = '${inPath}.js'; |
| 124 |
| 125 inPath = path.join(finalOutDir, inPath); |
| 126 outPath = path.join(finalOutDir, outPath); |
| 127 |
| 128 expect(Process.run('dart2js', ['-o$outPath', inPath]).then((res) { |
| 129 expect(res.exitCode, 0, reason: 'dart2js exit code: ' |
| 130 '${res.exitCode}. Contents of stderr: \n${res.stderr}. ' |
| 131 'Contents of stdout: \n${res.stdout}.'); |
| 132 expect(new File(outPath).existsSync(), true, reason: 'input file ' |
| 133 '$inPath should have been compiled to $outPath.'); |
| 134 }), completes); |
| 135 }); |
| 136 } |
| 137 } |
| 138 |
| 139 if (options.runAsDart) { |
| 140 runTests(''); |
| 141 } |
| 142 if (options.runAsJs) { |
| 143 ensureCompileToJs(); |
| 144 runTests('?js=1'); |
| 145 } |
| 146 if (options.forcePolyfillShadowDom) { |
| 147 ensureCompileToJs(); |
| 148 runTests('?js=1&shadowdomjs=1'); |
| 149 } |
| 150 } |
| 151 |
| 152 class _TestOptions { |
| 153 final String baseDir; |
| 154 final String inputDir; |
| 155 |
| 156 final String expectedDir; |
| 157 bool get isRenderTest => expectedDir != null; |
| 158 |
| 159 final String outDir; |
| 160 final bool deleteDir; |
| 161 |
| 162 final bool runAsDart; |
| 163 final bool runAsJs; |
| 164 final bool forcePolyfillShadowDom; |
| 165 |
| 166 final List<String> compilerArgs; |
| 167 final RegExp pattern; |
| 168 |
| 169 factory _TestOptions(String baseDir, String inputDir, String expectedDir, |
| 170 String outDir, {List<String> arguments, String script, String pattern, |
| 171 bool deleteDir: true}) { |
| 172 if (arguments == null) arguments = new Options().arguments; |
| 173 if (script == null) script = new Options().script; |
| 174 |
| 175 var args = _parseArgs(arguments, script); |
| 176 if (args == null) return null; |
| 177 var compilerArgs = args.rest; |
| 178 var filePattern; |
| 179 if (pattern != null) { |
| 180 filePattern = new RegExp(pattern); |
| 181 } else if (compilerArgs.length > 0) { |
| 182 filePattern = new RegExp(compilerArgs[0]); |
| 183 compilerArgs = compilerArgs.sublist(1); |
| 184 } else { |
| 185 filePattern = new RegExp('.'); |
| 186 } |
| 187 |
| 188 var scriptDir = path.absolute(path.dirname(script)); |
| 189 baseDir = path.join(scriptDir, baseDir); |
| 190 inputDir = path.join(scriptDir, inputDir); |
| 191 outDir = path.join(scriptDir, outDir); |
| 192 if (expectedDir != null) { |
| 193 expectedDir = path.join(scriptDir, expectedDir); |
| 194 } |
| 195 |
| 196 return new _TestOptions._(baseDir, inputDir, expectedDir, outDir, deleteDir, |
| 197 args['dart'] == true, args['js'] == true, args['shadowdom'] == true, |
| 198 compilerArgs, filePattern); |
| 199 } |
| 200 |
| 201 _TestOptions._(this.baseDir, this.inputDir, this.expectedDir, this.outDir, |
| 202 this.deleteDir, this.runAsDart, this.runAsJs, |
| 203 this.forcePolyfillShadowDom, this.compilerArgs, this.pattern); |
| 204 } |
| 205 |
| 206 ArgResults _parseArgs(List<String> arguments, String script) { |
| 207 var parser = new ArgParser() |
| 208 ..addFlag('dart', abbr: 'd', help: 'run on Dart VM', defaultsTo: true) |
| 209 ..addFlag('js', abbr: 'j', help: 'run compiled dart2js', defaultsTo: true) |
| 210 ..addFlag('shadowdom', abbr: 's', |
| 211 help: 'run dart2js and polyfilled ShadowDOM', defaultsTo: true) |
| 212 ..addFlag('help', abbr: 'h', help: 'Displays this help message', |
| 213 defaultsTo: false, negatable: false); |
| 214 |
| 215 showUsage() { |
| 216 print('Usage: $script [options...] [test_name_regexp]'); |
| 217 print(parser.getUsage()); |
| 218 return null; |
| 219 } |
| 220 |
| 221 try { |
| 222 var results = parser.parse(arguments); |
| 223 if (results['help']) return showUsage(); |
| 224 return results; |
| 225 } on FormatException catch (e) { |
| 226 print(e.message); |
| 227 return showUsage(); |
| 228 } |
| 229 } |
OLD | NEW |