Skip to content

Commit

Permalink
Get exit code from RunException
Browse files Browse the repository at this point in the history
  • Loading branch information
rehlma committed Jun 17, 2024
1 parent fd55130 commit e0179a3
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit_tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
- name: Install dependencies sidekick_core
run: cd sidekick_core && dart pub get --no-precompile
- name: Run tests sidekick_core
run: cd sidekick_core && dart test >> /test.txt
run: cd sidekick_core && dart test

vault_test:
runs-on: ubuntu-latest
Expand Down
9 changes: 8 additions & 1 deletion sidekick_core/lib/src/commands/bash_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,18 @@ class BashCommand extends ForwardCommand {
terminal: withStdIn,
);
} catch (e, stack) {
final int exitCode;
if (e is dcli.RunException) {
exitCode = e.exitCode ?? 1;
} else {
exitCode = progress.exitCode ?? 1;
}

throw BashCommandException(
script: bashScript,
commandName: name,
arguments: argResults!.arguments,
exitCode: progress.exitCode!,
exitCode: exitCode,
cause: e,
stack: stack,
);
Expand Down
31 changes: 20 additions & 11 deletions sidekick_core/lib/src/dart.dart
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,26 @@ int systemDart(
throw "Couldn't find dart executable on PATH.";
}

final process = dcli.startFromArgs(
systemDartExecutablePath,
args,
workingDirectory: workingDirectory?.path,
progress: progress,
terminal: progress == null,
nothrow: nothrow || throwOnError != null,
);

final exitCode = process.exitCode ?? -1;

int exitCode = -1;
try {
final process = dcli.startFromArgs(
systemDartExecutablePath,
args,
workingDirectory: workingDirectory?.path,
progress: progress,
terminal: progress == null,
nothrow: nothrow || throwOnError != null,
);

exitCode = process.exitCode ?? -1;
} catch (e) {
if (e is dcli.RunException) {
exitCode = e.exitCode ?? 1;
}
if (throwOnError == null) {
rethrow;
}
}
if (exitCode != 0 && throwOnError != null) {
throw throwOnError();
}
Expand Down
31 changes: 20 additions & 11 deletions sidekick_core/lib/src/flutter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,26 @@ int flutter(
FlutterInitializerConfig(sdk: sdk, packagePath: workingDirectory));
}

final process = dcli.startFromArgs(
Platform.isWindows ? 'bash' : sdk.file('bin/flutter').path,
[if (Platform.isWindows) sdk.file('bin/flutter.exe').path, ...args],
workingDirectory: workingDirectory?.absolute.path,
nothrow: nothrow || throwOnError != null,
progress: progress,
terminal: progress == null,
);

final exitCode = process.exitCode ?? -1;

int exitCode = -1;
try {
final process = dcli.startFromArgs(
Platform.isWindows ? 'bash' : sdk.file('bin/flutter').path,
[if (Platform.isWindows) sdk.file('bin/flutter.exe').path, ...args],
workingDirectory: workingDirectory?.absolute.path,
nothrow: nothrow || throwOnError != null,
progress: progress,
terminal: progress == null,
);

exitCode = process.exitCode ?? -1;
} catch (e) {
if (e is dcli.RunException) {
exitCode = e.exitCode ?? 1;
}
if (throwOnError == null) {
rethrow;
}
}
if (exitCode != 0 && throwOnError != null) {
throw throwOnError();
}
Expand Down
4 changes: 3 additions & 1 deletion sidekick_core/test/bash_command_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ void main() {
try {
await runner.run(['script']);
fail('should throw');
} catch (e) {
} catch (e, stack) {
printOnFailure(e.toString());
printOnFailure(stack.toString());
expect(
e,
isA<BashCommandException>()
Expand Down

0 comments on commit e0179a3

Please sign in to comment.