| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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 message_extraction_test; | 5 library message_extraction_test; |
| 6 | 6 |
| 7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
| 8 import 'dart:io'; | 8 import 'dart:io'; |
| 9 import 'dart:async'; | 9 import 'dart:async'; |
| 10 import 'dart:convert'; | 10 import 'dart:convert'; |
| 11 import 'package:path/path.dart' as path; | 11 import 'package:path/path.dart' as path; |
| 12 import '../data_directory.dart'; | 12 import '../data_directory.dart'; |
| 13 | 13 |
| 14 final dart = Platform.executable; | 14 final dart = Platform.executable; |
| 15 | 15 |
| 16 /** The VM arguments we were given, most important package-root. */ | 16 /** The VM arguments we were given, most important package-root. */ |
| 17 final vmArgs = Platform.executableArguments; | 17 final vmArgs = Platform.executableArguments; |
| 18 | 18 |
| 19 var tempDir = Directory.systemTemp.createTempSync('message_extraction_test' | 19 /** |
| 20 ).path; | 20 * For testing we move the files into a temporary directory so as not to leave |
| 21 * generated files around after a failed test. For debugging, we omit that |
| 22 * step if [useLocalDirectory] is true. The place we move them to is saved as |
| 23 * [tempDir]. |
| 24 */ |
| 25 String get tempDir => _tempDir == null ? _tempDir = _createTempDir() : _tempDir; |
| 26 var _tempDir; |
| 27 _createTempDir() => useLocalDirectory ? '.' : |
| 28 Directory.systemTemp.createTempSync('message_extraction_test').path; |
| 29 |
| 30 var useLocalDirectory = false; |
| 21 | 31 |
| 22 /** | 32 /** |
| 23 * Translate a relative file path into this test directory. This is | 33 * Translate a relative file path into this test directory. This is |
| 24 * applied to all the arguments of [run]. It will ignore a string that | 34 * applied to all the arguments of [run]. It will ignore a string that |
| 25 * is an absolute path or begins with "--", because some of the arguments | 35 * is an absolute path or begins with "--", because some of the arguments |
| 26 * might be command-line options. | 36 * might be command-line options. |
| 27 */ | 37 */ |
| 28 String asTestDirPath([String s]) { | 38 String asTestDirPath([String s]) { |
| 29 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; | 39 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; |
| 30 return path.join(intlDirectory, 'test', 'message_extraction', s); | 40 return path.join(intlDirectory, 'test', 'message_extraction', s); |
| 31 } | 41 } |
| 32 | 42 |
| 33 /** | 43 /** |
| 34 * Translate a relative file path into our temp directory. This is | 44 * Translate a relative file path into our temp directory. This is |
| 35 * applied to all the arguments of [run]. It will ignore a string that | 45 * applied to all the arguments of [run]. It will ignore a string that |
| 36 * is an absolute path or begins with "--", because some of the arguments | 46 * is an absolute path or begins with "--", because some of the arguments |
| 37 * might be command-line options. | 47 * might be command-line options. |
| 38 */ | 48 */ |
| 39 String asTempDirPath([String s]) { | 49 String asTempDirPath([String s]) { |
| 40 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; | 50 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; |
| 41 return path.join(tempDir, s); | 51 return path.join(tempDir, s); |
| 42 } | 52 } |
| 43 | 53 |
| 44 main() { | 54 main(arguments) { |
| 55 // If debugging, use --local to avoid copying everything to temporary |
| 56 // directories to make it even harder to debug. Note that this will also |
| 57 // not delete the generated files, so may require manual cleanup. |
| 58 if (arguments.contains("--local")) { |
| 59 print("Testing using local directory for generated files"); |
| 60 useLocalDirectory = true; |
| 61 } |
| 45 setUp(copyFilesToTempDirectory); | 62 setUp(copyFilesToTempDirectory); |
| 46 test("Test round trip message extraction, translation, code generation, " | 63 test("Test round trip message extraction, translation, code generation, " |
| 47 "and printing", () { | 64 "and printing", () { |
| 48 var makeSureWeVerify = expectAsync(runAndVerify); | 65 var makeSureWeVerify = expectAsync(runAndVerify); |
| 49 return extractMessages(null).then((result) { | 66 return extractMessages(null).then((result) { |
| 50 return generateTranslationFiles(result); | 67 return generateTranslationFiles(result); |
| 51 }).then((result) { | 68 }).then((result) { |
| 52 return generateCodeFromTranslation(result); | 69 return generateCodeFromTranslation(result); |
| 53 }).then(makeSureWeVerify).then(checkResult); | 70 }).then(makeSureWeVerify).then(checkResult); |
| 54 }); | 71 }); |
| 55 tearDown(deleteGeneratedFiles); | 72 tearDown(deleteGeneratedFiles); |
| 56 } | 73 } |
| 57 | 74 |
| 58 void copyFilesToTempDirectory() { | 75 void copyFilesToTempDirectory() { |
| 76 if (useLocalDirectory) return; |
| 59 var files = [asTestDirPath('sample_with_messages.dart'), | 77 var files = [asTestDirPath('sample_with_messages.dart'), |
| 60 asTestDirPath('part_of_sample_with_messages.dart'), | 78 asTestDirPath('part_of_sample_with_messages.dart'), |
| 61 asTestDirPath('verify_messages.dart'), | 79 asTestDirPath('verify_messages.dart'), |
| 62 asTestDirPath('run_and_verify.dart'), | 80 asTestDirPath('run_and_verify.dart'), |
| 63 asTestDirPath('print_to_list.dart')]; | 81 asTestDirPath('print_to_list.dart')]; |
| 64 for (var filename in files) { | 82 for (var filename in files) { |
| 65 var file = new File(filename); | 83 var file = new File(filename); |
| 66 file.copySync(path.join(tempDir, path.basename(filename))); | 84 file.copySync(path.join(tempDir, path.basename(filename))); |
| 67 } | 85 } |
| 68 } | 86 } |
| 69 | 87 |
| 70 void deleteGeneratedFiles() { | 88 void deleteGeneratedFiles() { |
| 89 if (useLocalDirectory) return; |
| 71 try { | 90 try { |
| 72 var dir = new Directory(tempDir); | 91 var dir = new Directory(tempDir); |
| 73 dir.listSync().forEach((x) => x.deleteSync()); | 92 dir.listSync().forEach((x) => x.deleteSync()); |
| 74 dir.deleteSync(); | 93 dir.deleteSync(); |
| 75 } on Error catch (e) { | 94 } on Error catch (e) { |
| 76 print("Failed to delete $tempDir"); | 95 print("Failed to delete $tempDir"); |
| 77 print("Exception:\n$e"); | 96 print("Exception:\n$e"); |
| 78 } | 97 } |
| 79 } | 98 } |
| 80 | 99 |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 'intl_messages.json']); | 144 'intl_messages.json']); |
| 126 | 145 |
| 127 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) | 146 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) |
| 128 => run(previousResult, [asTestDirPath('generate_from_json.dart'), | 147 => run(previousResult, [asTestDirPath('generate_from_json.dart'), |
| 129 '--generated-file-prefix=foo_', 'sample_with_messages.dart', | 148 '--generated-file-prefix=foo_', 'sample_with_messages.dart', |
| 130 'part_of_sample_with_messages.dart', 'translation_fr.json', | 149 'part_of_sample_with_messages.dart', 'translation_fr.json', |
| 131 'translation_de_DE.json']); | 150 'translation_de_DE.json']); |
| 132 | 151 |
| 133 Future<ProcessResult> runAndVerify(ProcessResult previousResult) => run( | 152 Future<ProcessResult> runAndVerify(ProcessResult previousResult) => run( |
| 134 previousResult, [asTempDirPath('run_and_verify.dart')]); | 153 previousResult, [asTempDirPath('run_and_verify.dart')]); |
| OLD | NEW |