-
Notifications
You must be signed in to change notification settings - Fork 1
/
dep_test.go
104 lines (100 loc) · 2.18 KB
/
dep_test.go
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package templit_test
import (
"testing"
"github.com/euforic/templit"
"github.com/google/go-cmp/cmp"
)
// TestParseDepURL tests the ParseDepURL function.
func TestParseDepURL(t *testing.T) {
tests := []struct {
name string
rawURL string
expected *templit.DepInfo
wantErr bool
}{
{
name: "Basic URL with path and fragment",
rawURL: "https://github.com/owner/repo/path/to/file#[email protected]",
expected: &templit.DepInfo{
Host: "github.com",
Owner: "owner",
Repo: "repo",
Path: "path/to/file",
Block: "block",
Tag: "v1.2.3",
},
wantErr: false,
},
{
name: "URL without path",
rawURL: "https://github.com/owner/repo",
expected: &templit.DepInfo{
Host: "github.com",
Owner: "owner",
Repo: "repo",
Path: "",
Block: "",
Tag: "",
},
wantErr: false,
},
{
name: "URL with tag in path",
rawURL: "https://github.com/owner/repo/path/to/[email protected]",
expected: &templit.DepInfo{
Host: "github.com",
Owner: "owner",
Repo: "repo",
Path: "path/to/file",
Block: "",
Tag: "v1.2.3",
},
wantErr: false,
},
{
name: "URL with tag in repo",
rawURL: "https://github.com/owner/[email protected]",
expected: &templit.DepInfo{
Host: "github.com",
Owner: "owner",
Repo: "repo",
Path: "",
Block: "",
Tag: "v1.2.3",
},
wantErr: false,
},
{
name: "URL without protocol",
rawURL: "https://github.com/owner/repo/some/path#[email protected]",
expected: &templit.DepInfo{
Host: "github.com",
Owner: "owner",
Repo: "repo",
Path: "some/path",
Block: "test_block",
Tag: "v1.2.3",
},
wantErr: false,
},
{
name: "Invalid URL missing repo name",
rawURL: "https://github.com/owner",
expected: nil,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := templit.ParseDepURL(tt.rawURL)
if (err != nil) != tt.wantErr {
t.Fatalf("expected error %v, got %v", tt.wantErr, err)
}
if err == nil {
if diff := cmp.Diff(tt.expected, result); diff != "" {
t.Errorf("result mismatch (-want +got):\n%s", diff)
}
}
})
}
}