summaryrefslogtreecommitdiffstats
path: root/src/tools/cargo/tests/testsuite/binary_name.rs
blob: 7735d6054a5579ed87bba24b7d34bc7979672b45 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
use cargo_test_support::install::{
    assert_has_installed_exe, assert_has_not_installed_exe, cargo_home,
};
use cargo_test_support::project;

#[cargo_test]
fn gated() {
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                [package]
                name =  "foo"
                version = "0.0.1"

                [[bin]]
                name = "foo"
                filename = "007bar"
                path = "src/main.rs"
            "#,
        )
        .file("src/main.rs", "fn main() { assert!(true) }")
        .build();

    // Run cargo build.
    p.cargo("build")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_status(101)
        .with_stderr_contains("[..]feature `different-binary-name` is required")
        .run();
}

#[cargo_test]
// This test checks if:
// 1. The correct binary is produced
// 2. The deps file has the correct content
// 3. Fingerprinting works
// 4. `cargo clean` command works
fn binary_name1() {
    // Create the project.
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                cargo-features = ["different-binary-name"]

                [package]
                name =  "foo"
                version = "0.0.1"

                [[bin]]
                name = "foo"
                filename = "007bar"
                path = "src/main.rs"
            "#,
        )
        .file("src/main.rs", "fn main() { assert!(true) }")
        .build();

    // Run cargo build.
    p.cargo("build")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();

    // Check the name of the binary that cargo has generated.
    // A binary with the name of the crate should NOT be created.
    let foo_path = p.bin("foo");
    assert!(!foo_path.is_file());
    // A binary with the name provided in `filename` parameter should be created.
    let bar_path = p.bin("007bar");
    assert!(bar_path.is_file());

    // Check if deps file exists.
    let deps_path = p.bin("007bar").with_extension("d");
    assert!(deps_path.is_file(), "{:?}", bar_path);

    let depinfo = p.read_file(deps_path.to_str().unwrap());

    // Prepare what content we expect to be present in deps file.
    let deps_exp = format!(
        "{}: {}",
        p.bin("007bar").to_str().unwrap(),
        p.root().join("src").join("main.rs").to_str().unwrap()
    );

    // Compare actual deps content with expected deps content.
    assert!(
        depinfo.lines().any(|line| line == deps_exp),
        "Content of `{}` is incorrect",
        deps_path.to_string_lossy()
    );

    // Run cargo second time, to verify fingerprint.
    p.cargo("build -p foo -v")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_stderr(
            "\
[FRESH] foo [..]
[FINISHED] [..]
",
        )
        .run();

    // Run cargo clean.
    p.cargo("clean -p foo")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();

    // Check if the appropriate file was removed.
    assert!(
        !bar_path.is_file(),
        "`cargo clean` did not remove the correct files"
    );
}

#[cargo_test]
// This test checks if:
// 1. Check `cargo run`
// 2. Check `cargo test`
// 3. Check `cargo install/uninstall`
fn binary_name2() {
    // Create the project.
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                cargo-features = ["different-binary-name"]

                [package]
                name =  "foo"
                version = "0.0.1"

                [[bin]]
                name = "foo"
                filename = "007bar"
            "#,
        )
        .file(
            "src/main.rs",
            r#"
                fn hello(name: &str) -> String {
                    format!("Hello, {}!", name)
                }

                fn main() {
                    println!("{}", hello("crabs"));
                }

                #[cfg(test)]
                mod tests {
                    use super::*;

                    #[test]
                    fn check_crabs() {
                        assert_eq!(hello("crabs"), "Hello, crabs!");
                    }
                }
            "#,
        )
        .build();

    // Run cargo build.
    p.cargo("build")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();

    // Check the name of the binary that cargo has generated.
    // A binary with the name of the crate should NOT be created.
    let foo_path = p.bin("foo");
    assert!(!foo_path.is_file());
    // A binary with the name provided in `filename` parameter should be created.
    let bar_path = p.bin("007bar");
    assert!(bar_path.is_file());

    // Check if `cargo test` works
    p.cargo("test")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_stderr(
            "\
[COMPILING] foo v0.0.1 ([CWD])
[FINISHED] test [unoptimized + debuginfo] target(s) in [..]
[RUNNING] [..] (target/debug/deps/foo-[..][EXE])",
        )
        .with_stdout_contains("test tests::check_crabs ... ok")
        .run();

    // Check if `cargo run` is able to execute the binary
    p.cargo("run")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_stdout("Hello, crabs!")
        .run();

    p.cargo("install")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();

    assert_has_installed_exe(cargo_home(), "007bar");

    p.cargo("uninstall")
        .with_stderr("[REMOVING] [ROOT]/home/.cargo/bin/007bar[EXE]")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();

    assert_has_not_installed_exe(cargo_home(), "007bar");
}

#[cargo_test]
fn check_env_vars() {
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                cargo-features = ["different-binary-name"]

                [package]
                name =  "foo"
                version = "0.0.1"

                [[bin]]
                name = "foo"
                filename = "007bar"
            "#,
        )
        .file(
            "src/main.rs",
            r#"
                fn main() {
                    println!("{}", option_env!("CARGO_BIN_NAME").unwrap());
                }
            "#,
        )
        .file(
            "tests/integration.rs",
            r#"
                #[test]
                fn check_env_vars2() {
                    let value = option_env!("CARGO_BIN_EXE_007bar").expect("Could not find environment variable.");
                    assert!(value.contains("007bar"));
                }
            "#
        )
        .build();

    // Run cargo build.
    p.cargo("build")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .run();
    p.cargo("run")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_stdout("007bar")
        .run();
    p.cargo("test")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_status(0)
        .run();
}

#[cargo_test]
fn check_msg_format_json() {
    // Create the project.
    let p = project()
        .file(
            "Cargo.toml",
            r#"
                cargo-features = ["different-binary-name"]

                [package]
                name =  "foo"
                version = "0.0.1"

                [[bin]]
                name = "foo"
                filename = "007bar"
                path = "src/main.rs"
            "#,
        )
        .file("src/main.rs", "fn main() { assert!(true) }")
        .build();

    let output = r#"
{
    "reason": "compiler-artifact",
    "package_id": "foo 0.0.1 [..]",
    "manifest_path": "[CWD]/Cargo.toml",
    "target": "{...}",
    "profile": "{...}",
    "features": [],
    "filenames": "{...}",
    "executable": "[ROOT]/foo/target/debug/007bar[EXE]",
    "fresh": false
}

{"reason":"build-finished", "success":true}
"#;

    // Run cargo build.
    p.cargo("build --message-format=json")
        .masquerade_as_nightly_cargo(&["different-binary-name"])
        .with_json(output)
        .run();
}