From 46156529f2682412b8867f7bc40b32b350a00bd9 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 4 Apr 2022 14:08:21 +0200 Subject: [PATCH 1/3] tests.pkgs-lib.formats: Allow strings with context in test runner --- pkgs/pkgs-lib/tests/formats.nix | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/pkgs/pkgs-lib/tests/formats.nix b/pkgs/pkgs-lib/tests/formats.nix index 99b88d36ec3c..25327acfe7a4 100644 --- a/pkgs/pkgs-lib/tests/formats.nix +++ b/pkgs/pkgs-lib/tests/formats.nix @@ -18,8 +18,11 @@ let }) [ def ]); in formatSet.generate "test-format-file" config; - runBuildTest = name: { drv, expected }: pkgs.runCommand name {} '' - if diff -u '${builtins.toFile "expected" expected}' '${drv}'; then + runBuildTest = name: { drv, expected }: pkgs.runCommand name { + passAsFile = ["expected"]; + inherit expected drv; + } '' + if diff -u "$expectedPath" "$drv"; then touch "$out" else echo From 337c72b5cde3a034e92cc6c99865869774be7d47 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 4 Apr 2022 14:20:53 +0200 Subject: [PATCH 2/3] pkgs.formats.javaProperties: Add type coercions A usability improvement. --- .../formats/java-properties/default.nix | 22 +++++++++++++++++-- .../formats/java-properties/test/default.nix | 6 +++++ pkgs/pkgs-lib/tests/formats.nix | 18 +++++++++++++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkgs/pkgs-lib/formats/java-properties/default.nix b/pkgs/pkgs-lib/formats/java-properties/default.nix index 6ac56bff4bf6..d3a4761f0f80 100644 --- a/pkgs/pkgs-lib/formats/java-properties/default.nix +++ b/pkgs/pkgs-lib/formats/java-properties/default.nix @@ -1,6 +1,10 @@ { lib, pkgs }: +let + inherit (lib) types; + inherit (types) attrsOf oneOf coercedTo str bool int float package; +in { - javaProperties = { comment ? "Generated with Nix" }: { + javaProperties = { comment ? "Generated with Nix", boolToString ? lib.boolToString }: { # Design note: # A nested representation of inevitably leads to bad UX: @@ -25,7 +29,21 @@ # We _can_ choose to support hierarchical config files # via nested attrsets, but the module author should # make sure that problem (2) does not occur. - type = lib.types.attrsOf lib.types.str; + type = let + elemType = + oneOf ([ + # `package` isn't generalized to `path` because path values + # are ambiguous. Are they host path strings (toString /foo/bar) + # or should they be added to the store? ("${/foo/bar}") + # The user must decide. + (coercedTo package toString str) + + (coercedTo bool boolToString str) + (coercedTo int toString str) + (coercedTo float toString str) + ]) + // { description = "string, package, bool, int or float"; }; + in attrsOf elemType; generate = name: value: pkgs.runCommandLocal name diff --git a/pkgs/pkgs-lib/formats/java-properties/test/default.nix b/pkgs/pkgs-lib/formats/java-properties/test/default.nix index 4b3845c10296..c45cfd90031f 100644 --- a/pkgs/pkgs-lib/formats/java-properties/test/default.nix +++ b/pkgs/pkgs-lib/formats/java-properties/test/default.nix @@ -5,6 +5,12 @@ , lib , stdenv }: + +# This test primarily tests correct escaping. +# See also testJavaProperties in +# pkgs/pkgs-lib/tests/formats.nix, which tests +# type coercions and is a bit easier to read. + let inherit (lib) concatStrings attrValues mapAttrs; diff --git a/pkgs/pkgs-lib/tests/formats.nix b/pkgs/pkgs-lib/tests/formats.nix index 25327acfe7a4..86ef4c6d2691 100644 --- a/pkgs/pkgs-lib/tests/formats.nix +++ b/pkgs/pkgs-lib/tests/formats.nix @@ -176,11 +176,21 @@ in runBuildTests { ''; }; - # See also java-properties/default.nix for more complete tests + # This test is responsible for + # 1. testing type coercions + # 2. providing a more readable example test + # Whereas java-properties/default.nix tests the low level escaping, etc. testJavaProperties = { drv = evalFormat formats.javaProperties {} { + floaty = 3.1415; + tautologies = true; + contradictions = false; foo = "bar"; - "1" = "2"; + # # Disallowed at eval time, because it's ambiguous: + # # add to store or convert to string? + # root = /root; + "1" = 2; + package = pkgs.hello; "ütf 8" = "dûh"; # NB: Some editors (vscode) show this _whole_ line in right-to-left order "الجبر" = "أكثر من مجرد أرقام"; @@ -189,7 +199,11 @@ in runBuildTests { # Generated with Nix 1 = 2 + contradictions = false + floaty = 3.141500 foo = bar + package = ${pkgs.hello} + tautologies = true \u00fctf\ 8 = d\u00fbh \u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645 ''; From 62d781f09be7bc526e89a6b9bb7ed432dc33df15 Mon Sep 17 00:00:00 2001 From: Robert Hensing Date: Mon, 4 Apr 2022 14:41:40 +0200 Subject: [PATCH 3/3] tests.pkgs-lib: Fix for darwin --- pkgs/pkgs-lib/formats/java-properties/test/default.nix | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkgs/pkgs-lib/formats/java-properties/test/default.nix b/pkgs/pkgs-lib/formats/java-properties/test/default.nix index c45cfd90031f..4a51179d1c86 100644 --- a/pkgs/pkgs-lib/formats/java-properties/test/default.nix +++ b/pkgs/pkgs-lib/formats/java-properties/test/default.nix @@ -77,7 +77,8 @@ stdenv.mkDerivation { src = lib.sourceByRegex ./. [ ".*\.java" ]; - LANG = "C.UTF-8"; + # On Linux, this can be C.UTF-8, but darwin + zulu requires en_US.UTF-8 + LANG = "en_US.UTF-8"; buildPhase = '' javac Main.java '';