forked from sourcegraph/src-cli
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgitutil_test.go
More file actions
83 lines (70 loc) · 1.77 KB
/
gitutil_test.go
File metadata and controls
83 lines (70 loc) · 1.77 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package codeintel
import (
"fmt"
"os"
"path/filepath"
"testing"
)
func TestInferRepo(t *testing.T) {
cur, err := os.Getwd()
if err != nil {
t.Fatal(err)
}
defer os.Chdir(cur)
tempDir := t.TempDir()
err = os.Chdir(tempDir)
if err != nil {
t.Fatal(err)
}
_, err = runGitCommand("init")
if err != nil {
t.Fatal(err)
}
want := "github.com/a/b"
_, err = runGitCommand("remote", "add", "origin", want)
if err != nil {
t.Fatal(err)
}
got, err := InferRepo()
if err != nil {
t.Fatalf("unexpected error inferring repo: %s", err)
}
if got != want {
t.Errorf("unexpected remote repo. want=%q have=%q", want, got)
}
}
func TestParseRemote(t *testing.T) {
testCases := map[string]string{
"git@github.com:sourcegraph/src-cli.git": "github.com/sourcegraph/src-cli",
"https://github.com/sourcegraph/src-cli": "github.com/sourcegraph/src-cli",
}
for input, expectedOutput := range testCases {
t.Run(fmt.Sprintf("input=%q", input), func(t *testing.T) {
output, err := parseRemote(input)
if err != nil {
t.Fatalf("unexpected error parsing remote: %s", err)
}
if output != expectedOutput {
t.Errorf("unexpected repo name. want=%q have=%q", expectedOutput, output)
}
})
}
}
func TestInferRoot(t *testing.T) {
testCases := map[string]string{
"gitutil.go": filepath.Join("internal", "codeintel"),
"../../cmd/src/lsif.go": filepath.Join("cmd", "src"),
"../../README.md": ".",
}
for input, expectedOutput := range testCases {
t.Run(fmt.Sprintf("input=%q", input), func(t *testing.T) {
root, err := InferRoot(input)
if err != nil {
t.Fatalf("unexpected error inferring root: %s", err)
}
if root != expectedOutput {
t.Errorf("unexpected remote root. want=%q have=%q", expectedOutput, root)
}
})
}
}