mirror of https://github.com/astral-sh/ruff
refactor: Introduce test_resource_path helper
This commit is contained in:
parent
cfd0693ae5
commit
df413d1ece
|
|
@ -119,50 +119,26 @@ pub fn detect_package_roots<'a>(
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::path::PathBuf;
|
use std::path::PathBuf;
|
||||||
|
|
||||||
use crate::packaging::detect_package_root;
|
use crate::{packaging::detect_package_root, test::test_resource_path};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn package_detection() {
|
fn package_detection() {
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
detect_package_root(
|
detect_package_root(&test_resource_path("package/src/package"), &[],),
|
||||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
Some(test_resource_path("package/src/package").as_path())
|
||||||
.join("resources/test/package/src/package")
|
);
|
||||||
.as_path(),
|
|
||||||
&[],
|
assert_eq!(
|
||||||
),
|
detect_package_root(&test_resource_path("project/python_modules/core/core"), &[],),
|
||||||
Some(
|
Some(test_resource_path("project/python_modules/core/core").as_path())
|
||||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
|
||||||
.join("resources/test/package/src/package")
|
|
||||||
.as_path()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
detect_package_root(
|
detect_package_root(
|
||||||
PathBuf::from(env!("CARGO_MANIFEST_DIR"))
|
&test_resource_path("project/examples/docs/docs/concepts"),
|
||||||
.join("resources/test/project/python_modules/core/core")
|
|
||||||
.as_path(),
|
|
||||||
&[],
|
&[],
|
||||||
),
|
),
|
||||||
Some(
|
Some(test_resource_path("project/examples/docs/docs").as_path())
|
||||||
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()
|
|
||||||
)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,7 @@ mod tests {
|
||||||
use crate::assert_yaml_snapshot;
|
use crate::assert_yaml_snapshot;
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::settings::Settings;
|
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_pass_init"), Path::new("example.py"); "INP001_0")]
|
||||||
#[test_case(Path::new("test_fail_empty"), Path::new("example.py"); "INP001_1")]
|
#[test_case(Path::new("test_fail_empty"), Path::new("example.py"); "INP001_1")]
|
||||||
|
|
@ -30,8 +30,8 @@ mod tests {
|
||||||
let diagnostics = test_path(
|
let diagnostics = test_path(
|
||||||
p.as_path(),
|
p.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
namespace_packages: vec![PathBuf::from(
|
namespace_packages: vec![test_resource_path(
|
||||||
"./resources/test/fixtures/flake8_no_pep420/test_pass_namespace_package",
|
"fixtures/flake8_no_pep420/test_pass_namespace_package",
|
||||||
)],
|
)],
|
||||||
..Settings::for_rule(Rule::ImplicitNamespacePackage)
|
..Settings::for_rule(Rule::ImplicitNamespacePackage)
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -237,7 +237,7 @@ mod tests {
|
||||||
use crate::assert_yaml_snapshot;
|
use crate::assert_yaml_snapshot;
|
||||||
use crate::registry::Rule;
|
use crate::registry::Rule;
|
||||||
use crate::settings::Settings;
|
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("add_newline_before_comments.py"))]
|
||||||
#[test_case(Path::new("combine_as_imports.py"))]
|
#[test_case(Path::new("combine_as_imports.py"))]
|
||||||
|
|
@ -288,7 +288,7 @@ mod tests {
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
src: vec![test_resource_path("fixtures/isort")],
|
||||||
..Settings::for_rule(Rule::UnsortedImports)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -306,8 +306,7 @@ mod tests {
|
||||||
// .join(path)
|
// .join(path)
|
||||||
// .as_path(),
|
// .as_path(),
|
||||||
// &Settings {
|
// &Settings {
|
||||||
// src:
|
// src: vec![test_resource_path("fixtures/isort")],
|
||||||
// vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
|
||||||
// ..Settings::for_rule(Rule::UnsortedImports)
|
// ..Settings::for_rule(Rule::UnsortedImports)
|
||||||
// },
|
// },
|
||||||
// )?;
|
// )?;
|
||||||
|
|
@ -327,7 +326,7 @@ mod tests {
|
||||||
combine_as_imports: true,
|
combine_as_imports: true,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -348,7 +347,7 @@ mod tests {
|
||||||
combine_as_imports: true,
|
combine_as_imports: true,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -368,7 +367,7 @@ mod tests {
|
||||||
split_on_trailing_comma: false,
|
split_on_trailing_comma: false,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -391,7 +390,7 @@ mod tests {
|
||||||
.collect::<BTreeSet<_>>(),
|
.collect::<BTreeSet<_>>(),
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -411,7 +410,7 @@ mod tests {
|
||||||
order_by_type: false,
|
order_by_type: false,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -441,7 +440,7 @@ mod tests {
|
||||||
]),
|
]),
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -473,7 +472,7 @@ mod tests {
|
||||||
]),
|
]),
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -503,7 +502,7 @@ mod tests {
|
||||||
]),
|
]),
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -524,7 +523,7 @@ mod tests {
|
||||||
force_sort_within_sections: true,
|
force_sort_within_sections: true,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -543,7 +542,7 @@ mod tests {
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
src: vec![test_resource_path("fixtures/isort")],
|
||||||
isort: super::settings::Settings {
|
isort: super::settings::Settings {
|
||||||
required_imports: BTreeSet::from([
|
required_imports: BTreeSet::from([
|
||||||
"from __future__ import annotations".to_string()
|
"from __future__ import annotations".to_string()
|
||||||
|
|
@ -567,7 +566,7 @@ mod tests {
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
src: vec![test_resource_path("fixtures/isort")],
|
||||||
isort: super::settings::Settings {
|
isort: super::settings::Settings {
|
||||||
required_imports: BTreeSet::from([
|
required_imports: BTreeSet::from([
|
||||||
"from __future__ import annotations".to_string(),
|
"from __future__ import annotations".to_string(),
|
||||||
|
|
@ -592,7 +591,7 @@ mod tests {
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
src: vec![test_resource_path("fixtures/isort")],
|
||||||
isort: super::settings::Settings {
|
isort: super::settings::Settings {
|
||||||
required_imports: BTreeSet::from(["from __future__ import annotations, \
|
required_imports: BTreeSet::from(["from __future__ import annotations, \
|
||||||
generator_stop"
|
generator_stop"
|
||||||
|
|
@ -616,7 +615,7 @@ mod tests {
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
&Settings {
|
&Settings {
|
||||||
src: vec![Path::new("resources/test/fixtures/isort").to_path_buf()],
|
src: vec![test_resource_path("fixtures/isort")],
|
||||||
isort: super::settings::Settings {
|
isort: super::settings::Settings {
|
||||||
required_imports: BTreeSet::from(["import os".to_string()]),
|
required_imports: BTreeSet::from(["import os".to_string()]),
|
||||||
..super::settings::Settings::default()
|
..super::settings::Settings::default()
|
||||||
|
|
@ -640,7 +639,7 @@ mod tests {
|
||||||
relative_imports_order: RelativeImportsOrder::ClosestToFurthest,
|
relative_imports_order: RelativeImportsOrder::ClosestToFurthest,
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
@ -666,7 +665,7 @@ mod tests {
|
||||||
]),
|
]),
|
||||||
..super::settings::Settings::default()
|
..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)
|
..Settings::for_rule(Rule::UnsortedImports)
|
||||||
},
|
},
|
||||||
)?;
|
)?;
|
||||||
|
|
|
||||||
|
|
@ -123,7 +123,6 @@ pub fn load_options<P: AsRef<Path>>(path: P) -> Result<Options> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::env::current_dir;
|
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
@ -141,6 +140,7 @@ mod tests {
|
||||||
find_settings_toml, parse_pyproject_toml, Options, Pyproject, Tools,
|
find_settings_toml, parse_pyproject_toml, Options, Pyproject, Tools,
|
||||||
};
|
};
|
||||||
use crate::settings::types::PatternPrefixPair;
|
use crate::settings::types::PatternPrefixPair;
|
||||||
|
use crate::test::test_resource_path;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn deserialize() -> Result<()> {
|
fn deserialize() -> Result<()> {
|
||||||
|
|
@ -270,13 +270,8 @@ other-attribute = 1
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn find_and_parse_pyproject_toml() -> Result<()> {
|
fn find_and_parse_pyproject_toml() -> Result<()> {
|
||||||
let cwd = current_dir()?;
|
let pyproject = find_settings_toml(test_resource_path("fixtures/__init__.py"))?.unwrap();
|
||||||
let pyproject =
|
assert_eq!(pyproject, test_resource_path("fixtures/pyproject.toml"));
|
||||||
find_settings_toml(cwd.join("resources/test/fixtures/__init__.py"))?.unwrap();
|
|
||||||
assert_eq!(
|
|
||||||
pyproject,
|
|
||||||
cwd.join("resources/test/fixtures/pyproject.toml")
|
|
||||||
);
|
|
||||||
|
|
||||||
let pyproject = parse_pyproject_toml(&pyproject)?;
|
let pyproject = parse_pyproject_toml(&pyproject)?;
|
||||||
let config = pyproject.tool.unwrap().ruff.unwrap();
|
let config = pyproject.tool.unwrap().ruff.unwrap();
|
||||||
|
|
|
||||||
|
|
@ -15,6 +15,10 @@ use crate::{
|
||||||
source_code::{Indexer, Locator, Stylist},
|
source_code::{Indexer, Locator, Stylist},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn test_resource_path(path: impl AsRef<std::path::Path>) -> std::path::PathBuf {
|
||||||
|
std::path::Path::new("./resources/test/").join(path)
|
||||||
|
}
|
||||||
|
|
||||||
/// A convenient wrapper around [`check_path`], that additionally
|
/// A convenient wrapper around [`check_path`], that additionally
|
||||||
/// asserts that autofixes converge after 10 iterations.
|
/// asserts that autofixes converge after 10 iterations.
|
||||||
pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
|
pub fn test_path(path: &Path, settings: &Settings) -> Result<Vec<Diagnostic>> {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue