Add msrv test template for `cargo dev new_lint --msrv`

This commit is contained in:
Alex Macleod 2023-09-23 18:11:56 +00:00
parent d732cce0d3
commit 39f7f695da
1 changed files with 51 additions and 12 deletions

View File

@ -58,7 +58,7 @@ pub fn create(
};
create_lint(&lint, msrv).context("Unable to create lint implementation")?;
create_test(&lint).context("Unable to create a test for the new lint")?;
create_test(&lint, msrv).context("Unable to create a test for the new lint")?;
if lint.ty.is_none() {
add_lint(&lint, msrv).context("Unable to add lint to clippy_lints/src/lib.rs")?;
@ -88,15 +88,21 @@ fn create_lint(lint: &LintData<'_>, enable_msrv: bool) -> io::Result<()> {
}
}
fn create_test(lint: &LintData<'_>) -> io::Result<()> {
fn create_project_layout<P: Into<PathBuf>>(lint_name: &str, location: P, case: &str, hint: &str) -> io::Result<()> {
fn create_test(lint: &LintData<'_>, msrv: bool) -> io::Result<()> {
fn create_project_layout<P: Into<PathBuf>>(
lint_name: &str,
location: P,
case: &str,
hint: &str,
msrv: bool,
) -> io::Result<()> {
let mut path = location.into().join(case);
fs::create_dir(&path)?;
write_file(path.join("Cargo.toml"), get_manifest_contents(lint_name, hint))?;
path.push("src");
fs::create_dir(&path)?;
write_file(path.join("main.rs"), get_test_file_contents(lint_name))?;
write_file(path.join("main.rs"), get_test_file_contents(lint_name, msrv))?;
Ok(())
}
@ -106,13 +112,25 @@ fn create_test(lint: &LintData<'_>) -> io::Result<()> {
let test_dir = lint.project_root.join(&relative_test_dir);
fs::create_dir(&test_dir)?;
create_project_layout(lint.name, &test_dir, "fail", "Content that triggers the lint goes here")?;
create_project_layout(lint.name, &test_dir, "pass", "This file should not trigger the lint")?;
create_project_layout(
lint.name,
&test_dir,
"fail",
"Content that triggers the lint goes here",
msrv,
)?;
create_project_layout(
lint.name,
&test_dir,
"pass",
"This file should not trigger the lint",
false,
)?;
println!("Generated test directories: `{relative_test_dir}/pass`, `{relative_test_dir}/fail`");
} else {
let test_path = format!("tests/ui/{}.rs", lint.name);
let test_contents = get_test_file_contents(lint.name);
let test_contents = get_test_file_contents(lint.name, msrv);
write_file(lint.project_root.join(&test_path), test_contents)?;
println!("Generated test file: `{test_path}`");
@ -194,8 +212,8 @@ pub(crate) fn get_stabilization_version() -> String {
parse_manifest(&contents).expect("Unable to find package version in `Cargo.toml`")
}
fn get_test_file_contents(lint_name: &str) -> String {
formatdoc!(
fn get_test_file_contents(lint_name: &str, msrv: bool) -> String {
let mut test = formatdoc!(
r#"
#![warn(clippy::{lint_name})]
@ -203,7 +221,29 @@ fn get_test_file_contents(lint_name: &str) -> String {
// test code goes here
}}
"#
)
);
if msrv {
let _ = writedoc!(
test,
r#"
// TODO: set xx to the version one below the MSRV used by the lint, and yy to
// the version used by the lint
#[clippy::msrv = "1.xx"]
fn msrv_1_xx() {{
// a simple example that would trigger the lint if the MSRV were met
}}
#[clippy::msrv = "1.yy"]
fn msrv_1_yy() {{
// the same example as above
}}
"#
);
}
test
}
fn get_manifest_contents(lint_name: &str, hint: &str) -> String {
@ -258,7 +298,7 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
)
});
let _: fmt::Result = write!(result, "{}", get_lint_declaration(&name_upper, category));
let _: fmt::Result = writeln!(result, "{}", get_lint_declaration(&name_upper, category));
result.push_str(&if enable_msrv {
formatdoc!(
@ -281,7 +321,6 @@ fn get_lint_file_contents(lint: &LintData<'_>, enable_msrv: bool) -> String {
}}
// TODO: Add MSRV level to `clippy_utils/src/msrvs.rs` if needed.
// TODO: Add MSRV test to `tests/ui/min_rust_version_attr.rs`.
// TODO: Update msrv config comment in `clippy_lints/src/utils/conf.rs`
"#
)