Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Merge configurations in lexicographic order #11

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/types/document.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::parser::parse_config;
use crate::*;

use std::collections::{BinaryHeap, HashSet};
use std::collections::HashSet;
use std::fs;
use std::path::{Path, PathBuf};

Expand Down Expand Up @@ -103,7 +103,7 @@ impl FontConfig {
self.merge_config(include_path)?;
} else if ty.is_dir() {
let dir = std::fs::read_dir(include_path)?;
let config_paths = dir
let mut config_paths: Vec<_> = dir
.filter_map(|entry| {
let entry = entry.ok()?;
let ty = entry.file_type().ok()?;
Expand All @@ -114,7 +114,12 @@ impl FontConfig {
None
}
})
.collect::<BinaryHeap<_>>();
.collect();

// Configs MUST be sorted in lexicographic order,
// otherwise `ConfigPart::ResetDirs` can occur out of intended order.
// See https://www.freedesktop.org/software/fontconfig/fontconfig-user.html#:~:text=sorted%20in%20lexicographic%20order
config_paths.sort();

for config_path in config_paths {
match self.merge_config(&config_path) {
Expand Down
11 changes: 11 additions & 0 deletions test-conf/conf.d/00-5_reset-dirs.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
<fontconfig>
<!-- This should get reset. -->
<dir>/before/reset</dir>

<reset-dirs />

<!-- This should survive. -->
<dir>/after/reset</dir>
</fontconfig>
17 changes: 17 additions & 0 deletions test-conf/conf.d/00-5_reset-dirs.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"Dir": {
"prefix": "Default",
"salt": "",
"path": "/before/reset"
}
},
"ResetDirs",
{
"Dir": {
"prefix": "Default",
"salt": "",
"path": "/after/reset"
}
}
]
8 changes: 8 additions & 0 deletions test-conf/conf.d/00-6_reset-dirs-lex-order.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version='1.0'?>
<!DOCTYPE fontconfig SYSTEM 'urn:fontconfig:fonts.dtd'>
<fontconfig>

<!-- This should not get reset by `00-5_reset-dirs.conf` as `00-6` runs after. -->
<dir>/after/after/reset</dir>

</fontconfig>
9 changes: 9 additions & 0 deletions test-conf/conf.d/00-6_reset-dirs-lex-order.conf.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"Dir": {
"prefix": "Default",
"salt": "",
"path": "/after/after/reset"
}
}
]
8 changes: 4 additions & 4 deletions test-conf/conf.d/10-scale-bitmap-fonts.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@
"value": {
"PixelSizeFixupFactor": {
"Simple": {
"Double": 1
"Double": 1.0
}
}
}
Expand Down Expand Up @@ -205,7 +205,7 @@
"value": {
"PixelSizeFixupFactor": {
"Simple": {
"Double": 1
"Double": 1.0
}
}
}
Expand Down Expand Up @@ -240,12 +240,12 @@
},
{
"Simple": {
"Double": 0
"Double": 0.0
}
},
{
"Simple": {
"Double": 0
"Double": 0.0
}
},
{
Expand Down
12 changes: 6 additions & 6 deletions test-conf/conf.d/65-fonts-persian.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -542,7 +542,7 @@
"Matrix": [
{
"Simple": {
"Double": 1
"Double": 1.0
}
},
{
Expand All @@ -552,12 +552,12 @@
},
{
"Simple": {
"Double": 0
"Double": 0.0
}
},
{
"Simple": {
"Double": 1
"Double": 1.0
}
}
]
Expand Down Expand Up @@ -731,7 +731,7 @@
"value": {
"Size": {
"Simple": {
"Double": 24
"Double": 24.0
}
}
}
Expand Down Expand Up @@ -787,7 +787,7 @@
"value": {
"Size": {
"Simple": {
"Double": 24
"Double": 24.0
}
}
}
Expand Down Expand Up @@ -843,7 +843,7 @@
"value": {
"Size": {
"Simple": {
"Double": 24
"Double": 24.0
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions test-conf/conf.d/90-synthetic.conf.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
"Matrix": [
{
"Simple": {
"Double": 1
"Double": 1.0
}
},
{
Expand All @@ -59,12 +59,12 @@
},
{
"Simple": {
"Double": 0
"Double": 0.0
}
},
{
"Simple": {
"Double": 1
"Double": 1.0
}
}
]
Expand Down
16 changes: 16 additions & 0 deletions tests/merge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ fn merge_full() {
let mut c = FontConfig::default();
c.merge_config("./test-conf/fonts.conf").unwrap();

// 00-5_reset-dirs.conf
assert!(!c.dirs.contains(&DirData {
path: "/before/reset".into(),
salt: "".into(),
}));
assert!(c.dirs.contains(&DirData {
path: "/after/reset".into(),
salt: "".into(),
}));

// 00-6_reset-dirs-lex-order.conf
assert!(c.dirs.contains(&DirData {
path: "/after/after/reset".into(),
salt: "".into(),
}));

// 00-nixos-cache.conf
assert!(c.dirs.contains(&DirData {
path: "/nix/store/i1yhgnfvaihqzs079lcx4zjnrdzcvaak-noto-fonts-2020-01-23".into(),
Expand Down
8 changes: 6 additions & 2 deletions tests/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,12 @@ fn test_single_conf(path: PathBuf) -> Result<()> {

let parts = parse_config_parts(std::fs::read_to_string(path)?.as_str())?;

let expected = std::fs::read_to_string(json_path)?;
let actual = serde_json::to_string(&parts).unwrap();
let expected_str = std::fs::read_to_string(json_path)?;
let expected: serde_json::Value = serde_json::from_str(&expected_str).unwrap();

let actual_str = serde_json::to_string(&parts).unwrap();
let actual: serde_json::Value = serde_json::from_str(&actual_str).unwrap();

k9::assert_equal!(expected, actual);

Ok(())
Expand Down