mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-12 02:00:18 +01:00
feat: add support for pretty/nix format
improve handling string test values
This commit is contained in:
parent
0a1bbae2c3
commit
e029fae0b8
4 changed files with 52 additions and 14 deletions
|
|
@ -110,6 +110,15 @@ func shouldSkip(input string, pattern string) bool {
|
||||||
return regex.MatchString(input)
|
return regex.MatchString(input)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func isString(value any) bool {
|
||||||
|
switch value.(type) {
|
||||||
|
case string:
|
||||||
|
return true
|
||||||
|
default:
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func runTest(spec TestSpec) TestResult {
|
func runTest(spec TestSpec) TestResult {
|
||||||
startTime := time.Now()
|
startTime := time.Now()
|
||||||
result := TestResult{
|
result := TestResult{
|
||||||
|
|
@ -168,22 +177,32 @@ func runTest(spec TestSpec) TestResult {
|
||||||
if reflect.DeepEqual(actual, expected) {
|
if reflect.DeepEqual(actual, expected) {
|
||||||
result.Status = StatusSuccess
|
result.Status = StatusSuccess
|
||||||
} else {
|
} else {
|
||||||
text1, err := json.MarshalIndent(expected, "", " ")
|
var text1, text2 string
|
||||||
if err != nil {
|
|
||||||
result.Status = StatusError
|
// just keep strings as is, only json marshal if any of them is not a string
|
||||||
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
|
if isString(actual) && isString(expected) {
|
||||||
goto end
|
text1 = actual.(string)
|
||||||
}
|
text2 = expected.(string)
|
||||||
text2, err := json.MarshalIndent(actual, "", " ")
|
} else {
|
||||||
if err != nil {
|
bytes1, err := json.MarshalIndent(expected, "", " ")
|
||||||
result.Status = StatusError
|
if err != nil {
|
||||||
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'actual': %v", err.Error())
|
result.Status = StatusError
|
||||||
goto end
|
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
|
||||||
|
goto end
|
||||||
|
}
|
||||||
|
bytes2, err := json.MarshalIndent(actual, "", " ")
|
||||||
|
if err != nil {
|
||||||
|
result.Status = StatusError
|
||||||
|
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'actual': %v", err.Error())
|
||||||
|
goto end
|
||||||
|
}
|
||||||
|
text1 = string(bytes1)
|
||||||
|
text2 = string(bytes2)
|
||||||
}
|
}
|
||||||
|
|
||||||
result.Status = StatusFailure
|
result.Status = StatusFailure
|
||||||
result.Expected = string(text1)
|
result.Expected = text1
|
||||||
result.Actual = string(text2)
|
result.Actual = text2
|
||||||
}
|
}
|
||||||
|
|
||||||
end:
|
end:
|
||||||
|
|
|
||||||
12
flake.nix
12
flake.nix
|
|
@ -77,6 +77,7 @@
|
||||||
pos = __curPos;
|
pos = __curPos;
|
||||||
expected = null;
|
expected = null;
|
||||||
actualDrv = pkgs.runCommand "test-error-drv" {} ''
|
actualDrv = pkgs.runCommand "test-error-drv" {} ''
|
||||||
|
echo "This works, but its better to just write 'fail' to \$out and expect 'success' or sth."
|
||||||
exit 1
|
exit 1
|
||||||
'';
|
'';
|
||||||
}
|
}
|
||||||
|
|
@ -88,6 +89,17 @@
|
||||||
pos = __curPos;
|
pos = __curPos;
|
||||||
actual = {hello = "world";};
|
actual = {hello = "world";};
|
||||||
}
|
}
|
||||||
|
{
|
||||||
|
name = "pretty-snapshot";
|
||||||
|
type = "snapshot";
|
||||||
|
format = "pretty";
|
||||||
|
actual = {
|
||||||
|
example = args: {};
|
||||||
|
example2 = {
|
||||||
|
drv = pkgs.hello;
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
{
|
{
|
||||||
name = "test-drv";
|
name = "test-drv";
|
||||||
pos = __curPos;
|
pos = __curPos;
|
||||||
|
|
|
||||||
|
|
@ -8,14 +8,20 @@
|
||||||
type ? "unit",
|
type ? "unit",
|
||||||
name,
|
name,
|
||||||
description ? "",
|
description ? "",
|
||||||
|
format ? "json",
|
||||||
expected ? null,
|
expected ? null,
|
||||||
actual ? null,
|
actual ? null,
|
||||||
actualDrv ? null,
|
actualDrv ? null,
|
||||||
pos ? null,
|
pos ? null,
|
||||||
}: let
|
}: let
|
||||||
fileRelative = lib.removePrefix ((toString self) + "/") pos.file;
|
fileRelative = lib.removePrefix ((toString self) + "/") pos.file;
|
||||||
|
actual' =
|
||||||
|
if format == "json"
|
||||||
|
then actual
|
||||||
|
else lib.generators.toPretty {} actual;
|
||||||
in {
|
in {
|
||||||
inherit type name description expected actual;
|
inherit type name description expected;
|
||||||
|
actual = actual';
|
||||||
# discard string context, otherwise it's being built instantly which we don't want
|
# discard string context, otherwise it's being built instantly which we don't want
|
||||||
actualDrv = builtins.unsafeDiscardStringContext (actualDrv.drvPath or "");
|
actualDrv = builtins.unsafeDiscardStringContext (actualDrv.drvPath or "");
|
||||||
pos =
|
pos =
|
||||||
|
|
|
||||||
1
snapshots/pretty-snapshot.snap.json
Normal file
1
snapshots/pretty-snapshot.snap.json
Normal file
|
|
@ -0,0 +1 @@
|
||||||
|
"{\n example = \u003cfunction\u003e;\n example2 = {\n drv = \u003cderivation hello-2.12.1\u003e;\n };\n}"
|
||||||
Loading…
Add table
Add a link
Reference in a new issue