@@ -7,6 +7,7 @@ use std::fs;
7
7
use std:: io:: { self , ErrorKind } ;
8
8
use std:: path:: { Path , PathBuf } ;
9
9
use std:: process:: Command ;
10
+ use std:: sync:: atomic:: { AtomicUsize , Ordering } ;
10
11
use std:: sync:: Mutex ;
11
12
12
13
static CARGO_INTEGRATION_TEST_DIR : & str = "cit" ;
@@ -50,20 +51,28 @@ pub fn global_root() -> PathBuf {
50
51
}
51
52
}
52
53
53
- // We need to give each test a unique id. The test name serve this
54
- // purpose. We are able to get the test name by having the `cargo-test-macro`
55
- // crate automatically insert an init function for each test that sets the
56
- // test name in a thread local variable.
54
+ // We need to give each test a unique id. The test name could serve this
55
+ // purpose, but the `test` crate doesn't have a way to obtain the current test
56
+ // name.[*] Instead, we used the `cargo-test-macro` crate to automatically
57
+ // insert an init function for each test that sets the test name in a thread
58
+ // local variable.
59
+ //
60
+ // [*] It does set the thread name, but only when running concurrently. If not
61
+ // running concurrently, all tests are run on the main thread.
57
62
thread_local ! {
58
- static TEST_NAME : RefCell <Option <PathBuf >> = RefCell :: new( None ) ;
63
+ static TEST_ID : RefCell <Option <usize >> = RefCell :: new( None ) ;
59
64
}
60
65
61
66
pub struct TestIdGuard {
62
67
_private : ( ) ,
63
68
}
64
69
65
- pub fn init_root ( tmp_dir : Option < & ' static str > , test_name : PathBuf ) -> TestIdGuard {
66
- TEST_NAME . with ( |n| * n. borrow_mut ( ) = Some ( test_name) ) ;
70
+ pub fn init_root ( tmp_dir : Option < & ' static str > ) -> TestIdGuard {
71
+ static NEXT_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
72
+
73
+ let id = NEXT_ID . fetch_add ( 1 , Ordering :: SeqCst ) ;
74
+ TEST_ID . with ( |n| * n. borrow_mut ( ) = Some ( id) ) ;
75
+
67
76
let guard = TestIdGuard { _private : ( ) } ;
68
77
69
78
set_global_root ( tmp_dir) ;
@@ -76,20 +85,20 @@ pub fn init_root(tmp_dir: Option<&'static str>, test_name: PathBuf) -> TestIdGua
76
85
77
86
impl Drop for TestIdGuard {
78
87
fn drop ( & mut self ) {
79
- TEST_NAME . with ( |n| * n. borrow_mut ( ) = None ) ;
88
+ TEST_ID . with ( |n| * n. borrow_mut ( ) = None ) ;
80
89
}
81
90
}
82
91
83
92
pub fn root ( ) -> PathBuf {
84
- let test_name = TEST_NAME . with ( |n| {
85
- n. borrow ( ) . clone ( ) . expect (
93
+ let id = TEST_ID . with ( |n| {
94
+ n. borrow ( ) . expect (
86
95
"Tests must use the `#[cargo_test]` attribute in \
87
96
order to be able to use the crate root.",
88
97
)
89
98
} ) ;
90
99
91
100
let mut root = global_root ( ) ;
92
- root. push ( & test_name ) ;
101
+ root. push ( & format ! ( "t{}" , id ) ) ;
93
102
root
94
103
}
95
104
@@ -336,26 +345,3 @@ pub fn windows_reserved_names_are_allowed() -> bool {
336
345
true
337
346
}
338
347
}
339
-
340
- /// This takes the test location (std::file!() should be passed) and the test name
341
- /// and outputs the location the test should be places in, inside of `target/tmp/cit`
342
- ///
343
- /// `path: tests/testsuite/workspaces.rs`
344
- /// `name: `workspace_in_git
345
- /// `output: "testsuite/workspaces/workspace_in_git`
346
- pub fn test_dir ( path : & str , name : & str ) -> std:: path:: PathBuf {
347
- let test_dir: std:: path:: PathBuf = std:: path:: PathBuf :: from ( path)
348
- . components ( )
349
- // Trim .rs from any files
350
- . map ( |c| c. as_os_str ( ) . to_str ( ) . unwrap ( ) . trim_end_matches ( ".rs" ) )
351
- // We only want to take once we have reached `tests` or `src`. This helps when in a
352
- // workspace: `workspace/more/src/...` would result in `src/...`
353
- . skip_while ( |c| c != & "tests" && c != & "src" )
354
- // We want to skip "tests" since it is taken in `skip_while`.
355
- // "src" is fine since you could have test in "src" named the same as one in "tests"
356
- // Skip "mod" since `snapbox` tests have a folder per test not a file and the files
357
- // are named "mod.rs"
358
- . filter ( |c| c != & "tests" && c != & "mod" )
359
- . collect ( ) ;
360
- test_dir. join ( name)
361
- }
0 commit comments