Skip to content

Commit 29ae40c

Browse files
authored
Merge pull request #2801 from HollowMan6/btreemap
Keep preprocessors/backends execution order deterministic
2 parents 21f2435 + 6746df7 commit 29ae40c

File tree

2 files changed

+38
-8
lines changed

2 files changed

+38
-8
lines changed

crates/mdbook-core/src/config.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use crate::utils::log_backtrace;
4848
use anyhow::{Context, Error, Result, bail};
4949
use log::{debug, trace};
5050
use serde::{Deserialize, Serialize};
51-
use std::collections::HashMap;
51+
use std::collections::{BTreeMap, HashMap};
5252
use std::env;
5353
use std::fs::File;
5454
use std::io::Read;
@@ -214,15 +214,15 @@ impl Config {
214214
}
215215

216216
/// Returns the configuration for all preprocessors.
217-
pub fn preprocessors<'de, T: Deserialize<'de>>(&self) -> Result<HashMap<String, T>> {
217+
pub fn preprocessors<'de, T: Deserialize<'de>>(&self) -> Result<BTreeMap<String, T>> {
218218
self.preprocessor
219219
.clone()
220220
.try_into()
221221
.with_context(|| "Failed to read preprocessors")
222222
}
223223

224224
/// Returns the configuration for all renderers.
225-
pub fn outputs<'de, T: Deserialize<'de>>(&self) -> Result<HashMap<String, T>> {
225+
pub fn outputs<'de, T: Deserialize<'de>>(&self) -> Result<BTreeMap<String, T>> {
226226
self.output
227227
.clone()
228228
.try_into()

crates/mdbook-driver/src/mdbook/tests.rs

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ fn config_defaults_to_link_and_index_preprocessor_if_not_set() {
4747
// make sure we haven't got anything in the `preprocessor` table
4848
assert!(cfg.preprocessors::<toml::Value>().unwrap().is_empty());
4949

50-
let got = determine_preprocessors(&cfg, Path::new(""));
50+
let got = determine_preprocessors(&cfg, Path::new("")).unwrap();
5151

52-
assert!(got.is_ok());
53-
assert_eq!(got.as_ref().unwrap().len(), 2);
54-
assert_eq!(got.as_ref().unwrap()[0].name(), "index");
55-
assert_eq!(got.as_ref().unwrap()[1].name(), "links");
52+
let names: Vec<_> = got.values().map(|p| p.name()).collect();
53+
assert_eq!(names, ["index", "links"]);
5654
}
5755

5856
#[test]
@@ -252,3 +250,35 @@ fn preprocessor_should_run_falls_back_to_supports_renderer_method() {
252250
let got = preprocessor_should_run(&BoolPreprocessor(should_be), &html, &cfg).unwrap();
253251
assert_eq!(got, should_be);
254252
}
253+
254+
// Default is to sort preprocessors alphabetically.
255+
#[test]
256+
fn preprocessor_sorted_by_name() {
257+
let cfg_str = r#"
258+
[preprocessor.xyz]
259+
[preprocessor.abc]
260+
"#;
261+
262+
let cfg = Config::from_str(cfg_str).unwrap();
263+
264+
let got = determine_preprocessors(&cfg, Path::new("")).unwrap();
265+
266+
let names: Vec<_> = got.values().map(|p| p.name()).collect();
267+
assert_eq!(names, ["abc", "index", "links", "xyz"]);
268+
}
269+
270+
// Default is to sort renderers alphabetically.
271+
#[test]
272+
fn renderers_sorted_by_name() {
273+
let cfg_str = r#"
274+
[output.xyz]
275+
[output.abc]
276+
"#;
277+
278+
let cfg = Config::from_str(cfg_str).unwrap();
279+
280+
let got = determine_renderers(&cfg).unwrap();
281+
282+
let names: Vec<_> = got.values().map(|p| p.name()).collect();
283+
assert_eq!(names, ["abc", "xyz"]);
284+
}

0 commit comments

Comments
 (0)