Skip to content

Commit dbea7ed

Browse files
committed
refactor(link-forever): fail_on_warnings in early returns
1 parent 111e038 commit dbea7ed

File tree

5 files changed

+45
-9
lines changed

5 files changed

+45
-9
lines changed

crates/mdbookkit/src/bin/link_forever/diagnostic.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::{Environment, LinkSpan, LinkStatus, LinkText, Pages, RelativeLink};
1414

1515
impl Environment {
1616
pub fn report<'a>(&'a self, content: &'a Pages<'a>) -> Reporter<'a> {
17+
// BTreeMap: sort output by paths
1718
let mut sorted: BTreeMap<&'_ Url, BTreeMap<LinkStatus, Vec<LinkDiagnostic<'_>>>> =
1819
Default::default();
1920

crates/mdbookkit/src/bin/link_forever/git.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@ use super::{Config, CustomPermalink, Environment, GitHubPermalink, PermalinkForm
1414

1515
impl Environment {
1616
pub fn try_from_env(book: &PreprocessorContext) -> Result<Result<Self>> {
17+
let config = config_from_book::<Config>(&book.config, "link-forever")
18+
.context("failed to read preprocessor config from book.toml")?;
19+
1720
let repo = match Repository::open_from_env()
1821
.context("preprocessor requires a git repository to work")
1922
.context("failed to find a git repository")
2023
{
2124
Ok(repo) => repo,
22-
Err(err) => return Ok(Err(err)),
25+
Err(err) => return config.fail_on_warnings.adjusted(Ok(Err(err))),
2326
};
2427

2528
let vcs_root = repo
@@ -44,12 +47,12 @@ impl Environment {
4447
}
4548
});
4649

47-
let config = config_from_book::<Config>(&book.config, "link-forever")?;
48-
4950
let Some(reference) =
5051
get_head(&repo).context("failed to get a tag or commit id to HEAD")?
5152
else {
52-
return Ok(Err(anyhow!("no commit found in this repo")));
53+
return config
54+
.fail_on_warnings
55+
.adjusted(Ok(Err(anyhow!("no commit found in this repo"))));
5356
};
5457

5558
let fmt_link: Box<dyn PermalinkFormat> = {
@@ -71,6 +74,7 @@ impl Environment {
7174
.context("failed to determine GitHub url to use for permalinks")
7275
.pipe(Err)
7376
.pipe(Ok)
77+
.pipe(|result| config.fail_on_warnings.adjusted(result))
7478
}
7579
};
7680
let (owner, repo) = remote_as_github(repo.as_ref())

crates/mdbookkit/src/env.rs

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tap::Pipe;
1313
#[derive(Deserialize, Debug, Default, Clone, Copy)]
1414
#[serde(rename_all = "lowercase")]
1515
pub enum ErrorHandling {
16-
/// Fail if the environment variable `CI` is set to a value other than `0`.
16+
/// Fail if the environment variable `CI` is set to a value other than `0` or `false`.
1717
/// Environments like GitHub Actions configure this automatically.
1818
#[default]
1919
#[serde(rename = "ci")]
@@ -35,10 +35,9 @@ impl ErrorHandling {
3535
.pipe(Err)
3636
}
3737
Self::Env => {
38-
let ci = std::env::var("CI").unwrap_or("".into());
39-
if matches!(ci.as_str(), "" | "0" | "false") {
38+
let Some(ci) = Self::warning_as_error() else {
4039
return Ok(());
41-
}
40+
};
4241
anyhow!("treating warnings as errors because fail-on-unresolved is \"ci\" and CI={ci}")
4342
.context("preprocessor has errors")
4443
.pipe(Err)
@@ -47,6 +46,22 @@ impl ErrorHandling {
4746
_ => Ok(()),
4847
}
4948
}
49+
50+
pub fn adjusted<T, E>(&self, result: Result<Result<T, E>, E>) -> Result<Result<T, E>, E> {
51+
match result {
52+
Ok(Err(error)) if Self::warning_as_error().is_some() => Err(error),
53+
result => result,
54+
}
55+
}
56+
57+
fn warning_as_error() -> Option<String> {
58+
let ci = std::env::var("CI").unwrap_or("".into());
59+
if matches!(ci.as_str(), "" | "0" | "false") {
60+
None
61+
} else {
62+
Some(ci)
63+
}
64+
}
5065
}
5166

5267
pub fn string_from_stdin() -> Result<String> {

crates/mdbookkit/tests/link_forever.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,12 +120,25 @@ fn test_minimum_env() -> Result<()> {
120120
log::info!("then: book builds with warnings");
121121
Command::new("mdbook")
122122
.arg("build")
123+
.env("CI", "false")
123124
.env("PATH", &path)
124125
.current_dir(&root)
125126
.assert()
126127
.success()
127128
.stderr(predicate::str::contains("requires a git repository"));
128129

130+
log::info!("when: CI=true");
131+
132+
log::info!("then: preprocessor fails");
133+
Command::new("mdbook")
134+
.arg("build")
135+
.env("CI", "true")
136+
.env("PATH", &path)
137+
.current_dir(&root)
138+
.assert()
139+
.failure()
140+
.stderr(predicate::str::contains("requires a git repository"));
141+
129142
log::info!("when: repo has no commit");
130143
Command::new("git")
131144
.arg("init")
@@ -137,6 +150,7 @@ fn test_minimum_env() -> Result<()> {
137150
log::info!("then: book builds with warnings");
138151
Command::new("mdbook")
139152
.arg("build")
153+
.env("CI", "false")
140154
.env("PATH", &path)
141155
.current_dir(&root)
142156
.assert()
@@ -159,6 +173,7 @@ fn test_minimum_env() -> Result<()> {
159173
log::info!("then: book builds with warnings");
160174
Command::new("mdbook")
161175
.arg("build")
176+
.env("CI", "false")
162177
.env("PATH", &path)
163178
.current_dir(&root)
164179
.assert()

crates/mdbookkit/tests/rustdoc_link.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,9 @@ fn test_minimum_env() -> Result<()> {
237237
.stderr(
238238
predicate::str::contains("failed to spawn rust-analyzer")
239239
// https://github.com/rust-lang/rustup/issues/3846
240-
// rustup shims rust-analyzer even when it's not installed
240+
// rustup shims rust-analyzer when it's not installed
241241
.or(predicate::str::contains("Unknown binary 'rust-analyzer")),
242+
// ^ doesn't have a closing `'` because on windows it says 'rust-analyzer.exe'
242243
);
243244

244245
log::info!("when: code extension is installed");

0 commit comments

Comments
 (0)