Files
ReleaseKit/internal/git/source_test.go

92 lines
3.7 KiB
Go

package git
import (
"context"
"errors"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
)
func TestInspectTaggedSourceSupportsLightweightAndAnnotatedTags(t *testing.T) {
fixture := newRemoteFixture(t)
gitRun(t, fixture.work, "tag", "v1.2.3")
gitRun(t, fixture.work, "checkout", "--detach", "HEAD")
lightweight, err := InspectTaggedSource(context.Background(), fixture.work, "v1.2.3")
if err != nil {
t.Fatal(err)
}
if lightweight.Head != fixture.commit || lightweight.TagCommit != fixture.commit || lightweight.TagObject != fixture.commit || lightweight.TagObjectType != "commit" || lightweight.Status != "" {
t.Fatalf("lightweight source = %#v", lightweight)
}
gitRun(t, fixture.work, "tag", "-d", "v1.2.3")
gitRun(t, fixture.work, "tag", "-a", "v1.2.3", "-m", "Historical release")
annotated, err := InspectTaggedSource(context.Background(), fixture.work, "v1.2.3")
if err != nil {
t.Fatal(err)
}
if annotated.Head != fixture.commit || annotated.TagCommit != fixture.commit || annotated.TagObject == fixture.commit || annotated.TagObjectType != "tag" || annotated.Status != "" {
t.Fatalf("annotated source = %#v", annotated)
}
}
func TestCandidateObjectReadsStayLocalInPartialClones(t *testing.T) {
fixture := newRemoteFixture(t)
gitRun(t, fixture.work, "config", "uploadpack.allowFilter", "true")
blob := strings.TrimSpace(gitText(t, fixture.work, "rev-parse", "HEAD:source.txt"))
for _, filter := range []string{"blob:none", "tree:0"} {
t.Run(filter, func(t *testing.T) {
repository := filepath.Join(t.TempDir(), "partial")
gitRun(t, fixture.work, "clone", "--filter="+filter, "--no-checkout", "file://"+filepath.ToSlash(fixture.work), repository)
before := gitText(t, repository, "cat-file", "--batch-all-objects", "--batch-check=%(objectname)")
if strings.Contains(before, blob) {
t.Fatal("fixture contains the filtered blob")
}
gitRun(t, repository, "config", "remote.origin.url", "ssh://releasekit-test.invalid/source")
attempts := filepath.Join(t.TempDir(), "attempts")
transport := filepath.Join(t.TempDir(), "ssh")
if err := os.WriteFile(transport, []byte("#!/bin/sh\nprintf 'remote accessed\\n' >> \"$RELEASEKIT_GIT_ATTEMPTS\"\nexit 1\n"), 0o755); err != nil {
t.Fatal(err)
}
t.Setenv("GIT_SSH_COMMAND", transport)
t.Setenv("RELEASEKIT_GIT_ATTEMPTS", attempts)
t.Setenv("GIT_NO_LAZY_FETCH", "0")
t.Setenv("GIT_ALLOW_PROTOCOL", "ssh")
if _, err := ReadTree(t.Context(), repository, fixture.commit); err == nil {
t.Fatal("tree inspection succeeded with unavailable objects")
}
_, err := ReadBlob(t.Context(), repository, blob)
var exitError *exec.ExitError
if !errors.As(err, &exitError) {
t.Fatalf("blob error %v lost its Git process cause", err)
}
if data, err := os.ReadFile(attempts); !errors.Is(err, os.ErrNotExist) {
t.Fatalf("object inspection invoked remote transport: %q, %v", data, err)
}
after := gitText(t, repository, "cat-file", "--batch-all-objects", "--batch-check=%(objectname)")
if before != after {
t.Fatal("object inspection populated the object database")
}
})
}
}
func TestInspectTaggedSourceReportsDirtyCheckoutAndReadCommitFile(t *testing.T) {
fixture := newRemoteFixture(t)
gitRun(t, fixture.work, "tag", "v1.2.3")
content, err := ReadCommitFile(context.Background(), fixture.work, fixture.commit, "source.txt")
if err != nil || string(content) != "candidate" {
t.Fatalf("committed content = %q, error = %v", content, err)
}
writeRepoFile(t, fixture.work, "untracked.txt", "dirty")
source, err := InspectTaggedSource(context.Background(), fixture.work, "v1.2.3")
if err != nil {
t.Fatal(err)
}
if !strings.Contains(source.Status, "untracked.txt") {
t.Fatalf("dirty status = %q", source.Status)
}
}