mirror of
https://github.com/rust-lang/mdBook.git
synced 2025-12-27 09:05:40 -05:00
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:
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -748,6 +748,12 @@ version = "0.31.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "07e28edb80900c19c28f1072f2e8aeca7fa06b23cd4169cefe1af5aa3260783f"
|
||||
|
||||
[[package]]
|
||||
name = "glob"
|
||||
version = "0.3.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0cc23270f6e1808e30a928bdc84dea0b9b4136a8bc82338574f23baf47bbd280"
|
||||
|
||||
[[package]]
|
||||
name = "globset"
|
||||
version = "0.4.16"
|
||||
@@ -1292,6 +1298,7 @@ dependencies = [
|
||||
"clap_complete",
|
||||
"env_logger",
|
||||
"futures-util",
|
||||
"glob",
|
||||
"ignore",
|
||||
"log",
|
||||
"mdbook-core",
|
||||
|
||||
@@ -35,6 +35,7 @@ elasticlunr-rs = "3.0.2"
|
||||
env_logger = "0.11.8"
|
||||
font-awesome-as-a-crate = "0.3.0"
|
||||
futures-util = "0.3.31"
|
||||
glob = "0.3.3"
|
||||
handlebars = "6.3.2"
|
||||
hex = "0.4.3"
|
||||
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 }
|
||||
|
||||
[dev-dependencies]
|
||||
glob.workspace = true
|
||||
regex.workspace = true
|
||||
select.workspace = true
|
||||
semver.workspace = true
|
||||
|
||||
@@ -139,24 +139,28 @@ impl BookTest {
|
||||
}
|
||||
|
||||
/// 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]
|
||||
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 {
|
||||
self.build();
|
||||
}
|
||||
let path = self.dir.join(path);
|
||||
let path = glob_one(&self.dir, path_pattern);
|
||||
let actual = read_to_string(&path);
|
||||
self.assert.eq(actual, expected);
|
||||
self
|
||||
}
|
||||
|
||||
/// 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]
|
||||
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 {
|
||||
self.build();
|
||||
}
|
||||
let path = self.dir.join(path);
|
||||
let path = glob_one(&self.dir, path_pattern);
|
||||
let actual = read_to_string(&path);
|
||||
assert!(
|
||||
actual.contains(expected),
|
||||
@@ -170,12 +174,14 @@ impl BookTest {
|
||||
/// Beware that using this is fragile, as it may be unable to catch
|
||||
/// regressions (it can't tell the difference between success, or the
|
||||
/// string being looked for changed).
|
||||
///
|
||||
/// The path can use glob-style wildcards, but it must match only a single file.
|
||||
#[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 {
|
||||
self.build();
|
||||
}
|
||||
let path = self.dir.join(path);
|
||||
let path = glob_one(&self.dir, path_pattern);
|
||||
let actual = read_to_string(&path);
|
||||
assert!(
|
||||
!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:?}"))
|
||||
.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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user