GameOver1337 / GitClone Public
К репозиторию
3.5 KB
internal/services/build_test.go
3.5 KB
1package services
2
3import (
4 "os"
5 "path/filepath"
6 "testing"
7 "time"
8)
9
10func TestDetectBuildType(t *testing.T) {
11 tests := []struct {
12 file string
13 want string
14 }{
15 {"go.mod", "go"},
16 {"package.json", "npm"},
17 {"Dockerfile", "docker"},
18 {"Makefile", "make"},
19 {"requirements.txt", "python"},
20 {"Cargo.toml", "rust"},
21 {"pom.xml", "maven"},
22 }
23
24 for _, tt := range tests {
25 dir := t.TempDir()
26 if err := os.WriteFile(filepath.Join(dir, tt.file), []byte("x"), 0644); err != nil {
27 t.Fatal(err)
28 }
29 if got := DetectBuildType(dir); got != tt.want {
30 t.Errorf("DetectBuildType with %s = %q, want %q", tt.file, got, tt.want)
31 }
32 }
33
34 if got := DetectBuildType(t.TempDir()); got != "" {
35 t.Errorf("DetectBuildType on empty dir = %q, want empty", got)
36 }
37}
38
39func TestGitPathInfo(t *testing.T) {
40 tests := []struct {
41 urlPath, repo, want string
42 }{
43 {"/user/repo.git/info/refs", "user/repo", "/user/repo.git/info/refs"},
44 {"/user/repo/info/refs", "user/repo", "/user/repo.git/info/refs"},
45 {"/user/repo.git/git-upload-pack", "user/repo", "/user/repo.git/git-upload-pack"},
46 {"/user/repo/git-upload-pack", "user/repo", "/user/repo.git/git-upload-pack"},
47 }
48 for _, tt := range tests {
49 if got := gitPathInfo(tt.urlPath, tt.repo); got != tt.want {
50 t.Errorf("gitPathInfo(%q, %q) = %q, want %q", tt.urlPath, tt.repo, got, tt.want)
51 }
52 }
53}
54
55func TestSplitName(t *testing.T) {
56 owner, repo, err := splitName("user/repo")
57 if err != nil || owner != "user" || repo != "repo" {
58 t.Errorf("splitName(\"user/repo\") = %q, %q, %v", owner, repo, err)
59 }
60 for _, bad := range []string{"user", "user/repo/extra", "/repo", "user/", "..", "a/../b", `a\b`} {
61 if _, _, err := splitName(bad); err == nil {
62 t.Errorf("splitName(%q): expected error", bad)
63 }
64 }
65}
66
67func TestRepoPath(t *testing.T) {
68 s := NewGitService(filepath.Join(string(filepath.Separator), "srv", "repos"))
69 want := filepath.Join(string(filepath.Separator), "srv", "repos", "user", "repo.git")
70 if got := s.RepoPath("user/repo"); got != want {
71 t.Errorf("RepoPath = %q, want %q", got, want)
72 }
73}
74
75func TestSortTreeEntries(t *testing.T) {
76 s := NewGitService(t.TempDir())
77 name := "sort-demo"
78 if err := s.CreateRepo(name); err != nil {
79 t.Fatal(err)
80 }
81
82 work := filepath.Join(t.TempDir(), "work")
83 steps := [][2]string{
84 {"zeta.txt", "z"},
85 {"beta/inner.txt", "i"},
86 {"alpha.txt", "a"},
87 {"Gamma/inner.txt", "g"},
88 }
89 for i, st := range steps {
90 p := filepath.Join(work, filepath.FromSlash(st[0]))
91 if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
92 t.Fatal(err)
93 }
94 if err := os.WriteFile(p, []byte(st[1]), 0644); err != nil {
95 t.Fatal(err)
96 }
97 msg := "c" + string(rune('0'+i))
98 if out, err := runGit(work, time.Minute, "init", "-b", "main"); err != nil {
99 t.Fatalf("init: %s", out)
100 }
101 runGit(work, time.Minute, "config", "user.email", "t@t")
102 runGit(work, time.Minute, "config", "user.name", "t")
103 if out, err := runGit(work, time.Minute, "add", "-A"); err != nil {
104 t.Fatalf("add: %s", out)
105 }
106 if out, err := runGit(work, time.Minute, "commit", "-m", msg); err != nil {
107 t.Fatalf("commit: %s", out)
108 }
109 }
110 if out, err := runGit(work, time.Minute, "push", s.RepoPath(name), "main"); err != nil {
111 t.Fatalf("push: %s", out)
112 }
113
114 entries, err := s.ListTree(name, "main", "")
115 if err != nil {
116 t.Fatal(err)
117 }
118 want := []string{"Gamma", "beta", "alpha.txt", "zeta.txt"}
119 if len(entries) != len(want) {
120 t.Fatalf("got %d entries, want %d: %+v", len(entries), len(want), entries)
121 }
122 for i, w := range want {
123 if entries[i].Name != w {
124 t.Errorf("entry %d = %q, want %q", i, entries[i].Name, w)
125 }
126 }
127}