Skip to content

Commit b70263a

Browse files
[golden_tester] Remove docker image after running the command (Resolves #2088) (#2089)
1 parent fc53920 commit b70263a

File tree

1 file changed

+30
-3
lines changed

1 file changed

+30
-3
lines changed

golden_runner/lib/src/commands.dart

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,12 @@ class GoldenTestCommand extends Command {
111111
...cmdArguments,
112112
],
113113
description: 'Golden tests',
114+
throwOnError: false,
114115
);
115116

117+
// After running the tests, we don't need the image anymore. Remove it.
118+
await _removeDockerImage();
119+
116120
// Mapping the failure directories causes them to be created automatically, even without any failing test.
117121
// Remove all the empty failure directories.
118122
for (final dirName in dirs) {
@@ -268,7 +272,11 @@ class UpdateGoldensCommand extends Command {
268272
...cmdArguments,
269273
],
270274
description: 'Update goldens',
275+
throwOnError: false,
271276
);
277+
278+
// After running the tests, we don't need the image anymore. Remove it.
279+
await _removeDockerImage();
272280
}
273281
}
274282

@@ -295,6 +303,20 @@ Future<void> _buildDockerImage() async {
295303
);
296304
}
297305

306+
/// Removes the image built by the golden tester.
307+
Future<void> _removeDockerImage() async {
308+
await _runProcess(
309+
executable: 'docker',
310+
arguments: [
311+
'image', 'rm', //
312+
'-f',
313+
'supereditor_golden_tester',
314+
],
315+
description: 'Removing image',
316+
throwOnError: false,
317+
);
318+
}
319+
298320
/// Runs [executable] with the given [arguments].
299321
///
300322
/// [executable] could be an absolute path or it could be resolved from the PATH.
@@ -305,12 +327,15 @@ Future<void> _buildDockerImage() async {
305327
///
306328
/// The child process stdout and stderr are written to the current process stdout.
307329
///
308-
/// Throws and exception using [description] in the message if the process exists with a non-zero exit code.
309-
Future<void> _runProcess({
330+
/// If [throwOnError] is `true`, throws an exception if the process exits with a non-zero exit code.
331+
///
332+
/// If [throwOnError] is `false`, the function returns the exit code.
333+
Future<int> _runProcess({
310334
required String executable,
311335
required List<String> arguments,
312336
required String description,
313337
String? workingDirectory,
338+
bool throwOnError = true,
314339
}) async {
315340
final process = await Process.start(
316341
executable,
@@ -323,7 +348,9 @@ Future<void> _runProcess({
323348

324349
final exitCode = await process.exitCode;
325350

326-
if (exitCode != 0) {
351+
if (exitCode != 0 && throwOnError) {
327352
throw Exception('$description failed');
328353
}
354+
355+
return exitCode;
329356
}

0 commit comments

Comments
 (0)