@@ -16,64 +16,136 @@ class FuchsiaEmulatorCFv2 extends FuchsiaEmulator {
16
16
static const String ffx = "./third_party/fuchsia/sdk/linux/tools/x64/ffx" ;
17
17
static const String testScriptRoot =
18
18
"./third_party/fuchsia/test_scripts/test/" ;
19
+ static const String withEnv = "./build/fuchsia/with_envs.py" ;
20
+ static const String tmpRoot = "/tmp/dart_ffi_test/" ;
21
+ static const String cmName = "fuchsia_ffi_test_component.cm" ;
19
22
23
+ final Map <String , String > envs = < String , String > {};
20
24
Process ? daemonProc;
21
25
Process ? emuProc;
22
26
String ? emuName;
27
+ Process ? repoProc;
23
28
24
29
@override
25
30
Future <void > publishPackage (String buildDir, String mode, String arch) async {
26
- housekeeping ();
27
-
31
+ try {
32
+ Directory (tmpRoot).deleteSync (recursive: true );
33
+ } catch (_) {}
34
+ // The /tmp/ should always be present, recursive creation is not expected.
35
+ Directory (tmpRoot).createSync ();
28
36
assert (daemonProc == null );
29
- daemonProc = await runWithOutput ("isolate_daemon.py" , []);
37
+ daemonProc = await _run ("isolate_daemon.py" , []);
38
+ var isolateDir = await _captureStdout (daemonProc! );
39
+ print ("+ ffx daemon running on $isolateDir should be ready now." );
40
+ envs["FFX_ISOLATE_DIR" ] = isolateDir;
30
41
assert (emuProc == null );
31
- emuProc = await run (
32
- "start_emulator.py" , ["--disable-graphics" , "--target-id-only" ]);
33
- emuName = await emuProc! .stdout.transform (utf8.decoder).first;
42
+ emuProc = await _run ("start_emulator.py" , [
43
+ "--disable-graphics" ,
44
+ "--target-id-only" ,
45
+ "--device-spec" ,
46
+ "virtual_device_large"
47
+ ]);
48
+ emuName = await _captureStdout (emuProc! );
34
49
print ("+ Targeting emu name $emuName " );
50
+ await _assertRun ("test_connection.py" , [emuName! ]);
51
+ await _assertRun ("publish_package.py" , [
52
+ "--packages" ,
53
+ _testPackagePath (buildDir, mode),
54
+ "--purge-repo" ,
55
+ "--repo" ,
56
+ _tempDirectoryOf ("repo" )
57
+ ]);
58
+ repoProc = await _run ("serve_repo.py" , [
59
+ "run" ,
60
+ "--serve-repo" ,
61
+ _tempDirectoryOf ("repo" ),
62
+ "--repo-name" ,
63
+ "dart-ffi-test-repo" ,
64
+ "--target-id" ,
65
+ emuName!
66
+ ]);
67
+ print ("+ Fuchsia repo ${await _captureStdout (repoProc !)} is running "
68
+ "at ${_tempDirectoryOf ('repo' )}" );
69
+ await _assertRun ("pkg_resolve.py" , [emuName! , cmName]);
35
70
}
36
71
37
72
@override
38
- void stop () {
39
- // isolate_daemon.py should respect the sigterm and gracefully stop the
40
- // daemon process.
73
+ Future <void > stop () async {
74
+ assert (repoProc != null );
75
+ repoProc! .kill ();
76
+ await repoProc! .exitCode;
77
+ assert (emuProc != null );
78
+ emuProc! .kill ();
79
+ await emuProc! .exitCode;
41
80
assert (daemonProc != null );
42
81
daemonProc! .kill ();
43
- emuProc! .kill ();
44
-
45
- // In case anything goes wrong, ensure everything is cleaned up.
46
- housekeeping ();
82
+ await daemonProc! .exitCode;
47
83
}
48
84
49
85
@override
50
- VMCommand getTestCommand (String mode, String arch, List <String > arguments) {
51
- return VMCommand ("echo" , arguments, < String , String > {});
86
+ VMCommand getTestCommand (
87
+ String buildDir, String mode, String arch, List <String > arguments) {
88
+ return VMCommand (
89
+ withEnv,
90
+ _runArgs ("run_executable_test.py" , [
91
+ "--target-id" ,
92
+ emuName! ,
93
+ "--out-dir" ,
94
+ _tempDirectoryOf ("out" ),
95
+ "--test-name" ,
96
+ "fuchsia-pkg://fuchsia.com/${_testPackageName (mode )}#meta/$cmName " ,
97
+ "--logs-dir" ,
98
+ _tempDirectoryOf ("logs" ),
99
+ "--package-deps" ,
100
+ _testPackagePath (buildDir, mode),
101
+ ...arguments
102
+ ]),
103
+ envs);
104
+ }
105
+
106
+ static String _testPackageName (String mode) {
107
+ return "dart_ffi_test_cfv2_$mode " ;
108
+ }
109
+
110
+ static String _testPackagePath (String buildDir, String mode) {
111
+ var farName = _testPackageName (mode);
112
+ return "$buildDir /gen/$farName /$farName .far" ;
113
+ }
114
+
115
+ static String _tempDirectoryOf (String name) {
116
+ return tmpRoot + name;
117
+ }
118
+
119
+ List <String > _runArgs (String script, List <String > args) {
120
+ return [testScriptRoot + script, ...args];
121
+ }
122
+
123
+ /// Executes a test script inside of third_party/fuchsia/test_scripts/test/
124
+ /// with the required environment setup and the arguments.
125
+ Future <Process > _run (String script, List <String > args) async {
126
+ var newArgs = _runArgs (script, args);
127
+ print ("+ Start $withEnv with $newArgs with environment $envs ." );
128
+ return Process .start (withEnv, newArgs, environment: envs);
52
129
}
53
130
54
- static void housekeeping () {
55
- Process . runSync (ffx, [ "emu" , "stop" , "--all" ]);
56
- Process . runSync (ffx, [ "repository" , "server" , "stop" ]);
57
- Process . runSync (ffx, [ "daemon" , "stop" , "-t" , "10000" ] );
131
+ /// Executes a test script and asserts its return code is 0; see _run and
132
+ /// _assert.
133
+ Future < void > _assertRun ( String script, List < String > args) async {
134
+ _assert (( await ( await _run (script, args)).exitCode) == 0 );
58
135
}
59
136
60
- // Same as run, but capture the stdout and stderr.
61
- static Future <Process > runWithOutput (String script, List <String > args) async {
62
- return run (script, args).then ((proc) {
63
- proc.stdout.transform (utf8.decoder).forEach ((x) {
64
- print ("++ [$script ] stdout: $x " );
65
- });
66
- proc.stderr.transform (utf8.decoder).forEach ((x) {
67
- print ("++ [$script ] stderr: $x " );
68
- });
69
- return proc;
70
- });
137
+ /// Captures the first line of output in utf8.
138
+ Future <String > _captureStdout (Process proc) async {
139
+ // The stderr needs to be fully consumed as well.
140
+ proc.stderr.transform (utf8.decoder).forEach ((x) => stderr.write (x));
141
+ return (await proc.stdout.transform (utf8.decoder).first).trim ();
71
142
}
72
143
73
- // Executes a test script inside of third_party/fuchsia/test_scripts/test/
74
- // with the required environment setup and the arguments.
75
- static Future <Process > run (String script, List <String > args) async {
76
- return Process .start ("./build/fuchsia/with_envs.py" ,
77
- [Uri .directory (testScriptRoot).resolve (script).toString (), ...args]);
144
+ /// Unlike assert keyword, always evaluates the input function and throws
145
+ /// exception when the evaluated result is false.
146
+ void _assert (bool condition) {
147
+ if (! condition) {
148
+ throw AssertionError ();
149
+ }
78
150
}
79
151
}
0 commit comments