Skip to content
quarry Artifact [bf697f1c52]
Login

Artifact [bf697f1c52]

Artifact bf697f1c522e04e32b5c61f4826914e86980eb30573eea62e27ba9c38805910f:


package main

import (
	"os"
	"path/filepath"
	"testing"
	"time"
)

// A read leaves the stamp alone; a write to the store or its WAL moves
// it; a store the stat cannot see is never "unchanged".
func TestStoreStampMovesOnlyOnWrites(t *testing.T) {
	dir := t.TempDir()
	store := filepath.Join(dir, ".zit")
	if err := os.WriteFile(store, []byte("one"), 0o600); err != nil {
		t.Fatal(err)
	}
	old := time.Now().Add(-time.Hour)
	os.Chtimes(store, old, old)

	first := stampStore(dir)
	if !first.ok {
		t.Fatal("FAIL: readable store stamped as not ok")
	}
	if _, err := os.ReadFile(store); err != nil {
		t.Fatal(err)
	}
	if got := stampStore(dir); got != first {
		t.Fatalf("FAIL: a read moved the stamp: %+v -> %+v", first, got)
	}
	if err := os.WriteFile(store, []byte("two"), 0o600); err != nil {
		t.Fatal(err)
	}
	afterWrite := stampStore(dir)
	if afterWrite == first {
		t.Fatal("FAIL: a store write did not move the stamp")
	}
	if err := os.WriteFile(store+"-wal", []byte("frame"), 0o600); err != nil {
		t.Fatal(err)
	}
	if got := stampStore(dir); got == afterWrite {
		t.Fatal("FAIL: a WAL write did not move the stamp")
	}
	if got := stampStore(store); !got.ok || got.size != 3 {
		t.Fatalf("FAIL: stamping the store file directly: %+v", got)
	}
	if got := stampStore(filepath.Join(dir, "missing")); got.ok {
		t.Fatal("FAIL: a missing store stamped as ok")
	}
	t.Log("ok: stamp moves on writes only, missing store is never unchanged")
}