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

Add internal hidden rules for testing #9747

Merged
merged 17 commits into from
Feb 1, 2024
Merged

Add internal hidden rules for testing #9747

merged 17 commits into from
Feb 1, 2024

Conversation

zanieb
Copy link
Member

@zanieb zanieb commented Jan 31, 2024

Updated implementation of #7369 which was left out in the cold.

This was motivated again following changes in #9691 and #9689 where we could not test the changes without actually deprecating or removing rules.


Follow-up to discussion in #7210

Moves integration tests from using rules that are transitively in nursery / preview groups to dedicated test rules that only exist during development. These rules always raise violations (they do not require specific file behavior). The rules are not available in production or in the documentation.

Uses features instead of cfg(test) for cross-crate support per rust-lang/cargo#8379

@zanieb zanieb force-pushed the zb/test-rules-new branch 3 times, most recently from 03363f1 to 306a263 Compare January 31, 2024 19:21
@zanieb zanieb force-pushed the zb/test-rules-new branch 2 times, most recently from 142f261 to 2367e7a Compare January 31, 2024 19:33
Copy link
Member

@BurntSushi BurntSushi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I think I buy it!

crates/ruff_linter/src/linter.rs Outdated Show resolved Hide resolved

This comment was marked as outdated.

@zanieb
Copy link
Member Author

zanieb commented Jan 31, 2024

The ecosystem checks won't succeed until this is main because RUF9XX does not exist on the baseline binary.

@@ -54,6 +54,8 @@ walkdir = { workspace = true }
wild = { workspace = true }

[dev-dependencies]
# Enable test rules during development
ruff_linter = { path = "../ruff_linter", features = ["clap", "test-rules"] }
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@BurntSushi - Can you double-confirm for me that these won't enable in non-cargo test builds?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I do cargo build --release --all-features they are present

❯ ./target/release/ruff check example.py --select RUF
example.py:1:1: RUF900 Hey this is a stable test rule.
example.py:1:1: RUF901 [*] Hey this is a stable test rule with a safe fix.
example.py:1:1: RUF902 Hey this is a stable test rule with an unsafe fix.
example.py:1:1: RUF903 Hey this is a stable test rule with a display only fix.
Found 4 errors.
[*] 1 fixable with the `--fix` option (1 hidden fix can be enabled with the `--unsafe-fixes` option).

but they are not present without --all-features in the release or debug targets.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idk what features we include in releases, it's buried in maturin?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't specify anything, so we get the default features.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Which should omit these.)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh great so I think we're good then

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah features enabled in dev-dependencies should definitely not cross over into standard builds. But yeah as Zanie mentioned, if you have anything that's using --all-features, then they will get enabled in that case.

@@ -289,6 +289,8 @@ mod schema {
(!prefix.is_empty()).then(|| prefix.to_string())
})),
)
// Strip the test rules from the schema
.filter(|code| !code.starts_with("RUF9"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wish there were something better here (e.g., what if we could use the rule source, and have a special lint_source() called test), but might be difficult.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could add something to the macro but it seems like more trouble than it's worth at this point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just put them all in a separate lint group e.g. RFT for "Ruff test"? Seems safer and easier to filter in general?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might end up being more work to implement overall though?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm actually wondering how these even up getting included in the schema, do we run with --all-features or something?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway, I'm cool with whatever here.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh this raises a preview warning

❯ ./target/debug/ruff check example.py --select RUF9
warning: Selection `RUF9` has no effect because the `--preview` flag was not included.

but doesn't do anything else.. RUF8 errors

❯ ./target/debug/ruff check example.py --select RUF8
error: invalid value 'RUF8' for '--select <RULE_CODE>'

I guess I'll fix that next..

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah this is actually confusing... why are they included in the schema in the first place?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They shouldn't even exist when we run cargo dev generate-all, right?

Copy link
Member Author

@zanieb zanieb Feb 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay this was a bug in the proc macro 🎉
8247b85 fixes it (but has a mediocre implementation I'm fixing up)

@@ -206,6 +206,9 @@ def to_ruff_args(self) -> list[str]:
"check",
"--no-cache",
"--exit-zero",
# Ignore internal test rules
"--ignore",
"RUF9",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why / how do these get enabled here? Why is this necessary?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Like how does the feature end up being enabled?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe we get our ecosystem binaries from the test builds, which have the development features enabled? I think they have to be turned on in the ruff binary because we use that for integration tests.

It'd be nice if they were not present here, but do we want to have a separate build for the ecosystem checks?

@zanieb
Copy link
Member Author

zanieb commented Feb 1, 2024

The schema generation tests use --all-features even though the real schema generation does not 😮‍💨

@charliermarsh
Copy link
Member

Noooooo

@zanieb zanieb merged commit f18e7d4 into main Feb 1, 2024
17 checks passed
@zanieb zanieb deleted the zb/test-rules-new branch February 1, 2024 14:44
zanieb added a commit that referenced this pull request Feb 1, 2024
zanieb added a commit that referenced this pull request Feb 1, 2024
@@ -146,7 +149,7 @@ jobs:
- name: "Run tests"
shell: bash
# We can't reject unreferenced snapshots on windows because flake8_executable can't run on windows
run: cargo insta test --all --all-features
run: cargo insta test --all --exclude ruff_dev --all-features
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add another run command that runs the test of ruff_dev but with the default features only? We otherwise lose the schema test.

Although I'm seeing test failures locally even when not using --all-features, making that test rather annoying.

A workaround could be to export a are_testing_rules_enabled and conditionally implement it depending on the presence of the feature flag and not run the schema test when it's enabled. But that still means that the test won't fail anymore locally and still requires an explicit cargo test ruff_dev run :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants