@@ -5,8 +5,8 @@ use cargo_test_support::compare::assert_match_exact;
5
5
use cargo_test_support:: git:: { self , init} ;
6
6
use cargo_test_support:: paths:: { self , CargoPathExt } ;
7
7
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 } ;
10
10
11
11
#[ cargo_test]
12
12
fn do_not_fix_broken_builds ( ) {
@@ -53,8 +53,7 @@ fn fix_broken_if_requested() {
53
53
. run ( ) ;
54
54
}
55
55
56
- #[ cargo_test]
57
- fn broken_fixes_backed_out ( ) {
56
+ fn rustc_shim_for_cargo_fix ( ) -> Project {
58
57
// This works as follows:
59
58
// - Create a `rustc` shim (the "foo" project) which will pretend that the
60
59
// verification step fails.
@@ -141,7 +140,13 @@ fn broken_fixes_backed_out() {
141
140
// Build our rustc shim
142
141
p. cargo ( "build" ) . cwd ( "foo" ) . run ( ) ;
143
142
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.
145
150
p. cargo ( "fix --allow-no-vcs --lib" )
146
151
. cwd ( "bar" )
147
152
. env ( "__CARGO_FIX_YOLO" , "1" )
@@ -179,111 +184,7 @@ fn broken_fixes_backed_out() {
179
184
180
185
#[ cargo_test]
181
186
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 ( ) ;
287
188
// Attempt to fix code, but our shim will always fail the second compile.
288
189
// Also, we use `clippy` as a workspace wrapper to make sure that we properly
289
190
// generate the report bug text.
@@ -292,10 +193,7 @@ fn broken_clippy_fixes_backed_out() {
292
193
. env ( "__CARGO_FIX_YOLO" , "1" )
293
194
. env ( "RUSTC" , p. root ( ) . join ( "foo/target/debug/foo" ) )
294
195
// 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 ( ) )
299
197
. with_stderr_contains (
300
198
"warning: failed to automatically apply fixes suggested by rustc \
301
199
to crate `bar`\n \
0 commit comments