Skip to content
Permalink

Comparing changes

This is a direct comparison between two commits made in this repository or its related repositories. View the default comparison for this range or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: rust-lang/rust
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: a242ded5d4bf850a8de6562996a0f9f803b8fdc3
Choose a base ref
..
head repository: rust-lang/rust
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: b71d6b951ec6ba61fac73f22314fa0714bc5899f
Choose a head ref
Showing with 60 additions and 15 deletions.
  1. +27 −9 src/bootstrap/src/core/build_steps/test.rs
  2. +33 −6 src/tools/rustbook/src/main.rs
36 changes: 27 additions & 9 deletions src/bootstrap/src/core/build_steps/test.rs
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ use std::{env, fs, iter};

use clap_complete::shells;

use crate::core::build_steps::compile::run_cargo;
use crate::core::build_steps::doc::DocumentationFormat;
use crate::core::build_steps::synthetic_targets::MirOptPanicAbortSyntheticTarget;
use crate::core::build_steps::tool::{self, SourceType, Tool};
@@ -2171,7 +2172,8 @@ struct BookTest {
path: PathBuf,
name: &'static str,
is_ext_doc: bool,
cli_args: Vec<&'static str>,
dependencies: Vec<PathBuf>,
cli_args: Vec<String>,
}

impl Step for BookTest {
@@ -2225,6 +2227,22 @@ impl BookTest {
rustbook_cmd.env("RUSTC_BOOTSTRAP", "1");
rustbook_cmd.env("PATH", new_path).arg("test").arg(path);

if !self.dependencies.is_empty() {
for dep in self.dependencies {
// TODO: get a Cargo etc.
let output_paths = run_cargo(
builder,
cargo,
tail_args,
stamp,
additional_target_deps,
is_check,
rlib_only_metadata,
);
todo!("run cargo build {dep}");
}
}

if !self.cli_args.is_empty() {
rustbook_cmd.args(self.cli_args);
}
@@ -2324,17 +2342,16 @@ macro_rules! test_book {
let mut dependencies = dependencies;
let mut cli_args = cli_args;
for dep in $dependencies {
dependencies.push(dep.into_string());
dependencies.push(dep.to_string());
}

if !dependencies.is_empty() {
cli_args.push("--library-path");
cli_args.extend(
dependencies
.iter()
.map(|dep| format!("{dep}/target/debug/deps"))
.collect()
);
cli_args.push(String::from("--library-path"));
let lib_paths = dependencies
.iter()
.map(|dep| format!("{dep}/target/debug/deps"))
.collect::<Vec<String>>();
cli_args.extend(lib_paths);
}
)?

@@ -2343,6 +2360,7 @@ macro_rules! test_book {
path: PathBuf::from($path),
name: $book_name,
is_ext_doc: !$default,
dependencies,
cli_args,
});
}
39 changes: 33 additions & 6 deletions src/tools/rustbook/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::env;
use std::path::{Path, PathBuf};

use clap::{ArgMatches, Command, arg, crate_version};
use mdbook::MDBook;
use clap::{arg, crate_version, ArgMatches, Command};
use mdbook::errors::Result as Result3;
use mdbook::MDBook;
use mdbook_i18n_helpers::preprocessors::Gettext;
use mdbook_spec::Spec;
use mdbook_trpl_listing::TrplListing;
@@ -26,6 +26,20 @@ fn main() {
(Defaults to the current directory when omitted)")
.value_parser(clap::value_parser!(PathBuf));

// Note: we don't parse this into a `PathBuf` because it is comma separated
// strings *and* we will ultimately pass it into `MDBook::test()`, which
// accepts `Vec<&str>`. Although it is a bit annoying that `-l/--lang` and
// `-L/--library-path` are so close, this is the same set of arguments we
// would pass when invoking mdbook on the CLI, so making them match when
// invoking rustbook makes for good consistency.
let library_path_arg = arg!(
-L --"library-path"
"A comma-separated list of directories to add to the crate search\n\
path when building tests"
)
.required(false)
.value_parser(parse_library_paths);

let matches = Command::new("rustbook")
.about("Build a book with mdBook")
.author("Steve Klabnik <steve@steveklabnik.com>")
@@ -42,7 +56,8 @@ fn main() {
.subcommand(
Command::new("test")
.about("Tests that a book's Rust code samples compile")
.arg(dir_arg),
.arg(dir_arg)
.arg(library_path_arg),
)
.get_matches();

@@ -106,14 +121,22 @@ pub fn build(args: &ArgMatches) -> Result3<()> {

fn test(args: &ArgMatches) -> Result3<()> {
let book_dir = get_book_dir(args);
let library_paths = args
.try_get_one::<Vec<String>>("library_path")?
.map(|v| v.iter().map(|s| s.as_str()).collect::<Vec<&str>>())
.unwrap_or_default();
let mut book = load_book(&book_dir)?;
book.test(vec![])
book.test(library_paths)
}

fn get_book_dir(args: &ArgMatches) -> PathBuf {
if let Some(p) = args.get_one::<PathBuf>("dir") {
// Check if path is relative from current dir, or absolute...
if p.is_relative() { env::current_dir().unwrap().join(p) } else { p.to_path_buf() }
if p.is_relative() {
env::current_dir().unwrap().join(p)
} else {
p.to_path_buf()
}
} else {
env::current_dir().unwrap()
}
@@ -125,12 +148,16 @@ fn load_book(book_dir: &Path) -> Result3<MDBook> {
Ok(book)
}

fn parse_library_paths(input: &str) -> Result<Vec<String>, String> {
Ok(input.split(",").map(String::from).collect())
}

fn handle_error(error: mdbook::errors::Error) -> ! {
eprintln!("Error: {}", error);

for cause in error.chain().skip(1) {
eprintln!("\tCaused By: {}", cause);
}

::std::process::exit(101);
std::process::exit(101);
}