Skip to content

Commit 64bfe7f

Browse files
committed
Add CARGO_TARGET_TMPDIR env var for integration tests & benches
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.
1 parent 29ea494 commit 64bfe7f

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

src/cargo/core/compiler/layout.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ pub struct Layout {
125125
examples: PathBuf,
126126
/// The directory for rustdoc output: `$root/doc`
127127
doc: PathBuf,
128+
/// The directory for temporary data of integration tests and benches: `$dest/tmp`
129+
tmp: PathBuf,
128130
/// The lockfile for a build (`.cargo-lock`). Will be unlocked when this
129131
/// struct is `drop`ped.
130132
_lock: FileLock,
@@ -170,6 +172,7 @@ impl Layout {
170172
fingerprint: dest.join(".fingerprint"),
171173
examples: dest.join("examples"),
172174
doc: root.join("doc"),
175+
tmp: dest.join("tmp"),
173176
root,
174177
dest,
175178
_lock: lock,
@@ -219,4 +222,9 @@ impl Layout {
219222
pub fn build(&self) -> &Path {
220223
&self.build
221224
}
225+
/// Create and return the tmp path.
226+
pub fn prepare_tmp(&self) -> CargoResult<&Path> {
227+
paths::create_dir_all(&self.tmp)?;
228+
Ok(&self.tmp)
229+
}
222230
}

src/cargo/core/compiler/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -581,6 +581,11 @@ fn prepare_rustc(
581581
base.env("CARGO_PRIMARY_PACKAGE", "1");
582582
}
583583

584+
if unit.target.is_test() || unit.target.is_bench() {
585+
let tmp = cx.files().layout(unit.kind).prepare_tmp()?;
586+
base.env("CARGO_TARGET_TMPDIR", tmp.display().to_string());
587+
}
588+
584589
if cx.bcx.config.cli_unstable().jobserver_per_rustc {
585590
let client = cx.new_jobserver()?;
586591
base.inherit_jobserver(&client);

src/doc/src/reference/environment-variables.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,12 @@ corresponding environment variable is set to the empty string, `""`.
221221
on the current directory and the default workspace members. This environment
222222
variable will not be set when building dependencies. This is only set when
223223
compiling the package (not when running binaries or tests).
224+
* `CARGO_TARGET_TMPDIR` — Only set when building [integration test] or benchmark code.
225+
This is a path to a directory inside the target directory
226+
where integration tests or benchmarks are free to put any data needed by
227+
the tests/benches. Cargo initially creates this directory but doesn't
228+
manage its content in any way, this is the responsibility of the test code.
229+
There are separate directories for `debug` and `release` profiles.
224230

225231
[integration test]: cargo-targets.md#integration-tests
226232
[`env` macro]: ../../std/macro.env.html

tests/testsuite/build.rs

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1334,12 +1334,18 @@ fn crate_env_vars() {
13341334
let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
13351335
VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
13361336
assert_eq!(s, VERSION);
1337+
1338+
// Verify CARGO_TARGET_TMPDIR isn't set for bins
1339+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
13371340
}
13381341
"#,
13391342
)
13401343
.file(
13411344
"src/lib.rs",
13421345
r#"
1346+
use std::env;
1347+
use std::path::PathBuf;
1348+
13431349
pub fn version() -> String {
13441350
format!("{}-{}-{} @ {} in {}",
13451351
env!("CARGO_PKG_VERSION_MAJOR"),
@@ -1348,9 +1354,60 @@ fn crate_env_vars() {
13481354
env!("CARGO_PKG_VERSION_PRE"),
13491355
env!("CARGO_MANIFEST_DIR"))
13501356
}
1357+
1358+
pub fn check_no_int_test_env() {
1359+
env::var("CARGO_TARGET_DIR").unwrap_err();
1360+
}
1361+
1362+
pub fn check_tmpdir(tmp: Option<&'static str>) {
1363+
let tmpdir: PathBuf = tmp.unwrap().into();
1364+
1365+
let exe: PathBuf = env::current_exe().unwrap().into();
1366+
let mut expected: PathBuf = exe.parent().unwrap().parent().unwrap().into();
1367+
expected.push("tmp");
1368+
assert_eq!(tmpdir, expected);
1369+
1370+
// Check that CARGO_TARGET_TMPDIR isn't set for lib code
1371+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1372+
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1373+
}
1374+
1375+
#[test]
1376+
fn env() {
1377+
// Check that CARGO_TARGET_TMPDIR isn't set for unit tests
1378+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1379+
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1380+
}
13511381
"#,
13521382
)
1353-
.build();
1383+
.file(
1384+
"tests/env.rs",
1385+
r#"
1386+
#[test]
1387+
fn env() {
1388+
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1389+
}
1390+
"#,
1391+
);
1392+
1393+
let p = if is_nightly() {
1394+
p.file(
1395+
"benches/env.rs",
1396+
r#"
1397+
#![feature(test)]
1398+
extern crate test;
1399+
use test::Bencher;
1400+
1401+
#[bench]
1402+
fn env(_: &mut Bencher) {
1403+
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1404+
}
1405+
"#,
1406+
)
1407+
.build()
1408+
} else {
1409+
p.build()
1410+
};
13541411

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

13631420
println!("test");
13641421
p.cargo("test -v").run();
1422+
1423+
if is_nightly() {
1424+
println!("bench");
1425+
p.cargo("bench -v").run();
1426+
}
13651427
}
13661428

13671429
#[cargo_test]

0 commit comments

Comments
 (0)