Skip to content

Fix occasional test failure due to env::set_var with multiple test threads #421

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ci/azure-steps.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ steps:

- script: cargo build
displayName: "Normal build"
- bash: cargo test $NO_RUN -- --test-threads 1
- bash: cargo test $NO_RUN
displayName: "Crate tests"
- bash: cargo test $NO_RUN --features parallel -- --test-threads 1
- bash: cargo test $NO_RUN --features parallel
displayName: "Crate tests (parallel)"
- bash: cargo test $NO_RUN --manifest-path cc-test/Cargo.toml --target $TARGET
displayName: "cc-test tests"
Expand Down
18 changes: 18 additions & 0 deletions tests/cflags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate cc;
extern crate tempdir;

mod support;

use std::env;
use support::Test;

/// This test is in its own module because it modifies the environment and would affect other tests
/// when run in parallel with them.
#[test]
fn gnu_no_warnings_if_cflags() {
env::set_var("CFLAGS", "-arbitrary");
let test = Test::gnu();
test.gcc().file("foo.c").compile("foo");

test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
}
18 changes: 18 additions & 0 deletions tests/cxxflags.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
extern crate cc;
extern crate tempdir;

mod support;

use std::env;
use support::Test;

/// This test is in its own module because it modifies the environment and would affect other tests
/// when run in parallel with them.
#[test]
fn gnu_no_warnings_if_cxxflags() {
env::set_var("CXXFLAGS", "-arbitrary");
let test = Test::gnu();
test.gcc().file("foo.cpp").cpp(true).compile("foo");

test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
}
33 changes: 28 additions & 5 deletions tests/support/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
use std::env;
use std::ffi::{OsStr, OsString};
use std::fs::{self, File};
use std::io;
use std::io::prelude::*;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use cc;
use tempdir::TempDir;
Expand Down Expand Up @@ -49,10 +50,13 @@ impl Test {
}

pub fn shim(&self, name: &str) -> &Test {
let fname = format!("{}{}", name, env::consts::EXE_SUFFIX);
fs::hard_link(&self.gcc, self.td.path().join(&fname))
.or_else(|_| fs::copy(&self.gcc, self.td.path().join(&fname)).map(|_| ()))
.unwrap();
link_or_copy(
&self.gcc,
self.td
.path()
.join(&format!("{}{}", name, env::consts::EXE_SUFFIX)),
)
.unwrap();
self
}

Expand Down Expand Up @@ -136,3 +140,22 @@ impl Execution {
self
}
}

/// Hard link an executable or copy it if that fails.
///
/// We first try to hard link an executable to save space. If that fails (as on Windows with
/// different mount points, issue #60), we copy.
#[cfg(not(target_os = "macos"))]
fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
let from = from.as_ref();
let to = to.as_ref();
fs::hard_link(from, to).or_else(|_| fs::copy(from, to).map(|_| ()))
}

/// Copy an executable.
///
/// On macOS, hard linking the executable leads to strange failures (issue #419), so we just copy.
#[cfg(target_os = "macos")]
fn link_or_copy<P: AsRef<Path>, Q: AsRef<Path>>(from: P, to: Q) -> io::Result<()> {
fs::copy(from, to).map(|_| ())
}
21 changes: 0 additions & 21 deletions tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
extern crate cc;
extern crate tempdir;

use std::env;
use support::Test;

mod support;
Expand Down Expand Up @@ -111,26 +110,6 @@ fn gnu_warnings_overridable() {
.must_have_in_order("-Wall", "-Wno-missing-field-initializers");
}

#[test]
fn gnu_no_warnings_if_cflags() {
env::set_var("CFLAGS", "-Wflag-does-not-exist");
let test = Test::gnu();
test.gcc().file("foo.c").compile("foo");

test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
env::set_var("CFLAGS", "");
}

#[test]
fn gnu_no_warnings_if_cxxflags() {
env::set_var("CXXFLAGS", "-Wflag-does-not-exist");
let test = Test::gnu();
test.gcc().file("foo.c").compile("foo");

test.cmd(0).must_not_have("-Wall").must_not_have("-Wextra");
env::set_var("CXXFLAGS", "");
}

#[test]
fn gnu_x86_64() {
for vendor in &["unknown-linux-gnu", "apple-darwin"] {
Expand Down