From df413d1eceeae25204027b8d0b0c4a55f419cd78 Mon Sep 17 00:00:00 2001 From: Martin Fischer Date: Wed, 1 Feb 2023 05:58:52 +0100 Subject: [PATCH] refactor: Introduce test_resource_path helper --- src/packaging.rs | 44 +++++++------------------------ src/rules/flake8_no_pep420/mod.rs | 6 ++--- src/rules/isort/mod.rs | 37 +++++++++++++------------- src/settings/pyproject.rs | 11 +++----- src/test.rs | 4 +++ 5 files changed, 38 insertions(+), 64 deletions(-) diff --git a/src/packaging.rs b/src/packaging.rs index 9601ad8d30..11bc39214a 100644 --- a/src/packaging.rs +++ b/src/packaging.rs @@ -119,50 +119,26 @@ pub fn detect_package_roots<'a>( mod tests { use std::path::PathBuf; - use crate::packaging::detect_package_root; + use crate::{packaging::detect_package_root, test::test_resource_path}; #[test] fn package_detection() { assert_eq!( - detect_package_root( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/package/src/package") - .as_path(), - &[], - ), - Some( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/package/src/package") - .as_path() - ) + detect_package_root(&test_resource_path("package/src/package"), &[],), + Some(test_resource_path("package/src/package").as_path()) + ); + + assert_eq!( + detect_package_root(&test_resource_path("project/python_modules/core/core"), &[],), + Some(test_resource_path("project/python_modules/core/core").as_path()) ); assert_eq!( detect_package_root( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/project/python_modules/core/core") - .as_path(), + &test_resource_path("project/examples/docs/docs/concepts"), &[], ), - Some( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/project/python_modules/core/core") - .as_path() - ) - ); - - assert_eq!( - detect_package_root( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/project/examples/docs/docs/concepts") - .as_path(), - &[], - ), - Some( - PathBuf::from(env!("CARGO_MANIFEST_DIR")) - .join("resources/test/project/examples/docs/docs") - .as_path() - ) + Some(test_resource_path("project/examples/docs/docs").as_path()) ); assert_eq!( diff --git a/src/rules/flake8_no_pep420/mod.rs b/src/rules/flake8_no_pep420/mod.rs index a33d9d5bc2..bd1e37af98 100644 --- a/src/rules/flake8_no_pep420/mod.rs +++ b/src/rules/flake8_no_pep420/mod.rs @@ -11,7 +11,7 @@ mod tests { use crate::assert_yaml_snapshot; use crate::registry::Rule; use crate::settings::Settings; - use crate::test::test_path; + use crate::test::{test_path, test_resource_path}; #[test_case(Path::new("test_pass_init"), Path::new("example.py"); "INP001_0")] #[test_case(Path::new("test_fail_empty"), Path::new("example.py"); "INP001_1")] @@ -30,8 +30,8 @@ mod tests { let diagnostics = test_path( p.as_path(), &Settings { - namespace_packages: vec![PathBuf::from( - "./resources/test/fixtures/flake8_no_pep420/test_pass_namespace_package", + namespace_packages: vec![test_resource_path( + "fixtures/flake8_no_pep420/test_pass_namespace_package", )], ..Settings::for_rule(Rule::ImplicitNamespacePackage) }, diff --git a/src/rules/isort/mod.rs b/src/rules/isort/mod.rs index 5e757efcd3..538e26537f 100644 --- a/src/rules/isort/mod.rs +++ b/src/rules/isort/mod.rs @@ -237,7 +237,7 @@ mod tests { use crate::assert_yaml_snapshot; use crate::registry::Rule; use crate::settings::Settings; - use crate::test::test_path; + use crate::test::{test_path, test_resource_path}; #[test_case(Path::new("add_newline_before_comments.py"))] #[test_case(Path::new("combine_as_imports.py"))] @@ -288,7 +288,7 @@ mod tests { .join(path) .as_path(), &Settings { - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -306,8 +306,7 @@ mod tests { // .join(path) // .as_path(), // &Settings { - // src: - // vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + // src: vec![test_resource_path("fixtures/isort")], // ..Settings::for_rule(Rule::UnsortedImports) // }, // )?; @@ -327,7 +326,7 @@ mod tests { combine_as_imports: true, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -348,7 +347,7 @@ mod tests { combine_as_imports: true, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -368,7 +367,7 @@ mod tests { split_on_trailing_comma: false, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -391,7 +390,7 @@ mod tests { .collect::>(), ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -411,7 +410,7 @@ mod tests { order_by_type: false, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -441,7 +440,7 @@ mod tests { ]), ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -473,7 +472,7 @@ mod tests { ]), ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -503,7 +502,7 @@ mod tests { ]), ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -524,7 +523,7 @@ mod tests { force_sort_within_sections: true, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -543,7 +542,7 @@ mod tests { .join(path) .as_path(), &Settings { - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], isort: super::settings::Settings { required_imports: BTreeSet::from([ "from __future__ import annotations".to_string() @@ -567,7 +566,7 @@ mod tests { .join(path) .as_path(), &Settings { - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], isort: super::settings::Settings { required_imports: BTreeSet::from([ "from __future__ import annotations".to_string(), @@ -592,7 +591,7 @@ mod tests { .join(path) .as_path(), &Settings { - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], isort: super::settings::Settings { required_imports: BTreeSet::from(["from __future__ import annotations, \ generator_stop" @@ -616,7 +615,7 @@ mod tests { .join(path) .as_path(), &Settings { - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], isort: super::settings::Settings { required_imports: BTreeSet::from(["import os".to_string()]), ..super::settings::Settings::default() @@ -640,7 +639,7 @@ mod tests { relative_imports_order: RelativeImportsOrder::ClosestToFurthest, ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; @@ -666,7 +665,7 @@ mod tests { ]), ..super::settings::Settings::default() }, - src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()], + src: vec![test_resource_path("fixtures/isort")], ..Settings::for_rule(Rule::UnsortedImports) }, )?; diff --git a/src/settings/pyproject.rs b/src/settings/pyproject.rs index ab2e4c8526..04f276ca72 100644 --- a/src/settings/pyproject.rs +++ b/src/settings/pyproject.rs @@ -123,7 +123,6 @@ pub fn load_options>(path: P) -> Result { #[cfg(test)] mod tests { - use std::env::current_dir; use std::str::FromStr; use anyhow::Result; @@ -141,6 +140,7 @@ mod tests { find_settings_toml, parse_pyproject_toml, Options, Pyproject, Tools, }; use crate::settings::types::PatternPrefixPair; + use crate::test::test_resource_path; #[test] fn deserialize() -> Result<()> { @@ -270,13 +270,8 @@ other-attribute = 1 #[test] fn find_and_parse_pyproject_toml() -> Result<()> { - let cwd = current_dir()?; - let pyproject = - find_settings_toml(cwd.join("resources/test/fixtures/__init__.py"))?.unwrap(); - assert_eq!( - pyproject, - cwd.join("resources/test/fixtures/pyproject.toml") - ); + let pyproject = find_settings_toml(test_resource_path("fixtures/__init__.py"))?.unwrap(); + assert_eq!(pyproject, test_resource_path("fixtures/pyproject.toml")); let pyproject = parse_pyproject_toml(&pyproject)?; let config = pyproject.tool.unwrap().ruff.unwrap(); diff --git a/src/test.rs b/src/test.rs index bee3d8901e..e163694434 100644 --- a/src/test.rs +++ b/src/test.rs @@ -15,6 +15,10 @@ use crate::{ source_code::{Indexer, Locator, Stylist}, }; +pub fn test_resource_path(path: impl AsRef) -> std::path::PathBuf { + std::path::Path::new("./resources/test/").join(path) +} + /// A convenient wrapper around [`check_path`], that additionally /// asserts that autofixes converge after 10 iterations. pub fn test_path(path: &Path, settings: &Settings) -> Result> {