Skip to content

Commit 53a3db0

Browse files
committed
Some more --sysroot tests.
1 parent bc5c441 commit 53a3db0

File tree

3 files changed

+83
-12
lines changed

3 files changed

+83
-12
lines changed

crates/cargo-test-support/src/paths.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::env;
66
use std::fs;
77
use std::io::{self, ErrorKind};
88
use std::path::{Path, PathBuf};
9+
use std::process::Command;
910
use std::sync::atomic::{AtomicUsize, Ordering};
1011
use std::sync::Mutex;
1112

@@ -252,3 +253,14 @@ pub fn get_lib_extension(kind: &str) -> &str {
252253
_ => unreachable!(),
253254
}
254255
}
256+
257+
/// Returns the sysroot as queried from rustc.
258+
pub fn sysroot() -> String {
259+
let output = Command::new("rustc")
260+
.arg("--print=sysroot")
261+
.output()
262+
.expect("rustc to run");
263+
assert!(output.status.success());
264+
let sysroot = String::from_utf8(output.stdout).unwrap();
265+
sysroot.trim().to_string()
266+
}

tests/build-std/main.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
//! Otherwise the tests are skipped.
2020
2121
use cargo_test_support::*;
22+
use std::env;
23+
use std::path::Path;
2224

2325
fn enable_build_std(e: &mut Execs, arg: Option<&str>) {
2426
e.env_remove("CARGO_HOME");
@@ -174,7 +176,21 @@ fn custom_test_framework() {
174176
)
175177
.build();
176178

179+
// This is a bit of a hack to use the rust-lld that ships with most toolchains.
180+
let sysroot = paths::sysroot();
181+
let sysroot = Path::new(&sysroot);
182+
let sysroot_bin = sysroot
183+
.join("lib")
184+
.join("rustlib")
185+
.join(rustc_host())
186+
.join("bin");
187+
let path = env::var_os("PATH").unwrap_or_default();
188+
let mut paths = env::split_paths(&path).collect::<Vec<_>>();
189+
paths.insert(0, sysroot_bin);
190+
let new_path = env::join_paths(paths).unwrap();
191+
177192
p.cargo("test --target target.json --no-run -v")
193+
.env("PATH", new_path)
178194
.build_std_arg("core")
179195
.run();
180196
}

tests/testsuite/standard_lib.rs

Lines changed: 55 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use cargo_test_support::registry::{Dependency, Package};
22
use cargo_test_support::ProjectBuilder;
33
use cargo_test_support::{is_nightly, paths, project, rustc_host, Execs};
44
use std::path::PathBuf;
5-
use std::process::Command;
65

76
struct Setup {
87
rustc_wrapper: PathBuf,
@@ -116,18 +115,9 @@ fn setup() -> Option<Setup> {
116115
.build();
117116
p.cargo("build").run();
118117

119-
let output = Command::new("rustc")
120-
.arg("--print")
121-
.arg("sysroot")
122-
.output()
123-
.unwrap();
124-
assert!(output.status.success());
125-
let real_sysroot = String::from_utf8(output.stdout).unwrap();
126-
let real_sysroot = real_sysroot.trim();
127-
128118
return Some(Setup {
129119
rustc_wrapper: p.bin("foo"),
130-
real_sysroot: real_sysroot.to_string(),
120+
real_sysroot: paths::sysroot(),
131121
});
132122
}
133123

@@ -514,7 +504,7 @@ fn doctest() {
514504
r#"
515505
/// Doc
516506
/// ```
517-
/// assert_eq!(1, 1);
507+
/// std::custom_api();
518508
/// ```
519509
pub fn f() {}
520510
"#,
@@ -523,6 +513,59 @@ fn doctest() {
523513

524514
p.cargo("test --doc -v")
525515
.build_std(&setup)
516+
.with_stdout_contains("test src/lib.rs - f [..] ... ok")
526517
.target_host()
527518
.run();
528519
}
520+
521+
#[cargo_test]
522+
fn no_implicit_alloc() {
523+
// Demonstrate that alloc is not implicitly in scope.
524+
let setup = match setup() {
525+
Some(s) => s,
526+
None => return,
527+
};
528+
let p = project()
529+
.file(
530+
"src/lib.rs",
531+
r#"
532+
pub fn f() {
533+
let _: Vec<i32> = alloc::vec::Vec::new();
534+
}
535+
"#,
536+
)
537+
.build();
538+
539+
p.cargo("build -v")
540+
.build_std(&setup)
541+
.target_host()
542+
.with_stderr_contains("[..]use of undeclared [..]`alloc`")
543+
.with_status(101)
544+
.run();
545+
}
546+
547+
#[cargo_test]
548+
fn macro_expanded_shadow() {
549+
// This tests a bug caused by the previous use of `--extern` to directly
550+
// load sysroot crates. This necessitated the switch to `--sysroot` to
551+
// retain existing behavior. See
552+
// https://github.com/rust-lang/wg-cargo-std-aware/issues/40 for more
553+
// detail.
554+
let setup = match setup() {
555+
Some(s) => s,
556+
None => return,
557+
};
558+
let p = project()
559+
.file(
560+
"src/lib.rs",
561+
r#"
562+
macro_rules! a {
563+
() => (extern crate std as alloc;)
564+
}
565+
a!();
566+
"#,
567+
)
568+
.build();
569+
570+
p.cargo("build -v").build_std(&setup).target_host().run();
571+
}

0 commit comments

Comments
 (0)