Skip to content

Commit b3efc44

Browse files
robinkrahlalexcrichton
authored andcommitted
Clear CFLAGS and CXXFLAGS before tests (#472)
Some test cases check that a compiler flag is not present. But cc::Build loads additional flags from the CFLAGS and CXXFLAGS environment variables. If these are set, they might interfere with the test cases. Therefore we clear the CFLAGS and CXXFLAGS environment variables before running a test that requires an absent flag.
1 parent 57afb42 commit b3efc44

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

tests/test.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,18 @@ use crate::support::Test;
22

33
mod support;
44

5+
// Some tests check that a flag is *not* present. These tests might fail if the flag is set in the
6+
// CFLAGS or CXXFLAGS environment variables. This function clears the CFLAGS and CXXFLAGS
7+
// variables to make sure that the tests can run correctly.
8+
fn reset_env() {
9+
std::env::set_var("CFLAGS", "");
10+
std::env::set_var("CXXFLAGS", "");
11+
}
12+
513
#[test]
614
fn gnu_smoke() {
15+
reset_env();
16+
717
let test = Test::gnu();
818
test.gcc().file("foo.c").compile("foo");
919

@@ -19,6 +29,8 @@ fn gnu_smoke() {
1929

2030
#[test]
2131
fn gnu_opt_level_1() {
32+
reset_env();
33+
2234
let test = Test::gnu();
2335
test.gcc().opt_level(1).file("foo.c").compile("foo");
2436

@@ -27,6 +39,8 @@ fn gnu_opt_level_1() {
2739

2840
#[test]
2941
fn gnu_opt_level_s() {
42+
reset_env();
43+
3044
let test = Test::gnu();
3145
test.gcc().opt_level_str("s").file("foo.c").compile("foo");
3246

@@ -56,6 +70,8 @@ fn gnu_debug_fp() {
5670

5771
#[test]
5872
fn gnu_debug_nofp() {
73+
reset_env();
74+
5975
let test = Test::gnu();
6076
test.gcc()
6177
.debug(true)
@@ -100,6 +116,8 @@ fn gnu_warnings() {
100116

101117
#[test]
102118
fn gnu_extra_warnings0() {
119+
reset_env();
120+
103121
let test = Test::gnu();
104122
test.gcc()
105123
.warnings(true)
@@ -113,6 +131,8 @@ fn gnu_extra_warnings0() {
113131

114132
#[test]
115133
fn gnu_extra_warnings1() {
134+
reset_env();
135+
116136
let test = Test::gnu();
117137
test.gcc()
118138
.warnings(false)
@@ -126,6 +146,8 @@ fn gnu_extra_warnings1() {
126146

127147
#[test]
128148
fn gnu_warnings_overridable() {
149+
reset_env();
150+
129151
let test = Test::gnu();
130152
test.gcc()
131153
.warnings(true)
@@ -154,6 +176,8 @@ fn gnu_x86_64() {
154176

155177
#[test]
156178
fn gnu_x86_64_no_pic() {
179+
reset_env();
180+
157181
for vendor in &["unknown-linux-gnu", "apple-darwin"] {
158182
let target = format!("x86_64-{}", vendor);
159183
let test = Test::gnu();
@@ -215,6 +239,8 @@ fn gnu_x86_64_no_plt() {
215239

216240
#[test]
217241
fn gnu_set_stdlib() {
242+
reset_env();
243+
218244
let test = Test::gnu();
219245
test.gcc()
220246
.cpp_set_stdlib(Some("foo"))
@@ -253,6 +279,8 @@ fn gnu_compile_assembly() {
253279

254280
#[test]
255281
fn gnu_shared() {
282+
reset_env();
283+
256284
let test = Test::gnu();
257285
test.gcc()
258286
.file("foo.c")
@@ -265,6 +293,8 @@ fn gnu_shared() {
265293

266294
#[test]
267295
fn gnu_flag_if_supported() {
296+
reset_env();
297+
268298
if cfg!(windows) {
269299
return;
270300
}
@@ -301,6 +331,8 @@ fn gnu_flag_if_supported_cpp() {
301331

302332
#[test]
303333
fn gnu_static() {
334+
reset_env();
335+
304336
let test = Test::gnu();
305337
test.gcc()
306338
.file("foo.c")
@@ -313,6 +345,8 @@ fn gnu_static() {
313345

314346
#[test]
315347
fn msvc_smoke() {
348+
reset_env();
349+
316350
let test = Test::msvc();
317351
test.gcc().file("foo.c").compile("foo");
318352

@@ -327,6 +361,8 @@ fn msvc_smoke() {
327361

328362
#[test]
329363
fn msvc_opt_level_0() {
364+
reset_env();
365+
330366
let test = Test::msvc();
331367
test.gcc().opt_level(0).file("foo.c").compile("foo");
332368

0 commit comments

Comments
 (0)