Skip to content

Commit

Permalink
[nextest-runner] tidy up libdir support
Browse files Browse the repository at this point in the history
Make a few changes:

- Remove the ToSummary/FromSummary traits.
- Add PlatformLibdir/PlatformLibdirSummary that represent both available and unavailable platforms
- No need for io::BufRead since the entire output is buffered into a string
- Reorganize code such that the most important types are first in each file.
  • Loading branch information
sunshowers committed May 23, 2024
1 parent 95dedaa commit dfd3e41
Show file tree
Hide file tree
Showing 17 changed files with 539 additions and 435 deletions.
26 changes: 13 additions & 13 deletions cargo-nextest/src/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use nextest_runner::{
TestList,
},
partition::PartitionerBuilder,
platform::{BuildPlatforms, BuildPlatformsTarget},
platform::{BuildPlatforms, HostPlatform, PlatformLibdir, TargetPlatform},
redact::Redactor,
reporter::{structured, FinalStatusLevel, StatusLevel, TestOutputDisplay, TestReporterBuilder},
reuse_build::{archive_to_file, ArchiveReporter, PathMapper, ReuseBuildInfo},
Expand Down Expand Up @@ -1009,20 +1009,20 @@ impl BaseApp {
let build_platforms = match reuse_build.binaries_metadata() {
Some(kind) => kind.binary_list.rust_build_meta.build_platforms.clone(),
None => {
let mut build_platforms = BuildPlatforms::new()?;
if let Some(output) = RustcCli::print_host_libdir().read() {
build_platforms.set_host_libdir_from_rustc_output(Cursor::new(output));
}
if let Some(triple) =
let host = HostPlatform::current(PlatformLibdir::from_rustc_stdout(
RustcCli::print_host_libdir().read(),
))?;
let target = if let Some(triple) =
discover_target_triple(&cargo_configs, cargo_opts.target.as_deref())
{
let mut target = BuildPlatformsTarget::new(triple.clone());
if let Some(output) = RustcCli::print_target_libdir(&triple).read() {
target.set_libdir_from_rustc_output(Cursor::new(output));
}
build_platforms.target = Some(target);
}
build_platforms
let libdir = PlatformLibdir::from_rustc_stdout(
RustcCli::print_target_libdir(&triple).read(),
);
Some(TargetPlatform::new(triple, libdir))
} else {
None
};
BuildPlatforms { host, target }
}
};

Expand Down
81 changes: 65 additions & 16 deletions nextest-metadata/src/test_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,19 @@ pub struct RustNonTestBinarySummary {
pub path: Utf8PathBuf,
}

/// Serialized representation of the host and the target platform.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct BuildPlatformsSummary {
/// The host platform used while compiling the Rust artifacts.
pub host: HostPlatformSummary,

/// The target platforms used while compiling the Rust artifacts.
///
/// With current versions of nextest, this will contain at most one element.
pub targets: Vec<TargetPlatformSummary>,
}

/// Serialized representation of the host platform.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
Expand All @@ -524,10 +537,7 @@ pub struct HostPlatformSummary {
pub platform: PlatformSummary,

/// The libdir for the host platform.
///
/// Empty if failed to discover.
#[serde(default)]
pub libdir: Option<Utf8PathBuf>,
pub libdir: PlatformLibdirSummary,
}

/// Serialized representation of the target platform.
Expand All @@ -539,22 +549,61 @@ pub struct TargetPlatformSummary {

/// The libdir for the target platform.
///
/// Empty if failed to discover.
#[serde(default)]
pub libdir: Option<Utf8PathBuf>,
/// Err if we failed to discover it.
pub libdir: PlatformLibdirSummary,
}

/// Serialized representation of the host and the target platform.
/// Serialized representation of a platform's library directory.
#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub struct BuildPlatformsSummary {
/// The host platform used while compiling the Rust artifacts.
pub host: HostPlatformSummary,
#[serde(tag = "status", rename_all = "kebab-case")]
pub enum PlatformLibdirSummary {
/// The libdir is available.
Available {
/// The libdir.
path: Utf8PathBuf,
},

/// The target platforms used while compiling the Rust artifacts.
///
/// With current versions of nextest, this will contain at most one element.
pub targets: Vec<TargetPlatformSummary>,
/// The libdir is unavailable, for the reason provided in the inner value.
Unavailable {
/// The reason why the libdir is unavailable.
reason: PlatformLibdirUnavailable,
},
}

/// The reason why a platform libdir is unavailable.
///
/// Part of [`PlatformLibdirSummary`].
///
/// This is an open-ended enum that may have additional deserializable variants in the future.
#[derive(Clone, Debug, Eq, PartialEq, Deserialize, Serialize)]
pub struct PlatformLibdirUnavailable(pub Cow<'static, str>);

impl PlatformLibdirUnavailable {
/// The libdir is not available because the rustc invocation to obtain it failed.
pub const RUSTC_FAILED: Self = Self::new_const("rustc-failed");

/// The libdir is not available because it was attempted to be read from rustc, but there was an
/// issue with its output.
pub const RUSTC_OUTPUT_ERROR: Self = Self::new_const("rustc-output-error");

/// The libdir is unavailable because it was deserialized from a summary serialized by an older
/// version of nextest.
pub const OLD_SUMMARY: Self = Self::new_const("old-summary");

/// Converts a static string into Self.
pub const fn new_const(reason: &'static str) -> Self {
Self(Cow::Borrowed(reason))
}

/// Converts a string into Self.
pub fn new(reason: impl Into<Cow<'static, str>>) -> Self {
Self(reason.into())
}

/// Returns self as a string.
pub fn as_str(&self) -> &str {
&self.0
}
}

/// Information about the kind of a Rust non-test binary.
Expand Down
3 changes: 1 addition & 2 deletions nextest-runner/src/cargo_config/test_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
// Copyright (c) The nextest Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

use super::TargetTriple;
use camino::Utf8PathBuf;
use camino_tempfile::Utf8TempDir;
use color_eyre::eyre::{Context, Result};
use target_spec::{Platform, TargetFeatures};

use super::TargetTriple;

pub(super) fn setup_temp_dir() -> Result<Utf8TempDir> {
let dir = camino_tempfile::Builder::new()
.tempdir()
Expand Down
4 changes: 2 additions & 2 deletions nextest-runner/src/config/overrides.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,8 +477,8 @@ impl CompiledOverride<PreBuildPlatform> {
self,
build_platforms: &BuildPlatforms,
) -> CompiledOverride<FinalConfig> {
let host_eval = self.data.host_spec.eval(&build_platforms.host);
let host_test_eval = self.data.target_spec.eval(&build_platforms.host);
let host_eval = self.data.host_spec.eval(&build_platforms.host.platform);
let host_test_eval = self.data.target_spec.eval(&build_platforms.host.platform);
let target_eval = build_platforms
.target
.as_ref()
Expand Down
4 changes: 2 additions & 2 deletions nextest-runner/src/config/scripts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,8 +383,8 @@ impl CompiledProfileScripts<PreBuildPlatform> {
self,
build_platforms: &BuildPlatforms,
) -> CompiledProfileScripts<FinalConfig> {
let host_eval = self.data.host_spec.eval(&build_platforms.host);
let host_test_eval = self.data.target_spec.eval(&build_platforms.host);
let host_eval = self.data.host_spec.eval(&build_platforms.host.platform);
let host_test_eval = self.data.target_spec.eval(&build_platforms.host.platform);
let target_eval = build_platforms
.target
.as_ref()
Expand Down
16 changes: 9 additions & 7 deletions nextest-runner/src/config/test_helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use crate::{
cargo_config::{TargetDefinitionLocation, TargetTriple, TargetTripleSource},
config::{CustomTestGroup, TestGroup},
platform::{BuildPlatforms, BuildPlatformsTarget},
platform::{BuildPlatforms, HostPlatform, PlatformLibdir, TargetPlatform},
};
use camino::{Utf8Path, Utf8PathBuf};
use guppy::{
Expand Down Expand Up @@ -85,17 +85,19 @@ pub(super) fn binary_query<'a>(

pub(super) fn build_platforms() -> BuildPlatforms {
BuildPlatforms {
host: Platform::new("x86_64-unknown-linux-gnu", TargetFeatures::Unknown).unwrap(),
host_libdir: Some(
Utf8PathBuf::from("/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib")
),
target: Some(BuildPlatformsTarget {
host: HostPlatform {
platform: Platform::new("x86_64-unknown-linux-gnu", TargetFeatures::Unknown).unwrap(),
libdir: PlatformLibdir::Available(
Utf8PathBuf::from("/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib")
),
},
target: Some(TargetPlatform {
triple: TargetTriple {
platform: Platform::new("aarch64-apple-darwin", TargetFeatures::Unknown).unwrap(),
source: TargetTripleSource::Env,
location: TargetDefinitionLocation::Builtin,
},
libdir: Some(
libdir: PlatformLibdir::Available(
Utf8PathBuf::from("/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-apple-darwin/lib")
),
}),
Expand Down
25 changes: 17 additions & 8 deletions nextest-runner/src/list/binary_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,11 @@ mod tests {
use crate::{
cargo_config::{TargetDefinitionLocation, TargetTriple, TargetTripleSource},
list::SerializableFormat,
platform::BuildPlatformsTarget,
platform::{HostPlatform, PlatformLibdir, TargetPlatform},
};
use indoc::indoc;
use maplit::btreeset;
use nextest_metadata::PlatformLibdirUnavailable;
use pretty_assertions::assert_eq;
use target_spec::{Platform, TargetFeatures};

Expand Down Expand Up @@ -428,13 +429,15 @@ mod tests {
location: TargetDefinitionLocation::Builtin,
};
let fake_host_libdir = "/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib";
let fake_target_libdir = "/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib";
let build_platforms = BuildPlatforms {
host: TargetTriple::x86_64_unknown_linux_gnu().platform,
host_libdir: Some(Utf8PathBuf::from(fake_host_libdir)),
target: Some(BuildPlatformsTarget {
host: HostPlatform {
platform: TargetTriple::x86_64_unknown_linux_gnu().platform,
libdir: PlatformLibdir::Available(Utf8PathBuf::from(fake_host_libdir)),
},
target: Some(TargetPlatform {
triple: fake_triple,
libdir: Some(Utf8PathBuf::from(fake_target_libdir)),
// Test out the error case for unavailable libdirs.
libdir: PlatformLibdir::Unavailable(PlatformLibdirUnavailable::RUSTC_OUTPUT_ERROR),
}),
};

Expand Down Expand Up @@ -515,15 +518,21 @@ mod tests {
"triple": "x86_64-unknown-linux-gnu",
"target-features": "unknown"
},
"libdir": "/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"
"libdir": {
"status": "available",
"path": "/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib"
}
},
"targets": [
{
"platform": {
"triple": "aarch64-unknown-linux-gnu",
"target-features": "unknown"
},
"libdir": "/home/fake/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/aarch64-unknown-linux-gnu/lib"
"libdir": {
"status": "unavailable",
"reason": "rustc-output-error"
}
}
]
},
Expand Down
Loading

0 comments on commit dfd3e41

Please sign in to comment.