Browse Source

Merge pull request #421 from spl/serialize-env-tests

Fix occasional test failure due to env::set_var with multiple test threads
wip-new-parallel
Alex Crichton 6 years ago
committed by GitHub
parent
commit
fb1ae59798
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      ci/azure-steps.yml
  2. 18
      tests/cflags.rs
  3. 18
      tests/cxxflags.rs
  4. 31
      tests/support/mod.rs
  5. 21
      tests/test.rs

4
ci/azure-steps.yml

@ -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"

18
tests/cflags.rs

@ -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
tests/cxxflags.rs

@ -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");
}

31
tests/support/mod.rs

@ -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;
@ -49,9 +50,12 @@ 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(|_| ()))
link_or_copy(
&self.gcc,
self.td
.path()
.join(&format!("{}{}", name, env::consts::EXE_SUFFIX)),
)
.unwrap();
self
}
@ -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
tests/test.rs

@ -1,7 +1,6 @@
extern crate cc;
extern crate tempdir;
use std::env;
use support::Test;
mod support;
@ -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"] {

Loading…
Cancel
Save