Skip to content
quarry Artifact [1b876b63b9]
Login

Artifact [1b876b63b9]

Artifact 1b876b63b9dffcc1580d12154f2de8460aa6ed83288c984375e84182ad6285b9:


package zitcli

import (
	"context"
	"os"
	"path/filepath"
	"strings"
	"testing"
)

// `zit info` is answered from memory while the store file is unchanged,
// and asked again as soon as a write changes it (zit#314: the call is
// ~1 ms inside zit and 15-30 ms of process start around it).
func TestInfoIsRememberedUntilTheStoreChanges(t *testing.T) {
	dir := t.TempDir()
	store := filepath.Join(dir, ".zit")
	if err := os.WriteFile(store, []byte("v1"), 0o644); err != nil {
		t.Fatal(err)
	}
	calls := filepath.Join(t.TempDir(), "calls")
	fakeZit(t, `echo x >> `+calls+`; echo `+strings.Repeat("ab", 32)+`; echo "branch: trunk"`)
	count := func() int {
		b, _ := os.ReadFile(calls)
		return strings.Count(string(b), "x")
	}
	c := New(store)
	ctx := context.Background()
	for range 3 {
		if h, b, err := c.Info(ctx, "trunk"); err != nil || b != "trunk" || len(h) != 64 {
			t.Fatalf("FAIL: info = %q %q %v", h, b, err)
		}
	}
	if n := count(); n != 1 {
		t.Fatalf("FAIL: three reads of an unchanged store ran zit %d times, want 1", n)
	}
	if err := os.WriteFile(store, []byte("v1 and an appended artifact"), 0o644); err != nil {
		t.Fatal(err)
	}
	if _, _, err := c.Info(ctx, "trunk"); err != nil {
		t.Fatal(err)
	}
	if n := count(); n != 2 {
		t.Fatalf("FAIL: a changed store was answered from memory (zit ran %d times, want 2)", n)
	}
	if _, _, err := c.Info(ctx, "other"); err != nil {
		t.Fatal(err)
	}
	if n := count(); n != 3 {
		t.Fatalf("FAIL: a different reference shared an answer (zit ran %d times, want 3)", n)
	}
	t.Log("ok: one zit per store change and reference")
}