use anyhow::{Result, ensure};
use rstest::rstest;
use super::{aborts_on_error, declares_one_shell, effective_shellflags, shell_aborts_on_error};
#[rstest]
#[case::unset("lint:\n\tcargo clippy\n", false)]
#[case::plain(".SHELLFLAGS := -e -c\n", true)]
#[case::similarly_named_variable(".SHELLFLAGS_NOTE := -e is missing here\n", false)]
#[case::later_assignment_removes_it(".SHELLFLAGS := -e -c\n.SHELLFLAGS := -c\n", false)]
#[case::later_assignment_adds_it(".SHELLFLAGS := -c\n.SHELLFLAGS := -e -c\n", true)]
#[case::appended_later(".SHELLFLAGS := -c\n.SHELLFLAGS += -e\n", true)]
#[case::appended_after_errexit(".SHELLFLAGS := -e -c\n.SHELLFLAGS += -u\n", true)]
#[case::conditional_after_a_value(".SHELLFLAGS := -c\n.SHELLFLAGS ?= -e -c\n", false)]
#[case::conditional_when_unset(".SHELLFLAGS ?= -e -c\n", true)]
#[case::trailing_comment(".SHELLFLAGS := -c # -e would go here\n", false)]
#[case::recipe_line("lint:\n\t.SHELLFLAGS := -e -c\n", false)]
#[case::bundled_with_pipefail(".SHELLFLAGS := -eo pipefail -c\n", true)]
#[case::named_option(".SHELLFLAGS := -o errexit -c\n", true)]
#[case::a_different_named_option(".SHELLFLAGS := -o pipefail -c\n", false)]
#[case::a_bundle_without_it(".SHELLFLAGS := -uc\n", false)]
fn only_a_final_value_carrying_dash_e_aborts(
#[case] makefile: &str,
#[case] aborts: bool,
) -> Result<()> {
let read = shell_aborts_on_error(makefile);
ensure!(
read == aborts,
"expected aborts={aborts} for {makefile:?}, effective flags {:?}",
effective_shellflags(makefile)
);
Ok(())
}
#[rstest]
#[case::dash_e("-e -c", true)]
#[case::bundle("-eo pipefail -c", true)]
#[case::named("-o errexit -c", true)]
#[case::default_only("-c", false)]
#[case::another_named_option("-o pipefail -c", false)]
#[case::plus_e("+e -c", false)]
#[case::an_argument_containing_e("-c errexit", false)]
fn errexit_counts_however_it_is_spelled(#[case] flags: &str, #[case] aborts: bool) -> Result<()> {
ensure!(
aborts_on_error(flags) == aborts,
"expected aborts={aborts} for {flags:?}"
);
Ok(())
}
#[rstest]
#[case::absent("lint:\n\tcargo clippy\n", false)]
#[case::plain(".ONESHELL:\n", true)]
#[case::spaced_colon(".ONESHELL :\n", true)]
#[case::tab_before_colon(".ONESHELL\t:\n", true)]
#[case::indented(" .ONESHELL:\n", true)]
#[case::with_a_prerequisite(".ONESHELL: lint\n", true)]
#[case::similarly_named_variable(".ONESHELL_NOTE := not a declaration\n", false)]
#[case::mentioned_in_a_comment("# .ONESHELL would go here\n", false)]
fn one_shell_is_recognised_however_make_would_parse_it(
#[case] makefile: &str,
#[case] declared: bool,
) -> Result<()> {
ensure!(
declares_one_shell(makefile) == declared,
"expected declared={declared} for {makefile:?}"
);
Ok(())
}