From e773b48135bd23d52ae7dd57348aebb18ea8f196 Mon Sep 17 00:00:00 2001 From: Aaron Kollasch Date: Wed, 26 Oct 2022 14:28:38 -0400 Subject: [PATCH] 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 --- src/bin/bat/app.rs | 35 +++++--- .../cache_source/syntaxes/c.sublime-syntax | 10 +++ .../cache_source/themes/example.tmTheme | 45 ++++++++++ tests/integration_tests.rs | 89 +++++++++++++++++++ 4 files changed, 167 insertions(+), 12 deletions(-) create mode 100644 tests/examples/cache_source/syntaxes/c.sublime-syntax create mode 100644 tests/examples/cache_source/themes/example.tmTheme diff --git a/src/bin/bat/app.rs b/src/bin/bat/app.rs index 8487010f..58389beb 100644 --- a/src/bin/bat/app.rs +++ b/src/bin/bat/app.rs @@ -49,13 +49,26 @@ impl App { } fn matches(interactive_output: bool) -> Result { - 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::>() + } 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()); + // 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 }; - 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)); - Ok(clap_app::build_app(interactive_output).get_matches_from(args)) } diff --git a/tests/examples/cache_source/syntaxes/c.sublime-syntax b/tests/examples/cache_source/syntaxes/c.sublime-syntax new file mode 100644 index 00000000..7f88e402 --- /dev/null +++ b/tests/examples/cache_source/syntaxes/c.sublime-syntax @@ -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 diff --git a/tests/examples/cache_source/themes/example.tmTheme b/tests/examples/cache_source/themes/example.tmTheme new file mode 100644 index 00000000..94b40cfd --- /dev/null +++ b/tests/examples/cache_source/themes/example.tmTheme @@ -0,0 +1,45 @@ + + + + + name + example + settings + + + settings + + background + #222222 + caret + #979797 + foreground + #F8F8F8 + invisibles + #777777 + lineHighlight + #000000 + selection + #57CCBF + + + + name + Comment + scope + comment + settings + + foreground + #777777 + + + + uuid + 0123-4567-89AB-CDEF + colorSpaceName + sRGB + semanticClass + theme + + diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 4df84746..69a3f5fc 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -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