git@cosmocr.at/gitweb.git @ HEAD · summary refs log
HEAD / internal / gitrepo / locate_test.go /
internal/gitrepo/locate_test.go · 2.0 KiB · raw
view: SourceMarkdown ☑ force

package gitrepo

import ( "testing"

"gitweb/internal/store"

)

// fakeStore implements store.Store with a fixed set of paths. Each path maps to // whether it is a directory. type fakeStore map[string]bool

func (f fakeStore) Stat(p string) (bool, error) { isDir, ok := f[p] if !ok { return false, store.ErrNotFound } return isDir, nil } func (f fakeStore) Get(string) ([]byte, error) { return nil, store.ErrNotFound } func (f fakeStore) List(string) ([]store.Entry, error) { return nil, store.ErrNotFound } func (f fakeStore) Seq(string) (int64, error) { return 0, nil }

// repoAt registers the marker files that make prefix look like a bare repo. func repoAt(f fakeStore, prefix string) { f[prefix] = true f[prefix+"/HEAD"] = false f[prefix+"/objects"] = true }

func TestLocate(t *testing.T) { fs := fakeStore{ "u@d.com": true, "u@d.com/code": true, "u@d.com/docs": true, "u@d.com/docs/a.md": false, } repoAt(fs, "u@d.com/code/proj.git")

tests := []struct {
	name      string
	clean     string
	wantRepo  string
	wantRest  string
	wantFound bool
}{
	{"repo root no slash", "/code/proj.git", "u@d.com/code/proj.git", "", true},
	{"repo root slash", "/code/proj.git/", "u@d.com/code/proj.git", "", true},
	{"inside repo", "/code/proj.git/log/main", "u@d.com/code/proj.git", "log/main", true},
	{"blob inside repo", "/code/proj.git/blob/main/src/x.go", "u@d.com/code/proj.git", "blob/main/src/x.go", true},
	{"plain dir", "/docs", "", "", false},
	{"plain file", "/docs/a.md", "", "", false},
	{"user root", "/", "", "", false},
}
for _, tt := range tests {
	t.Run(tt.name, func(t *testing.T) {
		repo, rest, found, err := Locate(fs, "u@d.com", tt.clean)
		if err != nil {
			t.Fatalf("Locate: %v", err)
		}
		if found != tt.wantFound || repo != tt.wantRepo || rest != tt.wantRest {
			t.Fatalf("got (%q, %q, %v), want (%q, %q, %v)",
				repo, rest, found, tt.wantRepo, tt.wantRest, tt.wantFound)
		}
	})
}

}