Skip to content

Commit 321a76b

Browse files
committed
Add track_caller to more test functions
This sprinkles track_caller on some more test functions to give more useful line numbers on errors when a test fails. read_to_string was changed since it couldn't track caller on a closure.
1 parent 8c5b72c commit 321a76b

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

tests/testsuite/book_test.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//! Utility for building and running tests against mdbook.
22
3+
use anyhow::Context;
34
use mdbook_driver::MDBook;
45
use mdbook_driver::init::BookBuilder;
56
use snapbox::IntoData;
@@ -91,6 +92,7 @@ impl BookTest {
9192
///
9293
/// Normally the contents outside of the `<main>` tag aren't interesting,
9394
/// and they add a significant amount of noise.
95+
#[track_caller]
9496
pub fn check_main_file(&mut self, path: &str, expected: impl IntoData) -> &mut Self {
9597
if !self.built {
9698
self.build();
@@ -107,6 +109,7 @@ impl BookTest {
107109
}
108110

109111
/// Checks the summary contents of `toc.js` against the expected value.
112+
#[track_caller]
110113
pub fn check_toc_js(&mut self, expected: impl IntoData) -> &mut Self {
111114
if !self.built {
112115
self.build();
@@ -119,6 +122,7 @@ impl BookTest {
119122
}
120123

121124
/// Returns the summary contents from `toc.js`.
125+
#[track_caller]
122126
pub fn toc_js_html(&self) -> String {
123127
let full_path = self.dir.join("book/toc.js");
124128
let actual = read_to_string(&full_path);
@@ -135,6 +139,7 @@ impl BookTest {
135139
}
136140

137141
/// Checks that the contents of the given file matches the expected value.
142+
#[track_caller]
138143
pub fn check_file(&mut self, path: &str, expected: impl IntoData) -> &mut Self {
139144
if !self.built {
140145
self.build();
@@ -146,6 +151,7 @@ impl BookTest {
146151
}
147152

148153
/// Checks that the given file contains the given string somewhere.
154+
#[track_caller]
149155
pub fn check_file_contains(&mut self, path: &str, expected: &str) -> &mut Self {
150156
if !self.built {
151157
self.build();
@@ -164,6 +170,7 @@ impl BookTest {
164170
/// Beware that using this is fragile, as it may be unable to catch
165171
/// regressions (it can't tell the difference between success, or the
166172
/// string being looked for changed).
173+
#[track_caller]
167174
pub fn check_file_doesnt_contain(&mut self, path: &str, string: &str) -> &mut Self {
168175
if !self.built {
169176
self.build();
@@ -178,6 +185,7 @@ impl BookTest {
178185
}
179186

180187
/// Checks that the list of files at the given path matches the given value.
188+
#[track_caller]
181189
pub fn check_file_list(&mut self, path: &str, expected: impl IntoData) -> &mut Self {
182190
let mut all_paths: Vec<_> = walkdir::WalkDir::new(&self.dir.join(path))
183191
.into_iter()
@@ -499,5 +507,7 @@ fn assert(root: &Path) -> snapbox::Assert {
499507
#[track_caller]
500508
pub fn read_to_string<P: AsRef<Path>>(path: P) -> String {
501509
let path = path.as_ref();
502-
std::fs::read_to_string(path).unwrap_or_else(|e| panic!("could not read file {path:?}: {e:?}"))
510+
std::fs::read_to_string(path)
511+
.with_context(|| format!("could not read file {path:?}"))
512+
.unwrap()
503513
}

0 commit comments

Comments
 (0)