Add globs to test path names

This adds the ability for some test functions to use a glob pattern to
match a single file. This will be helpful when testing hash-files
support.
This commit is contained in:
Eric Huss 2025-08-26 15:44:01 -07:00
parent 321a76bd27
commit d071d127ef
3 changed files with 39 additions and 6 deletions

7
Cargo.lock generated
View file

@ -748,6 +748,12 @@ version = "0.31.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f" checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
[[package]]
name = "glob"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
[[package]] [[package]]
name = "globset" name = "globset"
version = "0.4.16" version = "0.4.16"
@ -1292,6 +1298,7 @@ dependencies = [
"clap_complete", "clap_complete",
"env_logger", "env_logger",
"futures-util", "futures-util",
"glob",
"ignore", "ignore",
"log", "log",
"mdbook-core", "mdbook-core",

View file

@ -35,6 +35,7 @@ elasticlunr-rs = "3.0.2"
env_logger = "0.11.8" env_logger = "0.11.8"
font-awesome-as-a-crate = "0.3.0" font-awesome-as-a-crate = "0.3.0"
futures-util = "0.3.31" futures-util = "0.3.31"
glob = "0.3.3"
handlebars = "6.3.2" handlebars = "6.3.2"
hex = "0.4.3" hex = "0.4.3"
indexmap = "2.10.0" indexmap = "2.10.0"
@ -118,6 +119,7 @@ tokio = { workspace = true, features = ["macros", "rt-multi-thread"], optional =
tower-http = { workspace = true, features = ["fs", "trace"], optional = true } tower-http = { workspace = true, features = ["fs", "trace"], optional = true }
[dev-dependencies] [dev-dependencies]
glob.workspace = true
regex.workspace = true regex.workspace = true
select.workspace = true select.workspace = true
semver.workspace = true semver.workspace = true

View file

@ -139,24 +139,28 @@ impl BookTest {
} }
/// Checks that the contents of the given file matches the expected value. /// Checks that the contents of the given file matches the expected value.
///
/// The path can use glob-style wildcards, but it must match only a single file.
#[track_caller] #[track_caller]
pub fn check_file(&mut self, path: &str, expected: impl IntoData) -> &mut Self { pub fn check_file(&mut self, path_pattern: &str, expected: impl IntoData) -> &mut Self {
if !self.built { if !self.built {
self.build(); self.build();
} }
let path = self.dir.join(path); let path = glob_one(&self.dir, path_pattern);
let actual = read_to_string(&path); let actual = read_to_string(&path);
self.assert.eq(actual, expected); self.assert.eq(actual, expected);
self self
} }
/// Checks that the given file contains the given string somewhere. /// Checks that the given file contains the given string somewhere.
///
/// The path can use glob-style wildcards, but it must match only a single file.
#[track_caller] #[track_caller]
pub fn check_file_contains(&mut self, path: &str, expected: &str) -> &mut Self { pub fn check_file_contains(&mut self, path_pattern: &str, expected: &str) -> &mut Self {
if !self.built { if !self.built {
self.build(); self.build();
} }
let path = self.dir.join(path); let path = glob_one(&self.dir, path_pattern);
let actual = read_to_string(&path); let actual = read_to_string(&path);
assert!( assert!(
actual.contains(expected), actual.contains(expected),
@ -170,12 +174,14 @@ impl BookTest {
/// Beware that using this is fragile, as it may be unable to catch /// Beware that using this is fragile, as it may be unable to catch
/// regressions (it can't tell the difference between success, or the /// regressions (it can't tell the difference between success, or the
/// string being looked for changed). /// string being looked for changed).
///
/// The path can use glob-style wildcards, but it must match only a single file.
#[track_caller] #[track_caller]
pub fn check_file_doesnt_contain(&mut self, path: &str, string: &str) -> &mut Self { pub fn check_file_doesnt_contain(&mut self, path_pattern: &str, string: &str) -> &mut Self {
if !self.built { if !self.built {
self.build(); self.build();
} }
let path = self.dir.join(path); let path = glob_one(&self.dir, path_pattern);
let actual = read_to_string(&path); let actual = read_to_string(&path);
assert!( assert!(
!actual.contains(string), !actual.contains(string),
@ -511,3 +517,21 @@ pub fn read_to_string<P: AsRef<Path>>(path: P) -> String {
.with_context(|| format!("could not read file {path:?}")) .with_context(|| format!("could not read file {path:?}"))
.unwrap() .unwrap()
} }
/// Returns the first path from the given glob pattern.
pub fn glob_one<P: AsRef<Path>>(path: P, pattern: &str) -> PathBuf {
let path = path.as_ref();
let mut matches = glob::glob(path.join(pattern).to_str().unwrap()).unwrap();
let Some(first) = matches.next() else {
panic!("expected at least one file at `{path:?}` with pattern `{pattern}`, found none");
};
let first = first.unwrap();
if let Some(next) = matches.next() {
panic!(
"expected only one file for pattern `{pattern}` in `{path:?}`, \
found `{first:?}` and `{:?}`",
next.unwrap()
);
}
first
}