1+ //! Access common paths and manipulate the filesystem
2+
13use filetime:: FileTime ;
24
35use std:: cell:: RefCell ;
@@ -41,6 +43,9 @@ fn set_global_root(tmp_dir: Option<&'static str>) {
4143 }
4244}
4345
46+ /// Path to the parent directory of all test [`root`]s
47+ ///
48+ /// ex: `$CARGO_TARGET_TMPDIR/cit`
4449pub fn global_root ( ) -> PathBuf {
4550 let lock = GLOBAL_ROOT
4651 . get_or_init ( || Default :: default ( ) )
@@ -64,10 +69,12 @@ thread_local! {
6469 static TEST_ID : RefCell <Option <usize >> = RefCell :: new( None ) ;
6570}
6671
72+ /// See [`init_root`]
6773pub struct TestIdGuard {
6874 _private : ( ) ,
6975}
7076
77+ /// For test harnesses like [`crate::cargo_test`]
7178pub fn init_root ( tmp_dir : Option < & ' static str > ) -> TestIdGuard {
7279 static NEXT_ID : AtomicUsize = AtomicUsize :: new ( 0 ) ;
7380
@@ -90,6 +97,9 @@ impl Drop for TestIdGuard {
9097 }
9198}
9299
100+ /// Path to the test's filesystem scratchpad
101+ ///
102+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0`
93103pub fn root ( ) -> PathBuf {
94104 let id = TEST_ID . with ( |n| {
95105 n. borrow ( ) . expect (
@@ -103,17 +113,24 @@ pub fn root() -> PathBuf {
103113 root
104114}
105115
116+ /// Path to the current test's `$HOME`
117+ ///
118+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home`
106119pub fn home ( ) -> PathBuf {
107120 let mut path = root ( ) ;
108121 path. push ( "home" ) ;
109122 path. mkdir_p ( ) ;
110123 path
111124}
112125
126+ /// Path to the current test's `$CARGO_HOME`
127+ ///
128+ /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/home/.cargo`
113129pub fn cargo_home ( ) -> PathBuf {
114130 home ( ) . join ( ".cargo" )
115131}
116132
133+ /// Common path and file operations
117134pub trait CargoPathExt {
118135 fn to_url ( & self ) -> url:: Url ;
119136
@@ -275,18 +292,29 @@ where
275292
276293/// Get the filename for a library.
277294///
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`
279301///
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"`
284311pub fn get_lib_filename ( name : & str , kind : & str ) -> String {
285312 let prefix = get_lib_prefix ( kind) ;
286313 let extension = get_lib_extension ( kind) ;
287314 format ! ( "{}{}.{}" , prefix, name, extension)
288315}
289316
317+ /// See [`get_lib_filename`] for more details
290318pub fn get_lib_prefix ( kind : & str ) -> & str {
291319 match kind {
292320 "lib" | "rlib" => "lib" ,
@@ -301,6 +329,7 @@ pub fn get_lib_prefix(kind: &str) -> &str {
301329 }
302330}
303331
332+ /// See [`get_lib_filename`] for more details
304333pub fn get_lib_extension ( kind : & str ) -> & str {
305334 match kind {
306335 "lib" | "rlib" => "rlib" ,
@@ -324,7 +353,7 @@ pub fn get_lib_extension(kind: &str) -> &str {
324353 }
325354}
326355
327- /// Returns the sysroot as queried from rustc.
356+ /// Path to ` rustc`s sysroot
328357pub fn sysroot ( ) -> String {
329358 let output = Command :: new ( "rustc" )
330359 . arg ( "--print=sysroot" )
0 commit comments