2019-11-25 02:42:45 +00:00
//! Tests for the `cargo fix` command.
2021-02-18 04:41:38 +00:00
use cargo ::core ::Edition ;
2021-07-11 01:47:52 +00:00
use cargo_test_support ::compare ::assert_match_exact ;
2022-11-22 10:56:52 +00:00
use cargo_test_support ::git ::{ self , init } ;
2021-08-17 15:04:02 +00:00
use cargo_test_support ::paths ::{ self , CargoPathExt } ;
2021-03-14 22:01:31 +00:00
use cargo_test_support ::registry ::{ Dependency , Package } ;
2023-11-19 01:23:29 +00:00
use cargo_test_support ::tools ;
2024-01-02 22:29:12 +00:00
use cargo_test_support ::{ basic_manifest , is_nightly , project } ;
2018-07-14 01:49:26 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn do_not_fix_broken_builds ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) {
let mut x = 3 ;
drop ( x ) ;
}
pub fn foo2 ( ) {
let _x : u32 = " a " ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_status ( 101 )
2023-08-09 15:19:59 +00:00
. with_stderr_contains ( " [ERROR] could not compile `foo` (lib) due to 1 previous error " )
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-14 01:49:26 +00:00
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " let mut x = 3; " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_broken_if_requested ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
fn foo ( a : & u32 ) -> u32 { a + 1 }
pub fn bar ( ) {
foo ( 1 ) ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs --broken-code " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_path_deps ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
bar = { path = ' bar ' }
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-14 01:49:26 +00:00
" src/lib.rs " ,
r #"
extern crate bar ;
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " bar/Cargo.toml " , & basic_manifest ( " bar " , " 0.1.0 " ) )
2018-07-14 01:49:26 +00:00
. file (
" bar/src/lib.rs " ,
r #"
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs -p foo -p bar " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stdout ( " " )
2018-11-06 00:32:27 +00:00
. with_stderr_unordered (
2018-08-28 09:20:03 +00:00
" \
2018-07-14 01:49:26 +00:00
[ CHECKING ] bar v0 . 1.0 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] bar / src / lib . rs ( 1 fix )
2018-07-14 01:49:26 +00:00
[ CHECKING ] foo v0 . 1.0 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
2018-08-28 09:20:03 +00:00
" ,
2018-12-08 11:19:47 +00:00
)
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn do_not_fix_non_relevant_deps ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-24 12:59:42 +00:00
. no_manifest ( )
2018-07-14 01:49:26 +00:00
. file (
" foo/Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
bar = { path = ' .. / bar ' }
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " foo/src/lib.rs " , " " )
2018-07-24 22:35:01 +00:00
. file ( " bar/Cargo.toml " , & basic_manifest ( " bar " , " 0.1.0 " ) )
2018-07-14 01:49:26 +00:00
. file (
" bar/src/lib.rs " ,
r #"
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
2019-03-20 23:34:56 +00:00
. cwd ( " foo " )
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-14 01:49:26 +00:00
assert! ( p . read_file ( " bar/src/lib.rs " ) . contains ( " mut " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn prepare_for_2018 ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
#![ allow(unused) ]
mod foo {
pub const FOO : & str = " fooo " ;
}
mod bar {
use ::foo ::FOO ;
}
fn main ( ) {
let x = ::foo ::FOO ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
let stderr = " \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 05:36:28 +00:00
[ MIGRATING ] src / lib . rs from 2015 edition to 2018
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 2 fixes )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --edition --allow-no-vcs " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
println! ( " {} " , p . read_file ( " src/lib.rs " ) ) ;
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " use crate::foo::FOO; " ) ) ;
2018-12-08 11:19:47 +00:00
assert! ( p
. read_file ( " src/lib.rs " )
. contains ( " let x = crate::foo::FOO; " ) ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn local_paths ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
use test ::foo ;
mod test {
pub fn foo ( ) { }
}
pub fn f ( ) {
foo ( ) ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2021-02-18 04:41:38 +00:00
p . cargo ( " fix --edition --allow-no-vcs " )
. with_stderr (
" \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 05:36:28 +00:00
[ MIGRATING ] src / lib . rs from 2015 edition to 2018
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
2021-02-18 04:41:38 +00:00
" ,
)
2018-08-28 09:20:03 +00:00
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
println! ( " {} " , p . read_file ( " src/lib.rs " ) ) ;
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " use crate::test::foo; " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn upgrade_extern_crate ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
edition = ' 2018 '
[ workspace ]
[ dependencies ]
bar = { path = ' bar ' }
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-14 01:49:26 +00:00
" src/lib.rs " ,
r #"
#![ warn(rust_2018_idioms) ]
extern crate bar ;
use bar ::bar ;
pub fn foo ( ) {
::bar ::bar ( ) ;
bar ( ) ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " bar/Cargo.toml " , & basic_manifest ( " bar " , " 0.1.0 " ) )
2018-07-14 01:49:26 +00:00
. file ( " bar/src/lib.rs " , " pub fn bar() {} " )
. build ( ) ;
let stderr = " \
[ CHECKING ] bar v0 . 1.0 ( [ .. ] )
[ CHECKING ] foo v0 . 1.0 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
println! ( " {} " , p . read_file ( " src/lib.rs " ) ) ;
assert! ( ! p . read_file ( " src/lib.rs " ) . contains ( " extern crate " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn specify_rustflags ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
#![ allow(unused) ]
mod foo {
pub const FOO : & str = " fooo " ;
}
fn main ( ) {
let x = ::foo ::FOO ;
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --edition --allow-no-vcs " )
2019-07-20 22:58:37 +00:00
. env ( " RUSTFLAGS " , " -C linker=cc " )
2021-02-18 04:41:38 +00:00
. with_stderr (
" \
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 05:36:28 +00:00
[ MIGRATING ] src / lib . rs from 2015 edition to 2018
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
[ FINISHED ] [ .. ]
" ,
)
2018-08-28 09:20:03 +00:00
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn no_changes_necessary ( ) {
2018-08-28 09:20:03 +00:00
let p = project ( ) . file ( " src/lib.rs " , " " ) . build ( ) ;
2018-07-14 01:49:26 +00:00
let stderr = " \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fixes_extra_mut ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
let stderr = " \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fixes_two_missing_ampersands ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) -> u32 {
let mut x = 3 ;
let mut y = 3 ;
x + y
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
let stderr = " \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 2 fixes )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn tricky ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) -> u32 {
let mut x = 3 ; let mut y = 3 ;
x + y
}
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
let stderr = " \
2018-07-24 13:01:56 +00:00
[ CHECKING ] foo v0 . 0.1 ( [ .. ] )
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 2 fixes )
2018-07-14 01:49:26 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stderr ( stderr )
. with_stdout ( " " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn preserve_line_endings ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
2019-07-13 23:00:47 +00:00
" fn add(a: &u32) -> u32 { a + 1 } \r \n \
2018-07-14 01:49:26 +00:00
pub fn foo ( ) -> u32 { let mut x = 3 ; add ( & x ) } \ r \ n \
" ,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " \r \n " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_deny_warnings ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
2019-07-13 23:00:47 +00:00
" #![deny(warnings)]
pub fn foo ( ) { let mut x = 3 ; drop ( x ) ; }
2018-07-14 01:49:26 +00:00
" ,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_deny_warnings_but_not_others ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
"
2021-04-28 08:59:57 +00:00
#![ deny(unused_mut) ]
2018-07-14 01:49:26 +00:00
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
2021-06-10 23:49:29 +00:00
pub fn bar ( ) {
#[ allow(unused_mut) ]
let mut _y = 4 ;
}
2018-07-14 01:49:26 +00:00
" ,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. run ( ) ;
2018-07-14 01:49:26 +00:00
assert! ( ! p . read_file ( " src/lib.rs " ) . contains ( " let mut x = 3; " ) ) ;
2021-06-10 23:49:29 +00:00
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " let mut _y = 4; " ) ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_two_files ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
"
pub mod bar ;
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" ,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-14 01:49:26 +00:00
" src/bar.rs " ,
"
pub fn foo ( ) -> u32 {
let mut x = 3 ;
x
}
" ,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
2021-02-18 04:41:38 +00:00
. with_stderr_contains ( " [FIXED] src/bar.rs (1 fix) " )
. with_stderr_contains ( " [FIXED] src/lib.rs (1 fix) " )
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-14 01:49:26 +00:00
assert! ( ! p . read_file ( " src/lib.rs " ) . contains ( " let mut x = 3; " ) ) ;
assert! ( ! p . read_file ( " src/bar.rs " ) . contains ( " let mut x = 3; " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fixes_missing_ampersand ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-25 00:30:32 +00:00
. file ( " src/main.rs " , " fn main() { let mut x = 3; drop(x); } " )
2018-07-14 01:49:26 +00:00
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) { let mut x = 3 ; drop ( x ) ; }
#[ test ]
pub fn foo2 ( ) { let mut x = 3 ; drop ( x ) ; }
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-14 01:49:26 +00:00
" tests/a.rs " ,
r #"
#[ test ]
pub fn foo ( ) { let mut x = 3 ; drop ( x ) ; }
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " examples/foo.rs " , " fn main() { let mut x = 3; drop(x); } " )
2018-07-25 00:30:32 +00:00
. file ( " build.rs " , " fn main() { let mut x = 3; drop(x); } " )
2018-07-14 01:49:26 +00:00
. build ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --all-targets --allow-no-vcs " )
2018-12-08 11:19:47 +00:00
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stdout ( " " )
. with_stderr_contains ( " [COMPILING] foo v0.0.1 ([..]) " )
2021-02-18 04:41:38 +00:00
. with_stderr_contains ( " [FIXED] build.rs (1 fix) " )
2018-12-08 11:19:47 +00:00
// Don't assert number of fixes for this one, as we don't know if we're
// fixing it once or twice! We run this all concurrently, and if we
// compile (and fix) in `--test` mode first, we get two fixes. Otherwise
// we'll fix one non-test thing, and then fix another one later in
// test mode.
2021-02-18 04:41:38 +00:00
. with_stderr_contains ( " [FIXED] src/lib.rs[..] " )
. with_stderr_contains ( " [FIXED] src/main.rs (1 fix) " )
. with_stderr_contains ( " [FIXED] examples/foo.rs (1 fix) " )
. with_stderr_contains ( " [FIXED] tests/a.rs (1 fix) " )
2018-12-08 11:19:47 +00:00
. with_stderr_contains ( " [FINISHED] [..] " )
. run ( ) ;
2023-02-15 23:16:57 +00:00
p . cargo ( " check " ) . run ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " test " ) . run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn fix_features ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2018-07-14 01:49:26 +00:00
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ features ]
bar = [ ]
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-14 01:49:26 +00:00
" src/lib.rs " ,
r #"
2020-09-27 00:59:58 +00:00
#[ cfg(feature = " bar " ) ]
pub fn foo ( ) -> u32 { let mut x = 3 ; x }
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " ) . run ( ) ;
2023-02-15 23:16:57 +00:00
p . cargo ( " check " ) . run ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --features bar --allow-no-vcs " ) . run ( ) ;
2023-02-15 23:16:57 +00:00
p . cargo ( " check --features bar " ) . run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn shows_warnings ( ) {
2018-07-20 11:47:47 +00:00
let p = project ( )
2019-04-05 19:55:01 +00:00
. file (
" src/lib.rs " ,
" #[deprecated] fn bar() {} pub fn foo() { let _ = bar(); } " ,
)
2018-07-14 01:49:26 +00:00
. build ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
2020-07-31 01:04:22 +00:00
. with_stderr_contains ( " [..]warning: use of deprecated[..] " )
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn warns_if_no_vcs_detected ( ) {
2018-08-28 09:20:03 +00:00
let p = project ( ) . file ( " src/lib.rs " , " pub fn foo() {} " ) . build ( ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix " )
. with_status ( 101 )
. with_stderr (
2019-07-13 23:00:47 +00:00
" error: no VCS found for this package and `cargo fix` can potentially perform \
2018-08-28 09:20:03 +00:00
destructive changes ; if you ' d like to suppress this error pass ` - - allow - no - vcs ` \
" ,
2018-12-08 11:19:47 +00:00
)
. run ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " ) . run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn warns_about_dirty_working_directory ( ) {
2019-08-13 05:25:36 +00:00
let p = git ::new ( " foo " , | p | p . file ( " src/lib.rs " , " pub fn foo() {} " ) ) ;
2018-07-14 01:49:26 +00:00
2020-04-17 04:10:11 +00:00
p . change_file ( " src/lib.rs " , " " ) ;
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix " )
. with_status ( 101 )
. with_stderr (
" \
2018-09-21 06:47:09 +00:00
error : the working directory of this package has uncommitted changes , \
2018-08-20 10:46:29 +00:00
and ` cargo fix ` can potentially perform destructive changes ; if you ' d \
2018-08-21 06:58:08 +00:00
like to suppress this error pass ` - - allow - dirty ` , ` - - allow - staged ` , or \
commit the changes to these files :
2018-07-14 01:49:26 +00:00
2018-08-20 10:46:29 +00:00
* src / lib . rs ( dirty )
2018-07-14 01:49:26 +00:00
2018-08-28 09:20:03 +00:00
" ,
2018-12-08 11:19:47 +00:00
)
. run ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-dirty " ) . run ( ) ;
2018-07-14 01:49:26 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-08-21 07:06:19 +00:00
fn warns_about_staged_working_directory ( ) {
2019-08-13 05:25:36 +00:00
let ( p , repo ) = git ::new_repo ( " foo " , | p | p . file ( " src/lib.rs " , " pub fn foo() {} " ) ) ;
2018-08-21 07:06:19 +00:00
2020-04-17 04:10:11 +00:00
p . change_file ( " src/lib.rs " , " pub fn bar() {} " ) ;
2018-08-21 07:06:19 +00:00
git ::add ( & repo ) ;
2018-08-28 23:17:39 +00:00
p . cargo ( " fix " )
. with_status ( 101 )
. with_stderr (
" \
2018-09-21 06:47:09 +00:00
error : the working directory of this package has uncommitted changes , \
2018-08-21 07:06:19 +00:00
and ` cargo fix ` can potentially perform destructive changes ; if you ' d \
like to suppress this error pass ` - - allow - dirty ` , ` - - allow - staged ` , or \
commit the changes to these files :
* src / lib . rs ( staged )
2018-08-28 23:17:39 +00:00
" ,
2018-12-08 11:19:47 +00:00
)
. run ( ) ;
2018-08-28 23:17:39 +00:00
p . cargo ( " fix --allow-staged " ) . run ( ) ;
2018-08-21 07:06:19 +00:00
}
2022-11-22 01:05:21 +00:00
#[ cargo_test ]
2022-11-26 11:37:19 +00:00
fn errors_about_untracked_files ( ) {
2022-11-22 10:56:52 +00:00
let mut git_project = project ( ) . at ( " foo " ) ;
git_project = git_project . file ( " src/lib.rs " , " pub fn foo() {} " ) ;
let p = git_project . build ( ) ;
let _ = init ( & p . root ( ) ) ;
2022-11-22 01:05:21 +00:00
p . cargo ( " fix " )
. with_status ( 101 )
. with_stderr (
" \
2022-11-26 11:37:19 +00:00
error : the working directory of this package has uncommitted changes , \
and ` cargo fix ` can potentially perform destructive changes ; if you ' d \
like to suppress this error pass ` - - allow - dirty ` , ` - - allow - staged ` , or \
commit the changes to these files :
* Cargo . toml ( dirty )
* src / ( dirty )
2022-11-22 01:05:21 +00:00
" ,
)
. run ( ) ;
p . cargo ( " fix --allow-dirty " ) . run ( ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-14 01:49:26 +00:00
fn does_not_warn_about_clean_working_directory ( ) {
2019-08-13 05:25:36 +00:00
let p = git ::new ( " foo " , | p | p . file ( " src/lib.rs " , " pub fn foo() {} " ) ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix " ) . run ( ) ;
2018-07-14 01:49:26 +00:00
}
2018-07-22 18:01:07 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-22 18:01:07 +00:00
fn does_not_warn_about_dirty_ignored_files ( ) {
2019-08-13 05:25:36 +00:00
let p = git ::new ( " foo " , | p | {
p . file ( " src/lib.rs " , " pub fn foo() {} " )
. file ( " .gitignore " , " bar \n " )
} ) ;
2018-07-22 18:01:07 +00:00
2020-04-17 04:10:11 +00:00
p . change_file ( " bar " , " " ) ;
2018-07-22 18:01:07 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix " ) . run ( ) ;
2018-07-22 18:01:07 +00:00
}
2018-07-26 22:21:34 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-26 22:21:34 +00:00
fn fix_all_targets_by_default ( ) {
let p = project ( )
. file ( " src/lib.rs " , " pub fn foo() { let mut x = 3; drop(x); } " )
. file ( " tests/foo.rs " , " pub fn foo() { let mut x = 3; drop(x); } " )
. build ( ) ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --allow-no-vcs " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. run ( ) ;
2018-07-26 22:21:34 +00:00
assert! ( ! p . read_file ( " src/lib.rs " ) . contains ( " let mut x " ) ) ;
assert! ( ! p . read_file ( " tests/foo.rs " ) . contains ( " let mut x " ) ) ;
}
2018-07-28 20:22:32 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2021-02-18 06:12:46 +00:00
fn prepare_for_unstable ( ) {
// During the period where a new edition is coming up, but not yet stable,
// this test will verify that it cannot be migrated to on stable. If there
// is no next edition, it does nothing.
2021-02-19 20:57:36 +00:00
let next = match Edition ::LATEST_UNSTABLE {
Some ( next ) = > next ,
None = > {
eprintln! ( " Next edition is currently not available, skipping test. " ) ;
return ;
}
} ;
let latest_stable = Edition ::LATEST_STABLE ;
2021-08-15 09:16:39 +00:00
let prev = latest_stable . previous ( ) . unwrap ( ) ;
2018-07-28 20:22:32 +00:00
let p = project ( )
. file (
" Cargo.toml " ,
2021-02-18 06:12:46 +00:00
& format! (
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
edition = " {} "
" #,
latest_stable
) ,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
// -j1 to make the error more deterministic (otherwise there can be
// multiple errors since they run in parallel).
p . cargo ( " fix --edition --allow-no-vcs -j1 " )
2021-08-15 09:16:39 +00:00
. with_stderr ( & format_args! ( " \
2021-02-18 06:12:46 +00:00
[ CHECKING ] foo [ .. ]
2021-08-15 09:16:39 +00:00
[ WARNING ] ` src / lib . rs ` is on the latest edition , but trying to migrate to edition { next } .
2021-02-18 06:12:46 +00:00
Edition { next } is unstable and not allowed in this release , consider trying the nightly release channel .
2021-08-15 09:16:39 +00:00
If you are trying to migrate from the previous edition ( { prev } ) , the
process requires following these steps :
1. Start with ` edition = \ " {prev} \" ` in `Cargo.toml`
2. Run ` cargo fix - - edition `
3. Modify ` Cargo . toml ` to set ` edition = \ " {latest_stable} \" `
4. Run ` cargo build ` or ` cargo test ` to verify the fixes worked
More details may be found at
https ://doc.rust-lang.org/edition-guide/editions/transitioning-an-existing-project-to-a-new-edition.html
[ FINISHED ] [ .. ]
" , next=next, latest_stable=latest_stable, prev=prev))
2021-02-18 06:12:46 +00:00
. run ( ) ;
if ! is_nightly ( ) {
// The rest of this test is fundamentally always nightly.
return ;
}
p . cargo ( " fix --edition --allow-no-vcs " )
2022-07-16 02:32:23 +00:00
. masquerade_as_nightly_cargo ( & [ " always_nightly " ] )
2021-02-18 06:12:46 +00:00
. with_stderr ( & format! (
" \
[ CHECKING ] foo [ .. ]
[ MIGRATING ] src / lib . rs from { latest_stable } edition to { next }
[ FINISHED ] [ .. ]
" ,
latest_stable = latest_stable ,
next = next ,
) )
. run ( ) ;
}
#[ cargo_test ]
fn prepare_for_latest_stable ( ) {
// This is the stable counterpart of prepare_for_unstable.
2021-02-19 20:57:36 +00:00
let latest_stable = Edition ::LATEST_STABLE ;
2021-02-18 06:12:46 +00:00
let previous = latest_stable . previous ( ) . unwrap ( ) ;
let p = project ( )
. file (
" Cargo.toml " ,
& format! (
r #"
2018-07-28 20:22:32 +00:00
[ package ]
name = ' foo '
version = ' 0. 1.0 '
2021-02-18 06:12:46 +00:00
edition = ' { } '
2018-07-28 20:22:32 +00:00
" #,
2021-02-18 06:12:46 +00:00
previous
) ,
2018-12-08 11:19:47 +00:00
)
. file ( " src/lib.rs " , " " )
2018-07-28 20:22:32 +00:00
. build ( ) ;
2021-02-18 06:12:46 +00:00
p . cargo ( " fix --edition --allow-no-vcs " )
. with_stderr ( & format! (
" \
[ CHECKING ] foo [ .. ]
[ MIGRATING ] src / lib . rs from { } edition to { }
[ FINISHED ] [ .. ]
" ,
previous , latest_stable
) )
. run ( ) ;
}
2018-07-28 20:22:32 +00:00
2021-09-09 06:10:33 +00:00
#[ cargo_test(nightly, reason = " fundamentally always nightly " ) ]
2021-02-18 06:12:46 +00:00
fn prepare_for_already_on_latest_unstable ( ) {
// During the period where a new edition is coming up, but not yet stable,
// this test will check what happens if you are already on the latest. If
// there is no next edition, it does nothing.
2021-02-19 20:57:36 +00:00
let next_edition = match Edition ::LATEST_UNSTABLE {
Some ( next ) = > next ,
None = > {
eprintln! ( " Next edition is currently not available, skipping test. " ) ;
return ;
}
} ;
2021-02-18 06:12:46 +00:00
let p = project ( )
. file (
" Cargo.toml " ,
& format! (
r #"
cargo - features = [ " edition{} " ]
2018-07-28 20:22:32 +00:00
2021-02-18 06:12:46 +00:00
[ package ]
name = ' foo '
version = ' 0. 1.0 '
edition = ' { } '
" #,
next_edition , next_edition
) ,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
2018-07-28 20:22:32 +00:00
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --edition --allow-no-vcs " )
2022-07-16 02:32:23 +00:00
. masquerade_as_nightly_cargo ( & [ " always_nightly " ] )
2021-07-15 14:12:34 +00:00
. with_stderr_contains ( " [CHECKING] foo [..] " )
2021-02-18 06:12:46 +00:00
. with_stderr_contains ( & format! (
" \
[ WARNING ] ` src / lib . rs ` is already on the latest edition ( { next_edition } ) , unable to migrate further
" ,
next_edition = next_edition
) )
. run ( ) ;
}
#[ cargo_test ]
fn prepare_for_already_on_latest_stable ( ) {
// Stable counterpart of prepare_for_already_on_latest_unstable.
2021-02-19 20:57:36 +00:00
if Edition ::LATEST_UNSTABLE . is_some ( ) {
2021-02-18 06:12:46 +00:00
eprintln! ( " This test cannot run while the latest edition is unstable, skipping. " ) ;
return ;
}
2021-02-19 20:57:36 +00:00
let latest_stable = Edition ::LATEST_STABLE ;
2021-02-18 06:12:46 +00:00
let p = project ( )
. file (
" Cargo.toml " ,
& format! (
r #"
[ package ]
name = ' foo '
version = ' 0. 1.0 '
edition = ' { } '
" #,
latest_stable
) ,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
p . cargo ( " fix --edition --allow-no-vcs " )
2021-07-15 14:12:34 +00:00
. with_stderr_contains ( " [CHECKING] foo [..] " )
2021-02-18 06:12:46 +00:00
. with_stderr_contains ( & format! (
" \
[ WARNING ] ` src / lib . rs ` is already on the latest edition ( { latest_stable } ) , unable to migrate further
" ,
latest_stable = latest_stable
) )
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-28 20:22:32 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-31 20:08:46 +00:00
fn fix_overlapping ( ) {
let p = project ( )
. file (
" src/lib.rs " ,
r #"
pub fn foo < T > ( ) { }
pub struct A ;
pub mod bar {
pub fn baz ( ) {
::foo ::< ::A > ( ) ;
}
}
2018-08-28 09:20:03 +00:00
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-31 20:08:46 +00:00
2021-02-19 21:08:59 +00:00
p . cargo ( " fix --allow-no-vcs --edition --lib " )
2021-02-18 04:41:38 +00:00
. with_stderr (
" \
2018-07-31 20:08:46 +00:00
[ CHECKING ] foo [ .. ]
2021-02-18 05:36:28 +00:00
[ MIGRATING ] src / lib . rs from 2015 edition to 2018
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 2 fixes )
2018-07-31 20:08:46 +00:00
[ FINISHED ] dev [ .. ]
2021-02-18 04:41:38 +00:00
" ,
)
2018-08-28 09:20:03 +00:00
. run ( ) ;
2018-07-31 20:08:46 +00:00
let contents = p . read_file ( " src/lib.rs " ) ;
println! ( " {} " , contents ) ;
assert! ( contents . contains ( " crate::foo::<crate::A>() " ) ) ;
}
2018-08-01 00:01:09 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-31 22:42:38 +00:00
fn fix_idioms ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = ' foo '
version = ' 0. 1.0 '
edition = ' 2018 '
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-07-31 22:42:38 +00:00
" src/lib.rs " ,
r #"
use std ::any ::Any ;
pub fn foo ( ) {
let _x : Box < Any > = Box ::new ( 3 ) ;
}
2018-08-28 09:20:03 +00:00
" #,
2018-12-08 11:19:47 +00:00
)
. build ( ) ;
2018-07-31 22:42:38 +00:00
let stderr = " \
[ CHECKING ] foo [ .. ]
2021-02-18 04:41:38 +00:00
[ FIXED ] src / lib . rs ( 1 fix )
2018-07-31 22:42:38 +00:00
[ FINISHED ] [ .. ]
" ;
2018-08-28 09:20:03 +00:00
p . cargo ( " fix --edition-idioms --allow-no-vcs " )
. with_stderr ( stderr )
. run ( ) ;
2018-07-31 22:42:38 +00:00
assert! ( p . read_file ( " src/lib.rs " ) . contains ( " Box<dyn Any> " ) ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-31 22:42:38 +00:00
fn idioms_2015_ok ( ) {
2018-08-28 09:20:03 +00:00
let p = project ( ) . file ( " src/lib.rs " , " " ) . build ( ) ;
2018-07-31 22:42:38 +00:00
2019-05-09 17:58:18 +00:00
p . cargo ( " fix --edition-idioms --allow-no-vcs " ) . run ( ) ;
2018-07-31 22:42:38 +00:00
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-20 10:15:50 +00:00
fn shows_warnings_on_second_run_without_changes ( ) {
let p = project ( )
. file (
" src/lib.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
2018-07-20 10:15:50 +00:00
pub fn foo ( ) {
2019-02-10 12:14:03 +00:00
let _ = bar ( ) ;
2018-07-20 10:15:50 +00:00
}
" #,
)
. build ( ) ;
p . cargo ( " fix --allow-no-vcs " )
2020-07-31 01:04:22 +00:00
. with_stderr_contains ( " [..]warning: use of deprecated[..] " )
2018-07-20 10:15:50 +00:00
. run ( ) ;
p . cargo ( " fix --allow-no-vcs " )
2020-07-31 01:04:22 +00:00
. with_stderr_contains ( " [..]warning: use of deprecated[..] " )
2018-07-20 10:15:50 +00:00
. run ( ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-07-20 10:15:50 +00:00
fn shows_warnings_on_second_run_without_changes_on_multiple_targets ( ) {
let p = project ( )
. file (
" src/lib.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
2018-07-20 10:15:50 +00:00
2019-02-10 12:14:03 +00:00
pub fn foo ( ) {
let _ = bar ( ) ;
}
2018-07-20 10:15:50 +00:00
" #,
)
. file (
" src/main.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
fn main ( ) {
let _ = bar ( ) ;
}
2018-07-20 10:15:50 +00:00
" #,
)
. file (
" tests/foo.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
2018-07-20 10:15:50 +00:00
#[ test ]
fn foo_test ( ) {
2019-02-10 12:14:03 +00:00
let _ = bar ( ) ;
2018-07-20 10:15:50 +00:00
}
" #,
)
. file (
" tests/bar.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
2018-07-20 10:15:50 +00:00
#[ test ]
fn foo_test ( ) {
2019-02-10 12:14:03 +00:00
let _ = bar ( ) ;
2018-07-20 10:15:50 +00:00
}
" #,
)
. file (
" examples/fooxample.rs " ,
r #"
2019-02-10 12:14:03 +00:00
#[ deprecated ]
fn bar ( ) { }
2018-07-20 10:15:50 +00:00
fn main ( ) {
2019-02-10 12:14:03 +00:00
let _ = bar ( ) ;
2018-07-20 10:15:50 +00:00
}
" #,
)
. build ( ) ;
p . cargo ( " fix --allow-no-vcs --all-targets " )
2019-02-10 12:14:03 +00:00
. with_stderr_contains ( " --> examples/fooxample.rs:6:29 " )
. with_stderr_contains ( " --> src/lib.rs:6:29 " )
. with_stderr_contains ( " --> src/main.rs:6:29 " )
. with_stderr_contains ( " --> tests/bar.rs:7:29 " )
. with_stderr_contains ( " --> tests/foo.rs:7:29 " )
2018-07-20 10:15:50 +00:00
. run ( ) ;
p . cargo ( " fix --allow-no-vcs --all-targets " )
2019-02-10 12:14:03 +00:00
. with_stderr_contains ( " --> examples/fooxample.rs:6:29 " )
. with_stderr_contains ( " --> src/lib.rs:6:29 " )
. with_stderr_contains ( " --> src/main.rs:6:29 " )
. with_stderr_contains ( " --> tests/bar.rs:7:29 " )
. with_stderr_contains ( " --> tests/foo.rs:7:29 " )
2018-07-20 10:15:50 +00:00
. run ( ) ;
}
2018-08-29 19:24:16 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-08-29 19:24:16 +00:00
fn doesnt_rebuild_dependencies ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
bar = { path = ' bar ' }
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " src/lib.rs " , " extern crate bar; " )
2018-08-29 19:24:16 +00:00
. file ( " bar/Cargo.toml " , & basic_manifest ( " bar " , " 0.1.0 " ) )
. file ( " bar/src/lib.rs " , " " )
. build ( ) ;
p . cargo ( " fix --allow-no-vcs -p foo " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stdout ( " " )
2018-12-08 11:19:47 +00:00
. with_stderr (
" \
2018-08-30 05:28:20 +00:00
[ CHECKING ] bar v0 . 1.0 ( [ .. ] )
[ CHECKING ] foo v0 . 1.0 ( [ .. ] )
[ FINISHED ] dev [ unoptimized + debuginfo ] target ( s ) in [ .. ]
2018-12-08 11:19:47 +00:00
" ,
)
2018-08-29 19:24:16 +00:00
. run ( ) ;
p . cargo ( " fix --allow-no-vcs -p foo " )
. env ( " __CARGO_FIX_YOLO " , " 1 " )
. with_stdout ( " " )
2018-12-08 11:19:47 +00:00
. with_stderr (
" \
2018-08-30 05:28:20 +00:00
[ CHECKING ] foo v0 . 1.0 ( [ .. ] )
[ FINISHED ] dev [ unoptimized + debuginfo ] target ( s ) in [ .. ]
2018-12-08 11:19:47 +00:00
" ,
)
2018-08-29 19:24:16 +00:00
. run ( ) ;
}
2018-09-05 22:17:43 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-09-05 22:17:43 +00:00
fn does_not_crash_with_rustc_wrapper ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
" #,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
p . cargo ( " fix --allow-no-vcs " )
2021-06-10 00:13:33 +00:00
. env ( " RUSTC_WRAPPER " , tools ::echo_wrapper ( ) )
2018-09-05 22:17:43 +00:00
. run ( ) ;
2021-06-10 00:13:33 +00:00
p . build_dir ( ) . rm_rf ( ) ;
2020-12-13 22:50:15 +00:00
p . cargo ( " fix --allow-no-vcs --verbose " )
2021-06-10 00:13:33 +00:00
. env ( " RUSTC_WORKSPACE_WRAPPER " , tools ::echo_wrapper ( ) )
2019-10-22 14:53:51 +00:00
. run ( ) ;
}
#[ cargo_test ]
fn uses_workspace_wrapper_and_primary_wrapper_override ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
" #,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
2020-12-13 22:50:15 +00:00
p . cargo ( " fix --allow-no-vcs --verbose " )
2021-06-10 00:13:33 +00:00
. env ( " RUSTC_WORKSPACE_WRAPPER " , tools ::echo_wrapper ( ) )
2020-03-13 21:59:41 +00:00
. with_stderr_contains ( " WRAPPER CALLED: rustc src/lib.rs --crate-name foo [..] " )
2019-10-22 14:53:51 +00:00
. run ( ) ;
}
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-11-01 15:13:11 +00:00
fn only_warn_for_relevant_crates ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
a = { path = 'a' }
" #,
)
. file ( " src/lib.rs " , " " )
. file (
" a/Cargo.toml " ,
r #"
[ package ]
name = " a "
version = " 0.1.0 "
" #,
)
. file (
" a/src/lib.rs " ,
"
pub fn foo ( ) { }
pub mod bar {
use foo ;
pub fn baz ( ) { foo ( ) }
}
" ,
)
. build ( ) ;
p . cargo ( " fix --allow-no-vcs --edition " )
2018-12-08 11:19:47 +00:00
. with_stderr (
" \
2018-11-01 15:13:11 +00:00
[ CHECKING ] a v0 . 1.0 ( [ .. ] )
[ CHECKING ] foo v0 . 1.0 ( [ .. ] )
2021-02-18 05:36:28 +00:00
[ MIGRATING ] src / lib . rs from 2015 edition to 2018
2018-11-01 15:13:11 +00:00
[ FINISHED ] dev [ unoptimized + debuginfo ] target ( s ) in [ .. ]
2018-12-08 11:19:47 +00:00
" ,
)
2018-11-01 15:13:11 +00:00
. run ( ) ;
}
2018-11-13 20:08:57 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-11-13 20:08:57 +00:00
fn fix_to_broken_code ( ) {
let p = project ( )
. file (
" foo/Cargo.toml " ,
r #"
[ package ]
name = ' foo '
version = ' 0. 1.0 '
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-11-13 20:08:57 +00:00
" foo/src/main.rs " ,
2020-09-27 00:59:58 +00:00
r #"
2018-11-13 20:08:57 +00:00
use std ::env ;
use std ::fs ;
use std ::io ::Write ;
use std ::path ::{ Path , PathBuf } ;
use std ::process ::{ self , Command } ;
fn main ( ) {
2022-04-10 13:16:12 +00:00
// Ignore calls to things like --print=file-names and compiling build.rs.
// Also compatible for rustc invocations with `@path` argfile.
2018-11-13 20:08:57 +00:00
let is_lib_rs = env ::args_os ( )
. map ( PathBuf ::from )
2022-04-10 13:16:12 +00:00
. flat_map ( | p | if let Some ( p ) = p . to_str ( ) . unwrap_or_default ( ) . strip_prefix ( " @ " ) {
fs ::read_to_string ( p ) . unwrap ( ) . lines ( ) . map ( PathBuf ::from ) . collect ( )
} else {
vec! [ p ]
} )
2018-11-13 20:08:57 +00:00
. any ( | l | l = = Path ::new ( " src/lib.rs " ) ) ;
if is_lib_rs {
let path = PathBuf ::from ( env ::var_os ( " OUT_DIR " ) . unwrap ( ) ) ;
let path = path . join ( " foo " ) ;
if path . exists ( ) {
panic! ( )
} else {
fs ::File ::create ( & path ) . unwrap ( ) ;
}
}
let status = Command ::new ( " rustc " )
. args ( env ::args ( ) . skip ( 1 ) )
. status ( )
. expect ( " failed to run rustc " ) ;
process ::exit ( status . code ( ) . unwrap_or ( 2 ) ) ;
}
2020-09-27 00:59:58 +00:00
" #,
2018-12-08 11:19:47 +00:00
)
. file (
2018-11-13 20:08:57 +00:00
" bar/Cargo.toml " ,
r #"
[ package ]
name = ' bar '
version = ' 0. 1.0 '
[ workspace ]
" #,
2018-12-08 11:19:47 +00:00
)
. file ( " bar/build.rs " , " fn main() {} " )
. file ( " bar/src/lib.rs " , " pub fn foo() { let mut x = 3; drop(x); } " )
. build ( ) ;
2018-11-13 20:08:57 +00:00
// Build our rustc shim
2019-03-20 23:34:56 +00:00
p . cargo ( " build " ) . cwd ( " foo " ) . run ( ) ;
2018-11-13 20:08:57 +00:00
// Attempt to fix code, but our shim will always fail the second compile
p . cargo ( " fix --allow-no-vcs --broken-code " )
2019-03-20 23:34:56 +00:00
. cwd ( " bar " )
2018-11-13 20:08:57 +00:00
. env ( " RUSTC " , p . root ( ) . join ( " foo/target/debug/foo " ) )
. with_status ( 101 )
2018-12-29 01:47:50 +00:00
. with_stderr_contains ( " [WARNING] failed to automatically apply fixes [..] " )
2018-11-13 20:08:57 +00:00
. run ( ) ;
2018-12-08 11:19:47 +00:00
assert_eq! (
p . read_file ( " bar/src/lib.rs " ) ,
" pub fn foo() { let x = 3; drop(x); } "
) ;
2018-11-13 20:08:57 +00:00
}
2018-12-13 22:29:26 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2018-12-13 22:29:26 +00:00
fn fix_with_common ( ) {
let p = project ( )
. file ( " src/lib.rs " , " " )
2019-01-27 13:39:49 +00:00
. file (
" tests/t1.rs " ,
" mod common; #[test] fn t1() { common::try(); } " ,
)
. file (
" tests/t2.rs " ,
" mod common; #[test] fn t2() { common::try(); } " ,
)
2018-12-13 22:29:26 +00:00
. file ( " tests/common/mod.rs " , " pub fn try() {} " )
. build ( ) ;
p . cargo ( " fix --edition --allow-no-vcs " ) . run ( ) ;
assert_eq! ( p . read_file ( " tests/common/mod.rs " ) , " pub fn r#try() {} " ) ;
}
2019-03-20 19:22:44 +00:00
2019-06-05 18:52:53 +00:00
#[ cargo_test ]
2019-03-20 19:22:44 +00:00
fn fix_in_existing_repo_weird_ignore ( ) {
// Check that ignore doesn't ignore the repo itself.
let p = git ::new ( " foo " , | project | {
project
. file ( " src/lib.rs " , " " )
2022-11-26 12:15:25 +00:00
. file ( " .gitignore " , " foo \n inner \n Cargo.lock \n target \n " )
2019-03-20 19:22:44 +00:00
. file ( " inner/file " , " " )
2019-08-13 05:25:36 +00:00
} ) ;
2019-03-20 19:22:44 +00:00
p . cargo ( " fix " ) . run ( ) ;
// This is questionable about whether it is the right behavior. It should
// probably be checking if any source file for the current project is
// ignored.
p . cargo ( " fix " )
2019-03-20 23:34:56 +00:00
. cwd ( " inner " )
2019-03-20 19:22:44 +00:00
. with_stderr_contains ( " [ERROR] no VCS found[..] " )
. with_status ( 101 )
. run ( ) ;
2019-03-20 23:34:56 +00:00
p . cargo ( " fix " ) . cwd ( " src " ) . run ( ) ;
2019-03-20 19:22:44 +00:00
}
2019-06-26 17:30:24 +00:00
2019-10-28 19:00:09 +00:00
#[ cargo_test ]
fn fix_color_message ( ) {
// Check that color appears in diagnostics.
let p = project ( )
. file ( " src/lib.rs " , " std::compile_error!{ \" color test \" } " )
. build ( ) ;
p . cargo ( " fix --allow-no-vcs --color=always " )
. with_stderr_contains ( " [..] \x1b [[..] " )
. with_status ( 101 )
. run ( ) ;
p . cargo ( " fix --allow-no-vcs --color=never " )
. with_stderr_contains ( " error: color test " )
. with_stderr_does_not_contain ( " [..] \x1b [[..] " )
. with_status ( 101 )
. run ( ) ;
}
2021-03-14 22:01:31 +00:00
#[ cargo_test ]
fn edition_v2_resolver_report ( ) {
// Show a report if the V2 resolver shows differences.
Package ::new ( " common " , " 1.0.0 " )
. feature ( " f1 " , & [ ] )
2021-08-17 23:41:53 +00:00
. feature ( " dev-feat " , & [ ] )
2021-07-02 20:01:58 +00:00
. add_dep ( Dependency ::new ( " opt_dep " , " 1.0 " ) . optional ( true ) )
2021-03-14 22:01:31 +00:00
. publish ( ) ;
2021-07-02 20:01:58 +00:00
Package ::new ( " opt_dep " , " 1.0.0 " ) . publish ( ) ;
2021-03-14 22:01:31 +00:00
Package ::new ( " bar " , " 1.0.0 " )
. add_dep (
Dependency ::new ( " common " , " 1.0 " )
. target ( " cfg(whatever) " )
. enable_features ( & [ " f1 " ] ) ,
)
. publish ( ) ;
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
edition = " 2018 "
[ dependencies ]
common = " 1.0 "
bar = " 1.0 "
2021-07-02 20:01:58 +00:00
[ build - dependencies ]
common = { version = " 1.0 " , features = [ " opt_dep " ] }
2021-08-17 23:41:53 +00:00
[ dev - dependencies ]
common = { version = " 1.0 " , features = [ " dev-feat " ] }
2021-03-14 22:01:31 +00:00
" #,
)
. file ( " src/lib.rs " , " " )
. build ( ) ;
p . cargo ( " fix --edition --allow-no-vcs " )
. with_stderr_unordered ( " \
[ UPDATING ] [ .. ]
[ DOWNLOADING ] crates .. .
[ DOWNLOADED ] common v1 . 0.0 [ .. ]
[ DOWNLOADED ] bar v1 . 0.0 [ .. ]
2021-07-02 20:01:58 +00:00
[ DOWNLOADED ] opt_dep v1 . 0.0 [ .. ]
2021-03-14 22:01:31 +00:00
note : Switching to Edition 2021 will enable the use of the version 2 feature resolver in Cargo .
2021-07-02 20:01:58 +00:00
This may cause some dependencies to be built with fewer features enabled than previously .
More information about the resolver changes may be found at https ://doc.rust-lang.org/nightly/edition-guide/rust-2021/default-cargo-resolver.html
When building the following dependencies , the given features will no longer be used :
2021-03-14 22:01:31 +00:00
2021-08-17 23:41:53 +00:00
common v1 . 0.0 removed features : dev - feat , f1 , opt_dep
common v1 . 0.0 ( as host dependency ) removed features : dev - feat , f1
The following differences only apply when building with dev - dependencies :
common v1 . 0.0 removed features : f1 , opt_dep
2021-03-14 22:01:31 +00:00
2021-07-02 20:01:58 +00:00
[ CHECKING ] opt_dep v1 . 0.0
2021-03-14 22:01:31 +00:00
[ CHECKING ] common v1 . 0.0
[ CHECKING ] bar v1 . 0.0
[ CHECKING ] foo v0 . 1.0 [ .. ]
[ MIGRATING ] src / lib . rs from 2018 edition to 2021
[ FINISHED ] [ .. ]
" )
. run ( ) ;
}
2021-06-10 01:13:56 +00:00
#[ cargo_test ]
fn rustfix_handles_multi_spans ( ) {
// Checks that rustfix handles a single diagnostic with multiple
// suggestion spans (non_fmt_panic in this case).
let p = project ( )
. file ( " Cargo.toml " , & basic_manifest ( " foo " , " 0.1.0 " ) )
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) {
panic! ( format! ( " hey " ) ) ;
}
" #,
)
. build ( ) ;
p . cargo ( " fix --allow-no-vcs " ) . run ( ) ;
assert! ( p . read_file ( " src/lib.rs " ) . contains ( r # "panic!("hey");"# ) ) ;
}
2021-06-07 20:21:36 +00:00
#[ cargo_test ]
fn fix_edition_2021 ( ) {
// Can migrate 2021, even when lints are allowed.
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
edition = " 2018 "
" #,
)
. file (
" src/lib.rs " ,
r #"
#![ allow(ellipsis_inclusive_range_patterns) ]
pub fn f ( ) -> bool {
let x = 123 ;
match x {
0 .. . 100 = > true ,
_ = > false ,
}
}
" #,
)
. build ( ) ;
p . cargo ( " fix --edition --allow-no-vcs " )
. with_stderr (
" \
[ CHECKING ] foo v0 . 1.0 [ .. ]
[ MIGRATING ] src / lib . rs from 2018 edition to 2021
[ FIXED ] src / lib . rs ( 1 fix )
[ FINISHED ] [ .. ]
" ,
)
. run ( ) ;
assert! ( p . read_file ( " src/lib.rs " ) . contains ( r # "0..=100 => true,"# ) ) ;
}
2021-07-11 01:47:52 +00:00
#[ cargo_test ]
fn fix_shared_cross_workspace ( ) {
// Fixing a file that is shared between multiple packages in the same workspace.
// Make sure two processes don't try to fix the same file at the same time.
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ workspace ]
members = [ " foo " , " bar " ]
" #,
)
. file ( " foo/Cargo.toml " , & basic_manifest ( " foo " , " 0.1.0 " ) )
. file ( " foo/src/lib.rs " , " pub mod shared; " )
// This will fix both unused and bare trait.
. file ( " foo/src/shared.rs " , " pub fn fixme(x: Box<&Fn() -> ()>) {} " )
. file ( " bar/Cargo.toml " , & basic_manifest ( " bar " , " 0.1.0 " ) )
. file (
" bar/src/lib.rs " ,
r #"
#[ path= " ../../foo/src/shared.rs " ]
pub mod shared ;
" #,
)
. build ( ) ;
// The output here can be either of these two, depending on who runs first:
// [FIXED] bar/src/../../foo/src/shared.rs (2 fixes)
// [FIXED] foo/src/shared.rs (2 fixes)
p . cargo ( " fix --allow-no-vcs " )
2024-02-01 03:47:27 +00:00
. env ( " __CARGO_FIX_YOLO " , " 1 " )
2021-07-11 01:47:52 +00:00
. with_stderr_unordered (
" \
[ CHECKING ] foo v0 . 1.0 [ .. ]
[ CHECKING ] bar v0 . 1.0 [ .. ]
[ FIXED ] [ .. ] foo / src / shared . rs ( 2 fixes )
[ FINISHED ] [ .. ]
" ,
)
. run ( ) ;
assert_match_exact (
" pub fn fixme(_x: Box<&dyn Fn() -> ()>) {} " ,
& p . read_file ( " foo/src/shared.rs " ) ,
) ;
}
2021-08-17 15:04:02 +00:00
#[ cargo_test ]
fn abnormal_exit ( ) {
// rustc fails unexpectedly after applying fixes, should show some error information.
//
2024-01-02 22:42:21 +00:00
// This works with a proc-macro that runs twice:
2021-08-17 15:04:02 +00:00
// - First run (collect diagnostics pass): writes a file, exits normally.
// - Second run (verify diagnostics work): it detects the presence of the
// file, removes the file, and aborts the process.
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
pm = { path = " pm " }
" #,
)
. file (
" src/lib.rs " ,
r #"
pub fn f ( ) {
let mut x = 1 ;
pm ::crashme! ( ) ;
}
" #,
)
. file (
" pm/Cargo.toml " ,
r #"
[ package ]
name = " pm "
version = " 0.1.0 "
edition = " 2018 "
[ lib ]
proc - macro = true
" #,
)
. file (
" pm/src/lib.rs " ,
r #"
use proc_macro ::TokenStream ;
#[ proc_macro ]
pub fn crashme ( _input : TokenStream ) -> TokenStream {
// Use a file to succeed on the first pass, and fail on the second.
let p = std ::env ::var_os ( " ONCE_PATH " ) . unwrap ( ) ;
let check_path = std ::path ::Path ::new ( & p ) ;
if check_path . exists ( ) {
eprintln! ( " I'm not a diagnostic. " ) ;
std ::fs ::remove_file ( check_path ) . unwrap ( ) ;
std ::process ::abort ( ) ;
} else {
std ::fs ::write ( check_path , " " ) . unwrap ( ) ;
" " . parse ( ) . unwrap ( )
}
}
" #,
)
. build ( ) ;
p . cargo ( " fix --lib --allow-no-vcs " )
. env (
" ONCE_PATH " ,
paths ::root ( ) . join ( " proc-macro-run-once " ) . to_str ( ) . unwrap ( ) ,
)
. with_stderr_contains (
" [WARNING] failed to automatically apply fixes suggested by rustc to crate `foo` " ,
)
. with_stderr_contains ( " I'm not a diagnostic. " )
// "signal: 6, SIGABRT: process abort signal" on some platforms
. with_stderr_contains ( " rustc exited abnormally: [..] " )
. with_stderr_contains ( " Original diagnostics will follow. " )
. run ( ) ;
}
2021-08-21 15:48:49 +00:00
#[ cargo_test ]
fn fix_with_run_cargo_in_proc_macros ( ) {
let p = project ( )
. file (
" Cargo.toml " ,
r #"
2021-08-21 15:54:59 +00:00
[ package ]
name = " foo "
version = " 0.1.0 "
edition = " 2018 "
2021-08-21 15:48:49 +00:00
2021-08-21 15:54:59 +00:00
[ lib ]
proc - macro = true
" #,
2021-08-21 15:48:49 +00:00
)
. file (
" src/lib.rs " ,
r #"
2021-08-21 15:54:59 +00:00
use proc_macro ::* ;
2022-11-22 01:05:21 +00:00
2021-08-21 15:54:59 +00:00
#[ proc_macro ]
pub fn foo ( _input : TokenStream ) -> TokenStream {
2021-08-26 21:06:32 +00:00
let output = std ::process ::Command ::new ( env! ( " CARGO " ) )
2021-08-21 15:54:59 +00:00
. args ( & [ " metadata " , " --format-version=1 " ] )
. output ( )
. unwrap ( ) ;
eprintln! ( " {} " , std ::str ::from_utf8 ( & output . stderr ) . unwrap ( ) ) ;
println! ( " {} " , std ::str ::from_utf8 ( & output . stdout ) . unwrap ( ) ) ;
" " . parse ( ) . unwrap ( )
2022-11-22 01:05:21 +00:00
}
2021-08-21 15:54:59 +00:00
" #,
2021-08-21 15:48:49 +00:00
)
. file (
" src/bin/main.rs " ,
r #"
2021-08-21 15:54:59 +00:00
use foo ::foo ;
2021-08-21 15:48:49 +00:00
2021-08-21 15:54:59 +00:00
fn main ( ) {
foo! ( " bar " )
}
" #,
2021-08-21 15:48:49 +00:00
)
. build ( ) ;
p . cargo ( " fix --allow-no-vcs " )
. with_stderr_does_not_contain ( " error: could not find .rs file in rustc args " )
. run ( ) ;
}
2021-08-27 16:26:11 +00:00
#[ cargo_test ]
fn non_edition_lint_migration ( ) {
// Migrating to a new edition where a non-edition lint causes problems.
let p = project ( )
. file ( " Cargo.toml " , & basic_manifest ( " foo " , " 0.1.0 " ) )
. file (
" src/lib.rs " ,
r #"
// This is only used in a test.
// To be correct, this should be gated on #[cfg(test)], but
// sometimes people don't do that. If the unused_imports
// lint removes this, then the unittest will fail to compile.
use std ::str ::from_utf8 ;
pub mod foo {
pub const FOO : & [ u8 ] = & [ 102 , 111 , 111 ] ;
}
#[ test ]
fn example ( ) {
assert_eq! (
from_utf8 ( ::foo ::FOO ) , Ok ( " foo " )
) ;
}
" #,
)
. build ( ) ;
// Check that it complains about an unused import.
p . cargo ( " check --lib " )
. with_stderr_contains ( " [..]unused_imports[..] " )
. with_stderr_contains ( " [..]std::str::from_utf8[..] " )
. run ( ) ;
2022-07-15 00:55:44 +00:00
p . cargo ( " fix --edition --allow-no-vcs " ) . run ( ) ;
2021-08-27 16:26:11 +00:00
let contents = p . read_file ( " src/lib.rs " ) ;
// Check it does not remove the "unused" import.
assert! ( contents . contains ( " use std::str::from_utf8; " ) ) ;
// Check that it made the edition migration.
assert! ( contents . contains ( " from_utf8(crate::foo::FOO) " ) ) ;
}
2021-09-22 14:33:24 +00:00
#[ cargo_test ]
fn fix_in_dependency ( ) {
2023-12-29 20:20:57 +00:00
// Tests what happens if rustc emits a suggestion to modify a file from a
// dependency in cargo's home directory. This should never happen, and
// indicates a bug in rustc. However, there are several known bugs in
// rustc where it does this (often involving macros), so `cargo fix` has a
// guard that says if the suggestion points to some location in CARGO_HOME
// to not apply it.
//
// See https://github.com/rust-lang/cargo/issues/9857 for some other
// examples.
//
// This test uses a simulated rustc which replays a suggestion via a JSON
// message that points into CARGO_HOME. This does not use the real rustc
// because as the bugs are fixed in the real rustc, that would cause this
// test to stop working.
2021-09-22 14:33:24 +00:00
Package ::new ( " bar " , " 1.0.0 " )
. file (
" src/lib.rs " ,
r #"
#[ macro_export ]
macro_rules ! m {
( $i :tt ) = > {
let $i = 1 ;
} ;
}
" #,
)
. publish ( ) ;
let p = project ( )
. file (
" Cargo.toml " ,
r #"
[ package ]
name = " foo "
version = " 0.1.0 "
[ dependencies ]
bar = " 1.0 "
" #,
)
. file (
" src/lib.rs " ,
r #"
pub fn foo ( ) {
bar ::m! ( abc ) ;
}
" #,
)
. build ( ) ;
2023-12-29 20:20:57 +00:00
p . cargo ( " fetch " ) . run ( ) ;
// The path in CARGO_HOME.
let bar_path = std ::fs ::read_dir ( paths ::home ( ) . join ( " .cargo/registry/src " ) )
. unwrap ( )
. next ( )
. unwrap ( )
. unwrap ( )
. path ( ) ;
// Since this is a substitution into a Rust string (representing a JSON
// string), deal with backslashes like on Windows.
let bar_path_str = bar_path . to_str ( ) . unwrap ( ) . replace ( " \\ " , " / " ) ;
// This is a fake rustc that will emit a JSON message when the `foo` crate
// builds that tells cargo to modify a file it shouldn't.
let rustc = project ( )
. at ( " rustc-replay " )
. file ( " Cargo.toml " , & basic_manifest ( " rustc-replay " , " 1.0.0 " ) )
. file ( " src/main.rs " ,
& r ##"
fn main ( ) {
let pkg_name = match std ::env ::var ( " CARGO_PKG_NAME " ) {
Ok ( pkg_name ) = > pkg_name ,
Err ( _ ) = > {
let r = std ::process ::Command ::new ( " rustc " )
. args ( std ::env ::args_os ( ) . skip ( 1 ) )
. status ( ) ;
std ::process ::exit ( r . unwrap ( ) . code ( ) . unwrap_or ( 2 ) ) ;
}
} ;
if pkg_name = = " foo " {
eprintln! ( " {} " , r #" {
" $message_type " : " diagnostic " ,
" message " : " unused variable: `abc` " ,
" code " :
{
" code " : " unused_variables " ,
" explanation " : null
} ,
" level " : " warning " ,
" spans " :
[
{
" file_name " : " __BAR_PATH__/bar-1.0.0/src/lib.rs " ,
" byte_start " : 127 ,
" byte_end " : 129 ,
" line_start " : 5 ,
" line_end " : 5 ,
" column_start " : 29 ,
" column_end " : 31 ,
" is_primary " : true ,
" text " :
[
{
" text " : " let $i = 1; " ,
" highlight_start " : 29 ,
" highlight_end " : 31
}
] ,
" label " : null ,
" suggested_replacement " : null ,
" suggestion_applicability " : null ,
" expansion " : null
}
] ,
" children " :
[
{
" message " : " `#[warn(unused_variables)]` on by default " ,
" code " : null ,
" level " : " note " ,
" spans " :
[ ] ,
" children " :
[ ] ,
" rendered " : null
} ,
{
" message " : " if this is intentional, prefix it with an underscore " ,
" code " : null ,
" level " : " help " ,
" spans " :
[
{
" file_name " : " __BAR_PATH__/bar-1.0.0/src/lib.rs " ,
" byte_start " : 127 ,
" byte_end " : 129 ,
" line_start " : 5 ,
" line_end " : 5 ,
" column_start " : 29 ,
" column_end " : 31 ,
" is_primary " : true ,
" text " :
[
{
" text " : " let $i = 1; " ,
" highlight_start " : 29 ,
" highlight_end " : 31
}
] ,
" label " : null ,
" suggested_replacement " : " _abc " ,
" suggestion_applicability " : " MachineApplicable " ,
" expansion " : null
}
] ,
" children " :
[ ] ,
" rendered " : null
}
] ,
" rendered " : " warning: unused variable: `abc` \n --> __BAR_PATH__/bar-1.0.0/src/lib.rs:5:29 \n | \n 5 | let $i = 1; \n | ^^ help: if this is intentional, prefix it with an underscore: `_abc` \n | \n = note: `#[warn(unused_variables)]` on by default \n \n "
} " #.replace( " \ n " , " " ));
}
}
" ##.replace( " __BAR_PATH__ " , &bar_path_str))
. build ( ) ;
rustc . cargo ( " build " ) . run ( ) ;
let rustc_bin = rustc . bin ( " rustc-replay " ) ;
2021-09-22 14:33:24 +00:00
2023-12-29 20:20:57 +00:00
// The output here should not say `Fixed`.
//
// It is OK to compare the full diagnostic output here because the text is
// hard-coded in rustc-replay. Normally tests should not be checking the
// compiler output.
p . cargo ( " fix --lib --allow-no-vcs " )
. env ( " RUSTC " , & rustc_bin )
. with_stderr ( " \
[ CHECKING ] bar v1 . 0.0
[ CHECKING ] foo v0 . 1.0 [ .. ]
warning : unused variable : ` abc `
- -> [ ROOT ] / home / . cargo / registry / src / [ .. ] / bar - 1. 0.0 / src / lib . rs :5 :29
|
5 | let $i = 1 ;
| ^ ^ help : if this is intentional , prefix it with an underscore : ` _abc `
|
= note : ` #[ warn(unused_variables) ] ` on by default
warning : ` foo ` ( lib ) generated 1 warning ( run ` cargo fix - - lib - p foo ` to apply 1 suggestion )
[ FINISHED ] [ .. ]
" )
2021-09-22 14:33:24 +00:00
. run ( ) ;
}