1 use cargo_test_support::install::{
2     assert_has_installed_exe, assert_has_not_installed_exe, cargo_home,
3 };
4 use cargo_test_support::project;
5 
6 #[cargo_test]
gated()7 fn gated() {
8     let p = project()
9         .file(
10             "Cargo.toml",
11             r#"
12                 [project]
13                 name =  "foo"
14                 version = "0.0.1"
15 
16                 [[bin]]
17                 name = "foo"
18                 filename = "007bar"
19                 path = "src/main.rs"
20             "#,
21         )
22         .file("src/main.rs", "fn main() { assert!(true) }")
23         .build();
24 
25     // Run cargo build.
26     p.cargo("build")
27         .masquerade_as_nightly_cargo()
28         .with_status(101)
29         .with_stderr_contains("[..]feature `different-binary-name` is required")
30         .run();
31 }
32 
33 #[cargo_test]
34 // This test checks if:
35 // 1. The correct binary is produced
36 // 2. The deps file has the correct content
37 // 3. Fingerprinting works
38 // 4. `cargo clean` command works
binary_name1()39 fn binary_name1() {
40     // Create the project.
41     let p = project()
42         .file(
43             "Cargo.toml",
44             r#"
45                 cargo-features = ["different-binary-name"]
46 
47                 [project]
48                 name =  "foo"
49                 version = "0.0.1"
50 
51                 [[bin]]
52                 name = "foo"
53                 filename = "007bar"
54                 path = "src/main.rs"
55             "#,
56         )
57         .file("src/main.rs", "fn main() { assert!(true) }")
58         .build();
59 
60     // Run cargo build.
61     p.cargo("build").masquerade_as_nightly_cargo().run();
62 
63     // Check the name of the binary that cargo has generated.
64     // A binary with the name of the crate should NOT be created.
65     let foo_path = p.bin("foo");
66     assert!(!foo_path.is_file());
67     // A binary with the name provided in `filename` parameter should be created.
68     let bar_path = p.bin("007bar");
69     assert!(bar_path.is_file());
70 
71     // Check if deps file exists.
72     let deps_path = p.bin("007bar").with_extension("d");
73     assert!(deps_path.is_file(), "{:?}", bar_path);
74 
75     let depinfo = p.read_file(deps_path.to_str().unwrap());
76 
77     // Prepare what content we expect to be present in deps file.
78     let deps_exp = format!(
79         "{}: {}",
80         p.bin("007bar").to_str().unwrap(),
81         p.root().join("src").join("main.rs").to_str().unwrap()
82     );
83 
84     // Compare actual deps content with expected deps content.
85     assert!(
86         depinfo.lines().any(|line| line == deps_exp),
87         "Content of `{}` is incorrect",
88         deps_path.to_string_lossy()
89     );
90 
91     // Run cargo second time, to verify fingerprint.
92     p.cargo("build -p foo -v")
93         .masquerade_as_nightly_cargo()
94         .with_stderr(
95             "\
96 [FRESH] foo [..]
97 [FINISHED] [..]
98 ",
99         )
100         .run();
101 
102     // Run cargo clean.
103     p.cargo("clean -p foo").masquerade_as_nightly_cargo().run();
104 
105     // Check if the appropriate file was removed.
106     assert!(
107         !bar_path.is_file(),
108         "`cargo clean` did not remove the correct files"
109     );
110 }
111 
112 #[cargo_test]
113 // This test checks if:
114 // 1. Check `cargo run`
115 // 2. Check `cargo test`
116 // 3. Check `cargo install/uninstall`
binary_name2()117 fn binary_name2() {
118     // Create the project.
119     let p = project()
120         .file(
121             "Cargo.toml",
122             r#"
123                 cargo-features = ["different-binary-name"]
124 
125                 [project]
126                 name =  "foo"
127                 version = "0.0.1"
128 
129                 [[bin]]
130                 name = "foo"
131                 filename = "007bar"
132             "#,
133         )
134         .file(
135             "src/main.rs",
136             r#"
137                 fn hello(name: &str) -> String {
138                     format!("Hello, {}!", name)
139                 }
140 
141                 fn main() {
142                     println!("{}", hello("crabs"));
143                 }
144 
145                 #[cfg(test)]
146                 mod tests {
147                     use super::*;
148 
149                     #[test]
150                     fn check_crabs() {
151                         assert_eq!(hello("crabs"), "Hello, crabs!");
152                     }
153                 }
154             "#,
155         )
156         .build();
157 
158     // Run cargo build.
159     p.cargo("build").masquerade_as_nightly_cargo().run();
160 
161     // Check the name of the binary that cargo has generated.
162     // A binary with the name of the crate should NOT be created.
163     let foo_path = p.bin("foo");
164     assert!(!foo_path.is_file());
165     // A binary with the name provided in `filename` parameter should be created.
166     let bar_path = p.bin("007bar");
167     assert!(bar_path.is_file());
168 
169     // Check if `cargo test` works
170     p.cargo("test")
171         .masquerade_as_nightly_cargo()
172         .with_stderr(
173             "\
174 [COMPILING] foo v0.0.1 ([CWD])
175 [FINISHED] test [unoptimized + debuginfo] target(s) in [..]
176 [RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
177         )
178         .with_stdout_contains("test tests::check_crabs ... ok")
179         .run();
180 
181     // Check if `cargo run` is able to execute the binary
182     p.cargo("run")
183         .masquerade_as_nightly_cargo()
184         .with_stdout("Hello, crabs!")
185         .run();
186 
187     p.cargo("install").masquerade_as_nightly_cargo().run();
188 
189     assert_has_installed_exe(cargo_home(), "007bar");
190 
191     p.cargo("uninstall")
192         .with_stderr("[REMOVING] [ROOT]/home/.cargo/bin/007bar[EXE]")
193         .masquerade_as_nightly_cargo()
194         .run();
195 
196     assert_has_not_installed_exe(cargo_home(), "007bar");
197 }
198 
199 #[cargo_test]
check_env_vars()200 fn check_env_vars() {
201     let p = project()
202         .file(
203             "Cargo.toml",
204             r#"
205                 cargo-features = ["different-binary-name"]
206 
207                 [project]
208                 name =  "foo"
209                 version = "0.0.1"
210 
211                 [[bin]]
212                 name = "foo"
213                 filename = "007bar"
214             "#,
215         )
216         .file(
217             "src/main.rs",
218             r#"
219                 fn main() {
220                     println!("{}", option_env!("CARGO_BIN_NAME").unwrap());
221                 }
222             "#,
223         )
224         .file(
225             "tests/integration.rs",
226             r#"
227                 #[test]
228                 fn check_env_vars2() {
229                     let value = option_env!("CARGO_BIN_EXE_007bar").expect("Could not find environment variable.");
230                     assert!(value.contains("007bar"));
231                 }
232             "#
233         )
234         .build();
235 
236     // Run cargo build.
237     p.cargo("build").masquerade_as_nightly_cargo().run();
238     p.cargo("run")
239         .masquerade_as_nightly_cargo()
240         .with_stdout("007bar")
241         .run();
242     p.cargo("test")
243         .masquerade_as_nightly_cargo()
244         .with_status(0)
245         .run();
246 }
247 
248 #[cargo_test]
check_msg_format_json()249 fn check_msg_format_json() {
250     // Create the project.
251     let p = project()
252         .file(
253             "Cargo.toml",
254             r#"
255                 cargo-features = ["different-binary-name"]
256 
257                 [project]
258                 name =  "foo"
259                 version = "0.0.1"
260 
261                 [[bin]]
262                 name = "foo"
263                 filename = "007bar"
264                 path = "src/main.rs"
265             "#,
266         )
267         .file("src/main.rs", "fn main() { assert!(true) }")
268         .build();
269 
270     let output = r#"
271 {
272     "reason": "compiler-artifact",
273     "package_id": "foo 0.0.1 [..]",
274     "manifest_path": "[CWD]/Cargo.toml",
275     "target": "{...}",
276     "profile": "{...}",
277     "features": [],
278     "filenames": "{...}",
279     "executable": "[ROOT]/foo/target/debug/007bar[EXE]",
280     "fresh": false
281 }
282 
283 {"reason":"build-finished", "success":true}
284 "#;
285 
286     // Run cargo build.
287     p.cargo("build --message-format=json")
288         .masquerade_as_nightly_cargo()
289         .with_json(output)
290         .run();
291 }
292