From ae10354ff007f799c03c510fd2899ee956961807 Mon Sep 17 00:00:00 2001 From: PJ Date: Sat, 18 Apr 2026 06:46:24 +0700 Subject: [PATCH] test(sidecar): split tests across stub and withsidecar builds Default !withsidecar build: assert IsPlaceholder, empty JAR, Extract errors. withsidecar build: existing extract/checksum coverage. --- internal/sidecar/embed_test.go | 103 +++------------------ internal/sidecar/embed_withsidecar_test.go | 90 ++++++++++++++++++ 2 files changed, 104 insertions(+), 89 deletions(-) create mode 100644 internal/sidecar/embed_withsidecar_test.go diff --git a/internal/sidecar/embed_test.go b/internal/sidecar/embed_test.go index 8609290..8519865 100644 --- a/internal/sidecar/embed_test.go +++ b/internal/sidecar/embed_test.go @@ -1,95 +1,20 @@ +//go:build !withsidecar + package sidecar -import ( - "crypto/sha256" - "encoding/hex" - "os" - "path/filepath" - "testing" -) +import "testing" -func TestEmbeddedNonZero(t *testing.T) { - if EmbeddedSize() == 0 { - t.Errorf("expected embedded JAR to be non-empty") - } -} - -func TestExtract_WritesJARAndChecksum(t *testing.T) { - directory := t.TempDir() - path, err := Extract(directory) - if err != nil { - t.Fatal(err) - } - if path != filepath.Join(directory, "uatu-sidecar.jar") { - t.Errorf("unexpected path: %s", path) - } - body, err := os.ReadFile(path) - if err != nil { - t.Fatal(err) - } - if EmbeddedSize() != len(body) { - t.Errorf("size mismatch: embedded=%d, written=%d", EmbeddedSize(), len(body)) - } - sum := sha256.Sum256(body) - if hex.EncodeToString(sum[:]) != EmbeddedSHA256() { - t.Errorf("hash mismatch") - } - checksum, err := os.ReadFile(path + ".sha256") - if err != nil { - t.Fatal(err) - } - if string(checksum) != EmbeddedSHA256() { - t.Errorf("checksum file content wrong: %q", checksum) - } -} - -func TestExtract_ReusesIdenticalFile(t *testing.T) { - directory := t.TempDir() - path, err := Extract(directory) - if err != nil { - t.Fatal(err) - } - originalStat, err := os.Stat(path) - if err != nil { - t.Fatal(err) - } - originalModTime := originalStat.ModTime() - - // Second extract should be a no-op (no rewrite). - if _, err := Extract(directory); err != nil { - t.Fatal(err) - } - secondStat, err := os.Stat(path) - if err != nil { - t.Fatal(err) - } - if !secondStat.ModTime().Equal(originalModTime) { - t.Errorf("second extract should not have rewritten the file") - } -} - -func TestExtract_RewritesIfChecksumMissing(t *testing.T) { - directory := t.TempDir() - path, err := Extract(directory) - if err != nil { - t.Fatal(err) - } - if err := os.Remove(path + ".sha256"); err != nil { - t.Fatal(err) - } - if _, err := Extract(directory); err != nil { - t.Fatal(err) - } - if _, err := os.Stat(path + ".sha256"); err != nil { - t.Errorf("checksum should have been written: %v", err) - } -} - -func TestIsPlaceholder_FlagsDevBuilds(t *testing.T) { - // The repo ships with a placeholder so fresh clones build without - // needing `make sidecar` first. The flag lets `uatu test` warn loudly - // before trying to drive a real device. +func TestStub_IsPlaceholder(t *testing.T) { if !IsPlaceholder() { - t.Logf("running against a real sidecar JAR (size=%d)", EmbeddedSize()) + t.Errorf("default build should be a placeholder") + } + if EmbeddedSize() != 0 { + t.Errorf("stub should have empty embedded JAR, got size %d", EmbeddedSize()) + } +} + +func TestStub_ExtractFails(t *testing.T) { + if _, err := Extract(t.TempDir()); err == nil { + t.Fatal("expected Extract to fail in stub mode") } } diff --git a/internal/sidecar/embed_withsidecar_test.go b/internal/sidecar/embed_withsidecar_test.go new file mode 100644 index 0000000..c7fc636 --- /dev/null +++ b/internal/sidecar/embed_withsidecar_test.go @@ -0,0 +1,90 @@ +//go:build withsidecar + +package sidecar + +import ( + "crypto/sha256" + "encoding/hex" + "os" + "path/filepath" + "testing" +) + +func TestEmbeddedNonZero(t *testing.T) { + if EmbeddedSize() == 0 { + t.Errorf("expected embedded JAR to be non-empty") + } + if IsPlaceholder() { + t.Errorf("withsidecar build should not be a placeholder") + } +} + +func TestExtract_WritesJARAndChecksum(t *testing.T) { + directory := t.TempDir() + path, err := Extract(directory) + if err != nil { + t.Fatal(err) + } + if path != filepath.Join(directory, "uatu-sidecar.jar") { + t.Errorf("unexpected path: %s", path) + } + body, err := os.ReadFile(path) + if err != nil { + t.Fatal(err) + } + if EmbeddedSize() != len(body) { + t.Errorf("size mismatch: embedded=%d, written=%d", EmbeddedSize(), len(body)) + } + sum := sha256.Sum256(body) + if hex.EncodeToString(sum[:]) != EmbeddedSHA256() { + t.Errorf("hash mismatch") + } + checksum, err := os.ReadFile(path + ".sha256") + if err != nil { + t.Fatal(err) + } + if string(checksum) != EmbeddedSHA256() { + t.Errorf("checksum file content wrong: %q", checksum) + } +} + +func TestExtract_ReusesIdenticalFile(t *testing.T) { + directory := t.TempDir() + path, err := Extract(directory) + if err != nil { + t.Fatal(err) + } + originalStat, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + originalModTime := originalStat.ModTime() + + if _, err := Extract(directory); err != nil { + t.Fatal(err) + } + secondStat, err := os.Stat(path) + if err != nil { + t.Fatal(err) + } + if !secondStat.ModTime().Equal(originalModTime) { + t.Errorf("second extract should not have rewritten the file") + } +} + +func TestExtract_RewritesIfChecksumMissing(t *testing.T) { + directory := t.TempDir() + path, err := Extract(directory) + if err != nil { + t.Fatal(err) + } + if err := os.Remove(path + ".sha256"); err != nil { + t.Fatal(err) + } + if _, err := Extract(directory); err != nil { + t.Fatal(err) + } + if _, err := os.Stat(path + ".sha256"); err != nil { + t.Errorf("checksum should have been written: %v", err) + } +}