mirror of
https://github.com/Llewellynvdm/starship.git
synced 2024-11-28 15:56:28 +00:00
test: fix mocked commands (#1491)
Ensure that output of mocked commands faithfully replicates output of actual commands including any trailing whitespace.
This commit is contained in:
parent
c4fa87a213
commit
6763a7b006
@ -55,9 +55,9 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
|
|
||||||
fn format_crystal_version(crystal_version: &str) -> Option<String> {
|
fn format_crystal_version(crystal_version: &str) -> Option<String> {
|
||||||
let version = crystal_version
|
let version = crystal_version
|
||||||
// split into ["Crystal", "0.32.1", ...]
|
// split into ["Crystal", "0.35.1", ...]
|
||||||
.split_whitespace()
|
.split_whitespace()
|
||||||
// return "0.32.1"
|
// return "0.35.1"
|
||||||
.nth(1)?;
|
.nth(1)?;
|
||||||
|
|
||||||
let mut formatted_version = String::with_capacity(version.len() + 1);
|
let mut formatted_version = String::with_capacity(version.len() + 1);
|
||||||
@ -89,7 +89,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("shard.yml"))?.sync_all()?;
|
File::create(dir.path().join("shard.yml"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = render_module("crystal", dir.path(), None);
|
let actual = render_module("crystal", dir.path(), None);
|
||||||
let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.32.1")));
|
let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.35.1")));
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
dir.close()
|
dir.close()
|
||||||
@ -101,7 +101,7 @@ mod tests {
|
|||||||
File::create(dir.path().join("main.cr"))?.sync_all()?;
|
File::create(dir.path().join("main.cr"))?.sync_all()?;
|
||||||
|
|
||||||
let actual = render_module("crystal", dir.path(), None);
|
let actual = render_module("crystal", dir.path(), None);
|
||||||
let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.32.1")));
|
let expected = Some(format!("via {} ", Color::Red.bold().paint("🔮 v0.35.1")));
|
||||||
assert_eq!(expected, actual);
|
assert_eq!(expected, actual);
|
||||||
|
|
||||||
dir.close()
|
dir.close()
|
||||||
|
@ -57,10 +57,10 @@ fn get_erlang_version() -> Option<String> {
|
|||||||
&[
|
&[
|
||||||
"-noshell",
|
"-noshell",
|
||||||
"-eval",
|
"-eval",
|
||||||
"Fn=filename:join([code:root_dir(),\"releases\",erlang:system_info(otp_release),\"OTP_VERSION\"]),\
|
"'Fn=filename:join([code:root_dir(),\"releases\",erlang:system_info(otp_release),\"OTP_VERSION\"]),\
|
||||||
{ok,Content}=file:read_file(Fn),\
|
{ok,Content}=file:read_file(Fn),\
|
||||||
io:format(\"~s\",[Content]),\
|
io:format(\"~s\",[Content]),\
|
||||||
halt(0)."
|
halt(0).'"
|
||||||
]
|
]
|
||||||
)?.stdout.trim().to_string())
|
)?.stdout.trim().to_string())
|
||||||
}
|
}
|
||||||
|
@ -24,7 +24,7 @@ pub fn module<'a>(context: &'a Context) -> Option<Module<'a>> {
|
|||||||
"php",
|
"php",
|
||||||
&[
|
&[
|
||||||
"-nr",
|
"-nr",
|
||||||
"echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;",
|
"'echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;'",
|
||||||
],
|
],
|
||||||
) {
|
) {
|
||||||
Some(php_cmd_output) => {
|
Some(php_cmd_output) => {
|
||||||
|
54
src/utils.rs
54
src/utils.rs
@ -40,36 +40,42 @@ pub fn exec_cmd(cmd: &str, args: &[&str]) -> Option<CommandOutput> {
|
|||||||
};
|
};
|
||||||
match command.as_str() {
|
match command.as_str() {
|
||||||
"crystal --version" => Some(CommandOutput {
|
"crystal --version" => Some(CommandOutput {
|
||||||
stdout: String::from("Crystal 0.32.1 (2019-12-18)"),
|
stdout: String::from(
|
||||||
|
"\
|
||||||
|
Crystal 0.35.1 (2020-06-19)
|
||||||
|
|
||||||
|
LLVM: 10.0.0
|
||||||
|
Default target: x86_64-apple-macosx\n",
|
||||||
|
),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"dummy_command" => Some(CommandOutput {
|
"dummy_command" => Some(CommandOutput {
|
||||||
stdout: String::from("stdout ok!"),
|
stdout: String::from("stdout ok!\n"),
|
||||||
stderr: String::from("stderr ok!"),
|
stderr: String::from("stderr ok!\n"),
|
||||||
}),
|
}),
|
||||||
"elixir --version" => Some(CommandOutput {
|
"elixir --version" => Some(CommandOutput {
|
||||||
stdout: String::from(
|
stdout: String::from(
|
||||||
"\
|
"\
|
||||||
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
|
Erlang/OTP 22 [erts-10.6.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]
|
||||||
|
|
||||||
Elixir 1.10 (compiled with Erlang/OTP 22)",
|
Elixir 1.10 (compiled with Erlang/OTP 22)\n",
|
||||||
),
|
),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"elm --version" => Some(CommandOutput {
|
"elm --version" => Some(CommandOutput {
|
||||||
stdout: String::from("0.19.1"),
|
stdout: String::from("0.19.1\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"go version" => Some(CommandOutput {
|
"go version" => Some(CommandOutput {
|
||||||
stdout: String::from("go version go1.12.1 linux/amd64"),
|
stdout: String::from("go version go1.12.1 linux/amd64\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"helm version --short --client" => Some(CommandOutput {
|
"helm version --short --client" => Some(CommandOutput {
|
||||||
stdout: String::from("v3.1.1+gafe7058"),
|
stdout: String::from("v3.1.1+gafe7058\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"julia --version" => Some(CommandOutput {
|
"julia --version" => Some(CommandOutput {
|
||||||
stdout: String::from("julia version 1.4.0"),
|
stdout: String::from("julia version 1.4.0\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"nim --version" => Some(CommandOutput {
|
"nim --version" => Some(CommandOutput {
|
||||||
@ -84,7 +90,7 @@ active boot switches: -d:release\n",
|
|||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"node --version" => Some(CommandOutput {
|
"node --version" => Some(CommandOutput {
|
||||||
stdout: String::from("v12.0.0"),
|
stdout: String::from("v12.0.0\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"ocaml -vnum" => Some(CommandOutput {
|
"ocaml -vnum" => Some(CommandOutput {
|
||||||
@ -95,36 +101,30 @@ active boot switches: -d:release\n",
|
|||||||
stdout: String::from("4.08.1\n"),
|
stdout: String::from("4.08.1\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"php -nr echo PHP_MAJOR_VERSION.'.'.PHP_MINOR_VERSION.'.'.PHP_RELEASE_VERSION;" => {
|
"php -nr 'echo PHP_MAJOR_VERSION.\".\".PHP_MINOR_VERSION.\".\".PHP_RELEASE_VERSION;'" => {
|
||||||
Some(CommandOutput {
|
Some(CommandOutput {
|
||||||
stdout: String::from("7.3.8"),
|
stdout: String::from("7.3.8"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
"purs --version" => Some(CommandOutput {
|
"purs --version" => Some(CommandOutput {
|
||||||
stdout: String::from("0.13.5"),
|
stdout: String::from("0.13.5\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"python --version" => Some(CommandOutput {
|
"python --version" => Some(CommandOutput {
|
||||||
stdout: String::from("Python 2.7.17"),
|
stdout: String::default(),
|
||||||
stderr: String::default(),
|
stderr: String::from("Python 2.7.17\n"),
|
||||||
}),
|
}),
|
||||||
"python3 --version" => Some(CommandOutput {
|
"python3 --version" => Some(CommandOutput {
|
||||||
stdout: String::from("Python 3.8.0"),
|
stdout: String::from("Python 3.8.0\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"ruby -v" => Some(CommandOutput {
|
"ruby -v" => Some(CommandOutput {
|
||||||
stdout: String::from("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]"),
|
stdout: String::from("ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-linux-gnu]\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"stack --no-install-ghc --lock-file read-only ghc -- --numeric-version" => {
|
|
||||||
Some(CommandOutput {
|
|
||||||
stdout: String::from("8.6.5"),
|
|
||||||
stderr: String::default(),
|
|
||||||
})
|
|
||||||
}
|
|
||||||
"zig version" => Some(CommandOutput {
|
"zig version" => Some(CommandOutput {
|
||||||
stdout: String::from("0.6.0"),
|
stdout: String::from("0.6.0\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"cmake --version" => Some(CommandOutput {
|
"cmake --version" => Some(CommandOutput {
|
||||||
@ -137,11 +137,11 @@ CMake suite maintained and supported by Kitware (kitware.com/cmake).\n",
|
|||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
"terraform version" => Some(CommandOutput {
|
"terraform version" => Some(CommandOutput {
|
||||||
stdout: String::from("Terraform v0.12.14"),
|
stdout: String::from("Terraform v0.12.14\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
s if s.starts_with("erl") => Some(CommandOutput {
|
s if s.starts_with("erl -noshell -eval") => Some(CommandOutput {
|
||||||
stdout: String::from("22.1.3"),
|
stdout: String::from("22.1.3\n"),
|
||||||
stderr: String::default(),
|
stderr: String::default(),
|
||||||
}),
|
}),
|
||||||
// If we don't have a mocked command fall back to executing the command
|
// If we don't have a mocked command fall back to executing the command
|
||||||
@ -234,8 +234,8 @@ mod tests {
|
|||||||
fn exec_mocked_command() {
|
fn exec_mocked_command() {
|
||||||
let result = exec_cmd("dummy_command", &[]);
|
let result = exec_cmd("dummy_command", &[]);
|
||||||
let expected = Some(CommandOutput {
|
let expected = Some(CommandOutput {
|
||||||
stdout: String::from("stdout ok!"),
|
stdout: String::from("stdout ok!\n"),
|
||||||
stderr: String::from("stderr ok!"),
|
stderr: String::from("stderr ok!\n"),
|
||||||
});
|
});
|
||||||
|
|
||||||
assert_eq!(result, expected)
|
assert_eq!(result, expected)
|
||||||
|
Loading…
Reference in New Issue
Block a user