1#![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#[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#[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#[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
224pub struct Project {
228 root: PathBuf,
229}
230
231#[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 pub fn root(&self) -> PathBuf {
253 self.root.root()
254 }
255
256 pub fn target_debug_dir(&self) -> PathBuf {
260 self.root.target_debug_dir()
261 }
262
263 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 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 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 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 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 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 pub fn build(mut self) -> Project {
326 self.rm_root();
328
329 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 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 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 pub fn root(&self) -> PathBuf {
382 self.root.clone()
383 }
384
385 pub fn build_dir(&self) -> PathBuf {
389 self.root().join("target")
390 }
391
392 pub fn target_debug_dir(&self) -> PathBuf {
396 self.build_dir().join("debug")
397 }
398
399 pub fn url(&self) -> Url {
403 use paths::CargoPathExt;
404 self.root().to_url()
405 }
406
407 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 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 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 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 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 pub fn change_file(&self, path: impl AsRef<Path>, body: &str) {
463 FileBuilder::new(self.root().join(path), body, false).mk()
464 }
465
466 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 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 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 pub fn read_lockfile(&self) -> String {
529 self.read_file("Cargo.lock")
530 }
531
532 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 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
569pub fn project() -> ProjectBuilder {
571 ProjectBuilder::new(paths::root().join("foo"))
572}
573
574pub fn project_in(dir: impl AsRef<Path>) -> ProjectBuilder {
576 ProjectBuilder::new(paths::root().join(dir).join("foo"))
577}
578
579pub fn project_in_home(name: impl AsRef<Path>) -> ProjectBuilder {
581 ProjectBuilder::new(paths::home().join(name))
582}
583
584pub 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
614pub fn cargo_exe() -> PathBuf {
616 snapbox::cmd::cargo_bin("cargo")
617}
618
619pub struct RawOutput {
627 pub code: Option<i32>,
628 pub stdout: Vec<u8>,
629 pub stderr: Vec<u8>,
630}
631
632#[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
663impl Execs {
665 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 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 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 pub fn with_status(&mut self, expected: i32) -> &mut Self {
793 self.expect_exit_code = Some(expected);
794 self
795 }
796
797 pub fn without_status(&mut self) -> &mut Self {
801 self.expect_exit_code = None;
802 self
803 }
804
805 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 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 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 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 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
925impl Execs {
927 #[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 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 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
1026impl Execs {
1028 pub fn exec_with_output(&mut self) -> Result<Output> {
1029 self.ran = true;
1030 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 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 #[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
1215pub 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
1234pub 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
1248pub 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
1267pub 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
1316pub fn rustc_host() -> &'static str {
1318 &rustc_info().host
1319}
1320
1321pub 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 env::var("CARGO_TEST_DISABLE_NIGHTLY").is_err()
1333 && (vv.contains("-nightly") || vv.contains("-dev"))
1334}
1335
1336pub 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
1351pub trait ChannelChangerCommandExt {
1353 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
1371pub trait TestEnvCommandExt: Sized {
1373 fn test_env(mut self) -> Self {
1374 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 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 .env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "stable")
1418 .env("__CARGO_TEST_DISABLE_GLOBAL_KNOWN_HOST", "1")
1420 .env("__CARGO_TEST_FIXED_RETRY_SLEEP_MS", "1")
1422 .env("CARGO_INCREMENTAL", "0")
1426 .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") .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") .env_remove("USER") .env_remove("XDG_CONFIG_HOME") .env_remove("OUT_DIR"); 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
1488pub 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
1502pub 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
1539pub 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
1548pub 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
1559pub fn is_coarse_mtime() -> bool {
1561 cfg!(emulate_second_only_system) ||
1564 cfg!(target_os = "macos") && is_ci()
1568}
1569
1570pub 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 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
1617pub fn no_such_file_err_msg() -> String {
1619 std::io::Error::from_raw_os_error(2).to_string()
1620}
1621
1622pub 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
1652pub 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
1658pub 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#[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(|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 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 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}