Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(7)

Side by Side Diff: pkg/intl/test/message_extraction/message_extraction_test.dart

Issue 197283043: Avoid flaking in message_extraction tests by doing everything in a temp directory (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fixes from review Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 = Directory.systemTemp.createTempSync('message_extraction_test'
20 ).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 asTestDirPath([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; 30 return path.join(intlDirectory, 'test', 'message_extraction', s);
28 } else { 31 }
29 return path.join(intlDirectory, 'test', 'message_extraction', s); 32
30 } 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 asTempDirPath([String s]) {
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 = [asTestDirPath('sample_with_messages.dart'), asTestDirPath(
58 'part_of_sample_with_messages.dart'), asTestDirPath('verify_messages.dart' ),
59 asTestDirPath('run_and_verify.dart')];
60 for (var filename in files) {
61 var file = new File(filename);
62 file.copySync(path.join(tempDir, path.basename(filename)));
63 }
64 }
65
47 void deleteGeneratedFiles() { 66 void deleteGeneratedFiles() {
ricow1 2014/04/10 13:59:30 you don't call this, so this test is now leaving g
Alan Knight 2014/04/21 19:14:36 Should be fixed now.
48 var files = [dir('intl_messages.json'), dir('translation_fr.json'), 67 try {
49 dir('translation_de_DE.json')]; 68 var dir = new Directory(tempDir);
50 files.map((name) => new File(name)).forEach((x) { 69 dir.listSync().forEach((x) => x.deleteSync());
51 if (x.existsSync()) x.deleteSync(); 70 dir.deleteSync();
52 }); 71 } on Error catch (e) {
72 print("Failed to delete $tempDir");
73 print("Exception:\n$e");
74 }
53 } 75 }
54 76
55 /** 77 /**
56 * Run the process with the given list of filenames, which we assume 78 * 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 79 * are in dir() and need to be qualified in case that's not our working
58 * directory. 80 * directory.
59 */ 81 */
60 Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames) 82 Future<ProcessResult> run(ProcessResult previousResult, List<String> filenames)
61 { 83 {
62 // If there's a failure in one of the sub-programs, print its output. 84 // If there's a failure in one of the sub-programs, print its output.
63 if (previousResult != null) { 85 if (previousResult != null) {
64 if (previousResult.exitCode != 0) { 86 if (previousResult.exitCode != 0) {
65 print("Error running sub-program:"); 87 print("Error running sub-program:");
66 } 88 }
67 print(previousResult.stdout); 89 print(previousResult.stdout);
68 print(previousResult.stderr); 90 print(previousResult.stderr);
69 print("exitCode=${previousResult.exitCode}"); 91 print("exitCode=${previousResult.exitCode}");
70 } 92 }
71 var filesInTheRightDirectory = filenames.map((x) => dir(x)).toList(); 93 var filesInTheRightDirectory = filenames.map((x) => asTempDirPath(x)).toList(
94 );
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, stderrEncoding:
80 stderrEncoding: UTF8); 103 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, [asTestDirPath('extract_to_json.dart'),
86 ['extract_to_json.dart', '--suppress-warnings', 'sample_with_messages.dart', 109 '--suppress-warnings', 'sample_with_messages.dart',
87 'part_of_sample_with_messages.dart']); 110 'part_of_sample_with_messages.dart']);
88 111
89 Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) => 112 Future<ProcessResult> generateTranslationFiles(ProcessResult previousResult) =>
90 run( 113 run(previousResult,
91 previousResult, 114 [asTestDirPath('make_hardcoded_translation.dart'),
92 ['make_hardcoded_translation.dart', 'intl_messages.json']); 115 'intl_messages.json']);
93 116
94 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult) 117 Future<ProcessResult> generateCodeFromTranslation(ProcessResult previousResult)
95 => run( 118 => run(previousResult, [asTestDirPath('generate_from_json.dart'),
96 previousResult, 119 '--generated-file-prefix=foo_', 'sample_with_messages.dart',
97 ['generate_from_json.dart', '--generated-file-prefix=foo_', 120 'part_of_sample_with_messages.dart', 'translation_fr.json',
98 'sample_with_messages.dart', 121 'translation_de_DE.json']);
99 'part_of_sample_with_messages.dart', 'translation_fr.json',
100 'translation_de_DE.json' ]);
101 122
123 Future<ProcessResult> runAndVerify(ProcessResult previousResult) => run(
124 previousResult, [asTempDirPath('run_and_verify.dart')]);
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698