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