mirror of
https://gitlab.com/TECHNOFAB/nixtest.git
synced 2025-12-11 01:30:11 +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)
|
||||
}
|
||||
|
||||
func isString(value any) bool {
|
||||
switch value.(type) {
|
||||
case string:
|
||||
return true
|
||||
default:
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
func runTest(spec TestSpec) TestResult {
|
||||
startTime := time.Now()
|
||||
result := TestResult{
|
||||
|
|
@ -168,22 +177,32 @@ func runTest(spec TestSpec) TestResult {
|
|||
if reflect.DeepEqual(actual, expected) {
|
||||
result.Status = StatusSuccess
|
||||
} else {
|
||||
text1, err := json.MarshalIndent(expected, "", " ")
|
||||
if err != nil {
|
||||
result.Status = StatusError
|
||||
result.ErrorMessage = fmt.Sprintf("[system] failed to json marshal 'expected': %v", err.Error())
|
||||
goto end
|
||||
}
|
||||
text2, 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
|
||||
var text1, text2 string
|
||||
|
||||
// just keep strings as is, only json marshal if any of them is not a string
|
||||
if isString(actual) && isString(expected) {
|
||||
text1 = actual.(string)
|
||||
text2 = expected.(string)
|
||||
} else {
|
||||
bytes1, err := json.MarshalIndent(expected, "", " ")
|
||||
if err != nil {
|
||||
result.Status = StatusError
|
||||
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.Expected = string(text1)
|
||||
result.Actual = string(text2)
|
||||
result.Expected = text1
|
||||
result.Actual = text2
|
||||
}
|
||||
|
||||
end:
|
||||
|
|
|
|||
12
flake.nix
12
flake.nix
|
|
@ -77,6 +77,7 @@
|
|||
pos = __curPos;
|
||||
expected = null;
|
||||
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
|
||||
'';
|
||||
}
|
||||
|
|
@ -88,6 +89,17 @@
|
|||
pos = __curPos;
|
||||
actual = {hello = "world";};
|
||||
}
|
||||
{
|
||||
name = "pretty-snapshot";
|
||||
type = "snapshot";
|
||||
format = "pretty";
|
||||
actual = {
|
||||
example = args: {};
|
||||
example2 = {
|
||||
drv = pkgs.hello;
|
||||
};
|
||||
};
|
||||
}
|
||||
{
|
||||
name = "test-drv";
|
||||
pos = __curPos;
|
||||
|
|
|
|||
|
|
@ -8,14 +8,20 @@
|
|||
type ? "unit",
|
||||
name,
|
||||
description ? "",
|
||||
format ? "json",
|
||||
expected ? null,
|
||||
actual ? null,
|
||||
actualDrv ? null,
|
||||
pos ? null,
|
||||
}: let
|
||||
fileRelative = lib.removePrefix ((toString self) + "/") pos.file;
|
||||
actual' =
|
||||
if format == "json"
|
||||
then actual
|
||||
else lib.generators.toPretty {} actual;
|
||||
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
|
||||
actualDrv = builtins.unsafeDiscardStringContext (actualDrv.drvPath or "");
|
||||
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