@@ -1477,7 +1477,7 @@ fn cargo_default_env_metadata_env_var() {
1477
1477
}
1478
1478
1479
1479
#[ cargo_test]
1480
- fn crate_env_vars ( ) {
1480
+ fn crate_env_vars_without_workspace ( ) {
1481
1481
let p = project ( )
1482
1482
. file (
1483
1483
"Cargo.toml" ,
@@ -1545,6 +1545,9 @@ fn crate_env_vars() {
1545
1545
1546
1546
// Verify CARGO_TARGET_TMPDIR isn't set for bins
1547
1547
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1548
+
1549
+ // Verify CARGO_WORKSPACE_DIR isn't set for bins
1550
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1548
1551
}
1549
1552
"# ,
1550
1553
)
@@ -1588,6 +1591,10 @@ fn crate_env_vars() {
1588
1591
// Check that CARGO_TARGET_TMPDIR isn't set for unit tests
1589
1592
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1590
1593
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1594
+
1595
+ // Check that CARGO_WORKSPACE_DIR isn't set for unit tests
1596
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1597
+ env::var("CARGO_WORKSPACE_DIR").unwrap_err();
1591
1598
}
1592
1599
"# ,
1593
1600
)
@@ -1605,6 +1612,9 @@ fn crate_env_vars() {
1605
1612
1606
1613
// Verify CARGO_TARGET_TMPDIR isn't set for examples
1607
1614
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1615
+
1616
+ // Verify CARGO_WORKSPACE_DIR isn't set for examples
1617
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1608
1618
}
1609
1619
"# ,
1610
1620
)
@@ -1614,6 +1624,9 @@ fn crate_env_vars() {
1614
1624
#[test]
1615
1625
fn env() {
1616
1626
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1627
+
1628
+ // Verify CARGO_WORKSPACE_DIR isn't set for integration tests without masquerade_as_nightly_cargo()
1629
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1617
1630
}
1618
1631
"# ,
1619
1632
) ;
@@ -1629,6 +1642,9 @@ fn crate_env_vars() {
1629
1642
#[bench]
1630
1643
fn env(_: &mut Bencher) {
1631
1644
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1645
+
1646
+ // Verify CARGO_WORKSPACE_DIR isn't set for benches without masquerade_as_nightly_cargo()
1647
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1632
1648
}
1633
1649
"# ,
1634
1650
)
@@ -1657,6 +1673,232 @@ fn crate_env_vars() {
1657
1673
}
1658
1674
}
1659
1675
1676
+ #[ cargo_test]
1677
+ fn nightly_cargo_workspace_dir_env_var_with_workspace ( ) {
1678
+ Package :: new ( "bar" , "0.1.0" )
1679
+ . file ( "src/lib.rs" , "#[test] fn bar() {}" )
1680
+ . file (
1681
+ "tests/env.rs" ,
1682
+ r#"
1683
+ use std::path::Path;
1684
+
1685
+ #[test]
1686
+ fn env() {
1687
+ assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1688
+ assert_eq!(std::fs::canonicalize(option_env!("CARGO_WORKSPACE_DIR").unwrap()).unwrap().display().to_string(), option_env!("CARGO_MANIFEST_DIR").unwrap());
1689
+ }
1690
+ "# ,
1691
+ )
1692
+ . publish ( ) ;
1693
+
1694
+ let p = project ( )
1695
+ . file (
1696
+ "Cargo.toml" ,
1697
+ r#"
1698
+ [workspace]
1699
+ members = ["foo"]
1700
+ "# ,
1701
+ )
1702
+ . file (
1703
+ "foo/Cargo.toml" ,
1704
+ r#"
1705
+ [package]
1706
+ name = "foo"
1707
+ version = "0.0.1"
1708
+ authors = []
1709
+
1710
+ [dependencies]
1711
+ bar = "0.1.0"
1712
+
1713
+ [[bin]]
1714
+ name = "foo-bar"
1715
+ path = "src/main.rs"
1716
+ "# ,
1717
+ )
1718
+ . file (
1719
+ "foo/src/main.rs" ,
1720
+ r#"
1721
+ fn main() {
1722
+ // Verify CARGO_WORKSPACE_DIR isn't set for bins
1723
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1724
+ }
1725
+ "# ,
1726
+ )
1727
+ . file (
1728
+ "foo/src/lib.rs" ,
1729
+ r#"
1730
+ use std::env;
1731
+
1732
+ #[test]
1733
+ fn env() {
1734
+ // Check that CARGO_WORKSPACE_DIR isn't set for unit tests
1735
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1736
+ env::var("CARGO_WORKSPACE_DIR").unwrap_err();
1737
+ }
1738
+ "# ,
1739
+ )
1740
+ . file (
1741
+ "foo/examples/ex-env-vars.rs" ,
1742
+ r#"
1743
+ fn main() {
1744
+ // Verify CARGO_WORKSPACE_DIR isn't set for examples
1745
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1746
+ }
1747
+ "# ,
1748
+ )
1749
+ . file (
1750
+ "foo/tests/env.rs" ,
1751
+ r#"
1752
+ use std::path::Path;
1753
+
1754
+ #[test]
1755
+ fn env() {
1756
+ assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1757
+ }
1758
+ "# ,
1759
+ ) ;
1760
+
1761
+ let p = if is_nightly ( ) {
1762
+ p. file (
1763
+ "foo/benches/env.rs" ,
1764
+ r#"
1765
+ #![feature(test)]
1766
+ extern crate test;
1767
+ use std::path::Path;
1768
+ use test::Bencher;
1769
+
1770
+ #[bench]
1771
+ fn env(_: &mut Bencher) {
1772
+ assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1773
+ }
1774
+ "# ,
1775
+ )
1776
+ . build ( )
1777
+ } else {
1778
+ p. build ( )
1779
+ } ;
1780
+
1781
+ println ! ( "build" ) ;
1782
+ p. cargo ( "build -v" ) . run ( ) ;
1783
+
1784
+ println ! ( "bin" ) ;
1785
+ p. process ( & p. bin ( "foo-bar" ) ) . run ( ) ;
1786
+
1787
+ println ! ( "example" ) ;
1788
+ p. cargo ( "run --example ex-env-vars -v" ) . run ( ) ;
1789
+
1790
+ println ! ( "test" ) ;
1791
+ p. cargo ( "test -v" ) . masquerade_as_nightly_cargo ( & [ ] ) . run ( ) ;
1792
+
1793
+ if is_nightly ( ) {
1794
+ println ! ( "bench" ) ;
1795
+ p. cargo ( "bench -v" ) . masquerade_as_nightly_cargo ( & [ ] ) . run ( ) ;
1796
+ }
1797
+
1798
+ p. cargo ( "test -p bar" )
1799
+ . masquerade_as_nightly_cargo ( & [ ] )
1800
+ . with_stdout_contains ( "running 1 test\n test bar ... ok" )
1801
+ . run ( ) ;
1802
+ }
1803
+
1804
+ #[ cargo_test]
1805
+ fn nightly_cargo_workspace_dir_env_var_without_workspace ( ) {
1806
+ let p = project ( )
1807
+ . file (
1808
+ "Cargo.toml" ,
1809
+ r#"
1810
+ [package]
1811
+ name = "foo"
1812
+ version = "0.0.1"
1813
+ authors = []
1814
+
1815
+ [[bin]]
1816
+ name = "foo-bar"
1817
+ path = "src/main.rs"
1818
+ "# ,
1819
+ )
1820
+ . file (
1821
+ "src/main.rs" ,
1822
+ r#"
1823
+ extern crate foo;
1824
+
1825
+ fn main() {
1826
+ // Verify CARGO_WORKSPACE_DIR isn't set for bins
1827
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1828
+ }
1829
+ "# ,
1830
+ )
1831
+ . file (
1832
+ "src/lib.rs" ,
1833
+ r#"
1834
+ use std::env;
1835
+ #[test]
1836
+ fn env() {
1837
+ // Check that CARGO_WORKSPACE_DIR isn't set for unit tests
1838
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1839
+ env::var("CARGO_WORKSPACE_DIR").unwrap_err();
1840
+ }
1841
+ "# ,
1842
+ )
1843
+ . file (
1844
+ "examples/ex-env-vars.rs" ,
1845
+ r#"
1846
+ fn main() {
1847
+ // Verify CARGO_WORKSPACE_DIR isn't set for examples
1848
+ assert!(option_env!("CARGO_WORKSPACE_DIR").is_none());
1849
+ }
1850
+ "# ,
1851
+ )
1852
+ . file (
1853
+ "tests/env.rs" ,
1854
+ r#"
1855
+ use std::path::Path;
1856
+
1857
+ #[test]
1858
+ fn env() {
1859
+ assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1860
+ }
1861
+ "# ,
1862
+ ) ;
1863
+
1864
+ let p = if is_nightly ( ) {
1865
+ p. file (
1866
+ "benches/env.rs" ,
1867
+ r#"
1868
+ #![feature(test)]
1869
+ extern crate test;
1870
+ use std::path::Path;
1871
+ use test::Bencher;
1872
+
1873
+ #[bench]
1874
+ fn env(_: &mut Bencher) {
1875
+ assert!(Path::new(option_env!("CARGO_WORKSPACE_DIR").unwrap()).join(file!()).exists());
1876
+ }
1877
+ "# ,
1878
+ )
1879
+ . build ( )
1880
+ } else {
1881
+ p. build ( )
1882
+ } ;
1883
+
1884
+ println ! ( "build" ) ;
1885
+ p. cargo ( "build -v" ) . run ( ) ;
1886
+
1887
+ println ! ( "bin" ) ;
1888
+ p. process ( & p. bin ( "foo-bar" ) ) . run ( ) ;
1889
+
1890
+ println ! ( "example" ) ;
1891
+ p. cargo ( "run --example ex-env-vars -v" ) . run ( ) ;
1892
+
1893
+ println ! ( "test" ) ;
1894
+ p. cargo ( "test -v" ) . masquerade_as_nightly_cargo ( & [ ] ) . run ( ) ;
1895
+
1896
+ if is_nightly ( ) {
1897
+ println ! ( "bench" ) ;
1898
+ p. cargo ( "bench -v" ) . masquerade_as_nightly_cargo ( & [ ] ) . run ( ) ;
1899
+ }
1900
+ }
1901
+
1660
1902
#[ cargo_test]
1661
1903
fn crate_authors_env_vars ( ) {
1662
1904
let p = project ( )
0 commit comments