From 47f5b873ee087de86f53aa23dcf459e69abcaa82 Mon Sep 17 00:00:00 2001 From: h7x4 Date: Sun, 30 Jul 2023 01:45:03 +0200 Subject: [PATCH] pkgs.formats.libconfig: add tests for libconfig Co-authored-by: ckie <25263210+ckiee@users.noreply.github.com> --- .../formats/libconfig/test/comprehensive.nix | 69 +++++++++++++++++++ .../formats/libconfig/test/default.nix | 12 ++++ .../formats/libconfig/test/validator.c | 21 ++++++ pkgs/pkgs-lib/tests/default.nix | 1 + 4 files changed, 103 insertions(+) create mode 100644 pkgs/pkgs-lib/formats/libconfig/test/comprehensive.nix create mode 100644 pkgs/pkgs-lib/formats/libconfig/test/default.nix create mode 100644 pkgs/pkgs-lib/formats/libconfig/test/validator.c diff --git a/pkgs/pkgs-lib/formats/libconfig/test/comprehensive.nix b/pkgs/pkgs-lib/formats/libconfig/test/comprehensive.nix new file mode 100644 index 000000000000000..5b36b6464ead42f --- /dev/null +++ b/pkgs/pkgs-lib/formats/libconfig/test/comprehensive.nix @@ -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 + ''; +} \ No newline at end of file diff --git a/pkgs/pkgs-lib/formats/libconfig/test/default.nix b/pkgs/pkgs-lib/formats/libconfig/test/default.nix new file mode 100644 index 000000000000000..8a8c8d992fe624e --- /dev/null +++ b/pkgs/pkgs-lib/formats/libconfig/test/default.nix @@ -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; }; +} \ No newline at end of file diff --git a/pkgs/pkgs-lib/formats/libconfig/test/validator.c b/pkgs/pkgs-lib/formats/libconfig/test/validator.c new file mode 100644 index 000000000000000..738be0b774b5a0f --- /dev/null +++ b/pkgs/pkgs-lib/formats/libconfig/test/validator.c @@ -0,0 +1,21 @@ +// Copyright (C) 2005-2023 Mark A Lindner, ckie +// SPDX-License-Identifier: LGPL-2.1-or-later +#include +#include +int main(int argc, char **argv) +{ + config_t cfg; + config_init(&cfg); + if (argc != 2) + { + fprintf(stderr, "USAGE: validator "); + } + 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"); +} \ No newline at end of file diff --git a/pkgs/pkgs-lib/tests/default.nix b/pkgs/pkgs-lib/tests/default.nix index ae91e15aa9efada..e9a58d4f444096d 100644 --- a/pkgs/pkgs-lib/tests/default.nix +++ b/pkgs/pkgs-lib/tests/default.nix @@ -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: