pkgs-lib/formats: Add tests for serializing null values

In the effort to pass values directly as structured values, we ran into
issue with serializing null's.
This adds tests for null for all formats that support it and negative
tests for some others.

See https://github.com/NixOS/nixpkgs/pull/524404#issuecomment-4665694636
This commit is contained in:
Yuriy Taraday
2026-06-23 19:00:26 +02:00
parent 9f4e01777a
commit 7601f8a108
2 changed files with 90 additions and 1 deletions
+7 -1
View File
@@ -1017,7 +1017,13 @@ optionalAttrs allowAliases aliases
}:
if format == "badgerfish" then
{
type = serializableValueWith { typeName = "XML"; };
type =
attrsOf (serializableValueWith {
typeName = "XML";
})
// {
description = "XML value";
};
generate =
name: value:
+83
View File
@@ -142,6 +142,14 @@ runBuildTests {
'';
};
jsonNull = shouldPass {
format = formats.json { };
input = null;
expected = ''
null
'';
};
yaml_1_1Atoms = shouldPass {
format = formats.yaml_1_1 { };
input = {
@@ -176,6 +184,15 @@ runBuildTests {
'';
};
yaml_1_1Null = shouldPass {
format = formats.yaml_1_1 { };
input = null;
expected = ''
null
...
'';
};
yaml_1_2Atoms = shouldPass {
format = formats.yaml_1_2 { };
input = {
@@ -210,6 +227,16 @@ runBuildTests {
'';
};
yaml_1_2Null = shouldPass {
format = formats.yaml_1_2 { };
input = null;
# nixfmt insists on removing indentation, so force it with ${" "}
expected = ''
${" "}null
'';
};
iniAtoms = shouldPass {
format = formats.ini { };
input = {
@@ -879,6 +906,14 @@ runBuildTests {
'';
};
cdnNull = shouldPass {
format = formats.cdn { };
input = null;
expected = ''
null: null
'';
};
# This test is responsible for
# 1. testing type coercions
# 2. providing a more readable example test
@@ -989,6 +1024,14 @@ runBuildTests {
'';
};
luaNull = shouldPass {
format = formats.lua { };
input = null;
expected = ''
return nil
'';
};
nixConfAtoms = shouldPass {
format = formats.nixConf {
package = pkgs.nix;
@@ -1014,6 +1057,15 @@ runBuildTests {
'';
};
nixConfNull = shouldFail {
format = formats.nixConf {
package = pkgs.nix;
version = pkgs.nix.version;
extraOptions = "ignore-try = false";
};
input = null;
};
phpAtoms = shouldPass rec {
format = formats.php { finalVariable = "config"; };
input = {
@@ -1043,6 +1095,16 @@ runBuildTests {
'';
};
phpNull = shouldPass {
format = formats.php { finalVariable = "config"; };
input = null;
expected = ''
<?php
declare(strict_types=1);
$config = null;
'';
};
pythonVars = shouldPass (
let
format = formats.pythonVars { };
@@ -1100,6 +1162,11 @@ runBuildTests {
}
);
pythonVarsNull = shouldFail {
format = formats.pythonVars { };
input = null;
};
phpReturn = shouldPass {
format = formats.php { };
input = {
@@ -1145,6 +1212,11 @@ runBuildTests {
'';
};
xmlNull = shouldFail {
format = formats.xml { };
input = null;
};
PlistGenerate = shouldPass {
format = formats.plist { };
input = {
@@ -1220,6 +1292,17 @@ runBuildTests {
</plist>'';
};
PlistNull = shouldPass {
format = formats.plist { };
input = null;
expected = ''
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
</plist>'';
};
hcl1Atoms = shouldPass {
format = formats.hcl1 { };
input = {