-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_test.go
More file actions
154 lines (141 loc) · 3.6 KB
/
Copy pathtypes_test.go
File metadata and controls
154 lines (141 loc) · 3.6 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
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package purl
import (
"testing"
)
func TestTypeInfo(t *testing.T) {
tests := []struct {
purlType string
wantDescription string
wantRegistry string
wantNil bool
}{
{"npm", "PURL type for npm packages.", "https://registry.npmjs.org", false},
{"pypi", "Python packages", "https://pypi.org", false},
{"cargo", "Cargo packages for Rust", "https://crates.io", false},
{"unknown-type", "", "", true},
}
for _, tt := range tests {
t.Run(tt.purlType, func(t *testing.T) {
cfg := TypeInfo(tt.purlType)
switch {
case tt.wantNil:
if cfg != nil {
t.Errorf("TypeInfo(%q) = %v, want nil", tt.purlType, cfg)
}
case cfg == nil:
t.Errorf("TypeInfo(%q) = nil, want non-nil", tt.purlType)
default:
if cfg.Description != tt.wantDescription {
t.Errorf("Description = %q, want %q", cfg.Description, tt.wantDescription)
}
if cfg.DefaultRegistry == nil {
if tt.wantRegistry != "" {
t.Errorf("DefaultRegistry = nil, want %q", tt.wantRegistry)
}
} else if *cfg.DefaultRegistry != tt.wantRegistry {
t.Errorf("DefaultRegistry = %q, want %q", *cfg.DefaultRegistry, tt.wantRegistry)
}
}
})
}
}
func TestKnownTypes(t *testing.T) {
types := KnownTypes()
if len(types) == 0 {
t.Fatal("KnownTypes() returned empty slice")
}
// Check that some expected types are present
expected := []string{"npm", "pypi", "cargo", "gem", "maven", "golang"}
for _, e := range expected {
found := false
for _, typ := range types {
if typ == e {
found = true
break
}
}
if !found {
t.Errorf("KnownTypes() missing %q", e)
}
}
// Verify sorted
for i := 1; i < len(types); i++ {
if types[i-1] > types[i] {
t.Errorf("KnownTypes() not sorted: %q > %q", types[i-1], types[i])
break
}
}
}
func TestIsKnownType(t *testing.T) {
tests := []struct {
purlType string
want bool
}{
{"npm", true},
{"pypi", true},
{"cargo", true},
{"unknown-type", false},
{"", false},
}
for _, tt := range tests {
t.Run(tt.purlType, func(t *testing.T) {
if got := IsKnownType(tt.purlType); got != tt.want {
t.Errorf("IsKnownType(%q) = %v, want %v", tt.purlType, got, tt.want)
}
})
}
}
func TestDefaultRegistry(t *testing.T) {
tests := []struct {
purlType string
want string
}{
{"npm", "https://registry.npmjs.org"},
{"pypi", "https://pypi.org"},
{"cargo", "https://crates.io"},
{"apk", ""}, // no default registry
{"deb", ""}, // no default registry
{"unknown", ""},
}
for _, tt := range tests {
t.Run(tt.purlType, func(t *testing.T) {
if got := DefaultRegistry(tt.purlType); got != tt.want {
t.Errorf("DefaultRegistry(%q) = %q, want %q", tt.purlType, got, tt.want)
}
})
}
}
func TestNamespaceRequirement(t *testing.T) {
tests := []struct {
purlType string
wantRequired bool
wantProhibited bool
}{
{"maven", true, false},
{"composer", true, false},
{"gem", false, true},
{"cran", false, true},
{"npm", false, false}, // optional
{"cargo", false, false}, // no requirement
}
for _, tt := range tests {
t.Run(tt.purlType, func(t *testing.T) {
cfg := TypeInfo(tt.purlType)
if cfg == nil {
t.Fatalf("TypeInfo(%q) = nil", tt.purlType)
}
if got := cfg.NamespaceRequired(); got != tt.wantRequired {
t.Errorf("NamespaceRequired() = %v, want %v", got, tt.wantRequired)
}
if got := cfg.NamespaceProhibited(); got != tt.wantProhibited {
t.Errorf("NamespaceProhibited() = %v, want %v", got, tt.wantProhibited)
}
})
}
}
func TestTypesVersion(t *testing.T) {
v := TypesVersion()
if v == "" {
t.Error("TypesVersion() returned empty string")
}
}