From 03ceb367b77c88d99d31af840b15b06b1ee44428 Mon Sep 17 00:00:00 2001 From: "Emil \"AngryAnt\" Johansen" Date: Mon, 27 Mar 2023 15:46:32 +0200 Subject: lib.toPlist: support for path values --- lib/generators.nix | 2 ++ 1 file changed, 2 insertions(+) (limited to 'lib') diff --git a/lib/generators.nix b/lib/generators.nix index 4357a03533984..72c66a0bc7287 100644 --- a/lib/generators.nix +++ b/lib/generators.nix @@ -355,6 +355,7 @@ rec { # PLIST handling toPlist = {}: v: let isFloat = builtins.isFloat or (x: false); + isPath = x: builtins.typeOf x == "path"; expr = ind: x: with builtins; if x == null then "" else if isBool x then bool ind x else @@ -362,6 +363,7 @@ rec { if isString x then str ind x else if isList x then list ind x else if isAttrs x then attrs ind x else + if isPath x then str ind (toString x) else if isFloat x then float ind x else abort "generators.toPlist: should never happen (v = ${v})"; -- cgit 1.4.1 From 63a8c43d0954cf290aa18ae92738a76f8758d463 Mon Sep 17 00:00:00 2001 From: "Emil \"AngryAnt\" Johansen" Date: Mon, 27 Mar 2023 16:27:07 +0200 Subject: lib.toPlist: basic test coverage --- lib/tests/misc.nix | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) (limited to 'lib') diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index baa382f3e589c..d7173bbf3a3d2 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -914,6 +914,75 @@ runTests { expected = "«foo»"; }; + testToPlist = + let + deriv = derivation { name = "test"; builder = "/bin/sh"; system = "aarch64-linux"; }; + in { + expr = mapAttrs (const (generators.toPlist { })) { + value = { + nested.values = rec { + int = 42; + float = 0.1337; + bool = true; + emptystring = ""; + string = "fn\${o}\"r\\d"; + newlinestring = "\n"; + path = /. + "/foo"; + null_ = null; + list = [ 3 4 "test" ]; + emptylist = []; + attrs = { foo = null; "foo b/ar" = "baz"; }; + emptyattrs = {}; + }; + }; + }; + expected = { value = '' + + + + nested + + values + + attrs + + foo b/ar + baz + + bool + + emptyattrs + + + + emptylist + + + + emptystring + + float + 0.133700 + int + 42 + list + + 3 + 4 + test + + newlinestring + + + path + /foo + string + fn''${o}"r\d + + + +''; }; + }; # CLI -- cgit 1.4.1 From e932e98437ea9184365233bad439860c795f65a1 Mon Sep 17 00:00:00 2001 From: "Emil \"AngryAnt\" Johansen" Date: Mon, 27 Mar 2023 17:06:12 +0200 Subject: lib.toPlist: keep test output in external files for their tab indents --- .editorconfig | 4 +++ lib/tests/misc.nix | 47 +--------------------------------- lib/tests/test-to-plist-expected.plist | 46 +++++++++++++++++++++++++++++++++ 3 files changed, 51 insertions(+), 46 deletions(-) create mode 100644 lib/tests/test-to-plist-expected.plist (limited to 'lib') diff --git a/.editorconfig b/.editorconfig index c9711d519408a..ebb66b07945c1 100644 --- a/.editorconfig +++ b/.editorconfig @@ -101,3 +101,7 @@ end_of_line = unset insert_final_newline = unset trim_trailing_whitespace = unset charset = unset + +[lib/tests/*.plist] +indent_style = tab +insert_final_newline = unset diff --git a/lib/tests/misc.nix b/lib/tests/misc.nix index d7173bbf3a3d2..2cb8596703fc5 100644 --- a/lib/tests/misc.nix +++ b/lib/tests/misc.nix @@ -936,52 +936,7 @@ runTests { }; }; }; - expected = { value = '' - - - - nested - - values - - attrs - - foo b/ar - baz - - bool - - emptyattrs - - - - emptylist - - - - emptystring - - float - 0.133700 - int - 42 - list - - 3 - 4 - test - - newlinestring - - - path - /foo - string - fn''${o}"r\d - - - -''; }; + expected = { value = builtins.readFile ./test-to-plist-expected.plist; }; }; # CLI diff --git a/lib/tests/test-to-plist-expected.plist b/lib/tests/test-to-plist-expected.plist new file mode 100644 index 0000000000000..df0528a60767b --- /dev/null +++ b/lib/tests/test-to-plist-expected.plist @@ -0,0 +1,46 @@ + + + + + nested + + values + + attrs + + foo b/ar + baz + + bool + + emptyattrs + + + + emptylist + + + + emptystring + + float + 0.133700 + int + 42 + list + + 3 + 4 + test + + newlinestring + + + path + /foo + string + fn${o}"r\d + + + + \ No newline at end of file -- cgit 1.4.1