Skip to content

Commit e0f523a

Browse files
committed
test: add Add rerun-if-env-changed test example
1 parent 00ea165 commit e0f523a

File tree

1 file changed

+159
-0
lines changed

1 file changed

+159
-0
lines changed

tests/testsuite/build_script_env.rs

Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,165 @@ use cargo_test_support::project;
55
use cargo_test_support::sleep_ms;
66
use cargo_test_support::str;
77

8+
#[cargo_test]
9+
fn rerun_if_env_changes_config() {
10+
let p = project()
11+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
12+
.file("src/main.rs", "fn main() {}")
13+
.file(
14+
".cargo/config.toml",
15+
r#"
16+
[env]
17+
FOO = "good"
18+
"#,
19+
)
20+
.file(
21+
"build.rs",
22+
r#"
23+
fn main() {
24+
println!("cargo:rerun-if-env-changed=FOO");
25+
if let Ok(foo) = std::env::var("FOO") {
26+
assert!(&foo != "bad");
27+
}
28+
}
29+
"#,
30+
)
31+
.build();
32+
33+
p.cargo("check")
34+
.with_stderr_data(str![[r#"
35+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
36+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
37+
38+
"#]])
39+
.run();
40+
41+
p.change_file(
42+
".cargo/config.toml",
43+
r#"
44+
[env]
45+
FOO = "bad"
46+
"#,
47+
);
48+
49+
p.cargo("check")
50+
.with_stderr_data(str![[r#"
51+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
52+
53+
"#]])
54+
.run();
55+
56+
p.cargo("clean").run();
57+
p.cargo("check")
58+
.with_status(101)
59+
.with_stderr_data(
60+
"\
61+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
62+
[ERROR] failed to run custom build command for `foo v0.1.0 ([..])`
63+
...",
64+
)
65+
.run();
66+
}
67+
68+
#[cfg(windows)]
69+
#[cargo_test]
70+
fn rerun_if_env_changes_config_windows() {
71+
let p = project()
72+
.file("Cargo.toml", &basic_manifest("foo", "0.1.0"))
73+
.file("src/main.rs", "fn main() {}")
74+
.file(
75+
".cargo/config.toml",
76+
r#"
77+
[env]
78+
FOO = "good"
79+
"#,
80+
)
81+
.file(
82+
"build.rs",
83+
r#"
84+
fn main() {
85+
println!("cargo:rerun-if-env-changed=Foo");
86+
if let Ok(foo) = std::env::var("Foo") {
87+
assert!(&foo != "bad");
88+
}
89+
}
90+
"#,
91+
)
92+
.build();
93+
94+
p.cargo("check")
95+
.with_stderr_data(str![[r#"
96+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
97+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
98+
99+
"#]])
100+
.run();
101+
102+
p.change_file(
103+
".cargo/config.toml",
104+
r#"
105+
[env]
106+
FOO = "bad"
107+
"#,
108+
);
109+
110+
p.cargo("check")
111+
.with_stderr_data(str![[r#"
112+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
113+
114+
"#]])
115+
.run();
116+
117+
p.cargo("clean").run();
118+
p.cargo("check")
119+
.with_status(101)
120+
.with_stderr_data(
121+
"\
122+
[COMPILING] foo v0.1.0 ([ROOT]/foo)
123+
[ERROR] failed to run custom build command for `foo v0.1.0 ([..])`
124+
...",
125+
)
126+
.run();
127+
}
128+
129+
#[cargo_test]
130+
fn rerun_if_env_changes_cargo_set_env_variable() {
131+
let p = project()
132+
.file("src/main.rs", "fn main() {}")
133+
.file(
134+
"build.rs",
135+
r#"
136+
fn main() {
137+
println!("cargo:rerun-if-env-changed=OUT_DIR");
138+
}
139+
"#,
140+
)
141+
.build();
142+
143+
p.cargo("check")
144+
.with_stderr_data(str![[r#"
145+
[COMPILING] foo v0.0.1 ([ROOT]/foo)
146+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
147+
148+
"#]])
149+
.run();
150+
151+
p.change_file(
152+
".cargo/config.toml",
153+
r#"
154+
[env]
155+
OUT_DIR = "somedir"
156+
"#,
157+
);
158+
159+
p.cargo("check")
160+
.with_stderr_data(str![[r#"
161+
[FINISHED] `dev` profile [unoptimized + debuginfo] target(s) in [ELAPSED]s
162+
163+
"#]])
164+
.run();
165+
}
166+
8167
#[cargo_test]
9168
fn rerun_if_env_changes() {
10169
let p = project()

0 commit comments

Comments
 (0)