Skip to content

Commit 13bc9a1

Browse files
committed
Auto merge of #7350 - alexcrichton:mock-std, r=ehuss
Improve test suite for `-Zbuild-std` This commit is aimed directly at rust-lang/wg-cargo-std-aware#33 and in general making the `-Zbuild-std` tests more robust. The main change here is that a new source tree is checked in, `tests/testsuite/mock-std`, which mirrors rust-lang/rust's own tree for libstd. This mock tree is as empty as it can be, ideally duplicating almost nothing but for not requiring duplication of Cargo metadata about patches and such. The end result here looks like: * All `-Zbuild-std` tests are now run in parallel * All tests run much more quickly since they're compiling tiny crates instead of actually compiling libstd/libcore * No tests require network access * We verify that crates have access to the "custom" libraries that we build Coverage of tests is not currently expanded, but it's hoped that we could add that shortly afterwards. Coverage has actually gone down slightly since the custom target test was commented out temporarily and the full integration test of running `-Zbuild-std` isn't run on CI any more. Closes rust-lang/wg-cargo-std-aware#33
2 parents 4a04a1c + ebd1052 commit 13bc9a1

File tree

125 files changed

+1000
-467
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

125 files changed

+1000
-467
lines changed

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ features = [
103103

104104
[dev-dependencies]
105105
cargo-test-macro = { path = "crates/cargo-test-macro", version = "0.1.0" }
106+
cargo-test-support = { path = "crates/cargo-test-support", version = "0.1.0" }
106107

107108
[[bin]]
108109
name = "cargo"

azure-pipelines.yml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ jobs:
4343
x86_64-msvc:
4444
TOOLCHAIN: stable-x86_64-pc-windows-msvc
4545
OTHER_TARGET: i686-pc-windows-msvc
46+
4647
- job: rustfmt
4748
pool:
4849
vmImage: ubuntu-16.04
@@ -54,6 +55,8 @@ jobs:
5455
displayName: "Check rustfmt (cargo)"
5556
- bash: cd crates/cargo-test-macro && cargo fmt --all -- --check
5657
displayName: "Check rustfmt (cargo-test-macro)"
58+
- bash: cd crates/cargo-test-support && cargo fmt --all -- --check
59+
displayName: "Check rustfmt (cargo-test-support)"
5760
- bash: cd crates/crates-io && cargo fmt --all -- --check
5861
displayName: "Check rustfmt (crates-io)"
5962
- bash: cd crates/resolver-tests && cargo fmt --all -- --check
@@ -71,6 +74,20 @@ jobs:
7174
variables:
7275
TOOLCHAIN: stable
7376

77+
- job: build_std
78+
pool:
79+
vmImage: ubuntu-16.04
80+
steps:
81+
- template: ci/azure-install-rust.yml
82+
- bash: rustup component add rust-src
83+
displayName: "Install rust-src"
84+
- bash: cargo build
85+
- bash: cargo test --test build-std
86+
displayName: "tests"
87+
variables:
88+
TOOLCHAIN: nightly
89+
CARGO_RUN_BUILD_STD_TESTS: 1
90+
7491
- job: docs
7592
pool:
7693
vmImage: ubuntu-16.04
@@ -88,4 +105,3 @@ jobs:
88105
displayName: "Build mdbook documentation"
89106
variables:
90107
TOOLCHAIN: stable
91-

ci/azure-test-all.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,6 @@ steps:
1616
- bash: rustup component add clippy || echo "clippy not available"
1717
displayName: "Install clippy (maybe)"
1818

19-
# This is needed for standard library tests.
20-
- bash: rustup component add rust-src
21-
condition: and(succeeded(), eq(variables['TOOLCHAIN'], 'nightly'))
22-
displayName: "Install rust-src (maybe)"
23-
2419
# Deny warnings on CI to avoid warnings getting into the codebase, and note the
2520
# `force-system-lib-on-osx` which is intended to fix compile issues on OSX where
2621
# compiling curl from source on OSX yields linker errors on Azure.
@@ -31,3 +26,6 @@ steps:
3126
# fix the link errors.
3227
- bash: cargo test --features 'deny-warnings curl/force-system-lib-on-osx'
3328
displayName: "cargo test"
29+
30+
- bash: cargo test -p cargo-test-support
31+
displayName: "cargo test -p cargo-test-support"

crates/cargo-test-macro/src/lib.rs

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ extern crate proc_macro;
33
use proc_macro::*;
44

55
#[proc_macro_attribute]
6-
pub fn cargo_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
6+
pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
77
let span = Span::call_site();
88
let mut ret = TokenStream::new();
99
ret.extend(Some(TokenTree::from(Punct::new('#', Spacing::Alone))));
@@ -13,6 +13,8 @@ pub fn cargo_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
1313
test.into(),
1414
))));
1515

16+
let build_std = contains_ident(&attr, "build_std");
17+
1618
for token in item {
1719
let group = match token {
1820
TokenTree::Group(g) => {
@@ -29,25 +31,20 @@ pub fn cargo_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
2931
}
3032
};
3133

32-
let mut new_body = vec![
33-
TokenTree::from(Ident::new("let", span)),
34-
TokenTree::from(Ident::new("_test_guard", span)),
35-
TokenTree::from(Punct::new('=', Spacing::Alone)),
36-
TokenTree::from(Ident::new("crate", span)),
37-
TokenTree::from(Punct::new(':', Spacing::Joint)),
38-
TokenTree::from(Punct::new(':', Spacing::Alone)),
39-
TokenTree::from(Ident::new("support", span)),
40-
TokenTree::from(Punct::new(':', Spacing::Joint)),
41-
TokenTree::from(Punct::new(':', Spacing::Alone)),
42-
TokenTree::from(Ident::new("paths", span)),
43-
TokenTree::from(Punct::new(':', Spacing::Joint)),
44-
TokenTree::from(Punct::new(':', Spacing::Alone)),
45-
TokenTree::from(Ident::new("init_root", span)),
46-
TokenTree::from(Group::new(Delimiter::Parenthesis, TokenStream::new())),
47-
TokenTree::from(Punct::new(';', Spacing::Alone)),
48-
]
49-
.into_iter()
50-
.collect::<TokenStream>();
34+
let mut new_body =
35+
to_token_stream("let _test_guard = cargo_test_support::paths::init_root();");
36+
37+
// If this is a `build_std` test (aka `tests/build-std/*.rs`) then they
38+
// only run on nightly and they only run when specifically instructed to
39+
// on CI.
40+
if build_std {
41+
let ts = to_token_stream("if !cargo_test_support::is_nightly() { return }");
42+
new_body.extend(ts);
43+
let ts = to_token_stream(
44+
"if std::env::var(\"CARGO_RUN_BUILD_STD_TESTS\").is_err() { return }",
45+
);
46+
new_body.extend(ts);
47+
}
5148
new_body.extend(group.stream());
5249
ret.extend(Some(TokenTree::from(Group::new(
5350
group.delimiter(),
@@ -57,3 +54,14 @@ pub fn cargo_test(_attr: TokenStream, item: TokenStream) -> TokenStream {
5754

5855
return ret;
5956
}
57+
58+
fn contains_ident(t: &TokenStream, ident: &str) -> bool {
59+
t.clone().into_iter().any(|t| match t {
60+
TokenTree::Ident(i) => i.to_string() == ident,
61+
_ => false,
62+
})
63+
}
64+
65+
fn to_token_stream(code: &str) -> TokenStream {
66+
code.parse().unwrap()
67+
}

crates/cargo-test-support/Cargo.toml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
[package]
2+
name = "cargo-test-support"
3+
version = "0.1.0"
4+
authors = ["Alex Crichton <[email protected]>"]
5+
edition = "2018"
6+
7+
[lib]
8+
doctest = false
9+
10+
[dependencies]
11+
cargo = { path = "../.." }
12+
cargo-test-macro = { path = "../cargo-test-macro" }
13+
filetime = "0.2"
14+
flate2 = "1.0"
15+
git2 = "0.10"
16+
glob = "0.3"
17+
lazy_static = "1.0"
18+
remove_dir_all = "0.5"
19+
serde_json = "1.0"
20+
tar = "0.4"
21+
url = "2.0"

tests/testsuite/support/cross_compile.rs renamed to crates/cargo-test-support/src/cross_compile.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
use crate::{basic_bin_manifest, main_file, project};
12
use std::env;
23
use std::process::Command;
34
use std::sync::atomic::{AtomicBool, Ordering};
45
use std::sync::Once;
56

6-
use crate::support::{basic_bin_manifest, main_file, project};
7-
87
pub fn disabled() -> bool {
98
// First, disable if `./configure` requested so.
109
match env::var("CFG_DISABLE_CROSS_TESTS") {

tests/testsuite/support/git.rs renamed to crates/cargo-test-support/src/git.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,13 @@ use some of the helper functions in this file to interact with the repository.
3838
3939
*/
4040

41+
use crate::{path2url, project, Project, ProjectBuilder};
42+
use git2;
4143
use std::fs::{self, File};
4244
use std::io::prelude::*;
4345
use std::path::{Path, PathBuf};
44-
45-
use git2;
4646
use url::Url;
4747

48-
use crate::support::{path2url, project, Project, ProjectBuilder};
49-
5048
#[must_use]
5149
pub struct RepoBuilder {
5250
repo: git2::Repository,

tests/testsuite/support/install.rs renamed to crates/cargo-test-support/src/install.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1+
use crate::paths;
12
use std::env::consts::EXE_SUFFIX;
23
use std::path::{Path, PathBuf};
34

4-
use crate::support::paths;
5-
65
/// Used by `cargo install` tests to assert an executable binary
76
/// has been installed. Example usage:
87
///

tests/testsuite/support/mod.rs renamed to crates/cargo-test-support/src/lib.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ use url::Url;
125125

126126
use self::paths::CargoPathExt;
127127

128+
#[macro_export]
128129
macro_rules! t {
129130
($e:expr) => {
130131
match $e {
@@ -134,6 +135,8 @@ macro_rules! t {
134135
};
135136
}
136137

138+
pub use cargo_test_macro::cargo_test;
139+
137140
pub mod cross_compile;
138141
pub mod git;
139142
pub mod paths;
@@ -409,7 +412,7 @@ impl Project {
409412
/// .with_stdout("bar\n")
410413
/// .run();
411414
pub fn process<T: AsRef<OsStr>>(&self, program: T) -> Execs {
412-
let mut p = crate::support::process(program);
415+
let mut p = process(program);
413416
p.cwd(self.root());
414417
execs().with_process_builder(p)
415418
}
@@ -1425,7 +1428,7 @@ pub fn lines_match(expected: &str, mut actual: &str) -> bool {
14251428
actual.is_empty() || expected.ends_with("[..]")
14261429
}
14271430

1428-
#[cargo_test]
1431+
#[test]
14291432
fn lines_match_works() {
14301433
assert!(lines_match("a b", "a b"));
14311434
assert!(lines_match("a[..]b", "a b"));

tests/testsuite/support/paths.rs renamed to crates/cargo-test-support/src/paths.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use filetime::{self, FileTime};
2+
use lazy_static::lazy_static;
13
use std::cell::RefCell;
24
use std::collections::HashMap;
35
use std::env;
@@ -7,9 +9,6 @@ use std::path::{Path, PathBuf};
79
use std::sync::atomic::{AtomicUsize, Ordering};
810
use std::sync::Mutex;
911

10-
use filetime::{self, FileTime};
11-
use lazy_static::lazy_static;
12-
1312
static CARGO_INTEGRATION_TEST_DIR: &str = "cit";
1413

1514
lazy_static! {

tests/testsuite/support/publish.rs renamed to crates/cargo-test-support/src/publish.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1+
use crate::registry::{self, alt_api_path};
2+
use crate::{find_json_mismatch, lines_match};
3+
use flate2::read::GzDecoder;
14
use std::collections::{HashMap, HashSet};
25
use std::fs::File;
36
use std::io::{self, prelude::*, SeekFrom};
47
use std::path::{Path, PathBuf};
5-
6-
use crate::support::registry::{self, alt_api_path};
7-
use crate::support::{find_json_mismatch, lines_match};
8-
9-
use flate2::read::GzDecoder;
108
use tar::Archive;
119

1210
fn read_le_u32<R>(mut reader: R) -> io::Result<u32>

tests/testsuite/support/registry.rs renamed to crates/cargo-test-support/src/registry.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,16 @@
1-
use std::collections::HashMap;
2-
use std::fs::{self, File};
3-
use std::io::prelude::*;
4-
use std::path::{Path, PathBuf};
5-
1+
use crate::git::repo;
2+
use crate::paths;
63
use cargo::sources::CRATES_IO_INDEX;
74
use cargo::util::Sha256;
85
use flate2::write::GzEncoder;
96
use flate2::Compression;
7+
use std::collections::HashMap;
8+
use std::fs::{self, File};
9+
use std::io::prelude::*;
10+
use std::path::{Path, PathBuf};
1011
use tar::{Builder, Header};
1112
use url::Url;
1213

13-
use crate::support::git::repo;
14-
use crate::support::paths;
15-
1614
/// Gets the path to the local index pretending to be crates.io. This is a Git repo
1715
/// initialized with a `config.json` file pointing to `dl_path` for downloads
1816
/// and `api_path` for uploads.

src/cargo/core/compiler/standard_lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use crate::core::{Dependency, PackageId, PackageSet, Resolve, SourceId, Workspac
77
use crate::ops::{self, Packages};
88
use crate::util::errors::CargoResult;
99
use std::collections::{HashMap, HashSet};
10+
use std::env;
1011
use std::path::PathBuf;
1112

1213
/// Parse the `-Zbuild-std` flag.
@@ -148,6 +149,10 @@ pub fn generate_std_roots<'a>(
148149
}
149150

150151
fn detect_sysroot_src_path(ws: &Workspace<'_>) -> CargoResult<PathBuf> {
152+
if let Some(s) = env::var_os("__CARGO_TESTS_ONLY_SRC_ROOT") {
153+
return Ok(s.into());
154+
}
155+
151156
// NOTE: This is temporary until we figure out how to acquire the source.
152157
// If we decide to keep the sysroot probe, then BuildConfig will need to
153158
// be restructured so that the TargetInfo is created earlier and passed

0 commit comments

Comments
 (0)