// Copyright 2024 Steve Wayne Warren // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. use proc_macro2 as _; use proc_macro_error as _; use quote as _; use syn as _; use assert_cmd::Command; use predicates::prelude::*; use rstest::rstest; use skipif::skip_if; #[rstest] #[case::dont_skip_if_var_is_set_notskipped( "tests::dont_skip_if_var_is_set", "test tests::dont_skip_if_var_is_set ... ok", vec![] )] #[case::skipif_missing_env_skipped( "tests::skipif_missing_one_or_more_specified_envs", "test tests::skipif_missing_one_or_more_specified_envs_SKIPPED ... ok", vec![] )] #[case::skipif_missing_env_skipped( "tests::skipif_missing_env", "test tests::skipif_missing_env_SKIPPED ... ok", vec![] )] #[case::skipif_missing_env_with_nondefault_test_signature_skipped( "tests::skipif_missing_env_with_nondefault_test_signature", "test tests::skipif_missing_env_with_nondefault_test_signature_SKIPPED ... ok", vec![] )] #[case::skipif_missing_env_notskipped( "tests::skipif_missing_env", "test tests::skipif_missing_env ... ok", vec![("UNLIKELYTOBESET", "1")] )] #[case::skipif_missing_env_with_nondefault_test_signature_notskipped( "tests::skipif_missing_env_with_nondefault_test_signature", "test tests::skipif_missing_env_with_nondefault_test_signature ... ok", vec![("UNLIKELYTOBESET", "1")] )] #[case::skipif_missing_one_or_more_specified_envs_notskipped( "tests::skipif_missing_one_or_more_specified_envs", "test tests::skipif_missing_one_or_more_specified_envs ... ok", vec![("UNLIKELYTOBESET", "1"),("ALSOUNLIKELYTOBESET", "1")] )] fn recompile_validate_stdout( #[case] test_name: &str, #[case] expect_contains_str: &str, #[case] set_envs: Vec<(&str, &str)>, ) { Command::new("cargo") .arg("test") .arg(test_name) .arg("--") .arg("--show-output") .envs(set_envs) .assert() .success() .stdout(predicate::str::contains(expect_contains_str)); } mod tests { use super::*; fn print_envs() { let current_bin = std::env::current_exe().unwrap(); println!("{current_bin:?}"); for (k, v) in std::env::vars() { if k == "UNLIKELYTOBESET" || k == "ALSOUNLIKELYTOBESET" { println!("{k}={v}"); } } } #[skip_if(missing_env(UNLIKELYTOBESET))] #[test] fn skipif_missing_env() { print_envs(); assert!(true); } #[skip_if(missing_env(UNLIKELYTOBESET, ALSOUNLIKELYTOBESET))] #[test] fn skipif_missing_one_or_more_specified_envs() { print_envs(); assert!(true); } #[skip_if(missing_env(UNLIKELYTOBESET))] #[test] fn skipif_missing_env_with_nondefault_test_signature() -> std::result::Result<(), ()> { print_envs(); assert!(true); Ok(()) } #[skip_if(whatever(CARGO_PKG_NAME))] #[test] fn dont_skip_if_var_is_set() { print_envs(); assert!(true); } }