socle

clone_test.go

package main

import (
	"net/http/httptest"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
	"testing"
)

// TestCloneEndToEnd runs real `git clone`s against a real listener: the whole point
// of the smart-HTTP endpoints, exercised by the actual client.
// Variants cover protocol v2 (a modern client's default), forced v0 (the preamble path),
// and the ".git"-suffixed URL alias.  The client runs under the same hermetic env
// as fixture building, so no user config can steer its behavior.
func TestCloneEndToEnd(t *testing.T) {
	t.Parallel()
	srv := httptest.NewServer(fixtureServer(t))
	// Cleanup, not defer: the parallel subtests outlive this function body.
	t.Cleanup(srv.Close)
	for _, c := range []struct {
		name    string
		suffix  string
		preArgs []string
	}{
		{name: "protocol v2", suffix: ""},
		{name: "dot-git alias", suffix: ".git"},
		{name: "protocol v0", preArgs: []string{"-c", "protocol.version=0"}},
	} {
		t.Run(c.name, func(t *testing.T) {
			t.Parallel()
			scratch := t.TempDir()
			dest := filepath.Join(scratch, "clone")
			args := append(c.preArgs, "clone", "-q", srv.URL+"/fixturama"+c.suffix, dest)
			gitFixture(t, scratch, args...)

			got, err := os.ReadFile(filepath.Join(dest, "README.md"))
			if err != nil || string(got) != fixtureReadme {
				t.Errorf("cloned README = %q, %v; want the fixture README", got, err)
			}
			cmd := exec.Command("git", "-C", dest, "rev-parse", "HEAD")
			cmd.Env = append(os.Environ(), fixtureEnv...)
			out, err := cmd.Output()
			if err != nil {
				t.Fatalf("rev-parse in clone: %v", err)
			}
			if sha := strings.TrimSpace(string(out)); sha != fixtureShaMain {
				t.Errorf("cloned HEAD = %s, want %s", sha, fixtureShaMain)
			}
		})
	}
}

clone_test.go · 1.7 KiB · 42df532d73