forge
Explore
Log in

Don't you dare go Hollow

config: Add a diff empty to default test

Browse files at this commit

00a4e225660b845cc9826286421f0ca4c1519e63Ivan MolodetskikhParent: 19d21fc
Cargo.lockmodified+1 -0
@@ -2415,6 +2415,7 @@ version = "25.8.0"
2415 dependencies = [2415 dependencies = [
2416 "bitflags 2.9.3",2416 "bitflags 2.9.3",
2417 "csscolorparser",2417 "csscolorparser",
2418+ "diff",
2418 "insta",2419 "insta",
2419 "knuffel",2420 "knuffel",
2420 "miette",2421 "miette",
niri-config/Cargo.tomlmodified+1 -0
@@ -21,3 +21,4 @@ tracy-client.workspace = true
21 [dev-dependencies]21 [dev-dependencies]
22 insta.workspace = true22 insta.workspace = true
23 pretty_assertions = "1.4.1"23 pretty_assertions = "1.4.1"
24+diff = "0.1.13"
niri-config/src/lib.rsmodified+100 -1
@@ -234,7 +234,7 @@ impl ConfigPath {
234 234
235 #[cfg(test)]235 #[cfg(test)]
236 mod tests {236 mod tests {
237- use insta::assert_debug_snapshot;237+ use insta::{assert_debug_snapshot, assert_snapshot};
238 use pretty_assertions::assert_eq;238 use pretty_assertions::assert_eq;
239 239
240 use super::*;240 use super::*;
@@ -1773,4 +1773,103 @@ mod tests {
1773 }1773 }
1774 "#);1774 "#);
1775 }1775 }
1776+
1777+ fn diff_lines(expected: &str, actual: &str) -> String {
1778+ let mut output = String::new();
1779+ let mut in_change = false;
1780+
1781+ for change in diff::lines(expected, actual) {
1782+ match change {
1783+ diff::Result::Both(_, _) => {
1784+ in_change = false;
1785+ }
1786+ diff::Result::Left(line) => {
1787+ if !output.is_empty() && !in_change {
1788+ output.push('\n');
1789+ }
1790+ output.push('-');
1791+ output.push_str(line);
1792+ output.push('\n');
1793+ in_change = true;
1794+ }
1795+ diff::Result::Right(line) => {
1796+ if !output.is_empty() && !in_change {
1797+ output.push('\n');
1798+ }
1799+ output.push('+');
1800+ output.push_str(line);
1801+ output.push('\n');
1802+ in_change = true;
1803+ }
1804+ }
1805+ }
1806+
1807+ output
1808+ }
1809+
1810+ #[test]
1811+ fn diff_empty_to_default() {
1812+ // We try to write the config defaults in such a way that empty sections (and an empty
1813+ // config) give the same outcome as the default config bundled with niri. This test
1814+ // verifies the actual differences between the two.
1815+ let mut default_config = Config::default();
1816+ let empty_config = Config::parse("empty.kdl", "").unwrap();
1817+
1818+ // Some notable omissions: the default config has some window rules, and an empty config
1819+ // will not have any binds. Clear them out so they don't spam the diff.
1820+ default_config.window_rules.clear();
1821+ default_config.binds.0.clear();
1822+
1823+ assert_snapshot!(
1824+ diff_lines(
1825+ &format!("{empty_config:#?}"),
1826+ &format!("{default_config:#?}")
1827+ ),
1828+ @r#"
1829+ - numlock: false,
1830+ + numlock: true,
1831+
1832+ - tap: false,
1833+ + tap: true,
1834+
1835+ - natural_scroll: false,
1836+ + natural_scroll: true,
1837+
1838+ - spawn_at_startup: [],
1839+ + spawn_at_startup: [
1840+ + SpawnAtStartup {
1841+ + command: [
1842+ + "waybar",
1843+ + ],
1844+ + },
1845+ + ],
1846+
1847+ - a: 0.4392157,
1848+ + a: 0.46666667,
1849+
1850+ - preset_column_widths: [],
1851+ - default_column_width: None,
1852+ + preset_column_widths: [
1853+ + Proportion(
1854+ + 0.33333,
1855+ + ),
1856+ + Proportion(
1857+ + 0.5,
1858+ + ),
1859+ + Proportion(
1860+ + 0.66667,
1861+ + ),
1862+ + ],
1863+ + default_column_width: Some(
1864+ + DefaultPresetSize(
1865+ + Some(
1866+ + Proportion(
1867+ + 0.5,
1868+ + ),
1869+ + ),
1870+ + ),
1871+ + ),
1872+ "#
1873+ );
1874+ }
1776 }1875 }