Skip to content

Commit

Permalink
chore: add rustup tests (#47)
Browse files Browse the repository at this point in the history
  • Loading branch information
knopp authored Dec 22, 2023
1 parent 6ea1624 commit a3b7352
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 0 deletions.
56 changes: 56 additions & 0 deletions build_tool/lib/src/util.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,44 @@ class CommandFailedException implements Exception {
}
}

class TestRunCommandArgs {
final String executable;
final List<String> arguments;
final String? workingDirectory;
final Map<String, String>? environment;
final bool includeParentEnvironment;
final bool runInShell;
final Encoding? stdoutEncoding;
final Encoding? stderrEncoding;

TestRunCommandArgs({
required this.executable,
required this.arguments,
this.workingDirectory,
this.environment,
this.includeParentEnvironment = true,
this.runInShell = false,
this.stdoutEncoding,
this.stderrEncoding,
});
}

class TestRunCommandResult {
TestRunCommandResult({
this.pid = 1,
this.exitCode = 0,
this.stdout = '',
this.stderr = '',
});

final int pid;
final int exitCode;
final String stdout;
final String stderr;
}

TestRunCommandResult Function(TestRunCommandArgs args)? testRunCommandOverride;

ProcessResult runCommand(
String executable,
List<String> arguments, {
Expand All @@ -47,6 +85,24 @@ ProcessResult runCommand(
Encoding? stdoutEncoding = systemEncoding,
Encoding? stderrEncoding = systemEncoding,
}) {
if (testRunCommandOverride != null) {
final result = testRunCommandOverride!(TestRunCommandArgs(
executable: executable,
arguments: arguments,
workingDirectory: workingDirectory,
environment: environment,
includeParentEnvironment: includeParentEnvironment,
runInShell: runInShell,
stdoutEncoding: stdoutEncoding,
stderrEncoding: stderrEncoding,
));
return ProcessResult(
result.pid,
result.exitCode,
result.stdout,
result.stderr,
);
}
log.finer('Running command $executable ${arguments.join(' ')}');
final res = Process.runSync(
_resolveExecutable(executable),
Expand Down
66 changes: 66 additions & 0 deletions build_tool/test/rustup_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import 'package:build_tool/src/rustup.dart';
import 'package:build_tool/src/util.dart';
import 'package:test/test.dart';

void main() {
test('rustup with no toolchains', () {
bool didListToolchains = false;
bool didInstallStable = false;
bool didListTargets = false;
testRunCommandOverride = (args) {
expect(args.executable, 'rustup');
switch (args.arguments) {
case ['toolchain', 'list']:
didListToolchains = true;
return TestRunCommandResult(stdout: 'no installed toolchains\n');
case ['toolchain', 'install', 'stable']:
didInstallStable = true;
return TestRunCommandResult();
case ['target', 'list', '--toolchain', 'stable', '--installed']:
didListTargets = true;
return TestRunCommandResult(
stdout: 'x86_64-unknown-linux-gnu\nx86_64-apple-darwin\n');
default:
throw Exception('Unexpected call: ${args.arguments}');
}
};
final rustup = Rustup();
rustup.installToolchain('stable');
expect(didInstallStable, true);
expect(didListToolchains, true);
expect(didListTargets, true);
expect(rustup.installedTargets('stable'), [
'x86_64-unknown-linux-gnu',
'x86_64-apple-darwin',
]);
testRunCommandOverride = null;
});

test('rustup with esp toolchain', () {
final targetsQueried = <String>[];
testRunCommandOverride = (args) {
expect(args.executable, 'rustup');
switch (args.arguments) {
case ['toolchain', 'list']:
return TestRunCommandResult(
stdout: 'stable-aarch64-apple-darwin (default)\n'
'nightly-aarch64-apple-darwin\n'
'esp\n');
case ['target', 'list', '--toolchain', String toolchain, '--installed']:
targetsQueried.add(toolchain);
return TestRunCommandResult(stdout: '$toolchain:target\n');
default:
throw Exception('Unexpected call: ${args.arguments}');
}
};
final rustup = Rustup();
expect(targetsQueried, [
'stable-aarch64-apple-darwin',
'nightly-aarch64-apple-darwin',
]);
expect(rustup.installedTargets('stable'),
['stable-aarch64-apple-darwin:target']);
expect(rustup.installedTargets('nightly'),
['nightly-aarch64-apple-darwin:target']);
});
}

0 comments on commit a3b7352

Please sign in to comment.