Repository navigation
Expand file tree
/
Copy pathmetadata_test.go
More file actions
193 lines (180 loc) · 6.05 KB
/
Copy pathmetadata_test.go
File metadata and controls
193 lines (180 loc) · 6.05 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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package manifests
import (
"encoding/json"
"os"
"reflect"
"testing"
)
func stringPointer(value string) *string { return &value }
func parseMetadataFixture(t *testing.T, path, filename string) *ParseResult {
t.Helper()
content, err := os.ReadFile(path)
if err != nil {
t.Fatal(err)
}
result, err := Parse(filename, content)
if err != nil {
t.Fatal(err)
}
return result
}
// assertMetadata prints both sides as JSON so nil and empty values show apart.
func assertMetadata(t *testing.T, got, want Metadata) {
t.Helper()
if !reflect.DeepEqual(got, want) {
gotJSON, _ := json.MarshalIndent(got, "", " ")
wantJSON, _ := json.MarshalIndent(want, "", " ")
t.Errorf("metadata =\n%s\nwant\n%s", gotJSON, wantJSON)
}
}
func TestPackageJSONMetadata(t *testing.T) {
result := parseMetadataFixture(t, "testdata/npm/metadata/package.json", "package.json")
if result.Name != "example" || result.Version != "1.0.0" {
t.Errorf("identity = %s %s", result.Name, result.Version)
}
if !reflect.DeepEqual(result.Licenses, []string{"MIT"}) {
t.Errorf("licenses = %v", result.Licenses)
}
if !reflect.DeepEqual(result.Scripts, map[string][]string{"test": {"node test.js"}}) {
t.Errorf("scripts = %v", result.Scripts)
}
if len(result.Dependencies) != 1 || result.Dependencies[0].Name != "lodash" {
t.Errorf("dependencies = %+v", result.Dependencies)
}
assertMetadata(t, result.Metadata, Metadata{
Description: stringPointer("A currency conversion library"),
Keywords: []string{"currency", "conversion", "汇率"},
Homepage: stringPointer("https://example.test"),
Links: []Link{
{Label: "repository", URL: "git+https://github.com/example/example.git"},
{Label: "bugs", URL: "https://github.com/example/example/issues"},
{Label: "funding", URL: "https://github.com/sponsors/example"},
{Label: "funding", URL: "https://opencollective.com/example"},
},
})
}
func TestPackageJSONMetadataAbsentAndEmpty(t *testing.T) {
tests := []struct {
name string
content string
want Metadata
}{
{
name: "absent",
content: `{"name": "example"}`,
want: Metadata{},
},
{
name: "explicitly empty",
content: `{"description": "", "keywords": [], "homepage": ""}`,
want: Metadata{Description: stringPointer(""), Keywords: []string{}, Homepage: stringPointer("")},
},
{
name: "null is absent",
content: `{"description": null, "keywords": null, "homepage": null}`,
want: Metadata{},
},
{
name: "unexpected types are ignored",
content: `{"description": 1, "keywords": "a b", "homepage": {}, "repository": 2, "bugs": [], "dependencies": {"a": "1.0.0"}}`,
want: Metadata{},
},
{
name: "string repository and bugs",
content: `{"repository": "github:example/example", "bugs": "https://example.test/issues", "keywords": ["a", 1, "b"]}`,
want: Metadata{
Keywords: []string{"a", "b"},
Links: []Link{
{Label: "repository", URL: "github:example/example"},
{Label: "bugs", URL: "https://example.test/issues"},
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := Parse("package.json", []byte(test.content))
if err != nil {
t.Fatal(err)
}
assertMetadata(t, result.Metadata, test.want)
})
}
}
func TestNuspecMetadata(t *testing.T) {
result := parseMetadataFixture(t, "testdata/nuget/metadata.nuspec", "Example.Currency.nuspec")
if result.Name != "Example.Currency" || result.Version != "2.1.0" {
t.Errorf("identity = %s %s", result.Name, result.Version)
}
if !reflect.DeepEqual(result.Licenses, []string{"MIT"}) {
t.Errorf("licenses = %v", result.Licenses)
}
if len(result.Dependencies) != 1 || result.Dependencies[0].Name != "Newtonsoft.Json" {
t.Errorf("dependencies = %+v", result.Dependencies)
}
assertMetadata(t, result.Metadata, Metadata{
Summary: stringPointer("Currency conversion"),
Description: stringPointer("Converts between currencies.\nSupports ISO 4217 codes."),
Keywords: []string{"currency", "conversion", "finance"},
Homepage: stringPointer("https://example.test/currency"),
Readme: stringPointer("../docs/README.md"),
Links: []Link{
{Label: "repository", URL: "https://github.com/example/currency.git"},
{Label: "iconUrl", URL: "https://example.test/icon.png"},
},
})
}
func TestNuspecMetadataAbsentAndEmpty(t *testing.T) {
tests := []struct {
name string
content string
want Metadata
}{
{
name: "absent",
content: `<package><metadata><id>Example</id><version>1.0.0</version></metadata></package>`,
want: Metadata{},
},
{
name: "explicitly empty",
content: `<package><metadata><id>Example</id><version>1.0.0</version>` +
`<summary></summary><description/><tags> </tags><projectUrl></projectUrl><readme></readme>` +
`</metadata></package>`,
want: Metadata{
Summary: stringPointer(""),
Description: stringPointer(""),
Keywords: []string{},
Homepage: stringPointer(""),
Readme: stringPointer(""),
},
},
{
name: "legacy license URL",
content: `<package><metadata><id>Example</id><version>1.0.0</version>` +
`<licenseUrl>https://example.test/LICENSE</licenseUrl></metadata></package>`,
want: Metadata{Links: []Link{{Label: "licenseUrl", URL: "https://example.test/LICENSE"}}},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
result, err := Parse("Example.nuspec", []byte(test.content))
if err != nil {
t.Fatal(err)
}
assertMetadata(t, result.Metadata, test.want)
})
}
}
func TestMetadataReadmeIsNotRead(t *testing.T) {
content := []byte(`<package><metadata><id>Example</id><version>1.0.0</version><readme>missing/README.md</readme></metadata></package>`)
result, err := Parse("Example.nuspec", content, Options{FSRoot: t.TempDir()})
if err != nil {
t.Fatal(err)
}
if result.Metadata.Readme == nil || *result.Metadata.Readme != "missing/README.md" {
t.Errorf("readme = %v", result.Metadata.Readme)
}
if result.Metadata.Description != nil {
t.Errorf("description = %q, want absent rather than README content", *result.Metadata.Description)
}
}