-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpurl.test.ts
More file actions
237 lines (202 loc) · 7.58 KB
/
purl.test.ts
File metadata and controls
237 lines (202 loc) · 7.58 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { parsePURL, fullName, buildPURL, createFromPURL } from '../../src/core/purl.ts'
import { InvalidPURLError } from '../../src/core/errors.ts'
describe('purl', () => {
describe('parsePURL', () => {
it('parses simple PURL', () => {
const result = parsePURL('pkg:npm/lodash')
expect(result).toEqual({
type: 'npm',
namespace: '',
name: 'lodash',
version: '',
qualifiers: {},
subpath: '',
})
})
it('parses scoped package', () => {
const result = parsePURL('pkg:npm/%40babel/core@7.0.0')
expect(result).toEqual({
type: 'npm',
namespace: '@babel',
name: 'core',
version: '7.0.0',
qualifiers: {},
subpath: '',
})
})
it('parses PURL with version', () => {
const result = parsePURL('pkg:cargo/serde@1.0.0')
expect(result).toEqual({
type: 'cargo',
namespace: '',
name: 'serde',
version: '1.0.0',
qualifiers: {},
subpath: '',
})
})
it('parses PURL with qualifiers', () => {
const result = parsePURL('pkg:npm/lodash?repository_url=https://custom.registry.com')
expect(result).toEqual({
type: 'npm',
namespace: '',
name: 'lodash',
version: '',
qualifiers: {
repository_url: 'https://custom.registry.com',
},
subpath: '',
})
})
it('parses PURL with multiple qualifiers', () => {
const result = parsePURL('pkg:npm/lodash?arch=x86_64&os=linux')
expect(result).toEqual({
type: 'npm',
namespace: '',
name: 'lodash',
version: '',
qualifiers: {
arch: 'x86_64',
os: 'linux',
},
subpath: '',
})
})
it('parses PURL with subpath', () => {
const result = parsePURL('pkg:npm/lodash#some/path')
expect(result).toEqual({
type: 'npm',
namespace: '',
name: 'lodash',
version: '',
qualifiers: {},
subpath: 'some/path',
})
})
it('parses PURL with all components', () => {
const result = parsePURL('pkg:npm/%40babel/core@7.0.0?arch=x86_64#lib/index.js')
expect(result).toEqual({
type: 'npm',
namespace: '@babel',
name: 'core',
version: '7.0.0',
qualifiers: {
arch: 'x86_64',
},
subpath: 'lib/index.js',
})
})
it('normalizes PyPI package names', () => {
const result = parsePURL('pkg:pypi/My_Package')
expect(result.name).toBe('my-package')
})
it('normalizes PyPI package names with version', () => {
const result = parsePURL('pkg:pypi/Django_REST_Framework@3.14.0')
expect(result.name).toBe('django-rest-framework')
expect(result.version).toBe('3.14.0')
})
it('throws on missing pkg: prefix', () => {
expect(() => parsePURL('npm/lodash')).toThrow(InvalidPURLError)
expect(() => parsePURL('npm/lodash')).toThrow('must start with "pkg:"')
})
it('throws on missing type/name separator', () => {
expect(() => parsePURL('pkg:lodash')).toThrow(InvalidPURLError)
expect(() => parsePURL('pkg:lodash')).toThrow('missing type/name separator')
})
it('throws on empty type', () => {
expect(() => parsePURL('pkg:/lodash')).toThrow(InvalidPURLError)
expect(() => parsePURL('pkg:/lodash')).toThrow('empty type')
})
it('throws on empty name', () => {
expect(() => parsePURL('pkg:npm/')).toThrow(InvalidPURLError)
expect(() => parsePURL('pkg:npm/')).toThrow('empty name')
})
it('decodes URL-encoded components', () => {
const result = parsePURL('pkg:npm/my%20package@1.0.0')
expect(result.name).toBe('my package')
expect(result.version).toBe('1.0.0')
})
it('lowercases type', () => {
const result = parsePURL('pkg:NPM/lodash')
expect(result.type).toBe('npm')
})
})
describe('fullName', () => {
it('returns name without namespace', () => {
const parsed = parsePURL('pkg:npm/lodash')
expect(fullName(parsed)).toBe('lodash')
})
it('returns scoped name with namespace', () => {
const parsed = parsePURL('pkg:npm/%40babel/core')
expect(fullName(parsed)).toBe('@babel/core')
})
it('handles custom namespace', () => {
const parsed = parsePURL('pkg:npm/%40myorg/mylib')
expect(fullName(parsed)).toBe('@myorg/mylib')
})
})
describe('buildPURL', () => {
it('builds simple PURL', () => {
expect(buildPURL({ type: 'npm', name: 'lodash' })).toBe('pkg:npm/lodash')
})
it('builds PURL with version', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', version: '4.17.21' })).toBe('pkg:npm/lodash@4.17.21')
})
it('builds PURL with namespace', () => {
expect(buildPURL({ type: 'npm', name: 'core', version: '7.0.0', namespace: '@babel' })).toBe('pkg:npm/%40babel/core@7.0.0')
})
it('encodes special characters in name', () => {
expect(buildPURL({ type: 'npm', name: 'my package' })).toBe('pkg:npm/my%20package')
})
it('encodes special characters in namespace', () => {
expect(buildPURL({ type: 'npm', name: 'core', namespace: '@my org' })).toBe('pkg:npm/%40my%20org/core')
})
it('encodes special characters in version', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', version: '1.0.0+build.123' })).toBe('pkg:npm/lodash@1.0.0%2Bbuild.123')
})
it('builds PURL with qualifiers', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', version: '4.17.21', qualifiers: { repository_url: 'https://custom.registry.com' } }))
.toBe('pkg:npm/lodash@4.17.21?repository_url=https%3A%2F%2Fcustom.registry.com')
})
it('builds PURL with multiple qualifiers', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', qualifiers: { arch: 'x86_64', os: 'linux' } }))
.toBe('pkg:npm/lodash?arch=x86_64&os=linux')
})
it('builds PURL with subpath', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', subpath: 'lib/index.js' }))
.toBe('pkg:npm/lodash#lib/index.js')
})
it('builds PURL with all components', () => {
expect(buildPURL({ type: 'npm', name: 'core', version: '7.0.0', namespace: '@babel', qualifiers: { arch: 'x86_64' }, subpath: 'lib/index.js' }))
.toBe('pkg:npm/%40babel/core@7.0.0?arch=x86_64#lib/index.js')
})
it('skips empty qualifiers object', () => {
expect(buildPURL({ type: 'npm', name: 'lodash', version: '1.0.0', qualifiers: {} }))
.toBe('pkg:npm/lodash@1.0.0')
})
it('round-trips a simple PURL through parsePURL', () => {
const original = 'pkg:npm/lodash'
expect(buildPURL(parsePURL(original))).toBe(original)
})
it('round-trips a versioned PURL', () => {
const original = 'pkg:cargo/serde@1.0.0'
expect(buildPURL(parsePURL(original))).toBe(original)
})
it('round-trips a scoped PURL', () => {
const original = 'pkg:npm/%40babel/core@7.0.0'
expect(buildPURL(parsePURL(original))).toBe(original)
})
it('round-trips a PURL with qualifiers', () => {
const original = 'pkg:npm/lodash?repository_url=https%3A%2F%2Fcustom.registry.com'
expect(buildPURL(parsePURL(original))).toBe(original)
})
it('round-trips a PURL with subpath', () => {
const original = 'pkg:npm/lodash#lib/index.js'
expect(buildPURL(parsePURL(original))).toBe(original)
})
it('round-trips a full PURL with all components', () => {
const original = 'pkg:npm/%40babel/core@7.0.0?arch=x86_64#lib/index.js'
expect(buildPURL(parsePURL(original))).toBe(original)
})
})
})