Skip to content

Commit

Permalink
Add CARGO_TARGET_TMPDIR env var for integration tests & benches
Browse files Browse the repository at this point in the history
The variable is set to $target_dir/$config/tmp
This is a directory where tests & benches can place testcasei-related data
for use by the tests.
cargo makes sure the directory exists when building tests/benches.
  • Loading branch information
vojtechkral committed May 6, 2021
1 parent 29ea494 commit 64bfe7f
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/cargo/core/compiler/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ pub struct Layout {
examples: PathBuf,
/// The directory for rustdoc output: `$root/doc`
doc: PathBuf,
/// The directory for temporary data of integration tests and benches: `$dest/tmp`
tmp: PathBuf,
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
/// struct is `drop`ped.
_lock: FileLock,
Expand Down Expand Up @@ -170,6 +172,7 @@ impl Layout {
fingerprint: dest.join(".fingerprint"),
examples: dest.join("examples"),
doc: root.join("doc"),
tmp: dest.join("tmp"),
root,
dest,
_lock: lock,
Expand Down Expand Up @@ -219,4 +222,9 @@ impl Layout {
pub fn build(&self) -> &Path {
&self.build
}
/// Create and return the tmp path.
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
paths::create_dir_all(&self.tmp)?;
Ok(&self.tmp)
}
}
5 changes: 5 additions & 0 deletions src/cargo/core/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,11 @@ fn prepare_rustc(
base.env("CARGO_PRIMARY_PACKAGE", "1");
}

if unit.target.is_test() || unit.target.is_bench() {
let tmp = cx.files().layout(unit.kind).prepare_tmp()?;
base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
}

if cx.bcx.config.cli_unstable().jobserver_per_rustc {
let client = cx.new_jobserver()?;
base.inherit_jobserver(&client);
Expand Down
6 changes: 6 additions & 0 deletions src/doc/src/reference/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ corresponding environment variable is set to the empty string, `""`.
on the current directory and the default workspace members. This environment
variable will not be set when building dependencies. This is only set when
compiling the package (not when running binaries or tests).
* `CARGO_TARGET_TMPDIR` — Only set when building [integration test] or benchmark code.
This is a path to a directory inside the target directory
where integration tests or benchmarks are free to put any data needed by
the tests/benches. Cargo initially creates this directory but doesn't
manage its content in any way, this is the responsibility of the test code.
There are separate directories for `debug` and `release` profiles.

[integration test]: cargo-targets.md#integration-tests
[`env` macro]: ../../std/macro.env.html
Expand Down
64 changes: 63 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1334,12 +1334,18 @@ fn crate_env_vars() {
let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
assert_eq!(s, VERSION);
// Verify CARGO_TARGET_TMPDIR isn't set for bins
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
}
"#,
)
.file(
"src/lib.rs",
r#"
use std::env;
use std::path::PathBuf;
pub fn version() -> String {
format!("{}-{}-{} @ {} in {}",
env!("CARGO_PKG_VERSION_MAJOR"),
Expand All @@ -1348,9 +1354,60 @@ fn crate_env_vars() {
env!("CARGO_PKG_VERSION_PRE"),
env!("CARGO_MANIFEST_DIR"))
}
pub fn check_no_int_test_env() {
env::var("CARGO_TARGET_DIR").unwrap_err();
}
pub fn check_tmpdir(tmp: Option<&'static str>) {
let tmpdir: PathBuf = tmp.unwrap().into();
let exe: PathBuf = env::current_exe().unwrap().into();
let mut expected: PathBuf = exe.parent().unwrap().parent().unwrap().into();
expected.push("tmp");
assert_eq!(tmpdir, expected);
// Check that CARGO_TARGET_TMPDIR isn't set for lib code
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
}
#[test]
fn env() {
// Check that CARGO_TARGET_TMPDIR isn't set for unit tests
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
}
"#,
)
.build();
.file(
"tests/env.rs",
r#"
#[test]
fn env() {
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
}
"#,
);

let p = if is_nightly() {
p.file(
"benches/env.rs",
r#"
#![feature(test)]
extern crate test;
use test::Bencher;
#[bench]
fn env(_: &mut Bencher) {
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
}
"#,
)
.build()
} else {
p.build()
};

println!("build");
p.cargo("build -v").run();
Expand All @@ -1362,6 +1419,11 @@ fn crate_env_vars() {

println!("test");
p.cargo("test -v").run();

if is_nightly() {
println!("bench");
p.cargo("bench -v").run();
}
}

#[cargo_test]
Expand Down

0 comments on commit 64bfe7f

Please sign in to comment.