@@ -1545,6 +1545,11 @@ 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_RUSTC_CURRENT_DIR is set for examples
1550
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1551
+ let file_path = workspace_dir.join(file!());
1552
+ assert!(file_path.exists(), "{}", file_path.display());
1548
1553
}
1549
1554
"# ,
1550
1555
)
@@ -1581,6 +1586,11 @@ fn crate_env_vars() {
1581
1586
// Check that CARGO_TARGET_TMPDIR isn't set for lib code
1582
1587
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1583
1588
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1589
+
1590
+ // Verify CARGO_RUSTC_CURRENT_DIR is set for examples
1591
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1592
+ let file_path = workspace_dir.join(file!());
1593
+ assert!(file_path.exists(), "{}", file_path.display());
1584
1594
}
1585
1595
1586
1596
#[test]
@@ -1589,6 +1599,13 @@ fn crate_env_vars() {
1589
1599
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1590
1600
env::var("CARGO_TARGET_TMPDIR").unwrap_err();
1591
1601
}
1602
+
1603
+ #[test]
1604
+ fn unit_env_cargo_rustc_current_dir() {
1605
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1606
+ let file_path = workspace_dir.join(file!());
1607
+ assert!(file_path.exists(), "{}", file_path.display());
1608
+ }
1592
1609
"# ,
1593
1610
)
1594
1611
. file (
@@ -1605,6 +1622,11 @@ fn crate_env_vars() {
1605
1622
1606
1623
// Verify CARGO_TARGET_TMPDIR isn't set for examples
1607
1624
assert!(option_env!("CARGO_TARGET_TMPDIR").is_none());
1625
+
1626
+ // Verify CARGO_RUSTC_CURRENT_DIR is set for examples
1627
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1628
+ let file_path = workspace_dir.join(file!());
1629
+ assert!(file_path.exists(), "{}", file_path.display());
1608
1630
}
1609
1631
"# ,
1610
1632
)
@@ -1615,6 +1637,13 @@ fn crate_env_vars() {
1615
1637
fn integration_env_cargo_target_tmpdir() {
1616
1638
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1617
1639
}
1640
+
1641
+ #[test]
1642
+ fn integration_env_cargo_rustc_current_dir() {
1643
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1644
+ let file_path = workspace_dir.join(file!());
1645
+ assert!(file_path.exists(), "{}", file_path.display());
1646
+ }
1618
1647
"# ,
1619
1648
) ;
1620
1649
@@ -1630,6 +1659,13 @@ fn crate_env_vars() {
1630
1659
fn bench_env_cargo_target_tmpdir(_: &mut Bencher) {
1631
1660
foo::check_tmpdir(option_env!("CARGO_TARGET_TMPDIR"));
1632
1661
}
1662
+
1663
+ #[test]
1664
+ fn bench_env_cargo_rustc_current_dir() {
1665
+ let workspace_dir = std::path::Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1666
+ let file_path = workspace_dir.join(file!());
1667
+ assert!(file_path.exists(), "{}", file_path.display());
1668
+ }
1633
1669
"# ,
1634
1670
)
1635
1671
. build ( )
@@ -1638,23 +1674,185 @@ fn crate_env_vars() {
1638
1674
} ;
1639
1675
1640
1676
println ! ( "build" ) ;
1641
- p. cargo ( "build -v" ) . run ( ) ;
1677
+ p. cargo ( "build -v" )
1678
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1679
+ . run ( ) ;
1642
1680
1643
1681
println ! ( "bin" ) ;
1644
1682
p. process ( & p. bin ( "foo-bar" ) )
1645
1683
. with_stdout ( "0-5-1 @ alpha.1 in [CWD]" )
1646
1684
. run ( ) ;
1647
1685
1648
1686
println ! ( "example" ) ;
1649
- p. cargo ( "run --example ex-env-vars -v" ) . run ( ) ;
1687
+ p. cargo ( "run --example ex-env-vars -v" )
1688
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1689
+ . run ( ) ;
1650
1690
1651
1691
println ! ( "test" ) ;
1652
- p. cargo ( "test -v" ) . run ( ) ;
1692
+ p. cargo ( "test -v" )
1693
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1694
+ . run ( ) ;
1653
1695
1654
1696
if is_nightly ( ) {
1655
1697
println ! ( "bench" ) ;
1656
- p. cargo ( "bench -v" ) . run ( ) ;
1698
+ p. cargo ( "bench -v" )
1699
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1700
+ . run ( ) ;
1701
+ }
1702
+ }
1703
+
1704
+ #[ cargo_test]
1705
+ fn cargo_rustc_current_dir_foreign_workspace_dep ( ) {
1706
+ let foo = project ( )
1707
+ . file (
1708
+ "Cargo.toml" ,
1709
+ r#"
1710
+ [workspace]
1711
+
1712
+ [package]
1713
+ name = "foo"
1714
+ version = "0.0.1"
1715
+ authors = []
1716
+
1717
+ [dependencies]
1718
+ baz.path = "../baz"
1719
+ baz_member.path = "../baz/baz_member"
1720
+ "# ,
1721
+ )
1722
+ . file ( "src/lib.rs" , "" )
1723
+ . build ( ) ;
1724
+ let _baz = project ( )
1725
+ . at ( "baz" )
1726
+ . file (
1727
+ "Cargo.toml" ,
1728
+ r#"
1729
+ [workspace]
1730
+ members = ["baz_member"]
1731
+
1732
+ [package]
1733
+ name = "baz"
1734
+ version = "0.1.0"
1735
+ "# ,
1736
+ )
1737
+ . file ( "src/lib.rs" , "" )
1738
+ . file (
1739
+ "tests/env.rs" ,
1740
+ r#"
1741
+ use std::path::Path;
1742
+
1743
+ #[test]
1744
+ fn baz_env() {
1745
+ let workspace_dir = Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1746
+ let manifest_dir = Path::new(option_env!("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
1747
+ let current_dir = std::env::current_dir().expect("current_dir");
1748
+ let file_path = workspace_dir.join(file!());
1749
+ assert!(file_path.exists(), "{}", file_path.display());
1750
+ let workspace_dir = std::fs::canonicalize(current_dir.join(workspace_dir)).expect("CARGO_RUSTC_CURRENT_DIR");
1751
+ let manifest_dir = std::fs::canonicalize(current_dir.join(manifest_dir)).expect("CARGO_MANIFEST_DIR");
1752
+ assert_eq!(workspace_dir, manifest_dir);
1753
+ }
1754
+ "# ,
1755
+ )
1756
+ . file (
1757
+ "baz_member/Cargo.toml" ,
1758
+ r#"
1759
+ [package]
1760
+ name = "baz_member"
1761
+ version = "0.1.0"
1762
+ authors = []
1763
+ "# ,
1764
+ )
1765
+ . file ( "baz_member/src/lib.rs" , "" )
1766
+ . file (
1767
+ "baz_member/tests/env.rs" ,
1768
+ r#"
1769
+ use std::path::Path;
1770
+
1771
+ #[test]
1772
+ fn baz_member_env() {
1773
+ let workspace_dir = Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1774
+ let file_path = workspace_dir.join(file!());
1775
+ assert!(file_path.exists(), "{}", file_path.display());
1776
+ }
1777
+ "# ,
1778
+ )
1779
+ . build ( ) ;
1780
+
1781
+ // Verify it works from a different workspace
1782
+ foo. cargo ( "test -p baz" )
1783
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1784
+ . with_stdout_contains ( "running 1 test\n test baz_env ... ok" )
1785
+ . run ( ) ;
1786
+ foo. cargo ( "test -p baz_member" )
1787
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1788
+ . with_stdout_contains ( "running 1 test\n test baz_member_env ... ok" )
1789
+ . run ( ) ;
1790
+ }
1791
+
1792
+ #[ cargo_test]
1793
+ fn cargo_rustc_current_dir_non_local_dep ( ) {
1794
+ Package :: new ( "bar" , "0.1.0" )
1795
+ . file (
1796
+ "tests/bar_env.rs" ,
1797
+ r#"
1798
+ use std::path::Path;
1799
+
1800
+ #[test]
1801
+ fn bar_env() {
1802
+ let workspace_dir = Path::new(option_env!("CARGO_RUSTC_CURRENT_DIR").expect("CARGO_RUSTC_CURRENT_DIR"));
1803
+ let manifest_dir = Path::new(option_env!("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR"));
1804
+ let current_dir = std::env::current_dir().expect("current_dir");
1805
+ let file_path = workspace_dir.join(file!());
1806
+ assert!(file_path.exists(), "{}", file_path.display());
1807
+ let workspace_dir = std::fs::canonicalize(current_dir.join(workspace_dir)).expect("CARGO_RUSTC_CURRENT_DIR");
1808
+ let manifest_dir = std::fs::canonicalize(current_dir.join(manifest_dir)).expect("CARGO_MANIFEST_DIR");
1809
+ assert_eq!(workspace_dir, manifest_dir);
1810
+ }
1811
+ "# ,
1812
+ )
1813
+ . publish ( ) ;
1814
+
1815
+ let p = project ( )
1816
+ . file ( "src/lib.rs" , "" )
1817
+ . file (
1818
+ "Cargo.toml" ,
1819
+ r#"
1820
+ [package]
1821
+ name = "foo"
1822
+ version = "0.0.1"
1823
+
1824
+ [dependencies]
1825
+ bar = "0.1.0"
1826
+ "# ,
1827
+ )
1828
+ . build ( ) ;
1829
+
1830
+ p. cargo ( "test -p bar" )
1831
+ . masquerade_as_nightly_cargo ( & [ "CARGO_RUSTC_CURRENT_DIR" ] )
1832
+ . with_stdout_contains ( "running 1 test\n test bar_env ... ok" )
1833
+ . run ( ) ;
1834
+ }
1835
+
1836
+ #[ cargo_test]
1837
+ fn cargo_rustc_current_dir_is_not_stable ( ) {
1838
+ if is_nightly ( ) {
1839
+ return ;
1657
1840
}
1841
+ let p = project ( )
1842
+ . file (
1843
+ "tests/env.rs" ,
1844
+ r#"
1845
+ use std::path::Path;
1846
+
1847
+ #[test]
1848
+ fn env() {
1849
+ assert_eq!(option_env!("CARGO_RUSTC_CURRENT_DIR"), None);
1850
+ }
1851
+ "# ,
1852
+ )
1853
+ . build ( ) ;
1854
+
1855
+ p. cargo ( "test" ) . run ( ) ;
1658
1856
}
1659
1857
1660
1858
#[ cargo_test]
0 commit comments