1
+ //! Access common paths and manipulate the filesystem
2
+
1
3
use filetime:: FileTime ;
2
4
3
5
use std:: cell:: RefCell ;
@@ -41,6 +43,9 @@ fn set_global_root(tmp_dir: Option<&'static str>) {
41
43
}
42
44
}
43
45
46
+ /// Path to the parent directory of all test [`root`]s
47
+ ///
48
+ /// ex: `$CARGO_TARGET_TMPDIR/cit`
44
49
pub fn global_root ( ) -> PathBuf {
45
50
let lock = GLOBAL_ROOT
46
51
. get_or_init ( || Default :: default ( ) )
@@ -64,10 +69,12 @@ thread_local! {
64
69
static TEST_ID : RefCell <Option <usize >> = RefCell :: new( None ) ;
65
70
}
66
71
72
+ /// See [`init_root`]
67
73
pub struct TestIdGuard {
68
74
_private : ( ) ,
69
75
}
70
76
77
+ /// For test harnesses like [`crate::cargo_test`]
71
78
pub fn init_root ( tmp_dir : Option < & ' static str > ) -> TestIdGuard {
72
79
static NEXT_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
73
80
@@ -90,6 +97,9 @@ impl Drop for TestIdGuard {
90
97
}
91
98
}
92
99
100
+ /// Path to the test's filesystem scratchpad
101
+ ///
102
+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0`
93
103
pub fn root ( ) -> PathBuf {
94
104
let id = TEST_ID . with ( |n| {
95
105
n. borrow ( ) . expect (
@@ -103,17 +113,24 @@ pub fn root() -> PathBuf {
103
113
root
104
114
}
105
115
116
+ /// Path to the current test's `$HOME`
117
+ ///
118
+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home`
106
119
pub fn home ( ) -> PathBuf {
107
120
let mut path = root ( ) ;
108
121
path. push ( "home" ) ;
109
122
path. mkdir_p ( ) ;
110
123
path
111
124
}
112
125
126
+ /// Path to the current test's `$CARGO_HOME`
127
+ ///
128
+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo`
113
129
pub fn cargo_home ( ) -> PathBuf {
114
130
home ( ) . join ( ".cargo" )
115
131
}
116
132
133
+ /// Common path and file operations
117
134
pub trait CargoPathExt {
118
135
fn to_url ( & self ) -> url:: Url ;
119
136
@@ -275,18 +292,29 @@ where
275
292
276
293
/// Get the filename for a library.
277
294
///
278
- /// `kind` should be one of: "lib", "rlib", "staticlib", "dylib", "proc-macro"
295
+ /// `kind` should be one of:
296
+ /// - `lib`
297
+ /// - `rlib`
298
+ /// - `staticlib`
299
+ /// - `dylib`
300
+ /// - `proc-macro`
279
301
///
280
- /// For example, dynamic library named "foo" would return:
281
- /// - macOS: "libfoo.dylib"
282
- /// - Windows: "foo.dll"
283
- /// - Unix: "libfoo.so"
302
+ /// # Examples
303
+ /// ```
304
+ /// # use cargo_test_support::paths::get_lib_filename;
305
+ /// get_lib_filename("foo", "dylib");
306
+ /// ```
307
+ /// would return:
308
+ /// - macOS: `"libfoo.dylib"`
309
+ /// - Windows: `"foo.dll"`
310
+ /// - Unix: `"libfoo.so"`
284
311
pub fn get_lib_filename ( name : & str , kind : & str ) -> String {
285
312
let prefix = get_lib_prefix ( kind) ;
286
313
let extension = get_lib_extension ( kind) ;
287
314
format ! ( "{}{}.{}" , prefix, name, extension)
288
315
}
289
316
317
+ /// See [`get_lib_filename`] for more details
290
318
pub fn get_lib_prefix ( kind : & str ) -> & str {
291
319
match kind {
292
320
"lib" | "rlib" => "lib" ,
@@ -301,6 +329,7 @@ pub fn get_lib_prefix(kind: &str) -> &str {
301
329
}
302
330
}
303
331
332
+ /// See [`get_lib_filename`] for more details
304
333
pub fn get_lib_extension ( kind : & str ) -> & str {
305
334
match kind {
306
335
"lib" | "rlib" => "rlib" ,
@@ -324,7 +353,7 @@ pub fn get_lib_extension(kind: &str) -> &str {
324
353
}
325
354
}
326
355
327
- /// Returns the sysroot as queried from rustc.
356
+ /// Path to ` rustc`s sysroot
328
357
pub fn sysroot ( ) -> String {
329
358
let output = Command :: new ( "rustc" )
330
359
. arg ( "--print=sysroot" )
0 commit comments