Merge pull request #167172 from hercules-ci/javaProperties-type-coercions

`formats.javaProperties`: add type coercions
This commit is contained in:
Robert Hensing 2022-04-25 16:29:48 +02:00 committed by GitHub
commit 3c10819ecf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 7 deletions

View File

@ -1,6 +1,10 @@
{ lib, pkgs }: { 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: # Design note:
# A nested representation of inevitably leads to bad UX: # A nested representation of inevitably leads to bad UX:
@ -25,7 +29,21 @@
# We _can_ choose to support hierarchical config files # We _can_ choose to support hierarchical config files
# via nested attrsets, but the module author should # via nested attrsets, but the module author should
# make sure that problem (2) does not occur. # 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: generate = name: value:
pkgs.runCommandLocal name pkgs.runCommandLocal name

View File

@ -5,6 +5,12 @@
, lib , lib
, stdenv , 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 let
inherit (lib) concatStrings attrValues mapAttrs; inherit (lib) concatStrings attrValues mapAttrs;
@ -71,7 +77,8 @@ stdenv.mkDerivation {
src = lib.sourceByRegex ./. [ src = lib.sourceByRegex ./. [
".*\.java" ".*\.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 = '' buildPhase = ''
javac Main.java javac Main.java
''; '';

View File

@ -18,8 +18,11 @@ let
}) [ def ]); }) [ def ]);
in formatSet.generate "test-format-file" config; in formatSet.generate "test-format-file" config;
runBuildTest = name: { drv, expected }: pkgs.runCommand name {} '' runBuildTest = name: { drv, expected }: pkgs.runCommand name {
if diff -u '${builtins.toFile "expected" expected}' '${drv}'; then passAsFile = ["expected"];
inherit expected drv;
} ''
if diff -u "$expectedPath" "$drv"; then
touch "$out" touch "$out"
else else
echo echo
@ -171,11 +174,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 = { testJavaProperties = {
drv = evalFormat formats.javaProperties {} { drv = evalFormat formats.javaProperties {} {
floaty = 3.1415;
tautologies = true;
contradictions = false;
foo = "bar"; 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"; "ütf 8" = "dûh";
# NB: Some editors (vscode) show this _whole_ line in right-to-left order # NB: Some editors (vscode) show this _whole_ line in right-to-left order
"الجبر" = "أكثر من مجرد أرقام"; "الجبر" = "أكثر من مجرد أرقام";
@ -184,7 +197,11 @@ in runBuildTests {
# Generated with Nix # Generated with Nix
1 = 2 1 = 2
contradictions = false
floaty = 3.141500
foo = bar foo = bar
package = ${pkgs.hello}
tautologies = true
\u00fctf\ 8 = d\u00fbh \u00fctf\ 8 = d\u00fbh
\u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645 \u0627\u0644\u062c\u0628\u0631 = \u0623\u0643\u062b\u0631 \u0645\u0646 \u0645\u062c\u0631\u062f \u0623\u0631\u0642\u0627\u0645
''; '';