1 //! Tests for the cache file for the rustc version info.
2 
3 use cargo_test_support::{basic_bin_manifest, paths::CargoPathExt};
4 use cargo_test_support::{basic_manifest, project};
5 use std::env;
6 
7 const MISS: &str = "[..] rustc info cache miss[..]";
8 const HIT: &str = "[..]rustc info cache hit[..]";
9 const UPDATE: &str = "[..]updated rustc info cache[..]";
10 
11 #[cargo_test]
rustc_info_cache()12 fn rustc_info_cache() {
13     let p = project()
14         .file("src/main.rs", r#"fn main() { println!("hello"); }"#)
15         .build();
16 
17     p.cargo("build")
18         .env("CARGO_LOG", "cargo::util::rustc=debug")
19         .with_stderr_contains("[..]failed to read rustc info cache[..]")
20         .with_stderr_contains(MISS)
21         .with_stderr_does_not_contain(HIT)
22         .with_stderr_contains(UPDATE)
23         .run();
24 
25     p.cargo("build")
26         .env("CARGO_LOG", "cargo::util::rustc=debug")
27         .with_stderr_contains("[..]reusing existing rustc info cache[..]")
28         .with_stderr_contains(HIT)
29         .with_stderr_does_not_contain(MISS)
30         .with_stderr_does_not_contain(UPDATE)
31         .run();
32 
33     p.cargo("build")
34         .env("CARGO_LOG", "cargo::util::rustc=debug")
35         .env("CARGO_CACHE_RUSTC_INFO", "0")
36         .with_stderr_contains("[..]rustc info cache disabled[..]")
37         .with_stderr_does_not_contain(UPDATE)
38         .run();
39 
40     let other_rustc = {
41         let p = project()
42             .at("compiler")
43             .file("Cargo.toml", &basic_manifest("compiler", "0.1.0"))
44             .file(
45                 "src/main.rs",
46                 r#"
47                     use std::process::Command;
48                     use std::env;
49 
50                     fn main() {
51                         let mut cmd = Command::new("rustc");
52                         for arg in env::args_os().skip(1) {
53                             cmd.arg(arg);
54                         }
55                         std::process::exit(cmd.status().unwrap().code().unwrap());
56                     }
57                 "#,
58             )
59             .build();
60         p.cargo("build").run();
61 
62         p.root()
63             .join("target/debug/compiler")
64             .with_extension(env::consts::EXE_EXTENSION)
65     };
66 
67     p.cargo("build")
68         .env("CARGO_LOG", "cargo::util::rustc=debug")
69         .env("RUSTC", other_rustc.display().to_string())
70         .with_stderr_contains("[..]different compiler, creating new rustc info cache[..]")
71         .with_stderr_contains(MISS)
72         .with_stderr_does_not_contain(HIT)
73         .with_stderr_contains(UPDATE)
74         .run();
75 
76     p.cargo("build")
77         .env("CARGO_LOG", "cargo::util::rustc=debug")
78         .env("RUSTC", other_rustc.display().to_string())
79         .with_stderr_contains("[..]reusing existing rustc info cache[..]")
80         .with_stderr_contains(HIT)
81         .with_stderr_does_not_contain(MISS)
82         .with_stderr_does_not_contain(UPDATE)
83         .run();
84 
85     other_rustc.move_into_the_future();
86 
87     p.cargo("build")
88         .env("CARGO_LOG", "cargo::util::rustc=debug")
89         .env("RUSTC", other_rustc.display().to_string())
90         .with_stderr_contains("[..]different compiler, creating new rustc info cache[..]")
91         .with_stderr_contains(MISS)
92         .with_stderr_does_not_contain(HIT)
93         .with_stderr_contains(UPDATE)
94         .run();
95 
96     p.cargo("build")
97         .env("CARGO_LOG", "cargo::util::rustc=debug")
98         .env("RUSTC", other_rustc.display().to_string())
99         .with_stderr_contains("[..]reusing existing rustc info cache[..]")
100         .with_stderr_contains(HIT)
101         .with_stderr_does_not_contain(MISS)
102         .with_stderr_does_not_contain(UPDATE)
103         .run();
104 }
105 
106 #[cargo_test]
rustc_info_cache_with_wrappers()107 fn rustc_info_cache_with_wrappers() {
108     let wrapper_project = project()
109         .at("wrapper")
110         .file("Cargo.toml", &basic_bin_manifest("wrapper"))
111         .file("src/main.rs", r#"fn main() { }"#)
112         .build();
113     let wrapper = wrapper_project.bin("wrapper");
114 
115     let p = project()
116         .file(
117             "Cargo.toml",
118             r#"
119                 [package]
120                 name = "test"
121                 version = "0.0.0"
122                 authors = []
123                 [workspace]
124             "#,
125         )
126         .file("src/main.rs", r#"fn main() { println!("hello"); }"#)
127         .build();
128 
129     for &wrapper_env in ["RUSTC_WRAPPER", "RUSTC_WORKSPACE_WRAPPER"].iter() {
130         p.cargo("clean").with_status(0).run();
131         wrapper_project.change_file(
132             "src/main.rs",
133             r#"
134             fn main() {
135                 let mut args = std::env::args_os();
136                 let _me = args.next().unwrap();
137                 let rustc = args.next().unwrap();
138                 let status = std::process::Command::new(rustc).args(args).status().unwrap();
139                 std::process::exit(if status.success() { 0 } else { 1 })
140             }
141             "#,
142         );
143         wrapper_project.cargo("build").with_status(0).run();
144 
145         p.cargo("build")
146             .env("CARGO_LOG", "cargo::util::rustc=debug")
147             .env(wrapper_env, &wrapper)
148             .with_stderr_contains("[..]failed to read rustc info cache[..]")
149             .with_stderr_contains(MISS)
150             .with_stderr_contains(UPDATE)
151             .with_stderr_does_not_contain(HIT)
152             .with_status(0)
153             .run();
154         p.cargo("build")
155             .env("CARGO_LOG", "cargo::util::rustc=debug")
156             .env(wrapper_env, &wrapper)
157             .with_stderr_contains("[..]reusing existing rustc info cache[..]")
158             .with_stderr_contains(HIT)
159             .with_stderr_does_not_contain(UPDATE)
160             .with_stderr_does_not_contain(MISS)
161             .with_status(0)
162             .run();
163 
164         wrapper_project.change_file("src/main.rs", r#"fn main() { panic!() }"#);
165         wrapper_project.cargo("build").with_status(0).run();
166 
167         p.cargo("build")
168             .env("CARGO_LOG", "cargo::util::rustc=debug")
169             .env(wrapper_env, &wrapper)
170             .with_stderr_contains("[..]different compiler, creating new rustc info cache[..]")
171             .with_stderr_contains(MISS)
172             .with_stderr_contains(UPDATE)
173             .with_stderr_does_not_contain(HIT)
174             .with_status(101)
175             .run();
176         p.cargo("build")
177             .env("CARGO_LOG", "cargo::util::rustc=debug")
178             .env(wrapper_env, &wrapper)
179             .with_stderr_contains("[..]reusing existing rustc info cache[..]")
180             .with_stderr_contains(HIT)
181             .with_stderr_does_not_contain(UPDATE)
182             .with_stderr_does_not_contain(MISS)
183             .with_status(101)
184             .run();
185     }
186 }
187