-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add BashCommand * Example how to load from file * Add tests * Default to withStdIn:false * Make error message easier to understand * Throw BashCommandException * Test arguments * Test all exception properties
- Loading branch information
Showing
5 changed files
with
322 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,150 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:dcli/dcli.dart' as dcli; | ||
import 'package:sidekick_core/sidekick_core.dart'; | ||
|
||
/// A Command that wraps a bash script | ||
/// | ||
/// Easy way to convert an existing bash script into a command for sidekick | ||
/// | ||
/// This makes it easy to handle paths within the bash script. You can define a | ||
/// static [workingDirectory] and always know where the script is executed. | ||
/// | ||
/// Usage as plain text | ||
/// ```dart | ||
/// runner | ||
/// ..addCommand( | ||
/// BashCommand( | ||
/// name: 'test-bash-command', | ||
/// description: 'Prints inputs of a sidekick BashCommand', | ||
/// workingDirectory: runner.repository.root, | ||
/// script: () => ''' | ||
/// echo "arguments: \$@" | ||
/// echo "workingDirectory: \$(pwd)" | ||
/// # Access paths from sidekick | ||
/// ${systemFlutterSdkPath()}/bin/flutter --version | ||
/// ''', | ||
/// ), | ||
/// ); | ||
/// ``` | ||
/// | ||
/// Or load your script from a file | ||
/// ```dart | ||
/// runner | ||
/// ..addCommand( | ||
/// BashCommand( | ||
/// name: 'test-bash-command', | ||
/// description: 'Prints inputs of a sidekick BashCommand', | ||
/// workingDirectory: runner.repository.root, | ||
/// script: () => runner.repository.root | ||
/// .file('scripts/test-bash-command.sh') | ||
/// .readAsString(), | ||
/// ), | ||
/// ) | ||
/// ``` | ||
/// | ||
/// If your script is interactive, set [withStdIn] to `true` to allow stdin to | ||
/// be connected to the script. | ||
class BashCommand extends ForwardCommand { | ||
BashCommand({ | ||
required this.script, | ||
required this.description, | ||
required this.name, | ||
this.workingDirectory, | ||
this.withStdIn = false, | ||
}); | ||
|
||
@override | ||
final String name; | ||
|
||
@override | ||
final String description; | ||
|
||
/// The script to be executed. | ||
/// | ||
/// You may load this script from a file or generate it on the fly. | ||
final FutureOr<String> Function() script; | ||
|
||
/// The directory the bash script is running in. | ||
final Directory? workingDirectory; | ||
|
||
/// Whether to forward stdin to the bash script, default to true | ||
final bool withStdIn; | ||
|
||
@override | ||
Future<void> run() async { | ||
final bashScript = await script(); | ||
|
||
final scriptFile = tempExecutableScriptFile(bashScript); | ||
final Progress progress = | ||
Progress(print, stderr: printerr, captureStderr: true); | ||
|
||
try { | ||
dcli.startFromArgs( | ||
scriptFile.absolute.path, | ||
argResults!.arguments, | ||
workingDirectory: | ||
workingDirectory?.absolute.path ?? entryWorkingDirectory.path, | ||
progress: progress, | ||
terminal: withStdIn, | ||
); | ||
} catch (e, stack) { | ||
throw BashCommandException( | ||
script: bashScript, | ||
commandName: name, | ||
arguments: argResults!.arguments, | ||
exitCode: progress.exitCode!, | ||
cause: e, | ||
stack: stack, | ||
); | ||
} finally { | ||
scriptFile.deleteSync(recursive: true); | ||
} | ||
} | ||
} | ||
|
||
/// Exception thrown when a [BashCommand] fails containing all the information | ||
/// about the script and its error | ||
class BashCommandException implements Exception { | ||
/// The actual script content that was executed | ||
final String script; | ||
|
||
/// The name of the command that executed this script | ||
final String commandName; | ||
|
||
/// The arguments passed into the command | ||
final List<String> arguments; | ||
|
||
/// The exit code of the script that caused the error. (Always != `0`) | ||
final int exitCode; | ||
|
||
/// The complete stacktrace, going into dcli which was ultimately executing this script | ||
final StackTrace stack; | ||
|
||
/// The original exception from dcli | ||
final Object cause; | ||
|
||
const BashCommandException({ | ||
required this.script, | ||
required this.commandName, | ||
required this.arguments, | ||
required this.exitCode, | ||
required this.stack, | ||
required this.cause, | ||
}); | ||
|
||
@override | ||
String toString() { | ||
String args = arguments.joinToString(separator: ' '); | ||
if (!args.isBlank) { | ||
args = "($args)"; | ||
} else { | ||
args = '<no arguments>'; | ||
} | ||
return "Error (exitCode=$exitCode) executing script of command '$commandName' " | ||
"with arguments: $args\n\n" | ||
"'''bash\n" | ||
"${script.trimRight()}\n" | ||
"'''\n\n"; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
import 'package:sidekick_core/sidekick_core.dart'; | ||
import 'package:sidekick_test/fake_stdio.dart'; | ||
import 'package:sidekick_test/sidekick_test.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test('bash command receives arguments', () async { | ||
await insideFakeProjectWithSidekick((dir) async { | ||
final runner = initializeSidekick( | ||
name: 'dash', | ||
dartSdkPath: fakeDartSdk().path, | ||
); | ||
runner.addCommand( | ||
BashCommand( | ||
script: () => 'echo \$@', | ||
description: 'description', | ||
name: 'script', | ||
), | ||
); | ||
final fakeStdOut = FakeStdoutStream(); | ||
await overrideIoStreams( | ||
stdout: () => fakeStdOut, | ||
body: () => runner.run(['script', 'asdf', 'qwer']), | ||
); | ||
|
||
expect(fakeStdOut.lines, contains('asdf qwer')); | ||
}); | ||
}); | ||
|
||
test('workingDirectory default to cwd', () async { | ||
await insideFakeProjectWithSidekick((dir) async { | ||
final runner = initializeSidekick( | ||
name: 'dash', | ||
dartSdkPath: fakeDartSdk().path, | ||
); | ||
runner.addCommand( | ||
BashCommand( | ||
script: () => 'echo \$PWD', | ||
description: 'description', | ||
name: 'script', | ||
), | ||
); | ||
final fakeStdOut = FakeStdoutStream(); | ||
await overrideIoStreams( | ||
stdout: () => fakeStdOut, | ||
body: () => runner.run(['script']), | ||
); | ||
|
||
expect(fakeStdOut.lines.join(), endsWith(Directory.current.path)); | ||
}); | ||
}); | ||
|
||
test('workingDirectory can be set', () async { | ||
await insideFakeProjectWithSidekick((dir) async { | ||
final subDir = dir.directory('someSubDir')..createSync(); | ||
final runner = initializeSidekick( | ||
name: 'dash', | ||
dartSdkPath: fakeDartSdk().path, | ||
); | ||
runner.addCommand( | ||
BashCommand( | ||
script: () => 'echo \$PWD', | ||
description: 'description', | ||
name: 'script', | ||
workingDirectory: subDir, | ||
), | ||
); | ||
final fakeStdOut = FakeStdoutStream(); | ||
await overrideIoStreams( | ||
stdout: () => fakeStdOut, | ||
body: () => runner.run(['script']), | ||
); | ||
|
||
expect(fakeStdOut.lines.join(), endsWith(subDir.path)); | ||
}); | ||
}); | ||
|
||
test('throw BashCommandException on error', () async { | ||
await insideFakeProjectWithSidekick((dir) async { | ||
final runner = initializeSidekick( | ||
name: 'dash', | ||
dartSdkPath: fakeDartSdk().path, | ||
); | ||
runner.addCommand( | ||
BashCommand( | ||
script: () => '#scriptContent\nexit 34', | ||
description: 'description', | ||
name: 'script', | ||
), | ||
); | ||
try { | ||
await runner.run(['script']); | ||
fail('should throw'); | ||
} catch (e) { | ||
expect( | ||
e, | ||
isA<BashCommandException>() | ||
.having((it) => it.exitCode, 'exitCode', 34) | ||
.having((it) => it.script, 'script', contains('#scriptContent')) | ||
.having((it) => it.commandName, 'commandName', 'script') | ||
.having((it) => it.arguments, 'arguments', []) | ||
.having((it) => it.cause, 'cause', isA<RunException>()) | ||
.having( | ||
(it) => it.stack, | ||
'stack', | ||
isA<StackTrace>().having( | ||
(it) => it.toString(), | ||
'toString', | ||
contains('bash_command_test.dart'), | ||
)) | ||
.having( | ||
(it) => it.toString(), | ||
'toString()', | ||
contains('exitCode=34'), | ||
) | ||
.having( | ||
(it) => it.toString(), | ||
'toString()', | ||
contains('<no arguments>'), | ||
), | ||
); | ||
} | ||
}); | ||
}); | ||
|
||
test('BashCommandException contains arguments', () async { | ||
await insideFakeProjectWithSidekick((dir) async { | ||
final runner = initializeSidekick( | ||
name: 'dash', | ||
dartSdkPath: fakeDartSdk().path, | ||
); | ||
runner.addCommand( | ||
BashCommand( | ||
script: () => '#scriptContent\nexit 34', | ||
description: 'description', | ||
name: 'script', | ||
), | ||
); | ||
try { | ||
await runner.run(['script', 'asdf', 'qwer']); | ||
fail('should throw'); | ||
} catch (e) { | ||
expect( | ||
e, | ||
isA<BashCommandException>() | ||
.having((it) => it.exitCode, 'exitCode', 34) | ||
.having( | ||
(it) => it.arguments, 'arguments', ['asdf', 'qwer']).having( | ||
(it) => it.toString(), | ||
'toString()', | ||
contains('asdf qwer'), | ||
), | ||
); | ||
} | ||
}); | ||
}); | ||
} |