cargo_test_support/
lib.rs

1//! # Cargo test support.
2//!
3//! See <https://rust-lang.github.io/cargo/contrib/> for a guide on writing tests.
4//!
5//! There are two places you can find API documentation
6//!
7//! - <https://docs.rs/cargo-test-support>:
8//!   targeted at external tool developers testing cargo-related code
9//!   - Released with every rustc release
10//! - <https://doc.rust-lang.org/nightly/nightly-rustc/cargo_test_support>:
11//!   targeted at cargo contributors
12//!   - Updated on each update of the `cargo` submodule in `rust-lang/rust`
13//!
14//! > This crate is maintained by the Cargo team, primarily for use by Cargo
15//! > and not intended for external use. This
16//! > crate may make major changes to its APIs or be deprecated without warning.
17//!
18//! # Example
19//!
20//! ```rust,no_run
21//! use cargo_test_support::prelude::*;
22//! use cargo_test_support::str;
23//! use cargo_test_support::project;
24//!
25//! #[cargo_test]
26//! fn some_test() {
27//!     let p = project()
28//!         .file("src/main.rs", r#"fn main() { println!("hi!"); }"#)
29//!         .build();
30//!
31//!     p.cargo("run --bin foo")
32//!         .with_stderr_data(str![[r#"
33//! [COMPILING] foo [..]
34//! [FINISHED] [..]
35//! [RUNNING] `target/debug/foo`
36//! "#]])
37//!         .with_stdout_data(str![["hi!"]])
38//!         .run();
39//! }
40//! ```
41
42#![allow(clippy::disallowed_methods)]
43#![allow(clippy::print_stderr)]
44#![allow(clippy::print_stdout)]
45
46use std::env;
47use std::ffi::OsStr;
48use std::fmt::Write;
49use std::fs;
50use std::os;
51use std::path::{Path, PathBuf};
52use std::process::{Command, Output};
53use std::sync::OnceLock;
54use std::thread::JoinHandle;
55use std::time::{self, Duration};
56
57use anyhow::{bail, Result};
58use cargo_util::{is_ci, ProcessError};
59use snapbox::IntoData as _;
60use url::Url;
61
62use self::paths::CargoPathExt;
63
64/// Unwrap a `Result` with a useful panic message
65///
66/// # Example
67///
68/// ```rust
69/// use cargo_test_support::t;
70/// t!(std::fs::read_to_string("Cargo.toml"));
71/// ```
72#[macro_export]
73macro_rules! t {
74    ($e:expr) => {
75        match $e {
76            Ok(e) => e,
77            Err(e) => $crate::panic_error(&format!("failed running {}", stringify!($e)), e),
78        }
79    };
80}
81
82pub use cargo_util::ProcessBuilder;
83pub use snapbox::file;
84pub use snapbox::str;
85pub use snapbox::utils::current_dir;
86
87/// `panic!`, reporting the specified error , see also [`t!`]
88#[track_caller]
89pub fn panic_error(what: &str, err: impl Into<anyhow::Error>) -> ! {
90    let err = err.into();
91    pe(what, err);
92    #[track_caller]
93    fn pe(what: &str, err: anyhow::Error) -> ! {
94        let mut result = format!("{}\nerror: {}", what, err);
95        for cause in err.chain().skip(1) {
96            let _ = writeln!(result, "\nCaused by:");
97            let _ = write!(result, "{}", cause);
98        }
99        panic!("\n{}", result);
100    }
101}
102
103pub use cargo_test_macro::cargo_test;
104
105pub mod compare;
106pub mod containers;
107pub mod cross_compile;
108pub mod git;
109pub mod install;
110pub mod paths;
111pub mod publish;
112pub mod registry;
113pub mod tools;
114
115pub mod prelude {
116    pub use crate::cargo_test;
117    pub use crate::paths::CargoPathExt;
118    pub use crate::ArgLineCommandExt;
119    pub use crate::CargoCommandExt;
120    pub use crate::ChannelChangerCommandExt;
121    pub use crate::TestEnvCommandExt;
122    pub use snapbox::IntoData;
123}
124
125/*
126 *
127 * ===== Builders =====
128 *
129 */
130
131#[derive(PartialEq, Clone)]
132struct FileBuilder {
133    path: PathBuf,
134    body: String,
135    executable: bool,
136}
137
138impl FileBuilder {
139    pub fn new(path: PathBuf, body: &str, executable: bool) -> FileBuilder {
140        FileBuilder {
141            path,
142            body: body.to_string(),
143            executable: executable,
144        }
145    }
146
147    fn mk(&mut self) {
148        if self.executable {
149            let mut path = self.path.clone().into_os_string();
150            write!(path, "{}", env::consts::EXE_SUFFIX).unwrap();
151            self.path = path.into();
152        }
153
154        self.dirname().mkdir_p();
155        fs::write(&self.path, &self.body)
156            .unwrap_or_else(|e| panic!("could not create file {}: {}", self.path.display(), e));
157
158        #[cfg(unix)]
159        if self.executable {
160            use std::os::unix::fs::PermissionsExt;
161
162            let mut perms = fs::metadata(&self.path).unwrap().permissions();
163            let mode = perms.mode();
164            perms.set_mode(mode | 0o111);
165            fs::set_permissions(&self.path, perms).unwrap();
166        }
167    }
168
169    fn dirname(&self) -> &Path {
170        self.path.parent().unwrap()
171    }
172}
173
174#[derive(PartialEq, Clone)]
175struct SymlinkBuilder {
176    dst: PathBuf,
177    src: PathBuf,
178    src_is_dir: bool,
179}
180
181impl SymlinkBuilder {
182    pub fn new(dst: PathBuf, src: PathBuf) -> SymlinkBuilder {
183        SymlinkBuilder {
184            dst,
185            src,
186            src_is_dir: false,
187        }
188    }
189
190    pub fn new_dir(dst: PathBuf, src: PathBuf) -> SymlinkBuilder {
191        SymlinkBuilder {
192            dst,
193            src,
194            src_is_dir: true,
195        }
196    }
197
198    #[cfg(unix)]
199    fn mk(&self) {
200        self.dirname().mkdir_p();
201        t!(os::unix::fs::symlink(&self.dst, &self.src));
202    }
203
204    #[cfg(windows)]
205    fn mk(&mut self) {
206        self.dirname().mkdir_p();
207        if self.src_is_dir {
208            t!(os::windows::fs::symlink_dir(&self.dst, &self.src));
209        } else {
210            if let Some(ext) = self.dst.extension() {
211                if ext == env::consts::EXE_EXTENSION {
212                    self.src.set_extension(ext);
213                }
214            }
215            t!(os::windows::fs::symlink_file(&self.dst, &self.src));
216        }
217    }
218
219    fn dirname(&self) -> &Path {
220        self.src.parent().unwrap()
221    }
222}
223
224/// A cargo project to run tests against.
225///
226/// See [`ProjectBuilder`] or [`Project::from_template`] to get started.
227pub struct Project {
228    root: PathBuf,
229}
230
231/// Create a project to run tests against
232///
233/// - Creates a [`basic_manifest`] if one isn't supplied
234///
235/// To get started, see:
236/// - [`project`]
237/// - [`project_in`]
238/// - [`project_in_home`]
239/// - [`Project::from_template`]
240#[must_use]
241pub struct ProjectBuilder {
242    root: Project,
243    files: Vec<FileBuilder>,
244    symlinks: Vec<SymlinkBuilder>,
245    no_manifest: bool,
246}
247
248impl ProjectBuilder {
249    /// Root of the project
250    ///
251    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo`
252    pub fn root(&self) -> PathBuf {
253        self.root.root()
254    }
255
256    /// Project's debug dir
257    ///
258    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/debug`
259    pub fn target_debug_dir(&self) -> PathBuf {
260        self.root.target_debug_dir()
261    }
262
263    /// Create project in `root`
264    pub fn new(root: PathBuf) -> ProjectBuilder {
265        ProjectBuilder {
266            root: Project { root },
267            files: vec![],
268            symlinks: vec![],
269            no_manifest: false,
270        }
271    }
272
273    /// Create project, relative to [`paths::root`]
274    pub fn at<P: AsRef<Path>>(mut self, path: P) -> Self {
275        self.root = Project {
276            root: paths::root().join(path),
277        };
278        self
279    }
280
281    /// Adds a file to the project.
282    pub fn file<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
283        self._file(path.as_ref(), body, false);
284        self
285    }
286
287    /// Adds an executable file to the project.
288    pub fn executable<B: AsRef<Path>>(mut self, path: B, body: &str) -> Self {
289        self._file(path.as_ref(), body, true);
290        self
291    }
292
293    fn _file(&mut self, path: &Path, body: &str, executable: bool) {
294        self.files.push(FileBuilder::new(
295            self.root.root().join(path),
296            body,
297            executable,
298        ));
299    }
300
301    /// Adds a symlink to a file to the project.
302    pub fn symlink(mut self, dst: impl AsRef<Path>, src: impl AsRef<Path>) -> Self {
303        self.symlinks.push(SymlinkBuilder::new(
304            self.root.root().join(dst),
305            self.root.root().join(src),
306        ));
307        self
308    }
309
310    /// Create a symlink to a directory
311    pub fn symlink_dir(mut self, dst: impl AsRef<Path>, src: impl AsRef<Path>) -> Self {
312        self.symlinks.push(SymlinkBuilder::new_dir(
313            self.root.root().join(dst),
314            self.root.root().join(src),
315        ));
316        self
317    }
318
319    pub fn no_manifest(mut self) -> Self {
320        self.no_manifest = true;
321        self
322    }
323
324    /// Creates the project.
325    pub fn build(mut self) -> Project {
326        // First, clean the directory if it already exists
327        self.rm_root();
328
329        // Create the empty directory
330        self.root.root().mkdir_p();
331
332        let manifest_path = self.root.root().join("Cargo.toml");
333        if !self.no_manifest && self.files.iter().all(|fb| fb.path != manifest_path) {
334            self._file(
335                Path::new("Cargo.toml"),
336                &basic_manifest("foo", "0.0.1"),
337                false,
338            )
339        }
340
341        let past = time::SystemTime::now() - Duration::new(1, 0);
342        let ftime = filetime::FileTime::from_system_time(past);
343
344        for file in self.files.iter_mut() {
345            file.mk();
346            if is_coarse_mtime() {
347                // Place the entire project 1 second in the past to ensure
348                // that if cargo is called multiple times, the 2nd call will
349                // see targets as "fresh". Without this, if cargo finishes in
350                // under 1 second, the second call will see the mtime of
351                // source == mtime of output and consider it dirty.
352                filetime::set_file_times(&file.path, ftime, ftime).unwrap();
353            }
354        }
355
356        for symlink in self.symlinks.iter_mut() {
357            symlink.mk();
358        }
359
360        let ProjectBuilder { root, .. } = self;
361        root
362    }
363
364    fn rm_root(&self) {
365        self.root.root().rm_rf()
366    }
367}
368
369impl Project {
370    /// Copy the test project from a fixed state
371    pub fn from_template(template_path: impl AsRef<Path>) -> Self {
372        let root = paths::root();
373        let project_root = root.join("case");
374        snapbox::dir::copy_template(template_path.as_ref(), &project_root).unwrap();
375        Self { root: project_root }
376    }
377
378    /// Root of the project
379    ///
380    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo`
381    pub fn root(&self) -> PathBuf {
382        self.root.clone()
383    }
384
385    /// Project's target dir
386    ///
387    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target`
388    pub fn build_dir(&self) -> PathBuf {
389        self.root().join("target")
390    }
391
392    /// Project's debug dir
393    ///
394    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/debug`
395    pub fn target_debug_dir(&self) -> PathBuf {
396        self.build_dir().join("debug")
397    }
398
399    /// File url for root
400    ///
401    /// ex: `file://$CARGO_TARGET_TMPDIR/cit/t0/foo`
402    pub fn url(&self) -> Url {
403        use paths::CargoPathExt;
404        self.root().to_url()
405    }
406
407    /// Path to an example built as a library.
408    ///
409    /// `kind` should be one of: "lib", "rlib", "staticlib", "dylib", "proc-macro"
410    ///
411    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/debug/examples/libex.rlib`
412    pub fn example_lib(&self, name: &str, kind: &str) -> PathBuf {
413        self.target_debug_dir()
414            .join("examples")
415            .join(paths::get_lib_filename(name, kind))
416    }
417
418    /// Path to a debug binary.
419    ///
420    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/debug/foo`
421    pub fn bin(&self, b: &str) -> PathBuf {
422        self.build_dir()
423            .join("debug")
424            .join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
425    }
426
427    /// Path to a release binary.
428    ///
429    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/release/foo`
430    pub fn release_bin(&self, b: &str) -> PathBuf {
431        self.build_dir()
432            .join("release")
433            .join(&format!("{}{}", b, env::consts::EXE_SUFFIX))
434    }
435
436    /// Path to a debug binary for a specific target triple.
437    ///
438    /// ex: `$CARGO_TARGET_TMPDIR/cit/t0/foo/target/i686-apple-darwin/debug/foo`
439    pub fn target_bin(&self, target: &str, b: &str) -> PathBuf {
440        self.build_dir().join(target).join("debug").join(&format!(
441            "{}{}",
442            b,
443            env::consts::EXE_SUFFIX
444        ))
445    }
446
447    /// Returns an iterator of paths within [`Project::root`] matching the glob pattern
448    pub fn glob<P: AsRef<Path>>(&self, pattern: P) -> glob::Paths {
449        let pattern = self.root().join(pattern);
450        glob::glob(pattern.to_str().expect("failed to convert pattern to str"))
451            .expect("failed to glob")
452    }
453
454    /// Overwrite a file with new content
455    ///
456    // # Example:
457    ///
458    /// ```no_run
459    /// # let p = cargo_test_support::project().build();
460    /// p.change_file("src/lib.rs", "fn new_fn() {}");
461    /// ```
462    pub fn change_file(&self, path: impl AsRef<Path>, body: &str) {
463        FileBuilder::new(self.root().join(path), body, false).mk()
464    }
465
466    /// Creates a `ProcessBuilder` to run a program in the project
467    /// and wrap it in an Execs to assert on the execution.
468    ///
469    /// # Example:
470    ///
471    /// ```no_run
472    /// # use cargo_test_support::str;
473    /// # let p = cargo_test_support::project().build();
474    /// p.process(&p.bin("foo"))
475    ///     .with_stdout_data(str!["bar\n"])
476    ///     .run();
477    /// ```
478    pub fn process<T: AsRef<OsStr>>(&self, program: T) -> Execs {
479        let mut p = process(program);
480        p.cwd(self.root());
481        execs().with_process_builder(p)
482    }
483
484    /// Creates a `ProcessBuilder` to run cargo.
485    ///
486    /// Arguments can be separated by spaces.
487    ///
488    /// For `cargo run`, see [`Project::rename_run`].
489    ///
490    /// # Example:
491    ///
492    /// ```no_run
493    /// # let p = cargo_test_support::project().build();
494    /// p.cargo("build --bin foo").run();
495    /// ```
496    pub fn cargo(&self, cmd: &str) -> Execs {
497        let cargo = cargo_exe();
498        let mut execs = self.process(&cargo);
499        if let Some(ref mut p) = execs.process_builder {
500            p.env("CARGO", cargo);
501            p.arg_line(cmd);
502        }
503        execs
504    }
505
506    /// Safely run a process after `cargo build`.
507    ///
508    /// Windows has a problem where a process cannot be reliably
509    /// be replaced, removed, or renamed immediately after executing it.
510    /// The action may fail (with errors like Access is denied), or
511    /// it may succeed, but future attempts to use the same filename
512    /// will fail with "Already Exists".
513    ///
514    /// If you have a test that needs to do `cargo run` multiple
515    /// times, you should instead use `cargo build` and use this
516    /// method to run the executable. Each time you call this,
517    /// use a new name for `dst`.
518    /// See rust-lang/cargo#5481.
519    pub fn rename_run(&self, src: &str, dst: &str) -> Execs {
520        let src = self.bin(src);
521        let dst = self.bin(dst);
522        fs::rename(&src, &dst)
523            .unwrap_or_else(|e| panic!("Failed to rename `{:?}` to `{:?}`: {}", src, dst, e));
524        self.process(dst)
525    }
526
527    /// Returns the contents of `Cargo.lock`.
528    pub fn read_lockfile(&self) -> String {
529        self.read_file("Cargo.lock")
530    }
531
532    /// Returns the contents of a path in the project root
533    pub fn read_file(&self, path: impl AsRef<Path>) -> String {
534        let full = self.root().join(path);
535        fs::read_to_string(&full)
536            .unwrap_or_else(|e| panic!("could not read file {}: {}", full.display(), e))
537    }
538
539    /// Modifies `Cargo.toml` to remove all commented lines.
540    pub fn uncomment_root_manifest(&self) {
541        let contents = self.read_file("Cargo.toml").replace("#", "");
542        fs::write(self.root().join("Cargo.toml"), contents).unwrap();
543    }
544
545    pub fn symlink(&self, src: impl AsRef<Path>, dst: impl AsRef<Path>) {
546        let src = self.root().join(src.as_ref());
547        let dst = self.root().join(dst.as_ref());
548        #[cfg(unix)]
549        {
550            if let Err(e) = os::unix::fs::symlink(&src, &dst) {
551                panic!("failed to symlink {:?} to {:?}: {:?}", src, dst, e);
552            }
553        }
554        #[cfg(windows)]
555        {
556            if src.is_dir() {
557                if let Err(e) = os::windows::fs::symlink_dir(&src, &dst) {
558                    panic!("failed to symlink {:?} to {:?}: {:?}", src, dst, e);
559                }
560            } else {
561                if let Err(e) = os::windows::fs::symlink_file(&src, &dst) {
562                    panic!("failed to symlink {:?} to {:?}: {:?}", src, dst, e);
563                }
564            }
565        }
566    }
567}
568
569/// Generates a project layout, see [`ProjectBuilder`]
570pub fn project() -> ProjectBuilder {
571    ProjectBuilder::new(paths::root().join("foo"))
572}
573
574/// Generates a project layout in given directory, see [`ProjectBuilder`]
575pub fn project_in(dir: impl AsRef<Path>) -> ProjectBuilder {
576    ProjectBuilder::new(paths::root().join(dir).join("foo"))
577}
578
579/// Generates a project layout inside our fake home dir, see [`ProjectBuilder`]
580pub fn project_in_home(name: impl AsRef<Path>) -> ProjectBuilder {
581    ProjectBuilder::new(paths::home().join(name))
582}
583
584// === Helpers ===
585
586/// Generate a `main.rs` printing the specified text
587///
588/// ```rust
589/// # use cargo_test_support::main_file;
590/// # mod dep {
591/// #     fn bar() -> &'static str {
592/// #         "world"
593/// #     }
594/// # }
595/// main_file(
596///     r#""hello {}", dep::bar()"#,
597///     &[]
598/// );
599/// ```
600pub fn main_file(println: &str, externed_deps: &[&str]) -> String {
601    let mut buf = String::new();
602
603    for dep in externed_deps.iter() {
604        buf.push_str(&format!("extern crate {};\n", dep));
605    }
606
607    buf.push_str("fn main() { println!(");
608    buf.push_str(println);
609    buf.push_str("); }\n");
610
611    buf
612}
613
614/// Path to the cargo binary
615pub fn cargo_exe() -> PathBuf {
616    snapbox::cmd::cargo_bin("cargo")
617}
618
619/// This is the raw output from the process.
620///
621/// This is similar to `std::process::Output`, however the `status` is
622/// translated to the raw `code`. This is necessary because `ProcessError`
623/// does not have access to the raw `ExitStatus` because `ProcessError` needs
624/// to be serializable (for the Rustc cache), and `ExitStatus` does not
625/// provide a constructor.
626pub struct RawOutput {
627    pub code: Option<i32>,
628    pub stdout: Vec<u8>,
629    pub stderr: Vec<u8>,
630}
631
632/// Run and verify a [`ProcessBuilder`]
633///
634/// Construct with
635/// - [`execs`]
636/// - [`cargo_process`]
637/// - [`Project`] methods
638#[must_use]
639#[derive(Clone)]
640pub struct Execs {
641    ran: bool,
642    process_builder: Option<ProcessBuilder>,
643    expect_stdin: Option<String>,
644    expect_exit_code: Option<i32>,
645    expect_stdout_data: Option<snapbox::Data>,
646    expect_stderr_data: Option<snapbox::Data>,
647    expect_stdout_contains: Vec<String>,
648    expect_stderr_contains: Vec<String>,
649    expect_stdout_not_contains: Vec<String>,
650    expect_stderr_not_contains: Vec<String>,
651    expect_stderr_with_without: Vec<(Vec<String>, Vec<String>)>,
652    stream_output: bool,
653    assert: snapbox::Assert,
654}
655
656impl Execs {
657    pub fn with_process_builder(mut self, p: ProcessBuilder) -> Execs {
658        self.process_builder = Some(p);
659        self
660    }
661}
662
663/// # Configure assertions
664impl Execs {
665    /// Verifies that stdout is equal to the given lines.
666    ///
667    /// See [`compare::assert_e2e`] for assertion details.
668    ///
669    /// <div class="warning">
670    ///
671    /// Prefer passing in [`str!`] for `expected` to get snapshot updating.
672    ///
673    /// If `format!` is needed for content that changes from run to run that you don't care about,
674    /// consider whether you could have [`compare::assert_e2e`] redact the content.
675    /// If nothing else, a wildcard (`[..]`, `...`) may be useful.
676    ///
677    /// However, `""` may be preferred for intentionally empty output so people don't accidentally
678    /// bless a change.
679    ///
680    /// </div>
681    ///
682    /// # Examples
683    ///
684    /// ```no_run
685    /// use cargo_test_support::prelude::*;
686    /// use cargo_test_support::str;
687    /// use cargo_test_support::execs;
688    ///
689    /// execs().with_stdout_data(str![r#"
690    /// Hello world!
691    /// "#]);
692    /// ```
693    ///
694    /// Non-deterministic compiler output
695    /// ```no_run
696    /// use cargo_test_support::prelude::*;
697    /// use cargo_test_support::str;
698    /// use cargo_test_support::execs;
699    ///
700    /// execs().with_stdout_data(str![r#"
701    /// [COMPILING] foo
702    /// [COMPILING] bar
703    /// "#].unordered());
704    /// ```
705    ///
706    /// jsonlines
707    /// ```no_run
708    /// use cargo_test_support::prelude::*;
709    /// use cargo_test_support::str;
710    /// use cargo_test_support::execs;
711    ///
712    /// execs().with_stdout_data(str![r#"
713    /// [
714    ///   {},
715    ///   {}
716    /// ]
717    /// "#].is_json().against_jsonlines());
718    /// ```
719    pub fn with_stdout_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
720        self.expect_stdout_data = Some(expected.into_data());
721        self
722    }
723
724    /// Verifies that stderr is equal to the given lines.
725    ///
726    /// See [`compare::assert_e2e`] for assertion details.
727    ///
728    /// <div class="warning">
729    ///
730    /// Prefer passing in [`str!`] for `expected` to get snapshot updating.
731    ///
732    /// If `format!` is needed for content that changes from run to run that you don't care about,
733    /// consider whether you could have [`compare::assert_e2e`] redact the content.
734    /// If nothing else, a wildcard (`[..]`, `...`) may be useful.
735    ///
736    /// However, `""` may be preferred for intentionally empty output so people don't accidentally
737    /// bless a change.
738    ///
739    /// </div>
740    ///
741    /// # Examples
742    ///
743    /// ```no_run
744    /// use cargo_test_support::prelude::*;
745    /// use cargo_test_support::str;
746    /// use cargo_test_support::execs;
747    ///
748    /// execs().with_stderr_data(str![r#"
749    /// Hello world!
750    /// "#]);
751    /// ```
752    ///
753    /// Non-deterministic compiler output
754    /// ```no_run
755    /// use cargo_test_support::prelude::*;
756    /// use cargo_test_support::str;
757    /// use cargo_test_support::execs;
758    ///
759    /// execs().with_stderr_data(str![r#"
760    /// [COMPILING] foo
761    /// [COMPILING] bar
762    /// "#].unordered());
763    /// ```
764    ///
765    /// jsonlines
766    /// ```no_run
767    /// use cargo_test_support::prelude::*;
768    /// use cargo_test_support::str;
769    /// use cargo_test_support::execs;
770    ///
771    /// execs().with_stderr_data(str![r#"
772    /// [
773    ///   {},
774    ///   {}
775    /// ]
776    /// "#].is_json().against_jsonlines());
777    /// ```
778    pub fn with_stderr_data(&mut self, expected: impl snapbox::IntoData) -> &mut Self {
779        self.expect_stderr_data = Some(expected.into_data());
780        self
781    }
782
783    /// Writes the given lines to stdin.
784    pub fn with_stdin<S: ToString>(&mut self, expected: S) -> &mut Self {
785        self.expect_stdin = Some(expected.to_string());
786        self
787    }
788
789    /// Verifies the exit code from the process.
790    ///
791    /// This is not necessary if the expected exit code is `0`.
792    pub fn with_status(&mut self, expected: i32) -> &mut Self {
793        self.expect_exit_code = Some(expected);
794        self
795    }
796
797    /// Removes exit code check for the process.
798    ///
799    /// By default, the expected exit code is `0`.
800    pub fn without_status(&mut self) -> &mut Self {
801        self.expect_exit_code = None;
802        self
803    }
804
805    /// Verifies that stdout contains the given contiguous lines somewhere in
806    /// its output.
807    ///
808    /// See [`compare`] for supported patterns.
809    ///
810    /// <div class="warning">
811    ///
812    /// Prefer [`Execs::with_stdout_data`] where possible.
813    /// - `expected` cannot be snapshotted
814    /// - `expected` can end up being ambiguous, causing the assertion to succeed when it should fail
815    ///
816    /// </div>
817    pub fn with_stdout_contains<S: ToString>(&mut self, expected: S) -> &mut Self {
818        self.expect_stdout_contains.push(expected.to_string());
819        self
820    }
821
822    /// Verifies that stderr contains the given contiguous lines somewhere in
823    /// its output.
824    ///
825    /// See [`compare`] for supported patterns.
826    ///
827    /// <div class="warning">
828    ///
829    /// Prefer [`Execs::with_stderr_data`] where possible.
830    /// - `expected` cannot be snapshotted
831    /// - `expected` can end up being ambiguous, causing the assertion to succeed when it should fail
832    ///
833    /// </div>
834    pub fn with_stderr_contains<S: ToString>(&mut self, expected: S) -> &mut Self {
835        self.expect_stderr_contains.push(expected.to_string());
836        self
837    }
838
839    /// Verifies that stdout does not contain the given contiguous lines.
840    ///
841    /// See [`compare`] for supported patterns.
842    ///
843    /// See note on [`Self::with_stderr_does_not_contain`].
844    ///
845    /// <div class="warning">
846    ///
847    /// Prefer [`Execs::with_stdout_data`] where possible.
848    /// - `expected` cannot be snapshotted
849    /// - The absence of `expected` can either mean success or that the string being looked for
850    ///   changed.
851    ///
852    /// To mitigate this, consider matching this up with
853    /// [`Execs::with_stdout_contains`].
854    ///
855    /// </div>
856    pub fn with_stdout_does_not_contain<S: ToString>(&mut self, expected: S) -> &mut Self {
857        self.expect_stdout_not_contains.push(expected.to_string());
858        self
859    }
860
861    /// Verifies that stderr does not contain the given contiguous lines.
862    ///
863    /// See [`compare`] for supported patterns.
864    ///
865    /// <div class="warning">
866    ///
867    /// Prefer [`Execs::with_stdout_data`] where possible.
868    /// - `expected` cannot be snapshotted
869    /// - The absence of `expected` can either mean success or that the string being looked for
870    ///   changed.
871    ///
872    /// To mitigate this, consider either matching this up with
873    /// [`Execs::with_stdout_contains`] or replace it
874    /// with [`Execs::with_stderr_line_without`].
875    ///
876    /// </div>
877    pub fn with_stderr_does_not_contain<S: ToString>(&mut self, expected: S) -> &mut Self {
878        self.expect_stderr_not_contains.push(expected.to_string());
879        self
880    }
881
882    /// Verify that a particular line appears in stderr with and without the
883    /// given substrings. Exactly one line must match.
884    ///
885    /// The substrings are matched as `contains`.
886    ///
887    /// <div class="warning">
888    ///
889    /// Prefer [`Execs::with_stdout_data`] where possible.
890    /// - `with` cannot be snapshotted
891    /// - The absence of `without` can either mean success or that the string being looked for
892    ///   changed.
893    ///
894    /// </div>
895    ///
896    /// # Example
897    ///
898    /// ```no_run
899    /// use cargo_test_support::execs;
900    ///
901    /// execs().with_stderr_line_without(
902    ///     &[
903    ///         "[RUNNING] `rustc --crate-name build_script_build",
904    ///         "-C opt-level=3",
905    ///     ],
906    ///     &["-C debuginfo", "-C incremental"],
907    /// );
908    /// ```
909    ///
910    /// This will check that a build line includes `-C opt-level=3` but does
911    /// not contain `-C debuginfo` or `-C incremental`.
912    ///
913    pub fn with_stderr_line_without<S: ToString>(
914        &mut self,
915        with: &[S],
916        without: &[S],
917    ) -> &mut Self {
918        let with = with.iter().map(|s| s.to_string()).collect();
919        let without = without.iter().map(|s| s.to_string()).collect();
920        self.expect_stderr_with_without.push((with, without));
921        self
922    }
923}
924
925/// # Configure the process
926impl Execs {
927    /// Forward subordinate process stdout/stderr to the terminal.
928    /// Useful for printf debugging of the tests.
929    /// CAUTION: CI will fail if you leave this in your test!
930    #[allow(unused)]
931    pub fn stream(&mut self) -> &mut Self {
932        self.stream_output = true;
933        self
934    }
935
936    pub fn arg<T: AsRef<OsStr>>(&mut self, arg: T) -> &mut Self {
937        if let Some(ref mut p) = self.process_builder {
938            p.arg(arg);
939        }
940        self
941    }
942
943    pub fn args<T: AsRef<OsStr>>(&mut self, args: &[T]) -> &mut Self {
944        if let Some(ref mut p) = self.process_builder {
945            p.args(args);
946        }
947        self
948    }
949
950    pub fn cwd<T: AsRef<OsStr>>(&mut self, path: T) -> &mut Self {
951        if let Some(ref mut p) = self.process_builder {
952            if let Some(cwd) = p.get_cwd() {
953                let new_path = cwd.join(path.as_ref());
954                p.cwd(new_path);
955            } else {
956                p.cwd(path);
957            }
958        }
959        self
960    }
961
962    pub fn env<T: AsRef<OsStr>>(&mut self, key: &str, val: T) -> &mut Self {
963        if let Some(ref mut p) = self.process_builder {
964            p.env(key, val);
965        }
966        self
967    }
968
969    pub fn env_remove(&mut self, key: &str) -> &mut Self {
970        if let Some(ref mut p) = self.process_builder {
971            p.env_remove(key);
972        }
973        self
974    }
975
976    /// Enables nightly features for testing
977    ///
978    /// The list of reasons should be why nightly cargo is needed. If it is
979    /// because of an unstable feature put the name of the feature as the reason,
980    /// e.g. `&["print-im-a-teapot"]`
981    pub fn masquerade_as_nightly_cargo(&mut self, reasons: &[&str]) -> &mut Self {
982        if let Some(ref mut p) = self.process_builder {
983            p.masquerade_as_nightly_cargo(reasons);
984        }
985        self
986    }
987
988    /// Overrides the crates.io URL for testing.
989    ///
990    /// Can be used for testing crates-io functionality where alt registries
991    /// cannot be used.
992    pub fn replace_crates_io(&mut self, url: &Url) -> &mut Self {
993        if let Some(ref mut p) = self.process_builder {
994            p.env("__CARGO_TEST_CRATES_IO_URL_DO_NOT_USE_THIS", url.as_str());
995        }
996        self
997    }
998
999    pub fn overlay_registry(&mut self, url: &Url, path: &str) -> &mut Self {
1000        if let Some(ref mut p) = self.process_builder {
1001            let env_value = format!("{}={}", url, path);
1002            p.env(
1003                "__CARGO_TEST_DEPENDENCY_CONFUSION_VULNERABILITY_DO_NOT_USE_THIS",
1004                env_value,
1005            );
1006        }
1007        self
1008    }
1009
1010    pub fn enable_split_debuginfo_packed(&mut self) -> &mut Self {
1011        self.env("CARGO_PROFILE_DEV_SPLIT_DEBUGINFO", "packed")
1012            .env("CARGO_PROFILE_TEST_SPLIT_DEBUGINFO", "packed")
1013            .env("CARGO_PROFILE_RELEASE_SPLIT_DEBUGINFO", "packed")
1014            .env("CARGO_PROFILE_BENCH_SPLIT_DEBUGINFO", "packed");
1015        self
1016    }
1017
1018    pub fn enable_mac_dsym(&mut self) -> &mut Self {
1019        if cfg!(target_os = "macos") {
1020            return self.enable_split_debuginfo_packed();
1021        }
1022        self
1023    }
1024}
1025
1026/// # Run and verify the process
1027impl Execs {
1028    pub fn exec_with_output(&mut self) -> Result<Output> {
1029        self.ran = true;
1030        // TODO avoid unwrap
1031        let p = (&self.process_builder).clone().unwrap();
1032        p.exec_with_output()
1033    }
1034
1035    pub fn build_command(&mut self) -> Command {
1036        self.ran = true;
1037        // TODO avoid unwrap
1038        let p = (&self.process_builder).clone().unwrap();
1039        p.build_command()
1040    }
1041
1042    #[track_caller]
1043    pub fn run(&mut self) -> RawOutput {
1044        self.ran = true;
1045        let mut p = (&self.process_builder).clone().unwrap();
1046        if let Some(stdin) = self.expect_stdin.take() {
1047            p.stdin(stdin);
1048        }
1049
1050        match self.match_process(&p) {
1051            Err(e) => panic_error(&format!("test failed running {}", p), e),
1052            Ok(output) => output,
1053        }
1054    }
1055
1056    /// Runs the process, checks the expected output, and returns the first
1057    /// JSON object on stdout.
1058    #[track_caller]
1059    pub fn run_json(&mut self) -> serde_json::Value {
1060        let output = self.run();
1061        serde_json::from_slice(&output.stdout).unwrap_or_else(|e| {
1062            panic!(
1063                "\nfailed to parse JSON: {}\n\
1064                     output was:\n{}\n",
1065                e,
1066                String::from_utf8_lossy(&output.stdout)
1067            );
1068        })
1069    }
1070
1071    #[track_caller]
1072    pub fn run_output(&mut self, output: &Output) {
1073        self.ran = true;
1074        if let Err(e) = self.match_output(output.status.code(), &output.stdout, &output.stderr) {
1075            panic_error("process did not return the expected result", e)
1076        }
1077    }
1078
1079    #[track_caller]
1080    fn verify_checks_output(&self, stdout: &[u8], stderr: &[u8]) {
1081        if self.expect_exit_code.unwrap_or(0) != 0
1082            && self.expect_stdin.is_none()
1083            && self.expect_stdout_data.is_none()
1084            && self.expect_stderr_data.is_none()
1085            && self.expect_stdout_contains.is_empty()
1086            && self.expect_stderr_contains.is_empty()
1087            && self.expect_stdout_not_contains.is_empty()
1088            && self.expect_stderr_not_contains.is_empty()
1089            && self.expect_stderr_with_without.is_empty()
1090        {
1091            panic!(
1092                "`with_status()` is used, but no output is checked.\n\
1093                 The test must check the output to ensure the correct error is triggered.\n\
1094                 --- stdout\n{}\n--- stderr\n{}",
1095                String::from_utf8_lossy(stdout),
1096                String::from_utf8_lossy(stderr),
1097            );
1098        }
1099    }
1100
1101    #[track_caller]
1102    fn match_process(&self, process: &ProcessBuilder) -> Result<RawOutput> {
1103        println!("running {}", process);
1104        let res = if self.stream_output {
1105            if is_ci() {
1106                panic!("`.stream()` is for local debugging")
1107            }
1108            process.exec_with_streaming(
1109                &mut |out| {
1110                    println!("{}", out);
1111                    Ok(())
1112                },
1113                &mut |err| {
1114                    eprintln!("{}", err);
1115                    Ok(())
1116                },
1117                true,
1118            )
1119        } else {
1120            process.exec_with_output()
1121        };
1122
1123        match res {
1124            Ok(out) => {
1125                self.match_output(out.status.code(), &out.stdout, &out.stderr)?;
1126                return Ok(RawOutput {
1127                    stdout: out.stdout,
1128                    stderr: out.stderr,
1129                    code: out.status.code(),
1130                });
1131            }
1132            Err(e) => {
1133                if let Some(ProcessError {
1134                    stdout: Some(stdout),
1135                    stderr: Some(stderr),
1136                    code,
1137                    ..
1138                }) = e.downcast_ref::<ProcessError>()
1139                {
1140                    self.match_output(*code, stdout, stderr)?;
1141                    return Ok(RawOutput {
1142                        stdout: stdout.to_vec(),
1143                        stderr: stderr.to_vec(),
1144                        code: *code,
1145                    });
1146                }
1147                bail!("could not exec process {}: {:?}", process, e)
1148            }
1149        }
1150    }
1151
1152    #[track_caller]
1153    fn match_output(&self, code: Option<i32>, stdout: &[u8], stderr: &[u8]) -> Result<()> {
1154        self.verify_checks_output(stdout, stderr);
1155        let stdout = std::str::from_utf8(stdout).expect("stdout is not utf8");
1156        let stderr = std::str::from_utf8(stderr).expect("stderr is not utf8");
1157
1158        match self.expect_exit_code {
1159            None => {}
1160            Some(expected) if code == Some(expected) => {}
1161            Some(expected) => bail!(
1162                "process exited with code {} (expected {})\n--- stdout\n{}\n--- stderr\n{}",
1163                code.unwrap_or(-1),
1164                expected,
1165                stdout,
1166                stderr
1167            ),
1168        }
1169
1170        if let Some(expect_stdout_data) = &self.expect_stdout_data {
1171            if let Err(err) = self.assert.try_eq(
1172                Some(&"stdout"),
1173                stdout.into_data(),
1174                expect_stdout_data.clone(),
1175            ) {
1176                panic!("{err}")
1177            }
1178        }
1179        if let Some(expect_stderr_data) = &self.expect_stderr_data {
1180            if let Err(err) = self.assert.try_eq(
1181                Some(&"stderr"),
1182                stderr.into_data(),
1183                expect_stderr_data.clone(),
1184            ) {
1185                panic!("{err}")
1186            }
1187        }
1188        for expect in self.expect_stdout_contains.iter() {
1189            compare::match_contains(expect, stdout, self.assert.redactions())?;
1190        }
1191        for expect in self.expect_stderr_contains.iter() {
1192            compare::match_contains(expect, stderr, self.assert.redactions())?;
1193        }
1194        for expect in self.expect_stdout_not_contains.iter() {
1195            compare::match_does_not_contain(expect, stdout, self.assert.redactions())?;
1196        }
1197        for expect in self.expect_stderr_not_contains.iter() {
1198            compare::match_does_not_contain(expect, stderr, self.assert.redactions())?;
1199        }
1200        for (with, without) in self.expect_stderr_with_without.iter() {
1201            compare::match_with_without(stderr, with, without, self.assert.redactions())?;
1202        }
1203        Ok(())
1204    }
1205}
1206
1207impl Drop for Execs {
1208    fn drop(&mut self) {
1209        if !self.ran && !std::thread::panicking() {
1210            panic!("forgot to run this command");
1211        }
1212    }
1213}
1214
1215/// Run and verify a process, see [`Execs`]
1216pub fn execs() -> Execs {
1217    Execs {
1218        ran: false,
1219        process_builder: None,
1220        expect_stdin: None,
1221        expect_exit_code: Some(0),
1222        expect_stdout_data: None,
1223        expect_stderr_data: None,
1224        expect_stdout_contains: Vec::new(),
1225        expect_stderr_contains: Vec::new(),
1226        expect_stdout_not_contains: Vec::new(),
1227        expect_stderr_not_contains: Vec::new(),
1228        expect_stderr_with_without: Vec::new(),
1229        stream_output: false,
1230        assert: compare::assert_e2e(),
1231    }
1232}
1233
1234/// Generate a basic `Cargo.toml`
1235pub fn basic_manifest(name: &str, version: &str) -> String {
1236    format!(
1237        r#"
1238        [package]
1239        name = "{}"
1240        version = "{}"
1241        authors = []
1242        edition = "2015"
1243    "#,
1244        name, version
1245    )
1246}
1247
1248/// Generate a `Cargo.toml` with the specified `bin.name`
1249pub fn basic_bin_manifest(name: &str) -> String {
1250    format!(
1251        r#"
1252        [package]
1253
1254        name = "{}"
1255        version = "0.5.0"
1256        authors = ["wycats@example.com"]
1257        edition = "2015"
1258
1259        [[bin]]
1260
1261        name = "{}"
1262    "#,
1263        name, name
1264    )
1265}
1266
1267/// Generate a `Cargo.toml` with the specified `lib.name`
1268pub fn basic_lib_manifest(name: &str) -> String {
1269    format!(
1270        r#"
1271        [package]
1272
1273        name = "{}"
1274        version = "0.5.0"
1275        authors = ["wycats@example.com"]
1276        edition = "2015"
1277
1278        [lib]
1279
1280        name = "{}"
1281    "#,
1282        name, name
1283    )
1284}
1285
1286struct RustcInfo {
1287    verbose_version: String,
1288    host: String,
1289}
1290
1291impl RustcInfo {
1292    fn new() -> RustcInfo {
1293        let output = ProcessBuilder::new("rustc")
1294            .arg("-vV")
1295            .exec_with_output()
1296            .expect("rustc should exec");
1297        let verbose_version = String::from_utf8(output.stdout).expect("utf8 output");
1298        let host = verbose_version
1299            .lines()
1300            .filter_map(|line| line.strip_prefix("host: "))
1301            .next()
1302            .expect("verbose version has host: field")
1303            .to_string();
1304        RustcInfo {
1305            verbose_version,
1306            host,
1307        }
1308    }
1309}
1310
1311fn rustc_info() -> &'static RustcInfo {
1312    static RUSTC_INFO: OnceLock<RustcInfo> = OnceLock::new();
1313    RUSTC_INFO.get_or_init(RustcInfo::new)
1314}
1315
1316/// The rustc host such as `x86_64-unknown-linux-gnu`.
1317pub fn rustc_host() -> &'static str {
1318    &rustc_info().host
1319}
1320
1321/// The host triple suitable for use in a cargo environment variable (uppercased).
1322pub fn rustc_host_env() -> String {
1323    rustc_host().to_uppercase().replace('-', "_")
1324}
1325
1326pub fn is_nightly() -> bool {
1327    let vv = &rustc_info().verbose_version;
1328    // CARGO_TEST_DISABLE_NIGHTLY is set in rust-lang/rust's CI so that all
1329    // nightly-only tests are disabled there. Otherwise, it could make it
1330    // difficult to land changes which would need to be made simultaneously in
1331    // rust-lang/cargo and rust-lan/rust, which isn't possible.
1332    env::var("CARGO_TEST_DISABLE_NIGHTLY").is_err()
1333        && (vv.contains("-nightly") || vv.contains("-dev"))
1334}
1335
1336/// Run `$bin` in the test's environment, see [`ProcessBuilder`]
1337///
1338/// For more on the test environment, see
1339/// - [`paths::root`]
1340/// - [`TestEnvCommandExt`]
1341pub fn process<T: AsRef<OsStr>>(bin: T) -> ProcessBuilder {
1342    _process(bin.as_ref())
1343}
1344
1345fn _process(t: &OsStr) -> ProcessBuilder {
1346    let mut p = ProcessBuilder::new(t);
1347    p.cwd(&paths::root()).test_env();
1348    p
1349}
1350
1351/// Enable nightly features for testing
1352pub trait ChannelChangerCommandExt {
1353    /// The list of reasons should be why nightly cargo is needed. If it is
1354    /// because of an unstable feature put the name of the feature as the reason,
1355    /// e.g. `&["print-im-a-teapot"]`.
1356    fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self;
1357}
1358
1359impl ChannelChangerCommandExt for &mut ProcessBuilder {
1360    fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self {
1361        self.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
1362    }
1363}
1364
1365impl ChannelChangerCommandExt for snapbox::cmd::Command {
1366    fn masquerade_as_nightly_cargo(self, _reasons: &[&str]) -> Self {
1367        self.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
1368    }
1369}
1370
1371/// Establish a process's test environment
1372pub trait TestEnvCommandExt: Sized {
1373    fn test_env(mut self) -> Self {
1374        // In general just clear out all cargo-specific configuration already in the
1375        // environment. Our tests all assume a "default configuration" unless
1376        // specified otherwise.
1377        for (k, _v) in env::vars() {
1378            if k.starts_with("CARGO_") {
1379                self = self.env_remove(&k);
1380            }
1381        }
1382        if env::var_os("RUSTUP_TOOLCHAIN").is_some() {
1383            // Override the PATH to avoid executing the rustup wrapper thousands
1384            // of times. This makes the testsuite run substantially faster.
1385            static RUSTC_DIR: OnceLock<PathBuf> = OnceLock::new();
1386            let rustc_dir = RUSTC_DIR.get_or_init(|| {
1387                match ProcessBuilder::new("rustup")
1388                    .args(&["which", "rustc"])
1389                    .exec_with_output()
1390                {
1391                    Ok(output) => {
1392                        let s = std::str::from_utf8(&output.stdout).expect("utf8").trim();
1393                        let mut p = PathBuf::from(s);
1394                        p.pop();
1395                        p
1396                    }
1397                    Err(e) => {
1398                        panic!("RUSTUP_TOOLCHAIN was set, but could not run rustup: {}", e);
1399                    }
1400                }
1401            });
1402            let path = env::var_os("PATH").unwrap_or_default();
1403            let paths = env::split_paths(&path);
1404            let new_path =
1405                env::join_paths(std::iter::once(rustc_dir.clone()).chain(paths)).unwrap();
1406            self = self.env("PATH", new_path);
1407        }
1408
1409        self = self
1410            .current_dir(&paths::root())
1411            .env("HOME", paths::home())
1412            .env("CARGO_HOME", paths::cargo_home())
1413            .env("__CARGO_TEST_ROOT", paths::global_root())
1414            // Force Cargo to think it's on the stable channel for all tests, this
1415            // should hopefully not surprise us as we add cargo features over time and
1416            // cargo rides the trains.
1417            .env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
1418            // Keeps cargo within its sandbox.
1419            .env("__CARGO_TEST_DISABLE_GLOBAL_KNOWN_HOST", "1")
1420            // Set retry sleep to 1 millisecond.
1421            .env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS", "1")
1422            // Incremental generates a huge amount of data per test, which we
1423            // don't particularly need. Tests that specifically need to check
1424            // the incremental behavior should turn this back on.
1425            .env("CARGO_INCREMENTAL", "0")
1426            // Don't read the system git config which is out of our control.
1427            .env("GIT_CONFIG_NOSYSTEM", "1")
1428            .env_remove("__CARGO_DEFAULT_LIB_METADATA")
1429            .env_remove("ALL_PROXY")
1430            .env_remove("EMAIL")
1431            .env_remove("GIT_AUTHOR_EMAIL")
1432            .env_remove("GIT_AUTHOR_NAME")
1433            .env_remove("GIT_COMMITTER_EMAIL")
1434            .env_remove("GIT_COMMITTER_NAME")
1435            .env_remove("http_proxy")
1436            .env_remove("HTTPS_PROXY")
1437            .env_remove("https_proxy")
1438            .env_remove("MAKEFLAGS")
1439            .env_remove("MFLAGS")
1440            .env_remove("MSYSTEM") // assume cmd.exe everywhere on windows
1441            .env_remove("RUSTC")
1442            .env_remove("RUST_BACKTRACE")
1443            .env_remove("RUSTC_WORKSPACE_WRAPPER")
1444            .env_remove("RUSTC_WRAPPER")
1445            .env_remove("RUSTDOC")
1446            .env_remove("RUSTDOCFLAGS")
1447            .env_remove("RUSTFLAGS")
1448            .env_remove("SSH_AUTH_SOCK") // ensure an outer agent is never contacted
1449            .env_remove("USER") // not set on some rust-lang docker images
1450            .env_remove("XDG_CONFIG_HOME") // see #2345
1451            .env_remove("OUT_DIR"); // see #13204
1452        if cfg!(windows) {
1453            self = self.env("USERPROFILE", paths::home());
1454        }
1455        self
1456    }
1457
1458    fn current_dir<S: AsRef<std::path::Path>>(self, path: S) -> Self;
1459    fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self;
1460    fn env_remove(self, key: &str) -> Self;
1461}
1462
1463impl TestEnvCommandExt for &mut ProcessBuilder {
1464    fn current_dir<S: AsRef<std::path::Path>>(self, path: S) -> Self {
1465        let path = path.as_ref();
1466        self.cwd(path)
1467    }
1468    fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self {
1469        self.env(key, value)
1470    }
1471    fn env_remove(self, key: &str) -> Self {
1472        self.env_remove(key)
1473    }
1474}
1475
1476impl TestEnvCommandExt for snapbox::cmd::Command {
1477    fn current_dir<S: AsRef<std::path::Path>>(self, path: S) -> Self {
1478        self.current_dir(path)
1479    }
1480    fn env<S: AsRef<std::ffi::OsStr>>(self, key: &str, value: S) -> Self {
1481        self.env(key, value)
1482    }
1483    fn env_remove(self, key: &str) -> Self {
1484        self.env_remove(key)
1485    }
1486}
1487
1488/// Test the cargo command
1489pub trait CargoCommandExt {
1490    fn cargo_ui() -> Self;
1491}
1492
1493impl CargoCommandExt for snapbox::cmd::Command {
1494    fn cargo_ui() -> Self {
1495        Self::new(cargo_exe())
1496            .with_assert(compare::assert_ui())
1497            .env("CARGO_TERM_COLOR", "always")
1498            .test_env()
1499    }
1500}
1501
1502/// Add a list of arguments as a line
1503pub trait ArgLineCommandExt: Sized {
1504    fn arg_line(mut self, s: &str) -> Self {
1505        for mut arg in s.split_whitespace() {
1506            if (arg.starts_with('"') && arg.ends_with('"'))
1507                || (arg.starts_with('\'') && arg.ends_with('\''))
1508            {
1509                arg = &arg[1..(arg.len() - 1).max(1)];
1510            } else if arg.contains(&['"', '\''][..]) {
1511                panic!("shell-style argument parsing is not supported")
1512            }
1513            self = self.arg(arg);
1514        }
1515        self
1516    }
1517
1518    fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self;
1519}
1520
1521impl ArgLineCommandExt for &mut ProcessBuilder {
1522    fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self {
1523        self.arg(s)
1524    }
1525}
1526
1527impl ArgLineCommandExt for &mut Execs {
1528    fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self {
1529        self.arg(s)
1530    }
1531}
1532
1533impl ArgLineCommandExt for snapbox::cmd::Command {
1534    fn arg<S: AsRef<std::ffi::OsStr>>(self, s: S) -> Self {
1535        self.arg(s)
1536    }
1537}
1538
1539/// Run `cargo $arg_line`, see [`Execs`]
1540pub fn cargo_process(arg_line: &str) -> Execs {
1541    let cargo = cargo_exe();
1542    let mut p = process(&cargo);
1543    p.env("CARGO", cargo);
1544    p.arg_line(arg_line);
1545    execs().with_process_builder(p)
1546}
1547
1548/// Run `git $arg_line`, see [`ProcessBuilder`]
1549pub fn git_process(arg_line: &str) -> ProcessBuilder {
1550    let mut p = process("git");
1551    p.arg_line(arg_line);
1552    p
1553}
1554
1555pub fn sleep_ms(ms: u64) {
1556    ::std::thread::sleep(Duration::from_millis(ms));
1557}
1558
1559/// Returns `true` if the local filesystem has low-resolution mtimes.
1560pub fn is_coarse_mtime() -> bool {
1561    // If the filetime crate is being used to emulate HFS then
1562    // return `true`, without looking at the actual hardware.
1563    cfg!(emulate_second_only_system) ||
1564    // This should actually be a test that `$CARGO_TARGET_DIR` is on an HFS
1565    // filesystem, (or any filesystem with low-resolution mtimes). However,
1566    // that's tricky to detect, so for now just deal with CI.
1567    cfg!(target_os = "macos") && is_ci()
1568}
1569
1570/// A way for to increase the cut off for all the time based test.
1571///
1572/// Some CI setups are much slower then the equipment used by Cargo itself.
1573/// Architectures that do not have a modern processor, hardware emulation, etc.
1574pub fn slow_cpu_multiplier(main: u64) -> Duration {
1575    static SLOW_CPU_MULTIPLIER: OnceLock<u64> = OnceLock::new();
1576    let slow_cpu_multiplier = SLOW_CPU_MULTIPLIER.get_or_init(|| {
1577        env::var("CARGO_TEST_SLOW_CPU_MULTIPLIER")
1578            .ok()
1579            .and_then(|m| m.parse().ok())
1580            .unwrap_or(1)
1581    });
1582    Duration::from_secs(slow_cpu_multiplier * main)
1583}
1584
1585#[cfg(windows)]
1586pub fn symlink_supported() -> bool {
1587    if is_ci() {
1588        // We want to be absolutely sure this runs on CI.
1589        return true;
1590    }
1591    let src = paths::root().join("symlink_src");
1592    fs::write(&src, "").unwrap();
1593    let dst = paths::root().join("symlink_dst");
1594    let result = match os::windows::fs::symlink_file(&src, &dst) {
1595        Ok(_) => {
1596            fs::remove_file(&dst).unwrap();
1597            true
1598        }
1599        Err(e) => {
1600            eprintln!(
1601                "symlinks not supported: {:?}\n\
1602                 Windows 10 users should enable developer mode.",
1603                e
1604            );
1605            false
1606        }
1607    };
1608    fs::remove_file(&src).unwrap();
1609    return result;
1610}
1611
1612#[cfg(not(windows))]
1613pub fn symlink_supported() -> bool {
1614    true
1615}
1616
1617/// The error message for ENOENT.
1618pub fn no_such_file_err_msg() -> String {
1619    std::io::Error::from_raw_os_error(2).to_string()
1620}
1621
1622/// Helper to retry a function `n` times.
1623///
1624/// The function should return `Some` when it is ready.
1625pub fn retry<F, R>(n: u32, mut f: F) -> R
1626where
1627    F: FnMut() -> Option<R>,
1628{
1629    let mut count = 0;
1630    let start = std::time::Instant::now();
1631    loop {
1632        if let Some(r) = f() {
1633            return r;
1634        }
1635        count += 1;
1636        if count > n {
1637            panic!(
1638                "test did not finish within {n} attempts ({:?} total)",
1639                start.elapsed()
1640            );
1641        }
1642        sleep_ms(100);
1643    }
1644}
1645
1646#[test]
1647#[should_panic(expected = "test did not finish")]
1648fn retry_fails() {
1649    retry(2, || None::<()>);
1650}
1651
1652/// Helper that waits for a thread to finish, up to `n` tenths of a second.
1653pub fn thread_wait_timeout<T>(n: u32, thread: JoinHandle<T>) -> T {
1654    retry(n, || thread.is_finished().then_some(()));
1655    thread.join().unwrap()
1656}
1657
1658/// Helper that runs some function, and waits up to `n` tenths of a second for
1659/// it to finish.
1660pub fn threaded_timeout<F, R>(n: u32, f: F) -> R
1661where
1662    F: FnOnce() -> R + Send + 'static,
1663    R: Send + 'static,
1664{
1665    let thread = std::thread::spawn(|| f());
1666    thread_wait_timeout(n, thread)
1667}
1668
1669// Helper for testing dep-info files in the fingerprint dir.
1670#[track_caller]
1671pub fn assert_deps(project: &Project, fingerprint: &str, test_cb: impl Fn(&Path, &[(u8, &str)])) {
1672    let mut files = project
1673        .glob(fingerprint)
1674        .map(|f| f.expect("unwrap glob result"))
1675        // Filter out `.json` entries.
1676        .filter(|f| f.extension().is_none());
1677    let info_path = files
1678        .next()
1679        .unwrap_or_else(|| panic!("expected 1 dep-info file at {}, found 0", fingerprint));
1680    assert!(files.next().is_none(), "expected only 1 dep-info file");
1681    let dep_info = fs::read(&info_path).unwrap();
1682    let dep_info = &mut &dep_info[..];
1683
1684    // Consume the magic marker and version. Here they don't really matter.
1685    read_usize(dep_info);
1686    read_u8(dep_info);
1687    read_u8(dep_info);
1688
1689    let deps = (0..read_usize(dep_info))
1690        .map(|_| {
1691            let ty = read_u8(dep_info);
1692            let path = std::str::from_utf8(read_bytes(dep_info)).unwrap();
1693            let checksum_present = read_bool(dep_info);
1694            if checksum_present {
1695                // Read out the checksum info without using it
1696                let _file_len = read_u64(dep_info);
1697                let _checksum = read_bytes(dep_info);
1698            }
1699            (ty, path)
1700        })
1701        .collect::<Vec<_>>();
1702    test_cb(&info_path, &deps);
1703
1704    fn read_usize(bytes: &mut &[u8]) -> usize {
1705        let ret = &bytes[..4];
1706        *bytes = &bytes[4..];
1707
1708        u32::from_le_bytes(ret.try_into().unwrap()) as usize
1709    }
1710
1711    fn read_u8(bytes: &mut &[u8]) -> u8 {
1712        let ret = bytes[0];
1713        *bytes = &bytes[1..];
1714        ret
1715    }
1716
1717    fn read_bool(bytes: &mut &[u8]) -> bool {
1718        read_u8(bytes) != 0
1719    }
1720
1721    fn read_u64(bytes: &mut &[u8]) -> u64 {
1722        let ret = &bytes[..8];
1723        *bytes = &bytes[8..];
1724
1725        u64::from_le_bytes(ret.try_into().unwrap())
1726    }
1727
1728    fn read_bytes<'a>(bytes: &mut &'a [u8]) -> &'a [u8] {
1729        let n = read_usize(bytes);
1730        let ret = &bytes[..n];
1731        *bytes = &bytes[n..];
1732        ret
1733    }
1734}
1735
1736pub fn assert_deps_contains(project: &Project, fingerprint: &str, expected: &[(u8, &str)]) {
1737    assert_deps(project, fingerprint, |info_path, entries| {
1738        for (e_kind, e_path) in expected {
1739            let pattern = glob::Pattern::new(e_path).unwrap();
1740            let count = entries
1741                .iter()
1742                .filter(|(kind, path)| kind == e_kind && pattern.matches(path))
1743                .count();
1744            if count != 1 {
1745                panic!(
1746                    "Expected 1 match of {} {} in {:?}, got {}:\n{:#?}",
1747                    e_kind, e_path, info_path, count, entries
1748                );
1749            }
1750        }
1751    })
1752}