Skip to content

Commit fb1ae59

Browse files
authored
Merge pull request #421 from spl/serialize-env-tests
Fix occasional test failure due to env::set_var with multiple test threads
2 parents 17ef91a + 5acdb66 commit fb1ae59

File tree

5 files changed

+66
-28
lines changed

5 files changed

+66
-28
lines changed

ci/azure-steps.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ steps:
1616

1717
- script: cargo build
1818
displayName: "Normal build"
19-
- bash: cargo test $NO_RUN -- --test-threads 1
19+
- bash: cargo test $NO_RUN
2020
displayName: "Crate tests"
21-
- bash: cargo test $NO_RUN --features parallel -- --test-threads 1
21+
- bash: cargo test $NO_RUN --features parallel
2222
displayName: "Crate tests (parallel)"
2323
- bash: cargo test $NO_RUN --manifest-path cc-test/Cargo.toml --target $TARGET
2424
displayName: "cc-test tests"

tests/cflags.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate cc;
2+
extern crate tempdir;
3+
4+
mod support;
5+
6+
use std::env;
7+
use support::Test;
8+
9+
/// This test is in its own module because it modifies the environment and would affect other tests
10+
/// when run in parallel with them.
11+
#[test]
12+
fn gnu_no_warnings_if_cflags() {
13+
env::set_var("CFLAGS", "-arbitrary");
14+
let test = Test::gnu();
15+
test.gcc().file("foo.c").compile("foo");
16+
17+
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
18+
}

tests/cxxflags.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
extern crate cc;
2+
extern crate tempdir;
3+
4+
mod support;
5+
6+
use std::env;
7+
use support::Test;
8+
9+
/// This test is in its own module because it modifies the environment and would affect other tests
10+
/// when run in parallel with them.
11+
#[test]
12+
fn gnu_no_warnings_if_cxxflags() {
13+
env::set_var("CXXFLAGS", "-arbitrary");
14+
let test = Test::gnu();
15+
test.gcc().file("foo.cpp").cpp(true).compile("foo");
16+
17+
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
18+
}

tests/support/mod.rs

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
use std::env;
44
use std::ffi::{OsStr, OsString};
55
use std::fs::{self, File};
6+
use std::io;
67
use std::io::prelude::*;
7-
use std::path::PathBuf;
8+
use std::path::{Path, PathBuf};
89

910
use cc;
1011
use tempdir::TempDir;
@@ -49,10 +50,13 @@ impl Test {
4950
}
5051

5152
pub fn shim(&self, name: &str) -> &Test {
52-
let fname = format!("{}{}", name, env::consts::EXE_SUFFIX);
53-
fs::hard_link(&self.gcc, self.td.path().join(&fname))
54-
.or_else(|_| fs::copy(&self.gcc, self.td.path().join(&fname)).map(|_| ()))
55-
.unwrap();
53+
link_or_copy(
54+
&self.gcc,
55+
self.td
56+
.path()
57+
.join(&format!("{}{}", name, env::consts::EXE_SUFFIX)),
58+
)
59+
.unwrap();
5660
self
5761
}
5862

@@ -136,3 +140,22 @@ impl Execution {
136140
self
137141
}
138142
}
143+
144+
/// Hard link an executable or copy it if that fails.
145+
///
146+
/// We first try to hard link an executable to save space. If that fails (as on Windows with
147+
/// different mount points, issue #60), we copy.
148+
#[cfg(not(target_os = "macos"))]
149+
fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
150+
let from = from.as_ref();
151+
let to = to.as_ref();
152+
fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
153+
}
154+
155+
/// Copy an executable.
156+
///
157+
/// On macOS, hard linking the executable leads to strange failures (issue #419), so we just copy.
158+
#[cfg(target_os = "macos")]
159+
fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
160+
fs::copy(from, to).map(|_| ())
161+
}

tests/test.rs

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
extern crate cc;
22
extern crate tempdir;
33

4-
use std::env;
54
use support::Test;
65

76
mod support;
@@ -111,26 +110,6 @@ fn gnu_warnings_overridable() {
111110
.must_have_in_order("-Wall", "-Wno-missing-field-initializers");
112111
}
113112

114-
#[test]
115-
fn gnu_no_warnings_if_cflags() {
116-
env::set_var("CFLAGS", "-Wflag-does-not-exist");
117-
let test = Test::gnu();
118-
test.gcc().file("foo.c").compile("foo");
119-
120-
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
121-
env::set_var("CFLAGS", "");
122-
}
123-
124-
#[test]
125-
fn gnu_no_warnings_if_cxxflags() {
126-
env::set_var("CXXFLAGS", "-Wflag-does-not-exist");
127-
let test = Test::gnu();
128-
test.gcc().file("foo.c").compile("foo");
129-
130-
test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
131-
env::set_var("CXXFLAGS", "");
132-
}
133-
134113
#[test]
135114
fn gnu_x86_64() {
136115
for vendor in &["unknown-linux-gnu", "apple-darwin"] {

0 commit comments

Comments
 (0)