mirror of https://github.com/astral-sh/ruff
Add a test utility for running lint checks (#672)
This commit is contained in:
parent
3cc74c0564
commit
f6992cc98c
|
|
@ -6,32 +6,15 @@ mod tests {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rustpython_parser::lexer::LexResult;
|
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
use crate::autofix::fixer;
|
||||||
use crate::checks::{Check, CheckCode};
|
use crate::checks::CheckCode;
|
||||||
use crate::linter::tokenize;
|
use crate::linter::test_path;
|
||||||
use crate::{flake8_annotations, fs, linter, noqa, Settings, SourceCodeLocator};
|
use crate::{flake8_annotations, Settings};
|
||||||
|
|
||||||
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
|
|
||||||
let contents = fs::read_file(path)?;
|
|
||||||
let tokens: Vec<LexResult> = tokenize(&contents);
|
|
||||||
let locator = SourceCodeLocator::new(&contents);
|
|
||||||
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
|
|
||||||
linter::check_path(
|
|
||||||
path,
|
|
||||||
&contents,
|
|
||||||
tokens,
|
|
||||||
&locator,
|
|
||||||
&noqa_line_for,
|
|
||||||
settings,
|
|
||||||
autofix,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn defaults() -> Result<()> {
|
fn defaults() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_annotations/annotation_presence.py"),
|
Path::new("./resources/test/fixtures/flake8_annotations/annotation_presence.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
..Settings::for_rules(vec![
|
..Settings::for_rules(vec![
|
||||||
|
|
@ -57,7 +40,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn suppress_dummy_args() -> Result<()> {
|
fn suppress_dummy_args() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_annotations/suppress_dummy_args.py"),
|
Path::new("./resources/test/fixtures/flake8_annotations/suppress_dummy_args.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_annotations: flake8_annotations::settings::Settings {
|
flake8_annotations: flake8_annotations::settings::Settings {
|
||||||
|
|
@ -83,7 +66,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn mypy_init_return() -> Result<()> {
|
fn mypy_init_return() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_annotations/mypy_init_return.py"),
|
Path::new("./resources/test/fixtures/flake8_annotations/mypy_init_return.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_annotations: flake8_annotations::settings::Settings {
|
flake8_annotations: flake8_annotations::settings::Settings {
|
||||||
|
|
@ -109,7 +92,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn suppress_none_returning() -> Result<()> {
|
fn suppress_none_returning() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_annotations/suppress_none_returning.py"),
|
Path::new("./resources/test/fixtures/flake8_annotations/suppress_none_returning.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_annotations: flake8_annotations::settings::Settings {
|
flake8_annotations: flake8_annotations::settings::Settings {
|
||||||
|
|
@ -135,7 +118,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn allow_star_arg_any() -> Result<()> {
|
fn allow_star_arg_any() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_annotations/allow_star_arg_any.py"),
|
Path::new("./resources/test/fixtures/flake8_annotations/allow_star_arg_any.py"),
|
||||||
&Settings {
|
&Settings {
|
||||||
flake8_annotations: flake8_annotations::settings::Settings {
|
flake8_annotations: flake8_annotations::settings::Settings {
|
||||||
|
|
|
||||||
|
|
@ -6,30 +6,13 @@ mod tests {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rustpython_parser::lexer::LexResult;
|
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
use crate::autofix::fixer;
|
||||||
use crate::checks::{Check, CheckCode};
|
use crate::checks::CheckCode;
|
||||||
use crate::flake8_quotes::settings::Quote;
|
use crate::flake8_quotes::settings::Quote;
|
||||||
use crate::linter::tokenize;
|
use crate::linter::test_path;
|
||||||
use crate::{flake8_quotes, fs, linter, noqa, Settings, SourceCodeLocator};
|
use crate::{flake8_quotes, Settings};
|
||||||
|
|
||||||
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
|
|
||||||
let contents = fs::read_file(path)?;
|
|
||||||
let tokens: Vec<LexResult> = tokenize(&contents);
|
|
||||||
let locator = SourceCodeLocator::new(&contents);
|
|
||||||
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
|
|
||||||
linter::check_path(
|
|
||||||
path,
|
|
||||||
&contents,
|
|
||||||
tokens,
|
|
||||||
&locator,
|
|
||||||
&noqa_line_for,
|
|
||||||
settings,
|
|
||||||
autofix,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test_case(Path::new("doubles.py"))]
|
#[test_case(Path::new("doubles.py"))]
|
||||||
#[test_case(Path::new("doubles_escaped.py"))]
|
#[test_case(Path::new("doubles_escaped.py"))]
|
||||||
|
|
@ -38,7 +21,7 @@ mod tests {
|
||||||
#[test_case(Path::new("doubles_wrapped.py"))]
|
#[test_case(Path::new("doubles_wrapped.py"))]
|
||||||
fn doubles(path: &Path) -> Result<()> {
|
fn doubles(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("doubles_{}", path.to_string_lossy());
|
let snapshot = format!("doubles_{}", path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_quotes")
|
Path::new("./resources/test/fixtures/flake8_quotes")
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
|
|
@ -70,7 +53,7 @@ mod tests {
|
||||||
#[test_case(Path::new("singles_wrapped.py"))]
|
#[test_case(Path::new("singles_wrapped.py"))]
|
||||||
fn singles(path: &Path) -> Result<()> {
|
fn singles(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("singles_{}", path.to_string_lossy());
|
let snapshot = format!("singles_{}", path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_quotes")
|
Path::new("./resources/test/fixtures/flake8_quotes")
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
|
|
@ -107,7 +90,7 @@ mod tests {
|
||||||
#[test_case(Path::new("docstring_singles_function.py"))]
|
#[test_case(Path::new("docstring_singles_function.py"))]
|
||||||
fn double_docstring(path: &Path) -> Result<()> {
|
fn double_docstring(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("double_docstring_{}", path.to_string_lossy());
|
let snapshot = format!("double_docstring_{}", path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_quotes")
|
Path::new("./resources/test/fixtures/flake8_quotes")
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
|
|
@ -144,7 +127,7 @@ mod tests {
|
||||||
#[test_case(Path::new("docstring_singles_function.py"))]
|
#[test_case(Path::new("docstring_singles_function.py"))]
|
||||||
fn single_docstring(path: &Path) -> Result<()> {
|
fn single_docstring(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("single_docstring_{}", path.to_string_lossy());
|
let snapshot = format!("single_docstring_{}", path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/flake8_quotes")
|
Path::new("./resources/test/fixtures/flake8_quotes")
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
|
|
|
||||||
|
|
@ -201,29 +201,12 @@ mod tests {
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use rustpython_parser::lexer::LexResult;
|
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
use crate::autofix::fixer;
|
||||||
use crate::checks::{Check, CheckCode};
|
use crate::checks::CheckCode;
|
||||||
use crate::linter::tokenize;
|
use crate::linter::test_path;
|
||||||
use crate::{fs, linter, noqa, Settings, SourceCodeLocator};
|
use crate::Settings;
|
||||||
|
|
||||||
fn check_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
|
|
||||||
let contents = fs::read_file(path)?;
|
|
||||||
let tokens: Vec<LexResult> = tokenize(&contents);
|
|
||||||
let locator = SourceCodeLocator::new(&contents);
|
|
||||||
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
|
|
||||||
linter::check_path(
|
|
||||||
path,
|
|
||||||
&contents,
|
|
||||||
tokens,
|
|
||||||
&locator,
|
|
||||||
&noqa_line_for,
|
|
||||||
settings,
|
|
||||||
autofix,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test_case(Path::new("reorder_within_section.py"))]
|
#[test_case(Path::new("reorder_within_section.py"))]
|
||||||
#[test_case(Path::new("no_reorder_within_section.py"))]
|
#[test_case(Path::new("no_reorder_within_section.py"))]
|
||||||
|
|
@ -239,7 +222,7 @@ mod tests {
|
||||||
#[test_case(Path::new("trailing_suffix.py"))]
|
#[test_case(Path::new("trailing_suffix.py"))]
|
||||||
fn isort(path: &Path) -> Result<()> {
|
fn isort(path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("{}", path.to_string_lossy());
|
let snapshot = format!("{}", path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/isort")
|
Path::new("./resources/test/fixtures/isort")
|
||||||
.join(path)
|
.join(path)
|
||||||
.as_path(),
|
.as_path(),
|
||||||
|
|
|
||||||
|
|
@ -263,6 +263,23 @@ pub fn autoformat_path(path: &Path) -> Result<()> {
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
pub fn test_path(path: &Path, settings: &Settings, autofix: &fixer::Mode) -> Result<Vec<Check>> {
|
||||||
|
let contents = fs::read_file(path)?;
|
||||||
|
let tokens: Vec<LexResult> = tokenize(&contents);
|
||||||
|
let locator = SourceCodeLocator::new(&contents);
|
||||||
|
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
|
||||||
|
check_path(
|
||||||
|
path,
|
||||||
|
&contents,
|
||||||
|
tokens,
|
||||||
|
&locator,
|
||||||
|
&noqa_line_for,
|
||||||
|
settings,
|
||||||
|
autofix,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use std::convert::AsRef;
|
use std::convert::AsRef;
|
||||||
|
|
@ -270,34 +287,12 @@ mod tests {
|
||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use rustpython_parser::lexer::LexResult;
|
|
||||||
use test_case::test_case;
|
use test_case::test_case;
|
||||||
|
|
||||||
use crate::autofix::fixer;
|
use crate::autofix::fixer;
|
||||||
use crate::checks::{Check, CheckCode};
|
use crate::checks::CheckCode;
|
||||||
use crate::linter::tokenize;
|
use crate::linter::test_path;
|
||||||
use crate::source_code_locator::SourceCodeLocator;
|
use crate::settings;
|
||||||
use crate::{fs, linter, noqa, settings};
|
|
||||||
|
|
||||||
fn check_path(
|
|
||||||
path: &Path,
|
|
||||||
settings: &settings::Settings,
|
|
||||||
autofix: &fixer::Mode,
|
|
||||||
) -> Result<Vec<Check>> {
|
|
||||||
let contents = fs::read_file(path)?;
|
|
||||||
let tokens: Vec<LexResult> = tokenize(&contents);
|
|
||||||
let locator = SourceCodeLocator::new(&contents);
|
|
||||||
let noqa_line_for = noqa::extract_noqa_line_for(&tokens);
|
|
||||||
linter::check_path(
|
|
||||||
path,
|
|
||||||
&contents,
|
|
||||||
tokens,
|
|
||||||
&locator,
|
|
||||||
&noqa_line_for,
|
|
||||||
settings,
|
|
||||||
autofix,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test_case(CheckCode::A001, Path::new("A001.py"); "A001")]
|
#[test_case(CheckCode::A001, Path::new("A001.py"); "A001")]
|
||||||
#[test_case(CheckCode::A002, Path::new("A002.py"); "A002")]
|
#[test_case(CheckCode::A002, Path::new("A002.py"); "A002")]
|
||||||
|
|
@ -470,7 +465,7 @@ mod tests {
|
||||||
#[test_case(CheckCode::RUF003, Path::new("RUF003.py"); "RUF003")]
|
#[test_case(CheckCode::RUF003, Path::new("RUF003.py"); "RUF003")]
|
||||||
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
|
fn checks(check_code: CheckCode, path: &Path) -> Result<()> {
|
||||||
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
|
let snapshot = format!("{}_{}", check_code.as_ref(), path.to_string_lossy());
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures").join(path).as_path(),
|
Path::new("./resources/test/fixtures").join(path).as_path(),
|
||||||
&settings::Settings::for_rule(check_code.clone()),
|
&settings::Settings::for_rule(check_code.clone()),
|
||||||
&fixer::Mode::Generate,
|
&fixer::Mode::Generate,
|
||||||
|
|
@ -482,7 +477,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn f841_dummy_variable_rgx() -> Result<()> {
|
fn f841_dummy_variable_rgx() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/F841.py"),
|
Path::new("./resources/test/fixtures/F841.py"),
|
||||||
&settings::Settings {
|
&settings::Settings {
|
||||||
dummy_variable_rgx: Regex::new(r"^z$").unwrap(),
|
dummy_variable_rgx: Regex::new(r"^z$").unwrap(),
|
||||||
|
|
@ -497,7 +492,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn m001() -> Result<()> {
|
fn m001() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/M001.py"),
|
Path::new("./resources/test/fixtures/M001.py"),
|
||||||
&settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]),
|
&settings::Settings::for_rules(vec![CheckCode::M001, CheckCode::E501, CheckCode::F841]),
|
||||||
&fixer::Mode::Generate,
|
&fixer::Mode::Generate,
|
||||||
|
|
@ -509,7 +504,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn init() -> Result<()> {
|
fn init() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/__init__.py"),
|
Path::new("./resources/test/fixtures/__init__.py"),
|
||||||
&settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]),
|
&settings::Settings::for_rules(vec![CheckCode::F821, CheckCode::F822]),
|
||||||
&fixer::Mode::Generate,
|
&fixer::Mode::Generate,
|
||||||
|
|
@ -521,7 +516,7 @@ mod tests {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn future_annotations() -> Result<()> {
|
fn future_annotations() -> Result<()> {
|
||||||
let mut checks = check_path(
|
let mut checks = test_path(
|
||||||
Path::new("./resources/test/fixtures/future_annotations.py"),
|
Path::new("./resources/test/fixtures/future_annotations.py"),
|
||||||
&settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]),
|
&settings::Settings::for_rules(vec![CheckCode::F401, CheckCode::F821]),
|
||||||
&fixer::Mode::Generate,
|
&fixer::Mode::Generate,
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue