package zitcli
import (
"context"
"errors"
"testing"
)
// zit exits 1 for a hash it has no artifact for, and ALSO for running
// out of memory or failing to open the store. Only the first is the
// artifact being absent; a caller that remembers absence must be able
// to tell it apart, so only zit's own "no artifact matches" message
// answers ErrNoArtifact. Both still read as ErrNoEntry, as before.
func TestOnlyZitsNoMatchMessageIsAMissingArtifact(t *testing.T) {
c := &Client{repo: "tcl"}
for _, tc := range []struct {
what, script string
missing bool
}{
{"no match", `echo "zit artifact: no artifact matches 'dddd'" >&2; exit 1`, true},
{"out of memory", `echo "zit artifact: out of memory" >&2; exit 1`, false},
{"store unreadable", `echo "zit: cannot open repository: not a zit repository" >&2; exit 1`, false},
} {
fakeZit(t, tc.script)
for name, call := range map[string]func() error{
"size": func() error { _, err := c.ArtifactSize(context.Background(), "dddd"); return err },
"head": func() error { _, err := c.ArtifactHead(context.Background(), "dddd", 64); return err },
} {
err := call()
if !errors.Is(err, ErrNoEntry) {
t.Errorf("FAIL: %s %s: %v, want ErrNoEntry as before", tc.what, name, err)
}
if got := errors.Is(err, ErrNoArtifact); got != tc.missing {
t.Errorf("FAIL: %s %s: ErrNoArtifact=%v, want %v (%v)", tc.what, name, got, tc.missing, err)
}
}
}
t.Log("ok: only zit's no-match message is a missing artifact")
}