fix: prevent nil pointer panic in codereview compareTestResults
Fix for https://github.com/boldsoftware/sketch/issues/175
Fix nil pointer dereference when comparing test results between commits
where a package exists in the after state but not the before state.
The panic occurred at line 867 when accessing beforeResult.TestStatus[test]
without checking if beforeResult was nil. This happens when testing new
packages that didn't exist in the base commit.
Now properly check for nil beforeResult and default to testStatusUnknown
for tests in packages that didn't exist in the before state.
Add comprehensive unit tests covering:
- New package with passing tests (the panic scenario)
- New package with failing tests (regression detection)
- Existing package regressions (ensure normal flow still works)
- End-to-end integration test for new package scenario
Co-Authored-By: sketch <hello@sketch.dev>
Change-ID: sab802d91eff08039k
diff --git a/claudetool/codereview/testdata/new_package_with_tests.txtar b/claudetool/codereview/testdata/new_package_with_tests.txtar
new file mode 100644
index 0000000..8d6eede
--- /dev/null
+++ b/claudetool/codereview/testdata/new_package_with_tests.txtar
@@ -0,0 +1,33 @@
+New package with tests should not cause nil pointer panic
+
+-- go.mod --
+module sketch.dev
+
+go 1.21
+
+-- .commit --
+Initial commit with no packages
+
+-- newpkg/main.go --
+package newpkg
+
+func Hello() string {
+ return "hello"
+}
+
+-- newpkg/main_test.go --
+package newpkg
+
+import "testing"
+
+func TestHello(t *testing.T) {
+ if got := Hello(); got != "hello" {
+ t.Errorf("Hello() = %q, want %q", got, "hello")
+ }
+}
+
+-- .commit --
+Add new package with tests
+
+-- .run_test --
+OK