Chromium Code Reviews| 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 import 'verify_messages.dart'; | |
| 14 import 'sample_with_messages.dart' as sample; | |
| 15 | 13 |
| 16 final dart = Platform.executable; | 14 final dart = Platform.executable; |
| 17 | 15 |
| 18 /** The VM arguments we were given, most important package-root. */ | 16 /** The VM arguments we were given, most important package-root. */ |
| 19 final vmArgs = Platform.executableArguments; | 17 final vmArgs = Platform.executableArguments; |
| 20 | 18 |
| 19 var tempDir = | |
| 20 Directory.systemTemp.createTempSync('message_extraction_test').path; | |
| 21 | |
| 21 /** | 22 /** |
| 22 * Translate a file path into this test directory, regardless of the | 23 * Translate a relative file path into this test directory. This is |
| 23 * working directory. | 24 * 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 | |
| 26 * might be command-line options. | |
| 24 */ | 27 */ |
| 25 String dir([String s]) { | 28 String inTestDir([String s]) { |
| 26 if (s != null && s.startsWith("--")) { // Don't touch command-line options. | 29 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; |
| 27 return s; | |
| 28 } else { | |
| 29 return path.join(intlDirectory, 'test', 'message_extraction', s); | 30 return path.join(intlDirectory, 'test', 'message_extraction', s); |
| 30 } | 31 } |
| 32 | |
| 33 /** | |
| 34 * 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 | |
| 36 * is an absolute path or begins with "--", because some of the arguments | |
| 37 * might be command-line options. | |
| 38 */ | |
| 39 String inTempDir([String s]) { | |
|
Emily Fortuna
2014/03/26 17:53:07
maybe call this something like toTempDirPath or so
Alan Knight
2014/03/27 17:14:21
Done.
| |
| 40 if (s == null || s.startsWith("--") || path.isAbsolute(s)) return s; | |
| 41 return path.join(tempDir, s); | |
| 31 } | 42 } |
| 32 | 43 |
| 33 main() { | 44 main() { |
| 34 test("Test round trip message extraction, translation, code generation, " | 45 test("Test round trip message extraction, translation, code generation, " |
| 35 "and printing", () { | 46 "and printing", () { |
| 36 deleteGeneratedFiles(); | 47 copyFilesToTempDirectory(); |
| 37 return extractMessages(null).then((result) { | 48 return extractMessages(null).then((result) { |
| 38 return generateTranslationFiles(result); | 49 return generateTranslationFiles(result); |
| 39 }).then((result) { | 50 }).then((result) { |
| 40 return generateCodeFromTranslation(result); | 51 return generateCodeFromTranslation(result); |
| 41 }).then((_) => sample.main()) | 52 }).then((result) => runAndVerify(result)); |
| 42 .then(verifyResult) | |
| 43 .whenComplete(deleteGeneratedFiles); | |
| 44 }); | 53 }); |
| 45 } | 54 } |
| 46 | 55 |
| 56 void copyFilesToTempDirectory() { | |
| 57 var files = [inTestDir('sample_with_messages.dart'), | |
| 58 inTestDir('part_of_sample_with_messages.dart'), | |
| 59 inTestDir('verify_messages.dart'), | |
| 60 inTestDir('run_and_verify.dart')]; | |
| 61 for (var filename in files) { | |
| 62 var file = new File(filename); | |
| 63 file.copySync(path.join(tempDir, path.basename(filename))); | |
| 64 } | |
| 65 } | |
| 66 | |
| 47 void deleteGeneratedFiles() { | 67 void deleteGeneratedFiles() { |
| 48 var files = [dir('intl_messages.json'), dir('translation_fr.json'), | 68 try { |
| 49 dir('translation_de_DE.json')]; | 69 var dir = new Directory(tempDir); |
| 50 files.map((name) => new File(name)).forEach((x) { | 70 dir.listSync().forEach((x) => x.deleteSync()); |
| 51 if (x.existsSync()) x.deleteSync(); | 71 dir.deleteSync(); |
| 52 }); | 72 } on Error catch(e) { |
| 73 print("Failed to delete $tempDir"); | |
| 74 print("Exception:\n$e"); | |
| 75 } | |
| 53 } | 76 } |
| 54 | 77 |
| 55 /** | 78 /** |
| 56 * Run the process with the given list of filenames, which we assume | 79 * Run the process with the given list of filenames, which we assume |
| 57 * are in dir() and need to be qualified in case that's not our working | 80 * are in dir() and need to be qualified in case that's not our working |
| 58 * directory. | 81 * directory. |
| 59 */ | 82 */ |
| 60 Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames) | 83 Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames) |
| 61 { | 84 { |
| 62 // If there's a failure in one of the sub-programs, print its output. | 85 // If there's a failure in one of the sub-programs, print its output. |
| 63 if (previousResult != null) { | 86 if (previousResult != null) { |
| 64 if (previousResult.exitCode != 0) { | 87 if (previousResult.exitCode != 0) { |
| 65 print("Error running sub-program:"); | 88 print("Error running sub-program:"); |
| 66 } | 89 } |
| 67 print(previousResult.stdout); | 90 print(previousResult.stdout); |
| 68 print(previousResult.stderr); | 91 print(previousResult.stderr); |
| 69 print("exitCode=${previousResult.exitCode}"); | 92 print("exitCode=${previousResult.exitCode}"); |
| 70 } | 93 } |
| 71 var filesInTheRightDirectory = filenames.map((x) => dir(x)).toList(); | 94 var filesInTheRightDirectory = filenames.map((x) => inTempDir(x)).toList(); |
| 72 // Inject the script argument --output-dir in between the script and its | 95 // Inject the script argument --output-dir in between the script and its |
| 73 // arguments. | 96 // arguments. |
| 74 var args = [] | 97 var args = [] |
| 75 ..addAll(vmArgs) | 98 ..addAll(vmArgs) |
| 76 ..add(filesInTheRightDirectory.first) | 99 ..add(filesInTheRightDirectory.first) |
| 77 ..addAll(["--output-dir=${dir()}"]) | 100 ..addAll(["--output-dir=$tempDir"]) |
| 78 ..addAll(filesInTheRightDirectory.skip(1)); | 101 ..addAll(filesInTheRightDirectory.skip(1)); |
| 79 var result = Process.run(dart, args, stdoutEncoding: UTF8, | 102 var result = Process.run(dart, args, stdoutEncoding: UTF8, |
| 80 stderrEncoding: UTF8); | 103 stderrEncoding: UTF8); |
| 81 return result; | 104 return result; |
| 82 } | 105 } |
| 83 | 106 |
| 84 Future<ProcessResult> extractMessages(ProcessResult previousResult) => run( | 107 Future<ProcessResult> extractMessages(ProcessResult previousResult) => run( |
| 85 previousResult, | 108 previousResult, |
| 86 ['extract_to_json.dart', '--suppress-warnings', 'sample_with_messages.dart', | 109 [inTestDir('extract_to_json.dart'), '--suppress-warnings', |
| 87 'part_of_sample_with_messages.dart']); | 110 'sample_with_messages.dart', 'part_of_sample_with_messages.dart']); |
| 88 | 111 |
| 89 Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) => | 112 Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) => |
| 90 run( | 113 run( |
| 91 previousResult, | 114 previousResult, |
|
Emily Fortuna
2014/03/26 17:53:07
this feels like a curious indentation choice to me
Alan Knight
2014/03/27 17:14:21
Done.
| |
| 92 ['make_hardcoded_translation.dart', 'intl_messages.json']); | 115 [inTestDir('make_hardcoded_translation.dart'), 'intl_messages.json']); |
| 93 | 116 |
| 94 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) | 117 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) |
| 95 => run( | 118 => run( |
| 96 previousResult, | 119 previousResult, |
| 97 ['generate_from_json.dart', '--generated-file-prefix=foo_', | 120 [inTestDir('generate_from_json.dart'), '--generated-file-prefix=foo_', |
| 98 'sample_with_messages.dart', | 121 'sample_with_messages.dart', |
| 99 'part_of_sample_with_messages.dart', 'translation_fr.json', | 122 'part_of_sample_with_messages.dart', 'translation_fr.json', |
| 100 'translation_de_DE.json' ]); | 123 'translation_de_DE.json' ]); |
| 101 | 124 |
| 125 Future<ProcessResult> runAndVerify(ProcessResult previousResult) => | |
| 126 run( | |
| 127 previousResult, | |
| 128 [inTempDir('run_and_verify.dart')]); | |
| OLD | NEW |