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 library message_extraction_test; | |
6 | |
7 import 'package:unittest/unittest.dart'; | |
8 import 'dart:io'; | |
9 import 'dart:async'; | |
10 import 'dart:convert'; | |
11 import 'package:path/path.dart' as path; | |
12 import '../data_directory.dart'; | |
13 | |
14 final dart = Platform.executable; | |
15 | |
16 /** Should we use deferred loading. */ | |
17 bool useDeferredLoading = true; | |
18 | |
19 String get _deferredLoadPrefix => useDeferredLoading ? '' : 'no-'; | |
20 | |
21 String get deferredLoadArg => '--${_deferredLoadPrefix}use-deferred-loading'; | |
22 | |
23 /** The VM arguments we were given, most important package-root. */ | |
24 final vmArgs = Platform.executableArguments; | |
25 | |
26 /** | |
27 * For testing we move the files into a temporary directory so as not to leave | |
28 * generated files around after a failed test. For debugging, we omit that | |
29 * step if [useLocalDirectory] is true. The place we move them to is saved as | |
30 * [tempDir]. | |
31 */ | |
32 String get tempDir => _tempDir == null ? _tempDir = _createTempDir() : _tempDir; | |
33 var _tempDir; | |
34 _createTempDir() => useLocalDirectory ? '.' : | |
35 Directory.systemTemp.createTempSync('message_extraction_test').path; | |
36 | |
37 var useLocalDirectory = false; | |
38 | |
39 /** | |
40 * Translate a relative file path into this test directory. This is | |
41 * applied to all the arguments of [run]. It will ignore a string that | |
42 * is an absolute path or begins with "--", because some of the arguments | |
43 * might be command-line options. | |
44 */ | |
45 String asTestDirPath([String s]) { | |
46 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; | |
47 return path.join(intlDirectory, 'test', 'message_extraction', s); | |
48 } | |
49 | |
50 /** | |
51 * Translate a relative file path into our temp directory. This is | |
52 * applied to all the arguments of [run]. It will ignore a string that | |
53 * is an absolute path or begins with "--", because some of the arguments | |
54 * might be command-line options. | |
55 */ | |
56 String asTempDirPath([String s]) { | |
57 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; | |
58 return path.join(tempDir, s); | |
59 } | |
60 | |
61 main(arguments) { | |
62 // If debugging, use --local to avoid copying everything to temporary | |
63 // directories to make it even harder to debug. Note that this will also | |
64 // not delete the generated files, so may require manual cleanup. | |
65 if (arguments.contains("--local")) { | |
66 print("Testing using local directory for generated files"); | |
67 useLocalDirectory = true; | |
68 } | |
69 setUp(copyFilesToTempDirectory); | |
70 tearDown(deleteGeneratedFiles); | |
71 test("Test round trip message extraction, translation, code generation, " | |
72 "and printing", () { | |
73 var makeSureWeVerify = expectAsync(runAndVerify); | |
74 return extractMessages(null).then((result) { | |
75 return generateTranslationFiles(result); | |
76 }).then((result) { | |
77 return generateCodeFromTranslation(result); | |
78 }).then(makeSureWeVerify).then(checkResult); | |
79 }); | |
80 } | |
81 | |
82 void copyFilesToTempDirectory() { | |
83 if (useLocalDirectory) return; | |
84 var files = [asTestDirPath('sample_with_messages.dart'), | |
85 asTestDirPath('part_of_sample_with_messages.dart'), | |
86 asTestDirPath('verify_messages.dart'), | |
87 asTestDirPath('run_and_verify.dart'), | |
88 asTestDirPath('embedded_plural_text_before.dart'), | |
89 asTestDirPath('embedded_plural_text_after.dart'), | |
90 asTestDirPath('print_to_list.dart')]; | |
91 for (var filename in files) { | |
92 var file = new File(filename); | |
93 file.copySync(path.join(tempDir, path.basename(filename))); | |
94 } | |
95 } | |
96 | |
97 void deleteGeneratedFiles() { | |
98 if (useLocalDirectory) return; | |
99 try { | |
100 new Directory(tempDir).deleteSync(recursive: true); | |
101 } on Error catch (e) { | |
102 print("Failed to delete $tempDir"); | |
103 print("Exception:\n$e"); | |
104 } | |
105 } | |
106 | |
107 /** | |
108 * Run the process with the given list of filenames, which we assume | |
109 * are in dir() and need to be qualified in case that's not our working | |
110 * directory. | |
111 */ | |
112 Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames) | |
113 { | |
114 // If there's a failure in one of the sub-programs, print its output. | |
115 checkResult(previousResult); | |
116 var filesInTheRightDirectory = filenames.map((x) => asTempDirPath(x)).toList( | |
117 ); | |
118 // Inject the script argument --output-dir in between the script and its | |
119 // arguments. | |
120 var args = [] | |
121 ..addAll(vmArgs) | |
122 ..add(filesInTheRightDirectory.first) | |
123 ..addAll(["--output-dir=$tempDir"]) | |
124 ..addAll(filesInTheRightDirectory.skip(1)); | |
125 var result = Process.run(dart, args, stdoutEncoding: UTF8, stderrEncoding: | |
126 UTF8); | |
127 return result; | |
128 } | |
129 | |
130 void checkResult(ProcessResult previousResult) { | |
131 if (previousResult != null) { | |
132 if (previousResult.exitCode != 0) { | |
133 print("Error running sub-program:"); | |
134 } | |
135 print(previousResult.stdout); | |
136 print(previousResult.stderr); | |
137 print("exitCode=${previousResult.exitCode}"); | |
138 // Fail the test. | |
139 expect(previousResult.exitCode, 0); | |
140 } | |
141 } | |
142 | |
143 Future<ProcessResult> extractMessages(ProcessResult previousResult) => run( | |
144 previousResult, [asTestDirPath('../../bin/extract_to_arb.dart'), | |
145 '--suppress-warnings', 'sample_with_messages.dart', | |
146 'part_of_sample_with_messages.dart']); | |
147 | |
148 Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) => | |
149 run(previousResult, | |
150 [asTestDirPath('make_hardcoded_translation.dart'), | |
151 'intl_messages.arb']); | |
152 | |
153 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) | |
154 => run(previousResult, [asTestDirPath('../../bin/generate_from_arb.dart'), | |
155 deferredLoadArg, '--generated-file-prefix=foo_', | |
156 'sample_with_messages.dart', 'part_of_sample_with_messages.dart', | |
157 'translation_fr.arb', 'translation_de_DE.arb']); | |
158 | |
159 Future<ProcessResult> runAndVerify(ProcessResult previousResult) => run( | |
160 previousResult, [asTempDirPath('run_and_verify.dart')]); | |
OLD | NEW |