Improve correctness and add more tests

This commit is contained in:
Aaron Kollasch 2022-10-25 22:18:03 -04:00
parent 36ccc6a31e
commit 76aad7c74f
No known key found for this signature in database
GPG key ID: F813CAE853E39883
4 changed files with 134 additions and 6 deletions

View file

@ -54,7 +54,18 @@ impl App {
{
// Skip the arguments in bats config file
wild::args_os().collect::<Vec<_>>()
let mut cli_args = wild::args_os();
// Load selected env vars
let mut args = get_args_from_env_vars();
// Put the zero-th CLI argument (program name) first
args.insert(0, cli_args.next().unwrap());
// .. and the rest at the end
cli_args.for_each(|a| args.push(a));
args
} else {
let mut cli_args = wild::args_os();
@ -68,11 +79,11 @@ impl App {
// env vars supersede config vars
get_args_from_env_vars()
.iter()
.for_each(|a| args.push(a.into()));
.into_iter()
.for_each(|a| args.push(a));
// .. and the rest at the end
cli_args.for_each(|a| args.push(a.into()));
cli_args.for_each(|a| args.push(a));
args
};

View file

@ -145,8 +145,9 @@ pub fn get_args_from_env_vars() -> Vec<OsString> {
("--style", "BAT_STYLE"),
]
.iter()
.filter_map(|(flag, key)| env::var(key).ok().map(|var| [flag.into(), var.into()]))
.filter_map(|(flag, key)| env::var(key).ok().map(|var| [flag.to_string(), var]))
.flatten()
.map(|a| a.into())
.collect()
}

1
tests/examples/bat-tabs.conf vendored Normal file
View file

@ -0,0 +1 @@
--tabs=8

View file

@ -477,6 +477,79 @@ fn tabs_8() {
);
}
#[test]
fn tabs_4_env_overrides_config() {
bat_with_config()
.env("BAT_CONFIG_PATH", "bat-tabs.conf")
.env("BAT_TABS", "4")
.arg("tabs.txt")
.arg("--style=plain")
.arg("--decorations=always")
.assert()
.success()
.stdout(
" 1 2 3 4
1 ?
22 ?
333 ?
4444 ?
55555 ?
666666 ?
7777777 ?
88888888 ?
",
);
}
#[test]
fn tabs_4_arg_overrides_env() {
bat_with_config()
.env("BAT_CONFIG_PATH", "bat-tabs.conf")
.env("BAT_TABS", "6")
.arg("tabs.txt")
.arg("--tabs=4")
.arg("--style=plain")
.arg("--decorations=always")
.assert()
.success()
.stdout(
" 1 2 3 4
1 ?
22 ?
333 ?
4444 ?
55555 ?
666666 ?
7777777 ?
88888888 ?
",
);
}
#[test]
fn tabs_4_arg_overrides_env_noconfig() {
bat()
.env("BAT_TABS", "6")
.arg("tabs.txt")
.arg("--tabs=4")
.arg("--style=plain")
.arg("--decorations=always")
.assert()
.success()
.stdout(
" 1 2 3 4
1 ?
22 ?
333 ?
4444 ?
55555 ?
666666 ?
7777777 ?
88888888 ?
",
);
}
#[test]
fn fail_non_existing() {
bat().arg("non-existing-file").assert().failure();
@ -544,7 +617,7 @@ fn pager_disable() {
}
#[test]
fn pager_arg_override_env() {
fn pager_arg_override_env_withconfig() {
bat_with_config()
.env("BAT_CONFIG_PATH", "bat.conf")
.env("PAGER", "echo another-pager")
@ -557,6 +630,19 @@ fn pager_arg_override_env() {
.stdout(predicate::eq("pager-output\n").normalize());
}
#[test]
fn pager_arg_override_env_noconfig() {
bat()
.env("PAGER", "echo another-pager")
.env("BAT_PAGER", "echo other-pager")
.arg("--pager=echo pager-output")
.arg("--paging=always")
.arg("test.txt")
.assert()
.success()
.stdout(predicate::eq("pager-output\n").normalize());
}
#[test]
fn pager_env_bat_pager_override_config() {
bat_with_config()
@ -1031,6 +1117,35 @@ fn header_full_basic() {
.stderr("");
}
#[test]
fn header_env_basic() {
bat_with_config()
.env("BAT_STYLE", "header-filename,header-filesize")
.arg("test.txt")
.arg("--decorations=always")
.arg("-r=0:0")
.arg("--file-name=foo")
.assert()
.success()
.stdout("File: foo\nSize: 12 B\n")
.stderr("");
}
#[test]
fn header_arg_overrides_env() {
bat_with_config()
.env("BAT_STYLE", "header-filesize")
.arg("test.txt")
.arg("--decorations=always")
.arg("--style=header-filename")
.arg("-r=0:0")
.arg("--file-name=foo")
.assert()
.success()
.stdout("File: foo\n")
.stderr("");
}
#[test]
fn header_binary() {
bat()