package cli import ( "bytes" "context" "io" "os" "path/filepath" "strings" "testing" "github.com/VictorNagibator/pg_integrity_checksums/cli/internal/baseline" ) func writeBaseline(t *testing.T, path string, objects []baseline.Object) { t.Helper() b := baseline.New("test", baseline.KindLogical, "2.1.0", 160009, "app", baseline.NewScope(), objects) if err := baseline.Save(path, b, os.Stdout); err != nil { t.Fatal(err) } } func runCLI(args ...string) (int, string, string) { var out, errOut bytes.Buffer code := Run(context.Background(), args, &out, &errOut) return code, out.String(), errOut.String() } func TestVersionAndUsage(t *testing.T) { code, out, _ := runCLI("version") if code != 0 || !strings.Contains(out, "pg_integrity_checksums "+Version) { t.Fatalf("version: code=%d out=%q", code, out) } code, out, _ = runCLI("help") if code != 0 || !strings.Contains(out, "Usage:") { t.Fatalf("help: code=%d out=%q", code, out) } code, _, _ = runCLI() if code != 2 { t.Fatalf("no args: code=%d", code) } code, _, _ = runCLI("nope") if code != 2 { t.Fatalf("unknown: code=%d", code) } } func TestShowAndDiff(t *testing.T) { dir := t.TempDir() oldPath := filepath.Join(dir, "old.json") newPath := filepath.Join(dir, "new.json") writeBaseline(t, oldPath, []baseline.Object{ {Kind: baseline.ObjectTable, Schema: "public", Name: "t", Status: baseline.StatusOK, Checksum: "1"}, }) writeBaseline(t, newPath, []baseline.Object{ {Kind: baseline.ObjectTable, Schema: "public", Name: "t", Status: baseline.StatusOK, Checksum: "2"}, }) code, out, errOut := runCLI("show", oldPath) if code != 0 || !strings.Contains(out, "objects: 1") { t.Fatalf("show: code=%d out=%q err=%q", code, out, errOut) } code, out, _ = runCLI("show", "--format", "json", oldPath) if code != 0 || !strings.Contains(out, "pg_integrity_checksums.baseline/v1") { t.Fatalf("show json: code=%d out=%q", code, out) } code, out, _ = runCLI("diff", oldPath, newPath) if code != 1 || !strings.Contains(out, "CHANGED") { t.Fatalf("diff: code=%d out=%q", code, out) } code, out, _ = runCLI("diff", "--format", "json", oldPath, newPath) if code != 1 || !strings.Contains(out, "changed") { t.Fatalf("diff json: code=%d out=%q", code, out) } code, _, _ = runCLI("diff", oldPath, newPath, "--no-fail") if code != 0 { t.Fatalf("diff no-fail: code=%d", code) } code, _, _ = runCLI("diff", oldPath, oldPath) if code != 0 { t.Fatalf("diff same: code=%d", code) } } func TestInvalidBaseline(t *testing.T) { dir := t.TempDir() bad := filepath.Join(dir, "bad.json") if err := os.WriteFile(bad, []byte("{}"), 0o600); err != nil { t.Fatal(err) } code, _, _ := runCLI("show", bad) if code != 3 { t.Fatalf("show bad: code=%d", code) } code, _, _ = runCLI("verify", "--baseline", bad) if code != 3 { t.Fatalf("verify bad: code=%d", code) } code, _, _ = runCLI("diff", bad, bad) if code != 3 { t.Fatalf("diff bad: code=%d", code) } } func TestUpdateRefusesWithoutYes(t *testing.T) { code, _, errOut := runCLI("update", "--baseline", "does-not-exist.json") if code != 4 { t.Fatalf("update refusal: code=%d err=%q", code, errOut) } if !strings.Contains(errOut, "without --yes") { t.Fatalf("unexpected error: %q", errOut) } } func TestOutputDoesNotOverwriteInput(t *testing.T) { dir := t.TempDir() path := filepath.Join(dir, "baseline.json") writeBaseline(t, path, []baseline.Object{{ Kind: baseline.ObjectTable, Schema: "public", Name: "t", Status: baseline.StatusOK, Checksum: "1", }}) before, err := os.ReadFile(path) if err != nil { t.Fatal(err) } for _, args := range [][]string{ {"show", path, "--output", path}, {"diff", path, path, "--output", path}, {"verify", "--baseline", path, "--output", path}, {"update", "--baseline", path, "--output", path, "--yes"}, } { if code, _, _ := runCLI(args...); code != 2 { t.Fatalf("%v: code=%d", args, code) } } after, err := os.ReadFile(path) if err != nil || !bytes.Equal(before, after) { t.Fatalf("baseline changed: %v", err) } } func TestShowHelp(t *testing.T) { code, _, errOut := runCLI("show", "--help") if code != 0 || !strings.Contains(errOut, "usage:") { t.Fatalf("show help: code=%d err=%q", code, errOut) } code, _, errOut = runCLI("diff", "--help") if code != 0 || !strings.Contains(errOut, "usage:") { t.Fatalf("diff help: code=%d err=%q", code, errOut) } } func TestParseHash(t *testing.T) { for _, value := range []string{"fnv32", "fnv64", "crc32c", "xxh3"} { if got, err := parseHash(value); err != nil || got != value { t.Fatalf("parseHash(%q): %q %v", value, got, err) } } for _, value := range []string{"sha256", "XXH3", "crc32", ""} { _, err := parseHash(value) if err == nil { t.Fatalf("parseHash(%q): expected invalid hash error", value) } if !strings.Contains(err.Error(), "fnv32, fnv64, crc32c, xxh3") { t.Fatalf("parseHash(%q) error does not list the algorithms: %v", value, err) } } } func TestParseFormat(t *testing.T) { for _, value := range []string{"text", "json", "junit"} { if got, err := parseFormat(value); err != nil || got != value { t.Fatalf("parseFormat(%q): %q %v", value, got, err) } } _, err := parseFormat("xml") if err == nil || !strings.Contains(err.Error(), "text, json or junit") { t.Fatalf("expected error listing all formats, got %v", err) } } func TestDiffRejectsHashMismatch(t *testing.T) { dir := t.TempDir() oldPath := filepath.Join(dir, "old.json") newPath := filepath.Join(dir, "new.json") writeBaseline(t, oldPath, nil) b := baseline.NewWithHash("test", baseline.KindLogical, baseline.HashFNV64, "2.2.0", 160009, "app", baseline.NewScope(), nil) if err := baseline.Save(newPath, b, os.Stdout); err != nil { t.Fatal(err) } code, _, errOut := runCLI("diff", oldPath, newPath) if code != 3 || !strings.Contains(errOut, "hashes differ") { t.Fatalf("diff hash mismatch: code=%d err=%q", code, errOut) } } func TestCaptureRejectsInvalidHashBeforeConnecting(t *testing.T) { code, _, errOut := runCLI("capture", "--hash", "sha256") if code != 2 || !strings.Contains(errOut, "invalid --hash") { t.Fatalf("capture invalid hash: code=%d err=%q", code, errOut) } } func TestOutputWriterKeepsReportUntilFirstWrite(t *testing.T) { dir := t.TempDir() // A new report file that is never written does not stay behind. fresh := filepath.Join(dir, "fresh.txt") _, closeFresh, err := outputWriter(io.Discard, fresh) if err != nil { t.Fatal(err) } if _, err := os.Stat(fresh); err != nil { t.Fatalf("path should be validated up front: %v", err) } closeFresh() if _, err := os.Stat(fresh); !os.IsNotExist(err) { t.Fatalf("unwritten new report should be removed, stat err = %v", err) } // An existing report survives a failed command and is replaced on write. old := filepath.Join(dir, "old.txt") if err := os.WriteFile(old, []byte("previous report, longer than the new one"), 0o644); err != nil { t.Fatal(err) } _, closeOld, err := outputWriter(io.Discard, old) if err != nil { t.Fatal(err) } closeOld() if got, _ := os.ReadFile(old); !strings.HasPrefix(string(got), "previous") { t.Fatalf("unwritten run must keep the old report, got %q", got) } w, closeW, err := outputWriter(io.Discard, old) if err != nil { t.Fatal(err) } io.WriteString(w, "new") closeW() if got, _ := os.ReadFile(old); string(got) != "new" { t.Fatalf("written run must replace the report, got %q", got) } }