// SPDX-License-Identifier: AGPL-3.0-only package provenance import ( "context" "errors" "strings" "testing" ) type recordingRunner struct { responses map[string][]byte calls []string } func (runner *recordingRunner) Run(_ context.Context, _ string, _ map[string]string, name string, args ...string) ([]byte, error) { key := name + " " + strings.Join(args, " ") runner.calls = append(runner.calls, key) response, ok := runner.responses[key] if !ok { return nil, errors.New("unexpected command: " + key) } return response, nil } func TestInspectAcceptsStandaloneExactPushedCheckout(t *testing.T) { commit := strings.Repeat("a", 40) runner := &recordingRunner{responses: map[string][]byte{ "git rev-parse --git-dir": []byte(".git\n"), "git rev-parse --git-common-dir": []byte(".git\n"), "git status --porcelain=v1 --untracked-files=all": nil, "git rev-parse HEAD": []byte(commit + "\n"), "git ls-remote --exit-code origin refs/heads/main": []byte(commit + "\trefs/heads/main\n"), "git show -s --format=%ct " + commit: []byte("1720000000\n"), }} result, err := Inspect(context.Background(), runner, "/source", "main") if err != nil || result.Commit != commit || result.Epoch != 1720000000 { t.Fatalf("result=%+v err=%v", result, err) } } func TestInspectExplainsUnsupportedLinkedWorktreeBeforeRemoteOrBuildWork(t *testing.T) { runner := &recordingRunner{responses: map[string][]byte{ "git rev-parse --git-dir": []byte("/repo/.git/worktrees/release\n"), "git rev-parse --git-common-dir": []byte("/repo/.git\n"), }} _, err := Inspect(context.Background(), runner, "/source", "main") if err == nil || !strings.Contains(err.Error(), "linked Git worktrees") || len(runner.calls) != 2 { t.Fatalf("calls=%#v err=%v", runner.calls, err) } }