Skip to content

Commit 08169fd

Browse files
committed
Add rustc_shim_for_cargo_fix and wrapped_clippy_driver to reuse code
Signed-off-by: hi-rustin <[email protected]>
1 parent 47f6e2d commit 08169fd

File tree

3 files changed

+37
-138
lines changed

3 files changed

+37
-138
lines changed

crates/cargo-test-support/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -517,6 +517,29 @@ pub fn cargo_exe() -> PathBuf {
517517
snapbox::cmd::cargo_bin("cargo")
518518
}
519519

520+
/// A wrapper around `rustc` instead of calling `clippy`.
521+
pub fn wrapped_clippy_driver() -> PathBuf {
522+
let clippy_driver = project()
523+
.at(paths::global_root().join("clippy-driver"))
524+
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
525+
.file(
526+
"src/main.rs",
527+
r#"
528+
fn main() {
529+
let mut args = std::env::args_os();
530+
let _me = args.next().unwrap();
531+
let rustc = args.next().unwrap();
532+
let status = std::process::Command::new(rustc).args(args).status().unwrap();
533+
std::process::exit(status.code().unwrap_or(1));
534+
}
535+
"#,
536+
)
537+
.build();
538+
clippy_driver.cargo("build").run();
539+
540+
clippy_driver.bin("clippy-driver")
541+
}
542+
520543
/// This is the raw output from the process.
521544
///
522545
/// This is similar to `std::process::Output`, however the `status` is

tests/testsuite/check.rs

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ use crate::messages::raw_rustc_output;
66
use cargo_test_support::install::exe;
77
use cargo_test_support::paths::CargoPathExt;
88
use cargo_test_support::registry::Package;
9-
use cargo_test_support::tools;
109
use cargo_test_support::{basic_bin_manifest, basic_manifest, git, project};
10+
use cargo_test_support::{tools, wrapped_clippy_driver};
1111

1212
#[cargo_test]
1313
fn check_success() {
@@ -1416,25 +1416,6 @@ fn check_fixable_mixed() {
14161416

14171417
#[cargo_test]
14181418
fn check_fixable_warning_for_clippy() {
1419-
// A wrapper around `rustc` instead of calling `clippy`
1420-
let clippy_driver = project()
1421-
.at(cargo_test_support::paths::global_root().join("clippy-driver"))
1422-
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
1423-
.file(
1424-
"src/main.rs",
1425-
r#"
1426-
fn main() {
1427-
let mut args = std::env::args_os();
1428-
let _me = args.next().unwrap();
1429-
let rustc = args.next().unwrap();
1430-
let status = std::process::Command::new(rustc).args(args).status().unwrap();
1431-
std::process::exit(status.code().unwrap_or(1));
1432-
}
1433-
"#,
1434-
)
1435-
.build();
1436-
clippy_driver.cargo("build").run();
1437-
14381419
let foo = project()
14391420
.file(
14401421
"Cargo.toml",
@@ -1452,10 +1433,7 @@ fn check_fixable_warning_for_clippy() {
14521433

14531434
foo.cargo("check")
14541435
// We can't use `clippy` so we use a `rustc` workspace wrapper instead
1455-
.env(
1456-
"RUSTC_WORKSPACE_WRAPPER",
1457-
clippy_driver.bin("clippy-driver"),
1458-
)
1436+
.env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver())
14591437
.with_stderr_contains("[..] (run `cargo clippy --fix --lib -p foo` to apply 1 suggestion)")
14601438
.run();
14611439
}

tests/testsuite/fix.rs

Lines changed: 12 additions & 114 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use cargo_test_support::compare::assert_match_exact;
55
use cargo_test_support::git::{self, init};
66
use cargo_test_support::paths::{self, CargoPathExt};
77
use cargo_test_support::registry::{Dependency, Package};
8-
use cargo_test_support::tools;
9-
use cargo_test_support::{basic_manifest, is_nightly, project};
8+
use cargo_test_support::{basic_manifest, is_nightly, project, Project};
9+
use cargo_test_support::{tools, wrapped_clippy_driver};
1010

1111
#[cargo_test]
1212
fn do_not_fix_broken_builds() {
@@ -53,8 +53,7 @@ fn fix_broken_if_requested() {
5353
.run();
5454
}
5555

56-
#[cargo_test]
57-
fn broken_fixes_backed_out() {
56+
fn rustc_shim_for_cargo_fix() -> Project {
5857
// This works as follows:
5958
// - Create a `rustc` shim (the "foo" project) which will pretend that the
6059
// verification step fails.
@@ -141,7 +140,13 @@ fn broken_fixes_backed_out() {
141140
// Build our rustc shim
142141
p.cargo("build").cwd("foo").run();
143142

144-
// Attempt to fix code, but our shim will always fail the second compile
143+
p
144+
}
145+
146+
#[cargo_test]
147+
fn broken_fixes_backed_out() {
148+
let p = rustc_shim_for_cargo_fix();
149+
// Attempt to fix code, but our shim will always fail the second compile.
145150
p.cargo("fix --allow-no-vcs --lib")
146151
.cwd("bar")
147152
.env("__CARGO_FIX_YOLO", "1")
@@ -179,111 +184,7 @@ fn broken_fixes_backed_out() {
179184

180185
#[cargo_test]
181186
fn broken_clippy_fixes_backed_out() {
182-
// A wrapper around `rustc` instead of calling `clippy`
183-
let clippy_driver = project()
184-
.at(cargo_test_support::paths::global_root().join("clippy-driver"))
185-
.file("Cargo.toml", &basic_manifest("clippy-driver", "0.0.1"))
186-
.file(
187-
"src/main.rs",
188-
r#"
189-
fn main() {
190-
let mut args = std::env::args_os();
191-
let _me = args.next().unwrap();
192-
let rustc = args.next().unwrap();
193-
let status = std::process::Command::new(rustc).args(args).status().unwrap();
194-
std::process::exit(status.code().unwrap_or(1));
195-
}
196-
"#,
197-
)
198-
.build();
199-
clippy_driver.cargo("build").run();
200-
201-
// This works as follows:
202-
// - Create a `rustc` shim (the "foo" project) which will pretend that the
203-
// verification step fails.
204-
// - There is an empty build script so `foo` has `OUT_DIR` to track the steps.
205-
// - The first "check", `foo` creates a file in OUT_DIR, and it completes
206-
// successfully with a warning diagnostic to remove unused `mut`.
207-
// - rustfix removes the `mut`.
208-
// - The second "check" to verify the changes, `foo` swaps out the content
209-
// with something that fails to compile. It creates a second file so it
210-
// won't do anything in the third check.
211-
// - cargo fix discovers that the fix failed, and it backs out the changes.
212-
// - The third "check" is done to display the original diagnostics of the
213-
// original code.
214-
let p = project()
215-
.file(
216-
"foo/Cargo.toml",
217-
r#"
218-
[package]
219-
name = 'foo'
220-
version = '0.1.0'
221-
[workspace]
222-
"#,
223-
)
224-
.file(
225-
"foo/src/main.rs",
226-
r#"
227-
use std::env;
228-
use std::fs;
229-
use std::io::Write;
230-
use std::path::{Path, PathBuf};
231-
use std::process::{self, Command};
232-
233-
fn main() {
234-
// Ignore calls to things like --print=file-names and compiling build.rs.
235-
// Also compatible for rustc invocations with `@path` argfile.
236-
let is_lib_rs = env::args_os()
237-
.map(PathBuf::from)
238-
.flat_map(|p| if let Some(p) = p.to_str().unwrap_or_default().strip_prefix("@") {
239-
fs::read_to_string(p).unwrap().lines().map(PathBuf::from).collect()
240-
} else {
241-
vec![p]
242-
})
243-
.any(|l| l == Path::new("src/lib.rs"));
244-
if is_lib_rs {
245-
let path = PathBuf::from(env::var_os("OUT_DIR").unwrap());
246-
let first = path.join("first");
247-
let second = path.join("second");
248-
if first.exists() && !second.exists() {
249-
fs::write("src/lib.rs", b"not rust code").unwrap();
250-
fs::File::create(&second).unwrap();
251-
} else {
252-
fs::File::create(&first).unwrap();
253-
}
254-
}
255-
let status = Command::new("rustc")
256-
.args(env::args().skip(1))
257-
.status()
258-
.expect("failed to run rustc");
259-
process::exit(status.code().unwrap_or(2));
260-
}
261-
"#,
262-
)
263-
.file(
264-
"bar/Cargo.toml",
265-
r#"
266-
[package]
267-
name = 'bar'
268-
version = '0.1.0'
269-
[workspace]
270-
"#,
271-
)
272-
.file("bar/build.rs", "fn main() {}")
273-
.file(
274-
"bar/src/lib.rs",
275-
r#"
276-
pub fn foo() {
277-
let mut x = 3;
278-
drop(x);
279-
}
280-
"#,
281-
)
282-
.build();
283-
284-
// Build our rustc shim
285-
p.cargo("build").cwd("foo").run();
286-
187+
let p = rustc_shim_for_cargo_fix();
287188
// Attempt to fix code, but our shim will always fail the second compile.
288189
// Also, we use `clippy` as a workspace wrapper to make sure that we properly
289190
// generate the report bug text.
@@ -292,10 +193,7 @@ fn broken_clippy_fixes_backed_out() {
292193
.env("__CARGO_FIX_YOLO", "1")
293194
.env("RUSTC", p.root().join("foo/target/debug/foo"))
294195
// We can't use `clippy` so we use a `rustc` workspace wrapper instead
295-
.env(
296-
"RUSTC_WORKSPACE_WRAPPER",
297-
clippy_driver.bin("clippy-driver"),
298-
)
196+
.env("RUSTC_WORKSPACE_WRAPPER", wrapped_clippy_driver())
299197
.with_stderr_contains(
300198
"warning: failed to automatically apply fixes suggested by rustc \
301199
to crate `bar`\n\

0 commit comments

Comments
 (0)