From e029fae0b86b8b6ee39368f7870585809c050b8f Mon Sep 17 00:00:00 2001 From: technofab Date: Sat, 10 May 2025 21:55:08 +0200 Subject: [PATCH] feat: add support for pretty/nix format improve handling string test values --- cmd/nixtest/main.go | 45 ++++++++++++++++++++--------- flake.nix | 12 ++++++++ lib/default.nix | 8 ++++- snapshots/pretty-snapshot.snap.json | 1 + 4 files changed, 52 insertions(+), 14 deletions(-) create mode 100644 snapshots/pretty-snapshot.snap.json diff --git a/cmd/nixtest/main.go b/cmd/nixtest/main.go index b48f7a3..5f2d5bd 100644 --- a/cmd/nixtest/main.go +++ b/cmd/nixtest/main.go @@ -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: diff --git a/flake.nix b/flake.nix index 30f637f..52a5a2e 100644 --- a/flake.nix +++ b/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; diff --git a/lib/default.nix b/lib/default.nix index aad9b60..9587c19 100644 --- a/lib/default.nix +++ b/lib/default.nix @@ -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 = diff --git a/snapshots/pretty-snapshot.snap.json b/snapshots/pretty-snapshot.snap.json new file mode 100644 index 0000000..d7ee4ed --- /dev/null +++ b/snapshots/pretty-snapshot.snap.json @@ -0,0 +1 @@ +"{\n example = \u003cfunction\u003e;\n example2 = {\n drv = \u003cderivation hello-2.12.1\u003e;\n };\n}" \ No newline at end of file