Skip to content

Commit

Permalink
add crate_env_vars_with_workspace test
Browse files Browse the repository at this point in the history
  • Loading branch information
yerke committed May 19, 2023
1 parent ebe1e7b commit 512d2f5
Showing 1 changed file with 203 additions and 1 deletion.
204 changes: 203 additions & 1 deletion tests/testsuite/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1383,7 +1383,7 @@ fn cargo_default_env_metadata_env_var() {
}

#[cargo_test]
fn crate_env_vars() {
fn crate_env_vars_without_workspace() {
let p = project()
.file(
"Cargo.toml",
Expand Down Expand Up @@ -1578,6 +1578,208 @@ fn crate_env_vars() {
}
}

#[cargo_test]
fn crate_env_vars_with_workspace() {
let p = project()
.file(
"Cargo.toml",
r#"
[workspace]
members = ["foo"]
"#,
)
.file(
"foo/Cargo.toml",
r#"
[package]
name = "foo"
version = "0.5.1-alpha.1"
description = "This is foo"
homepage = "https://example.com"
repository = "https://example.com/repo.git"
authors = ["[email protected]"]
license = "MIT OR Apache-2.0"
license-file = "license.txt"
rust-version = "1.61.0"
readme = "../../README.md"
[[bin]]
name = "foo-bar"
path = "src/main.rs"
"#,
)
.file(
"foo/src/main.rs",
r#"
extern crate foo;
static VERSION_MAJOR: &'static str = env!("CARGO_PKG_VERSION_MAJOR");
static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE");
static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY");
static LICENSE: &'static str = env!("CARGO_PKG_LICENSE");
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
static README: &'static str = env!("CARGO_PKG_README");
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
fn main() {
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
CARGO_MANIFEST_DIR);
assert_eq!(s, foo::version());
println!("{}", s);
assert_eq!("foo", PKG_NAME);
assert_eq!("foo-bar", BIN_NAME);
assert_eq!("foo_bar", CRATE_NAME);
assert_eq!("https://example.com", HOMEPAGE);
assert_eq!("https://example.com/repo.git", REPOSITORY);
assert_eq!("MIT OR Apache-2.0", LICENSE);
assert_eq!("license.txt", LICENSE_FILE);
assert_eq!("This is foo", DESCRIPTION);
assert_eq!("1.61.0", RUST_VERSION);
assert_eq!("../../README.md", README);
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());
// Verify CARGO_WORKSPACE_DIR isn't set for bins
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
}
"#,
)
.file(
"foo/src/lib.rs",
r#"
use std::env;
use std::path::PathBuf;
pub fn version() -> String {
format!("{}-{}-{} @ {} in {}",
env!("CARGO_PKG_VERSION_MAJOR"),
env!("CARGO_PKG_VERSION_MINOR"),
env!("CARGO_PKG_VERSION_PATCH"),
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()
.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();
// Check that CARGO_WORKSPACE_DIR isn't set for unit tests
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
env::var("CARGO_WORKSPACE_DIR").unwrap_err();
}
"#,
)
.file(
"foo/examples/ex-env-vars.rs",
r#"
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
fn main() {
assert_eq!("foo", PKG_NAME);
assert_eq!("ex-env-vars", BIN_NAME);
assert_eq!("ex_env_vars", CRATE_NAME);
// Verify CARGO_TARGET_TMPDIR isn't set for examples
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
// Verify CARGO_WORKSPACE_DIR isn't set for examples
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
}
"#,
)
.file(
"foo/tests/env.rs",
r#"
use std::path::Path;
#[test]
fn env() {
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
}
"#,
);

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

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

println!("bin");
p.process(&p.bin("foo-bar"))
.with_stdout("0-5-1 @ alpha.1 in [CWD]/foo")
.run();

println!("example");
p.cargo("run --example ex-env-vars -v").run();

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

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

#[cargo_test]
fn crate_authors_env_vars() {
let p = project()
Expand Down

0 comments on commit 512d2f5

Please sign in to comment.