git@cosmocr.at/gitweb.git @ main · summary refs log
main / internal / gitrepo / locate_test.go /
internal/gitrepo/locate_test.go · 2.0 KiB · raw
view: Source ☐ force
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
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)
			}
		})
	}
}