Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split out cargo-util package for cargo-test-support. #9292

Merged
merged 10 commits into from
Mar 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ jobs:
- run: cargo test --features 'deny-warnings'
- run: cargo test --features 'deny-warnings' -p cargo-test-support
- run: cargo test -p cargo-platform
- run: cargo test -p cargo-util
- run: cargo test --manifest-path crates/mdman/Cargo.toml
- run: cargo build --manifest-path crates/credential/cargo-credential-1password/Cargo.toml
- run: cargo build --manifest-path crates/credential/cargo-credential-gnome-secret/Cargo.toml
Expand Down
8 changes: 1 addition & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ path = "src/cargo/lib.rs"
atty = "0.2"
bytesize = "1.0"
cargo-platform = { path = "crates/cargo-platform", version = "0.1.1" }
cargo-util = { path = "crates/cargo-util", version = "0.1.0" }
crates-io = { path = "crates/crates-io", version = "0.33.0" }
crossbeam-utils = "0.8"
crypto-hash = "0.3.1"
curl = { version = "0.4.23", features = ["http2"] }
curl-sys = "0.4.22"
env_logger = "0.8.1"
Expand All @@ -50,12 +50,10 @@ num_cpus = "1.0"
opener = "0.4"
percent-encoding = "2.0"
rustfix = "0.5.0"
same-file = "1"
semver = { version = "0.10", features = ["serde"] }
serde = { version = "1.0.123", features = ["derive"] }
serde_ignored = "0.1.0"
serde_json = { version = "1.0.30", features = ["raw_value"] }
shell-escape = "0.1.4"
strip-ansi-escapes = "0.1.0"
tar = { version = "0.4.26", default-features = false }
tempfile = "3.0"
Expand All @@ -75,11 +73,7 @@ im-rc = "15.0.0"
rustc-workspace-hack = "1.0.0"
rand = "0.8.3"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }

[target.'cfg(windows)'.dependencies]
miow = "0.3.6"
fwdansi = "1.1.0"

[target.'cfg(windows)'.dependencies.winapi]
Expand Down
3 changes: 2 additions & 1 deletion crates/cargo-test-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ edition = "2018"
doctest = false

[dependencies]
cargo = { path = "../.." }
anyhow = "1.0.34"
cargo-test-macro = { path = "../cargo-test-macro" }
cargo-util = { path = "../cargo-util" }
filetime = "0.2"
flate2 = { version = "1.0", default-features = false, features = ["zlib"] }
git2 = "0.13.16"
Expand Down
5 changes: 2 additions & 3 deletions crates/cargo-test-support/src/cross_compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
//! These tests are all disabled on rust-lang/rust's CI, but run in Cargo's CI.

use crate::{basic_manifest, main_file, project};
use cargo::util::ProcessError;
use cargo::CargoResult;
use cargo_util::ProcessError;
use std::env;
use std::fmt::Write;
use std::process::{Command, Output};
Expand Down Expand Up @@ -41,7 +40,7 @@ pub fn disabled() -> bool {

let cross_target = alternate();

let run_cross_test = || -> CargoResult<Output> {
let run_cross_test = || -> anyhow::Result<Output> {
let p = project()
.at("cross_test")
.file("Cargo.toml", &basic_manifest("cross_test", "1.0.0"))
Expand Down
57 changes: 38 additions & 19 deletions crates/cargo-test-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::process::{Command, Output};
use std::str;
use std::time::{self, Duration};

use cargo::util::{is_ci, CargoResult, ProcessBuilder, ProcessError, Rustc};
use cargo_util::{is_ci, ProcessBuilder, ProcessError};
use serde_json::{self, Value};
use url::Url;

Expand Down Expand Up @@ -701,7 +701,7 @@ impl Execs {
self
}

pub fn exec_with_output(&mut self) -> CargoResult<Output> {
pub fn exec_with_output(&mut self) -> anyhow::Result<Output> {
self.ran = true;
// TODO avoid unwrap
let p = (&self.process_builder).clone().unwrap();
Expand Down Expand Up @@ -1548,33 +1548,52 @@ fn substitute_macros(input: &str) -> String {

pub mod install;

thread_local!(
pub static RUSTC: Rustc = Rustc::new(
PathBuf::from("rustc"),
None,
None,
Path::new("should be path to rustup rustc, but we don't care in tests"),
None,
).unwrap()
);
struct RustcInfo {
verbose_version: String,
host: String,
}

impl RustcInfo {
fn new() -> RustcInfo {
let output = ProcessBuilder::new("rustc")
.arg("-vV")
.exec_with_output()
.expect("rustc should exec");
let verbose_version = String::from_utf8(output.stdout).expect("utf8 output");
let host = verbose_version
.lines()
.filter_map(|line| line.strip_prefix("host: "))
.next()
.expect("verbose version has host: field")
.to_string();
RustcInfo {
verbose_version,
host,
}
}
}

lazy_static::lazy_static! {
static ref RUSTC_INFO: RustcInfo = RustcInfo::new();
}

/// The rustc host such as `x86_64-unknown-linux-gnu`.
pub fn rustc_host() -> String {
RUSTC.with(|r| r.host.to_string())
pub fn rustc_host() -> &'static str {
&RUSTC_INFO.host
}

pub fn is_nightly() -> bool {
let vv = &RUSTC_INFO.verbose_version;
env::var("CARGO_TEST_DISABLE_NIGHTLY").is_err()
&& RUSTC
.with(|r| r.verbose_version.contains("-nightly") || r.verbose_version.contains("-dev"))
&& (vv.contains("-nightly") || vv.contains("-dev"))
}

pub fn process<T: AsRef<OsStr>>(t: T) -> cargo::util::ProcessBuilder {
pub fn process<T: AsRef<OsStr>>(t: T) -> ProcessBuilder {
_process(t.as_ref())
}

fn _process(t: &OsStr) -> cargo::util::ProcessBuilder {
let mut p = cargo::util::process(t);
fn _process(t: &OsStr) -> ProcessBuilder {
let mut p = ProcessBuilder::new(t);

// In general just clear out all cargo-specific configuration already in the
// environment. Our tests all assume a "default configuration" unless
Expand Down Expand Up @@ -1643,7 +1662,7 @@ pub trait ChannelChanger: Sized {
fn masquerade_as_nightly_cargo(&mut self) -> &mut Self;
}

impl ChannelChanger for cargo::util::ProcessBuilder {
impl ChannelChanger for ProcessBuilder {
fn masquerade_as_nightly_cargo(&mut self) -> &mut Self {
self.env("__CARGO_TEST_CHANNEL_OVERRIDE_DO_NOT_USE_THIS", "nightly")
}
Expand Down
9 changes: 5 additions & 4 deletions crates/cargo-test-support/src/registry.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::git::repo;
use crate::paths;
use cargo::sources::CRATES_IO_INDEX;
use cargo::util::Sha256;
use cargo_util::Sha256;
use flate2::write::GzEncoder;
use flate2::Compression;
use std::collections::BTreeMap;
Expand Down Expand Up @@ -560,7 +559,7 @@ impl Package {

/// Sets the index schema version for this package.
///
/// See [`cargo::sources::registry::RegistryPackage`] for more information.
/// See `cargo::sources::registry::RegistryPackage` for more information.
pub fn schema_version(&mut self, version: u32) -> &mut Package {
self.v = Some(version);
self
Expand All @@ -585,7 +584,9 @@ impl Package {
let registry_url = match (self.alternative, dep.registry.as_deref()) {
(false, None) => None,
(false, Some("alternative")) => Some(alt_registry_url().to_string()),
(true, None) => Some(CRATES_IO_INDEX.to_string()),
(true, None) => {
Some("https://github.com/rust-lang/crates.io-index".to_string())
}
(true, Some("alternative")) => None,
_ => panic!("registry_dep currently only supports `alternative`"),
};
Expand Down
29 changes: 29 additions & 0 deletions crates/cargo-util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "cargo-util"
version = "0.1.0"
authors = ["The Cargo Project Developers"]
edition = "2018"
license = "MIT OR Apache-2.0"
homepage = "https://github.com/rust-lang/cargo"
repository = "https://github.com/rust-lang/cargo"
description = "Miscellaneous support code used by Cargo."

[dependencies]
anyhow = "1.0.34"
crypto-hash = "0.3.1"
filetime = "0.2.9"
hex = "0.4.2"
jobserver = "0.1.21"
libc = "0.2.88"
log = "0.4.6"
same-file = "1.0.6"
shell-escape = "0.1.4"
tempfile = "3.1.0"
walkdir = "2.3.1"

[target.'cfg(target_os = "macos")'.dependencies]
core-foundation = { version = "0.9.0", features = ["mac_os_10_7_support"] }

[target.'cfg(windows)'.dependencies]
miow = "0.3.6"
winapi = { version = "0.3.9", features = ["consoleapi", "minwindef"] }
1 change: 1 addition & 0 deletions crates/cargo-util/LICENSE-APACHE
1 change: 1 addition & 0 deletions crates/cargo-util/LICENSE-MIT
17 changes: 17 additions & 0 deletions crates/cargo-util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
//! Miscellaneous support code used by Cargo.

pub use self::read2::read2;
pub use process_builder::ProcessBuilder;
pub use process_error::{exit_status_to_string, is_simple_exit_code, ProcessError};
pub use sha256::Sha256;

pub mod paths;
mod process_builder;
mod process_error;
mod read2;
mod sha256;

/// Whether or not this running in a Continuous Integration environment.
pub fn is_ci() -> bool {
std::env::var("CI").is_ok() || std::env::var("TF_BUILD").is_ok()
}
Loading