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

New inline_test_mod lint to encouage independent test.rs files for rebuild performance #13589

Open
epage opened this issue Oct 23, 2024 · 2 comments
Labels
A-lint Area: New lints

Comments

@epage
Copy link

epage commented Oct 23, 2024

What it does

Check for mod test {}s and instead encourage those being separate files.

Checking for mod test {} was to align with tests_outside_test_module. I could imagine there being other cases to key off of.

Advantage

Rebuild performance. If someone changes a "unit test", then only the test lib rebuilds and not the lib.

Drawbacks

For foo.rs files in projects that use foo/mod.rs, adding a separate test mod would require creating a directory when it wasn't needed otherwise. Except for when visibility is a concern, a parallel test_foo.rs file could be used, e.g. snapbox does this

Overall, this would increase the friction for writing tests.

Example

#[cfg(test)]
mod test {
}

Could be written as:

#[cfg(test)]
mod test;
@epage epage added the A-lint Area: New lints label Oct 23, 2024
@WiSaGaN
Copy link

WiSaGaN commented Dec 14, 2024

I am a little confused by the drawback. I assume the drawback is if you are having src/foo.rs instead of src/foo/mod.rs, you would need to change to the latter to have a separate src/foo/test.rs to conform the separate test file pattern. Also, how does test_foo.rs work exactly?

@epage
Copy link
Author

epage commented Dec 16, 2024

I am a little confused by the drawback. I assume the drawback is if you are having src/foo.rs instead of src/foo/mod.rs, you would need to change to the latter to have a separate src/foo/test.rs to conform the separate test file pattern.

Say you have

// lib.rs
#![deny(clippy::self_named_module_files)]
mod foo;

// foo.rs
#[test]
fn some_test() {}

to resolve this clippy lint, you would need to switch to

// lib.rs
#![deny(clippy::self_named_module_files)]
mod foo;

// foo/mod.rs
#[cfg(test)]
mod test;

// foo/test.rs
#[test]
fn some_test() {}

Compare that to

// lib.rs
#![deny(clippy::mod_module_files)]
mod foo;

// foo.rs
#[test]
fn some_test() {}

to resolve this clippy lint, you would need to switch to

// lib.rs
#![deny(clippy::self_named_module_files)]
mod foo;

// foo.rs
#[cfg(test)]
mod test;

// foo/test.rs
#[test]
fn some_test() {}

In either case, using this lint requires a lot more directories than before. When using mod.rs, the migration also requires moving your source.

Also, how does test_foo.rs work exactly?

You could take

// lib.rs
#![deny(clippy::self_named_module_files)]
mod foo;

// foo.rs
#[test]
fn some_test() {}

and switch it to

// lib.rs
#![deny(clippy::self_named_module_files)]
mod foo;
#[cfg(test)]
mod test_foo;

// foo.rs

// test_foo.rs
#[test]
fn some_test() {}

Doing so requires everything you want to test in foo.rs to be pub(super) at least.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lint Area: New lints
Projects
None yet
Development

No branches or pull requests

2 participants