Fix cache subcommand and add tests

Treat the cache subcommand differently from --no-config:
For --no-config, insert args from selected environment variables
For cache, don't insert args
This commit is contained in:
Aaron Kollasch 2022-10-26 14:28:38 -04:00
parent 4bcea01e9d
commit e773b48135
No known key found for this signature in database
GPG key ID: F813CAE853E39883
4 changed files with 167 additions and 12 deletions

View file

@ -49,13 +49,26 @@ impl App {
}
fn matches(interactive_output: bool) -> Result<ArgMatches> {
let mut args = if wild::args_os().nth(1) == Some("cache".into())
|| wild::args_os().any(|arg| arg == "--no-config")
{
let args = if wild::args_os().nth(1) == Some("cache".into()) {
// Skip the config file and env vars
wild::args_os().collect::<Vec<_>>()
} else if wild::args_os().any(|arg| arg == "--no-config") {
// Skip the arguments in bats config file
get_args_from_env_vars()
let mut cli_args = wild::args_os();
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();
// Read arguments from bats config file
let mut args = get_args_from_env_opts_var()
.unwrap_or_else(get_args_from_config_file)
@ -64,17 +77,15 @@ impl App {
// Selected env vars supersede config vars
args.extend(get_args_from_env_vars());
args
};
let mut cli_args = wild::args_os();
// 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
};
Ok(clap_app::build_app(interactive_output).get_matches_from(args))
}

View file

@ -0,0 +1,10 @@
%YAML 1.2
---
name: C
file_extensions: [c, h]
scope: source.c
contexts:
main:
- match: \b(if|else|for|while)\b
scope: keyword.control.c

View file

@ -0,0 +1,45 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>name</key>
<string>example</string>
<key>settings</key>
<array>
<dict>
<key>settings</key>
<dict>
<key>background</key>
<string>#222222</string>
<key>caret</key>
<string>#979797</string>
<key>foreground</key>
<string>#F8F8F8</string>
<key>invisibles</key>
<string>#777777</string>
<key>lineHighlight</key>
<string>#000000</string>
<key>selection</key>
<string>#57CCBF</string>
</dict>
</dict>
<dict>
<key>name</key>
<string>Comment</string>
<key>scope</key>
<string>comment</string>
<key>settings</key>
<dict>
<key>foreground</key>
<string>#777777</string>
</dict>
</dict>
</array>
<key>uuid</key>
<string>0123-4567-89AB-CDEF</string>
<key>colorSpaceName</key>
<string>sRGB</string>
<key>semanticClass</key>
<string>theme</string>
</dict>
</plist>

View file

@ -892,6 +892,95 @@ fn config_read_arguments_from_file() {
.stdout(predicate::eq("dummy-pager-from-config\n").normalize());
}
// ignore this test for now as cargo cache --clear only targets the default projects dir
#[cfg(unix)]
#[test]
#[ignore]
fn cache_clear() {
let src_dir = "cache_source";
let tmp_dir = tempdir().expect("can create temporary directory");
let themes_filename = "themes.bin";
let syntaxes_filename = "syntaxes.bin";
let metadata_filename = "metadata.yaml";
[themes_filename, syntaxes_filename, metadata_filename]
.iter()
.map(|filename| {
let fp = tmp_dir.path().join(filename);
let mut file = File::create(fp).expect("can create temporary file");
writeln!(file, "dummy content").expect("can write to file");
})
.count();
// Clear the targeted cache
bat_with_config()
.current_dir(Path::new(EXAMPLES_DIR).join(src_dir))
.env("BAT_CONFIG_PATH", "bat.conf")
.env("BAT_THEME", "1337")
.arg("cache")
.arg("--clear")
.arg("--source")
.arg(".")
.arg("--target")
.arg(tmp_dir.path().to_str().unwrap())
.assert()
.success()
.stdout(
predicate::str::is_match(
"Clearing theme set cache ... okay
Clearing syntax set cache ... okay
Clearing metadata file ... okay",
)
.unwrap(),
);
// We expect these files to be removed
assert!(!tmp_dir.path().join(themes_filename).exists());
assert!(!tmp_dir.path().join(syntaxes_filename).exists());
assert!(!tmp_dir.path().join(metadata_filename).exists());
}
#[cfg(unix)]
#[test]
fn cache_build() {
let src_dir = "cache_source";
let tmp_dir = tempdir().expect("can create temporary directory");
let tmp_themes_path = tmp_dir.path().join("themes.bin");
let tmp_syntaxes_path = tmp_dir.path().join("syntaxes.bin");
let tmp_acknowledgements_path = tmp_dir.path().join("acknowledgements.bin");
let tmp_metadata_path = tmp_dir.path().join("metadata.yaml");
// Build the cache
bat_with_config()
.current_dir(Path::new(EXAMPLES_DIR).join(src_dir))
.env("BAT_CONFIG_PATH", "bat.conf")
.env("BAT_THEME", "1337")
.arg("cache")
.arg("--build")
.arg("--blank")
.arg("--source")
.arg(".")
.arg("--target")
.arg(tmp_dir.path().to_str().unwrap())
.arg("--acknowledgements")
.assert()
.success()
.stdout(
predicate::str::is_match(
"Writing theme set to .*/themes.bin ... okay
Writing syntax set to .*/syntaxes.bin ... okay
Writing acknowledgements to .*/acknowledgements.bin ... okay
Writing metadata to folder .* ... okay",
)
.unwrap(),
);
// Now we expect the files to exist. If they exist, we assume contents are correct
assert!(tmp_themes_path.exists());
assert!(tmp_syntaxes_path.exists());
assert!(tmp_acknowledgements_path.exists());
assert!(tmp_metadata_path.exists());
}
#[test]
fn utf16() {
// The output will be converted to UTF-8 with the leading UTF-16