From c6f2dfd7274c1b42c25d55a9f589e565ba53eaf1 Mon Sep 17 00:00:00 2001 From: konsti Date: Thu, 2 Nov 2023 16:42:59 +0100 Subject: [PATCH] Use shared insta filters (#270) Internal refactoring for consistency between tests --- crates/puffin-cli/tests/add.rs | 4 +- crates/puffin-cli/tests/common/mod.rs | 8 ++++ crates/puffin-cli/tests/pip_compile.rs | 52 +++++------------------- crates/puffin-cli/tests/pip_sync.rs | 40 ++++++------------ crates/puffin-cli/tests/pip_uninstall.rs | 4 +- crates/puffin-cli/tests/remove.rs | 4 +- crates/puffin-cli/tests/venv.rs | 4 +- 7 files changed, 43 insertions(+), 73 deletions(-) create mode 100644 crates/puffin-cli/tests/common/mod.rs diff --git a/crates/puffin-cli/tests/add.rs b/crates/puffin-cli/tests/add.rs index 206b7afdd..8560a19d6 100644 --- a/crates/puffin-cli/tests/add.rs +++ b/crates/puffin-cli/tests/add.rs @@ -4,7 +4,9 @@ use anyhow::Result; use assert_fs::prelude::*; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::BIN_NAME; + +mod common; #[test] fn missing_pyproject_toml() -> Result<()> { diff --git a/crates/puffin-cli/tests/common/mod.rs b/crates/puffin-cli/tests/common/mod.rs new file mode 100644 index 000000000..691881e4d --- /dev/null +++ b/crates/puffin-cli/tests/common/mod.rs @@ -0,0 +1,8 @@ +pub(crate) const BIN_NAME: &str = "puffin"; +// Not all tests use them and cargo warns otherwise +#[allow(dead_code)] +pub(crate) const INSTA_FILTERS: &[(&str, &str)] = &[ + (r"(\d+\.)?\d+(ms|s)", "[TIME]"), + (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), + (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), +]; diff --git a/crates/puffin-cli/tests/pip_compile.rs b/crates/puffin-cli/tests/pip_compile.rs index 85e10bfaf..b337dd122 100644 --- a/crates/puffin-cli/tests/pip_compile.rs +++ b/crates/puffin-cli/tests/pip_compile.rs @@ -8,7 +8,9 @@ use assert_fs::prelude::*; use insta_cmd::_macro_support::insta; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::{BIN_NAME, INSTA_FILTERS}; + +mod common; #[test] fn missing_requirements_in() -> Result<()> { @@ -69,11 +71,7 @@ fn compile_requirements_in() -> Result<()> { requirements_in.write_str("django==5.0b1")?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -119,11 +117,7 @@ dependencies = [ )?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -163,11 +157,7 @@ fn compile_constraints_txt() -> Result<()> { constraints_txt.write_str("sqlparse<0.4.4")?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -210,11 +200,7 @@ fn compile_constraints_inline() -> Result<()> { constraints_txt.write_str("sqlparse<0.4.4")?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -311,11 +297,7 @@ optional-dependencies.foo = [ )?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -364,11 +346,7 @@ optional-dependencies."FrIeNdLy-._.-bArD" = [ )?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -417,11 +395,7 @@ optional-dependencies.foo = [ )?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") @@ -470,11 +444,7 @@ optional-dependencies.foo = [ )?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - (r"# .* pip-compile", "# [BIN_PATH] pip-compile"), - (r"--cache-dir .*", "--cache-dir [CACHE_DIR]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-compile") diff --git a/crates/puffin-cli/tests/pip_sync.rs b/crates/puffin-cli/tests/pip_sync.rs index 418aa6ba2..ae93aa557 100644 --- a/crates/puffin-cli/tests/pip_sync.rs +++ b/crates/puffin-cli/tests/pip_sync.rs @@ -8,7 +8,9 @@ use assert_fs::prelude::*; use insta_cmd::_macro_support::insta; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::{BIN_NAME, INSTA_FILTERS}; + +mod common; #[test] fn missing_requirements_txt() -> Result<()> { @@ -70,9 +72,7 @@ fn install() -> Result<()> { requirements_txt.write_str("MarkupSafe==2.1.3")?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -115,9 +115,7 @@ fn install_copy() -> Result<()> { requirements_txt.write_str("MarkupSafe==2.1.3")?; insta::with_settings!({ - filters => vec![ - (r"(\d|\.)+(ms|s)", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -162,9 +160,7 @@ fn install_hardlink() -> Result<()> { requirements_txt.write_str("MarkupSafe==2.1.3")?; insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -209,9 +205,7 @@ fn install_many() -> Result<()> { requirements_txt.write_str("MarkupSafe==2.1.3\ntomli==2.0.1")?; insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -264,9 +258,7 @@ fn noop() -> Result<()> { .success(); insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -331,9 +323,7 @@ fn link() -> Result<()> { venv.assert(predicates::path::is_dir()); insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -391,9 +381,7 @@ fn add_remove() -> Result<()> { requirements_txt.write_str("tomli==2.0.1")?; insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -458,9 +446,7 @@ fn install_sequential() -> Result<()> { requirements_txt.write_str("MarkupSafe==2.1.3\ntomli==2.0.1")?; insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") @@ -518,9 +504,7 @@ fn upgrade() -> Result<()> { requirements_txt.write_str("tomli==2.0.1")?; insta::with_settings!({ - filters => vec![ - (r"\d+ms|\d+\.\d+s", "[TIME]"), - ] + filters => INSTA_FILTERS.to_vec() }, { assert_cmd_snapshot!(Command::new(get_cargo_bin(BIN_NAME)) .arg("pip-sync") diff --git a/crates/puffin-cli/tests/pip_uninstall.rs b/crates/puffin-cli/tests/pip_uninstall.rs index 5123e2aff..81d4dbef5 100644 --- a/crates/puffin-cli/tests/pip_uninstall.rs +++ b/crates/puffin-cli/tests/pip_uninstall.rs @@ -4,7 +4,9 @@ use anyhow::Result; use assert_fs::prelude::*; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::BIN_NAME; + +mod common; #[test] fn no_arguments() -> Result<()> { diff --git a/crates/puffin-cli/tests/remove.rs b/crates/puffin-cli/tests/remove.rs index 05b3480e3..021b441d6 100644 --- a/crates/puffin-cli/tests/remove.rs +++ b/crates/puffin-cli/tests/remove.rs @@ -4,7 +4,9 @@ use anyhow::Result; use assert_fs::prelude::*; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::BIN_NAME; + +mod common; #[test] fn missing_pyproject_toml() -> Result<()> { diff --git a/crates/puffin-cli/tests/venv.rs b/crates/puffin-cli/tests/venv.rs index f8ff31a06..ecc3b14d9 100644 --- a/crates/puffin-cli/tests/venv.rs +++ b/crates/puffin-cli/tests/venv.rs @@ -7,7 +7,9 @@ use assert_fs::prelude::*; use insta_cmd::_macro_support::insta; use insta_cmd::{assert_cmd_snapshot, get_cargo_bin}; -const BIN_NAME: &str = "puffin"; +use common::BIN_NAME; + +mod common; #[test] fn create_venv() -> Result<()> {