Skip to content

Commit

Permalink
pkgs.formats.libconfig: add tests for libconfig
Browse files Browse the repository at this point in the history
Co-authored-by: ckie <[email protected]>
  • Loading branch information
h7x4 and ckiee committed Aug 13, 2023
1 parent e333347 commit 47f5b87
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 0 deletions.
69 changes: 69 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/test/comprehensive.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{ pkgs, lib, stdenvNoCC, validator, ... }:
let
libconfig = (pkgs.formats.libconfig { });

include_expr = {
val = 1;
};

include_file = libconfig.generate "libconfig-test-include" include_expr;

expression = {
simple_top_level_attr = "1.0";
nested.attrset.has.a.integer.value = 100;
some_floaty = 29.95;
## Same syntax here on these two, but they should get serialized differently:
# > A list may have zero or more elements, each of which can be a scalar value, an array, a group, or another list.
list1d = libconfig.lib.mkList [ 1 "mixed!" 5 2 ];
# You might also omit the mkList, as a list will be a list (in contrast to an array) by default.
list2d = [ 1 [ 1 1.2 "foo" ] [ "bar" 1.2 1 ] ];
# > An array may have zero or more elements, but the elements must all be scalar values of the same type.
array1d = libconfig.lib.mkArray [ 1 5 2 ];
array2d = [
(libconfig.lib.mkArray [ 1 2 ])
(libconfig.lib.mkArray [ 2 1 ])
];
nasty_string = "\"\n\\\t\r^*\b\f\n\0\";'''$";

weirderTypes = {
_includes = [ include_file ];
pi = 3.141592654;
bigint = 9223372036854775807;
hex = libconfig.lib.mkHex "0x1FC3";
octal = libconfig.lib.mkOctal "0027";
float = libconfig.lib.mkFloat "1.2E-3";
array_of_ints = libconfig.lib.mkArray [
(libconfig.lib.mkOctal "0732")
(libconfig.lib.mkHex "0xA3")
1234
];
list_of_weird_types = [
3.141592654
9223372036854775807
(libconfig.lib.mkHex "0x1FC3")
(libconfig.lib.mkOctal "0027")
(libconfig.lib.mkFloat "1.2E-32")
];
};
};
testLibconfig = libconfig.generate "libconfig-test.cfg" expression;
in
stdenvNoCC.mkDerivation {
name = "libconfig-test";
dontUnpack = true;
dontBuild = true;

doCheck = true;
checkPhase = ''
${validator} ${testLibconfig} > validation.log
'';

json = builtins.toJSON expression;
passAsFile = [ "json" ];

installPhase = ''
mkdir -p $out
cp $jsonPath $out/test.json
ln -s ${testLibconfig} $out/test.cfg
'';
}
12 changes: 12 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/test/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{ pkgs, ... }:
let
validator = pkgs.runCommandCC "libconfig-validator"
{
buildInputs = [ pkgs.libconfig ];
} ''
$CC -lconfig -x c - -o "$out" ${./validator.c}
'';
in
{
comprehensive = pkgs.callPackage ./comprehensive.nix { inherit validator; };
}
21 changes: 21 additions & 0 deletions pkgs/pkgs-lib/formats/libconfig/test/validator.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright (C) 2005-2023 Mark A Lindner, ckie
// SPDX-License-Identifier: LGPL-2.1-or-later
#include <stdio.h>
#include <libconfig.h>
int main(int argc, char **argv)
{
config_t cfg;
config_init(&cfg);
if (argc != 2)
{
fprintf(stderr, "USAGE: validator <path-to-validate>");
}
if(! config_read_file(&cfg, argv[1]))
{
fprintf(stderr, "[libconfig] %s:%d - %s\n", config_error_file(&cfg),
config_error_line(&cfg), config_error_text(&cfg));
config_destroy(&cfg);
return 1;
}
printf("[libconfig] validation ok\n");
}
1 change: 1 addition & 0 deletions pkgs/pkgs-lib/tests/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ let
jdk11 = pkgs.callPackage ../formats/java-properties/test { jdk = pkgs.jdk11_headless; };
jdk17 = pkgs.callPackage ../formats/java-properties/test { jdk = pkgs.jdk17_headless; };
};
libconfig = import ../formats/libconfig/test { inherit pkgs; };
};

flatten = prefix: as:
Expand Down

0 comments on commit 47f5b87

Please sign in to comment.