Skip to content

Commit 512d2f5

Browse files
committed
add crate_env_vars_with_workspace test
1 parent ebe1e7b commit 512d2f5

File tree

1 file changed

+203
-1
lines changed

1 file changed

+203
-1
lines changed

tests/testsuite/build.rs

Lines changed: 203 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1383,7 +1383,7 @@ fn cargo_default_env_metadata_env_var() {
13831383
}
13841384

13851385
#[cargo_test]
1386-
fn crate_env_vars() {
1386+
fn crate_env_vars_without_workspace() {
13871387
let p = project()
13881388
.file(
13891389
"Cargo.toml",
@@ -1578,6 +1578,208 @@ fn crate_env_vars() {
15781578
}
15791579
}
15801580

1581+
#[cargo_test]
1582+
fn crate_env_vars_with_workspace() {
1583+
let p = project()
1584+
.file(
1585+
"Cargo.toml",
1586+
r#"
1587+
[workspace]
1588+
members = ["foo"]
1589+
"#,
1590+
)
1591+
.file(
1592+
"foo/Cargo.toml",
1593+
r#"
1594+
[package]
1595+
name = "foo"
1596+
version = "0.5.1-alpha.1"
1597+
description = "This is foo"
1598+
homepage = "https://example.com"
1599+
repository = "https://example.com/repo.git"
1600+
authors = ["[email protected]"]
1601+
license = "MIT OR Apache-2.0"
1602+
license-file = "license.txt"
1603+
rust-version = "1.61.0"
1604+
readme = "../../README.md"
1605+
1606+
[[bin]]
1607+
name = "foo-bar"
1608+
path = "src/main.rs"
1609+
"#,
1610+
)
1611+
.file(
1612+
"foo/src/main.rs",
1613+
r#"
1614+
extern crate foo;
1615+
1616+
static VERSION_MAJOR: &'static str = env!("CARGO_PKG_VERSION_MAJOR");
1617+
static VERSION_MINOR: &'static str = env!("CARGO_PKG_VERSION_MINOR");
1618+
static VERSION_PATCH: &'static str = env!("CARGO_PKG_VERSION_PATCH");
1619+
static VERSION_PRE: &'static str = env!("CARGO_PKG_VERSION_PRE");
1620+
static VERSION: &'static str = env!("CARGO_PKG_VERSION");
1621+
static CARGO_MANIFEST_DIR: &'static str = env!("CARGO_MANIFEST_DIR");
1622+
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
1623+
static HOMEPAGE: &'static str = env!("CARGO_PKG_HOMEPAGE");
1624+
static REPOSITORY: &'static str = env!("CARGO_PKG_REPOSITORY");
1625+
static LICENSE: &'static str = env!("CARGO_PKG_LICENSE");
1626+
static LICENSE_FILE: &'static str = env!("CARGO_PKG_LICENSE_FILE");
1627+
static DESCRIPTION: &'static str = env!("CARGO_PKG_DESCRIPTION");
1628+
static RUST_VERSION: &'static str = env!("CARGO_PKG_RUST_VERSION");
1629+
static README: &'static str = env!("CARGO_PKG_README");
1630+
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
1631+
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
1632+
1633+
1634+
fn main() {
1635+
let s = format!("{}-{}-{} @ {} in {}", VERSION_MAJOR,
1636+
VERSION_MINOR, VERSION_PATCH, VERSION_PRE,
1637+
CARGO_MANIFEST_DIR);
1638+
assert_eq!(s, foo::version());
1639+
println!("{}", s);
1640+
assert_eq!("foo", PKG_NAME);
1641+
assert_eq!("foo-bar", BIN_NAME);
1642+
assert_eq!("foo_bar", CRATE_NAME);
1643+
assert_eq!("https://example.com", HOMEPAGE);
1644+
assert_eq!("https://example.com/repo.git", REPOSITORY);
1645+
assert_eq!("MIT OR Apache-2.0", LICENSE);
1646+
assert_eq!("license.txt", LICENSE_FILE);
1647+
assert_eq!("This is foo", DESCRIPTION);
1648+
assert_eq!("1.61.0", RUST_VERSION);
1649+
assert_eq!("../../README.md", README);
1650+
let s = format!("{}.{}.{}-{}", VERSION_MAJOR,
1651+
VERSION_MINOR, VERSION_PATCH, VERSION_PRE);
1652+
assert_eq!(s, VERSION);
1653+
1654+
// Verify CARGO_TARGET_TMPDIR isn't set for bins
1655+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1656+
1657+
// Verify CARGO_WORKSPACE_DIR isn't set for bins
1658+
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1659+
}
1660+
"#,
1661+
)
1662+
.file(
1663+
"foo/src/lib.rs",
1664+
r#"
1665+
use std::env;
1666+
use std::path::PathBuf;
1667+
1668+
pub fn version() -> String {
1669+
format!("{}-{}-{} @ {} in {}",
1670+
env!("CARGO_PKG_VERSION_MAJOR"),
1671+
env!("CARGO_PKG_VERSION_MINOR"),
1672+
env!("CARGO_PKG_VERSION_PATCH"),
1673+
env!("CARGO_PKG_VERSION_PRE"),
1674+
env!("CARGO_MANIFEST_DIR"))
1675+
}
1676+
1677+
pub fn check_no_int_test_env() {
1678+
env::var("CARGO_TARGET_DIR").unwrap_err();
1679+
}
1680+
1681+
pub fn check_tmpdir(tmp: Option<&'static str>) {
1682+
let tmpdir: PathBuf = tmp.unwrap().into();
1683+
1684+
let exe: PathBuf = env::current_exe().unwrap().into();
1685+
let mut expected: PathBuf = exe.parent().unwrap()
1686+
.parent().unwrap()
1687+
.parent().unwrap()
1688+
.into();
1689+
expected.push("tmp");
1690+
assert_eq!(tmpdir, expected);
1691+
1692+
// Check that CARGO_TARGET_TMPDIR isn't set for lib code
1693+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1694+
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1695+
}
1696+
1697+
#[test]
1698+
fn env() {
1699+
// Check that CARGO_TARGET_TMPDIR isn't set for unit tests
1700+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1701+
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1702+
1703+
// Check that CARGO_WORKSPACE_DIR isn't set for unit tests
1704+
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1705+
env::var("CARGO_WORKSPACE_DIR").unwrap_err();
1706+
}
1707+
"#,
1708+
)
1709+
.file(
1710+
"foo/examples/ex-env-vars.rs",
1711+
r#"
1712+
static PKG_NAME: &'static str = env!("CARGO_PKG_NAME");
1713+
static BIN_NAME: &'static str = env!("CARGO_BIN_NAME");
1714+
static CRATE_NAME: &'static str = env!("CARGO_CRATE_NAME");
1715+
1716+
fn main() {
1717+
assert_eq!("foo", PKG_NAME);
1718+
assert_eq!("ex-env-vars", BIN_NAME);
1719+
assert_eq!("ex_env_vars", CRATE_NAME);
1720+
1721+
// Verify CARGO_TARGET_TMPDIR isn't set for examples
1722+
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1723+
1724+
// Verify CARGO_WORKSPACE_DIR isn't set for examples
1725+
assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1726+
}
1727+
"#,
1728+
)
1729+
.file(
1730+
"foo/tests/env.rs",
1731+
r#"
1732+
use std::path::Path;
1733+
1734+
#[test]
1735+
fn env() {
1736+
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1737+
assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1738+
}
1739+
"#,
1740+
);
1741+
1742+
let p = if is_nightly() {
1743+
p.file(
1744+
"foo/benches/env.rs",
1745+
r#"
1746+
#![feature(test)]
1747+
extern crate test;
1748+
use std::path::Path;
1749+
use test::Bencher;
1750+
1751+
#[bench]
1752+
fn env(_: &mut Bencher) {
1753+
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1754+
assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1755+
}
1756+
"#,
1757+
)
1758+
.build()
1759+
} else {
1760+
p.build()
1761+
};
1762+
1763+
println!("build");
1764+
p.cargo("build -v").run();
1765+
1766+
println!("bin");
1767+
p.process(&p.bin("foo-bar"))
1768+
.with_stdout("0-5-1 @ alpha.1 in [CWD]/foo")
1769+
.run();
1770+
1771+
println!("example");
1772+
p.cargo("run --example ex-env-vars -v").run();
1773+
1774+
println!("test");
1775+
p.cargo("test -v").run();
1776+
1777+
if is_nightly() {
1778+
println!("bench");
1779+
p.cargo("bench -v").run();
1780+
}
1781+
}
1782+
15811783
#[cargo_test]
15821784
fn crate_authors_env_vars() {
15831785
let p = project()

0 commit comments

Comments
 (0)