Skip to content

Stricter test directives - #7165

Open
ytmimi wants to merge 5 commits into
rust-lang:mainfrom
ytmimi:stricter_test_directive
Open

ytmimi wants to merge 5 commits into
rust-lang:mainfrom
ytmimi:stricter_test_directive

Conversation

@ytmimi

@ytmimi ytmimi commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor
  • I did not use an LLM to create a change in this PR.
  • I used an LLM to create a change in this PR, and I have explained below how it was used.

Discussed in Zulip #t-rustfmt > Stricter test directives?.

I updated the entire test suite to use //@ rustfmt-* directives when setting configuration values for tests. There's also some validation to make sure that the directives aren't malformed.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Oct 7, 2026
@jieyouxu jieyouxu added the A-test-harness Area: rustfmt test harness label Oct 7, 2026
@jieyouxu

jieyouxu commented Oct 7, 2026

Copy link
Copy Markdown
Member

Nice! I'll review this gradually over this week

@ytmimi ytmimi changed the title Stricter test directive Stricter test directives Oct 7, 2026

@matthewhughes934 matthewhughes934 left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A quick first look over the changes.

I was wondering: should we add any handling for any in-flight changes/open PRs that will be using the old format? Currently any old-style // rustfmt-* line will just be silently ignored (but hopefully at least result in a test failure) which I think could cause confusion

View changes since this review

Comment thread src/test/mod.rs
let reader = BufReader::new(file);
let pattern = r"^\s*//\s*rustfmt-([^:]+):\s*(\S+)";
// Matches `//@ rustfmt-{name}: {value}`
let pattern = r"^\s*//@\s+rustfmt-(?P<name>[A-Za-z_]+)\s?:\s*(?P<value>\S+)$";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Is there anything us from being stricter here, there are currently no comments in files that need the extra optional spaces

Suggested change
let pattern = r"^\s*//@\s+rustfmt-(?P<name>[A-Za-z_]+)\s?:\s*(?P<value>\S+)$";
let pattern = r"^//@\s+rustfmt-(?P<name>[A-Za-z_]+):\s*(?P<value>\S+)$";

verified via

# no comments use a space between line start and `//`
git grep --perl-regexp '^\s+//@\s+rustfmt-(?P<name>[A-Za-z_]+)\s?:\s*(?P<value>\S+)$' -- tests/
#                          ^ changed '*' to '+'
# similarly, no comment uses a space between 'rustfmt-<name>' and then ':'
git grep --perl-regexp '^\s*//@\s+rustfmt-(?P<name>[A-Za-z_]+)\s+:\s*(?P<value>\S+)$' -- tests/
#                                                               ^ changed '?' to '+'

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

We could probably remove the second \s?, but I see some value in allowing arbitrary whitespace before the directive. Maybe we want to set the test directive in a nested comment instead of at the top of the file.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

We could probably remove the second \s?, but I see some value in allowing arbitrary whitespace before the directive. Maybe we want to set the test directive in a nested comment instead of at the top of the file.

My preference is to limit things to what we know/expect we will need, then change it in the future if needed. But I'm not opposed to leaving it as-is.

Comment thread src/test/mod.rs

config.override_value(key, val);

if !config.was_option_set(key) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

what motivated adding this check/how can we end up in this situation?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just an extra check to make sure that values are actually getting set as we'd expect. I don't see any harm in keeping this check, but let me know if you think otherwise.

Comment thread src/test/mod.rs Outdated
ytmimi added 5 commits October 7, 2026 15:23
Replace `// rustfmt-*` test config comments with `//@ rustfmt-*`.
…}: {value}`

Panics if a test directive is malformed.
test authors will now get feedback when test directive:
1. are malformed
2. use invalid config option names
3. use invalid config values
4. somehow don't set configs
Make using `// rustfmt-{name}: {value}` a hard error. Also informs users that
they should be using `//@ rustfmt-{name}: {value}` instead.
@ytmimi
ytmimi force-pushed the stricter_test_directive branch from 8417490 to 64dca2f Compare October 7, 2026 19:28
@ytmimi

ytmimi commented Oct 7, 2026 •

Copy link
Copy Markdown
Contributor Author

I was wondering: should we add any handling for any in-flight changes/open PRs that will be using the old format? Currently any old-style // rustfmt-* line will just be silently ignored (but hopefully at least result in a test failure) which I think could cause confusion

@matthewhughes934 Thank you for the suggestion. I added a new check that will lead to a hard error if you're using the old directive syntax.

Comment thread src/test/mod.rs

for file in &files {
let mut config = read_config(file);
let mut config = read_config(file).expect("config is valid");

@matthewhughes934 matthewhughes934 Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think this causes confusing error messages, e.g. using //@ rustfmt-foo: true, the error printed is:

config is valid: "tests/source/doc.rs set unrecognized config option \"foo\""

Reading that: is the error telling me the config is valid?. The docs recommend using should, and I think that would work:

Suggested change
let mut config = read_config(file).expect("config is valid");
let mut config = read_config(file).expect("config should be valid");

View changes since the review

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thoughts on updating all the expect calls to say something like "invalid config" or "expected valid config"?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thoughts on updating all the expect calls to say something like "invalid config" or "expected valid config"?

👍 sounds good to me

Comment thread tests/source/comment5.rs

@ytmimi ytmimi Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Had to change this test because it used //@

View changes since the review

Comment thread tests/target/comment5.rs

@ytmimi ytmimi Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Same as the source file. Had to change this test because it used //@

View changes since the review

@ytmimi ytmimi Oct 9, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Need to double check, but I changed this from just Bar -> struct Bar because I was seeing parse errors. I think my editor changed the line ending and that might have accidentally changed the test. Will double check this one. Same goes for the tests/source/preserves_carriage_return_for_windows.rs test and their target files.

View changes since the review

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-test-harness Area: rustfmt test harness S-waiting-on-review Status: awaiting review from the assignee but also interested parties.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants