Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[red-knot] Support --exit-zero and --error-on-warning #15746

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions crates/red_knot/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,14 @@ pub(crate) struct CheckCommand {
#[clap(flatten)]
pub(crate) rules: RulesArg,

/// Use exit code 1 if there are any warning-level diagnostics.
#[arg(long, conflicts_with = "exit_zero")]
pub(crate) error_on_warning: bool,

/// Always use exit code 0, even when there are error-level diagnostics.
#[arg(long)]
pub(crate) exit_zero: bool,

/// Run in watch mode by re-running whenever files change.
#[arg(long, short = 'W')]
pub(crate) watch: bool,
Expand Down Expand Up @@ -97,6 +105,7 @@ impl CheckCommand {
..EnvironmentOptions::default()
}),
rules,
error_on_warning: Some(self.error_on_warning),
..Default::default()
}
}
Expand Down
41 changes: 34 additions & 7 deletions crates/red_knot/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use red_knot_project::watch;
use red_knot_project::watch::ProjectWatcher;
use red_knot_project::{ProjectDatabase, ProjectMetadata};
use red_knot_server::run_server;
use ruff_db::diagnostic::Diagnostic;
use ruff_db::diagnostic::{Diagnostic, Severity};
use ruff_db::system::{OsSystem, System, SystemPath, SystemPathBuf};
use salsa::plumbing::ZalsaDatabase;

Expand Down Expand Up @@ -84,6 +84,7 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {

let system = OsSystem::new(cwd);
let watch = args.watch;
let exit_zero = args.exit_zero;
let cli_options = args.into_options();
let mut workspace_metadata = ProjectMetadata::discover(system.current_directory(), &system)?;
workspace_metadata.apply_cli_options(cli_options.clone());
Expand Down Expand Up @@ -112,7 +113,11 @@ fn run_check(args: CheckCommand) -> anyhow::Result<ExitStatus> {

std::mem::forget(db);

Ok(exit_status)
if exit_zero {
Ok(ExitStatus::Success)
} else {
Ok(exit_status)
}
}

#[derive(Copy, Clone)]
Expand Down Expand Up @@ -213,7 +218,18 @@ impl MainLoop {
result,
revision: check_revision,
} => {
let has_diagnostics = !result.is_empty();
let (has_warnings, has_errors) = result.iter().fold(
(false, false),
|(has_warnings, has_errors), diagnostic| {
let severity = diagnostic.severity();

(
has_warnings || severity == Severity::Warning,
has_errors || severity >= Severity::Error,
)
},
);

if check_revision == revision {
#[allow(clippy::print_stdout)]
for diagnostic in result {
Expand All @@ -226,10 +242,21 @@ impl MainLoop {
}

if self.watcher.is_none() {
return if has_diagnostics {
ExitStatus::Failure
} else {
ExitStatus::Success
let error_on_warning =
self.cli_options.error_on_warning.unwrap_or_default();

return match (has_warnings, has_errors) {
(false, false) => ExitStatus::Success,

(true, false) => {
if error_on_warning {
ExitStatus::Failure
} else {
ExitStatus::Success
}
}

(_, true) => ExitStatus::Failure,
};
}

Expand Down
163 changes: 154 additions & 9 deletions crates/red_knot/tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,8 @@ fn configuration_rule_severity() -> anyhow::Result<()> {
)?;

assert_cmd_snapshot!(case.command(), @r"
success: false
exit_code: 1
success: true
exit_code: 0
----- stdout -----
warning[lint:division-by-zero] <temp_dir>/test.py:2:5 Cannot divide object of type `Literal[4]` by zero

Expand Down Expand Up @@ -251,8 +251,8 @@ fn cli_rule_severity() -> anyhow::Result<()> {
.arg("--warn")
.arg("unresolved-import"),
@r"
success: false
exit_code: 1
success: true
exit_code: 0
----- stdout -----
warning[lint:unresolved-import] <temp_dir>/test.py:2:8 Cannot resolve import `does_not_exit`
warning[lint:division-by-zero] <temp_dir>/test.py:4:5 Cannot divide object of type `Literal[4]` by zero
Expand Down Expand Up @@ -303,8 +303,8 @@ fn cli_rule_severity_precedence() -> anyhow::Result<()> {
.arg("--ignore")
.arg("possibly-unresolved-reference"),
@r"
success: false
exit_code: 1
success: true
exit_code: 0
----- stdout -----
warning[lint:division-by-zero] <temp_dir>/test.py:2:5 Cannot divide object of type `Literal[4]` by zero

Expand All @@ -330,8 +330,8 @@ fn configuration_unknown_rules() -> anyhow::Result<()> {
])?;

assert_cmd_snapshot!(case.command(), @r"
success: false
exit_code: 1
success: true
exit_code: 0
----- stdout -----
warning[unknown-rule] <temp_dir>/pyproject.toml:3:1 Unknown lint rule `division-by-zer`

Expand All @@ -347,10 +347,155 @@ fn cli_unknown_rules() -> anyhow::Result<()> {
let case = TestCase::with_file("test.py", "print(10)")?;

assert_cmd_snapshot!(case.command().arg("--ignore").arg("division-by-zer"), @r"
success: true
exit_code: 0
----- stdout -----
warning[unknown-rule] Unknown lint rule `division-by-zer`

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_only_warnings() -> anyhow::Result<()> {
let case = TestCase::with_file("test.py", r"print(x) # [unresolved-reference]")?;

assert_cmd_snapshot!(case.command(), @r"
success: true
exit_code: 0
----- stdout -----
warning[lint:unresolved-reference] <temp_dir>/test.py:1:7 Name `x` used when not defined

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_only_info() -> anyhow::Result<()> {
let case = TestCase::with_file(
"test.py",
r#"
from typing_extensions import reveal_type
reveal_type(1)
"#,
)?;

assert_cmd_snapshot!(case.command(), @r"
success: true
exit_code: 0
----- stdout -----
info[revealed-type] <temp_dir>/test.py:3:1 Revealed type is `Literal[1]`

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_only_info_and_error_on_warning_is_true() -> anyhow::Result<()> {
let case = TestCase::with_file(
"test.py",
r#"
from typing_extensions import reveal_type
reveal_type(1)
"#,
)?;

assert_cmd_snapshot!(case.command().arg("--error-on-warning"), @r"
success: true
exit_code: 0
----- stdout -----
info[revealed-type] <temp_dir>/test.py:3:1 Revealed type is `Literal[1]`

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_no_errors_but_error_on_warning_is_true() -> anyhow::Result<()> {
let case = TestCase::with_file("test.py", r"print(x) # [unresolved-reference]")?;

assert_cmd_snapshot!(case.command().arg("--error-on-warning"), @r"
success: false
exit_code: 1
----- stdout -----
warning[unknown-rule] Unknown lint rule `division-by-zer`
warning[lint:unresolved-reference] <temp_dir>/test.py:1:7 Name `x` used when not defined

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_both_warnings_and_errors() -> anyhow::Result<()> {
let case = TestCase::with_file(
"test.py",
r#"
print(x) # [unresolved-reference]
print(4[1]) # [non-subscriptable]
"#,
)?;

assert_cmd_snapshot!(case.command(), @r"
success: false
exit_code: 1
----- stdout -----
warning[lint:unresolved-reference] <temp_dir>/test.py:2:7 Name `x` used when not defined
error[lint:non-subscriptable] <temp_dir>/test.py:3:7 Cannot subscript object of type `Literal[4]` with no `__getitem__` method

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_both_warnings_and_errors_and_error_on_warning_is_true() -> anyhow::Result<()> {
let case = TestCase::with_file(
"test.py",
r#"
print(x) # [unresolved-reference]
print(4[1]) # [non-subscriptable]
"#,
)?;

assert_cmd_snapshot!(case.command().arg("--error-on-warning"), @r"
success: false
exit_code: 1
----- stdout -----
warning[lint:unresolved-reference] <temp_dir>/test.py:2:7 Name `x` used when not defined
error[lint:non-subscriptable] <temp_dir>/test.py:3:7 Cannot subscript object of type `Literal[4]` with no `__getitem__` method

----- stderr -----
");

Ok(())
}

#[test]
fn exit_code_exit_zero_is_true() -> anyhow::Result<()> {
let case = TestCase::with_file(
"test.py",
r#"
print(x) # [unresolved-reference]
print(4[1]) # [non-subscriptable]
"#,
)?;

assert_cmd_snapshot!(case.command().arg("--exit-zero"), @r"
success: true
exit_code: 0
----- stdout -----
warning[lint:unresolved-reference] <temp_dir>/test.py:2:7 Name `x` used when not defined
error[lint:non-subscriptable] <temp_dir>/test.py:3:7 Cannot subscript object of type `Literal[4]` with no `__getitem__` method

----- stderr -----
");
Expand Down
3 changes: 3 additions & 0 deletions crates/red_knot_project/src/metadata/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ pub struct Options {

#[serde(skip_serializing_if = "Option::is_none")]
pub rules: Option<Rules>,

#[serde(skip_serializing_if = "Option::is_none")]
pub error_on_warning: Option<bool>,
}

impl Options {
Expand Down
Loading