-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodule_test.go
137 lines (131 loc) · 3.1 KB
/
module_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package k6foundry
import (
"errors"
"testing"
)
func TestParseModule(t *testing.T) {
t.Parallel()
testCases := []struct {
title string
dependency string
expectError error
expect Module
}{
{
title: "path with canonical version",
dependency: "github.com/path/[email protected]",
expect: Module{
Path: "github.com/path/module",
Version: "v0.1.0",
},
},
{
title: "path with incomplete version",
dependency: "github.com/path/[email protected]",
expect: Module{
Path: "github.com/path/module",
Version: "v0.1.0",
},
},
{
title: "path without version",
dependency: "github.com/path/module",
expect: Module{
Path: "github.com/path/module",
Version: "",
},
},
{
title: "path with latest version",
dependency: "github.com/path/module@latest",
expect: Module{
Path: "github.com/path/module",
Version: "latest",
},
},
{
title: "path with invalid version",
dependency: "github.com/path/module@1",
expectError: ErrInvalidDependencyFormat,
},
{
title: "path with invalid incomplete version",
dependency: "github.com/path/module@v",
expectError: ErrInvalidDependencyFormat,
},
{
title: "invalid path",
dependency: "github.com/@v1",
expectError: ErrInvalidDependencyFormat,
},
{
// this is considered valid according to go's module rules
title: "path with only domain",
dependency: "github.com@v1",
expect: Module{
Path: "github.com",
Version: "v1.0.0",
},
},
{
title: "versioned replace",
dependency: "github.com/path/module=github.com/another/[email protected]",
expect: Module{
Path: "github.com/path/module",
Version: "",
ReplacePath: "github.com/another/module",
ReplaceVersion: "v0.1.0",
},
},
{
title: "unversioned replace",
dependency: "github.com/path/module=github.com/another/module",
expect: Module{
Path: "github.com/path/module",
Version: "",
ReplacePath: "github.com/another/module",
ReplaceVersion: "",
},
},
{
title: "relative replace",
dependency: "github.com/path/module=./another/module",
expect: Module{
Path: "github.com/path/module",
Version: "",
ReplacePath: "./another/module",
ReplaceVersion: "",
},
},
{
title: "versioned relative replace",
dependency: "github.com/path/module=./another/[email protected]",
expectError: ErrInvalidDependencyFormat,
},
{
title: "only module name",
dependency: "module",
expect: Module{
Path: "module",
},
},
{
title: "empty module",
dependency: "",
expectError: ErrInvalidDependencyFormat,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.title, func(t *testing.T) {
t.Parallel()
module, err := ParseModule(tc.dependency)
if !errors.Is(err, tc.expectError) {
t.Fatalf("expected %v got %v", tc.expectError, err)
}
if tc.expectError == nil && tc.expect != module {
t.Fatalf("expected %v got %v", tc.expect, module)
}
})
}
}