| 1 |
const t = require('tap') |
| 2 |
const depValid = require('../lib/dep-valid.js') |
| 3 |
const npa = require('npm-package-arg') |
| 4 |
const { normalizePaths } = require('./fixtures/utils.js') |
| 5 |
const { resolve } = require('node:path') |
| 6 |
|
| 7 |
// dep-valid reads from requestor.edgesOut so we use this instead of {} in these tests |
| 8 |
const emptyRequestor = { |
| 9 |
edgesOut: new Map(), |
| 10 |
} |
| 11 |
|
| 12 |
t.test('basic', t => { |
| 13 |
t.ok(depValid({}, '', null, emptyRequestor), '* is always ok') |
| 14 |
|
| 15 |
t.ok(depValid({ |
| 16 |
package: { |
| 17 |
version: '1.2.3', |
| 18 |
}, |
| 19 |
get version () { |
| 20 |
return this.package.version |
| 21 |
}, |
| 22 |
}, '1.x', null, emptyRequestor), 'range that is satisfied') |
| 23 |
|
| 24 |
t.ok(depValid({ |
| 25 |
package: { |
| 26 |
version: '2.2.3', |
| 27 |
}, |
| 28 |
get version () { |
| 29 |
return this.package.version |
| 30 |
}, |
| 31 |
}, '1.x', '2.x', emptyRequestor), 'range that is acceptable') |
| 32 |
|
| 33 |
t.ok(depValid({ |
| 34 |
isLink: true, |
| 35 |
realpath: '/some/path', |
| 36 |
}, normalizePaths(npa('file:/some/path')), null, emptyRequestor), 'links must point at intended target') |
| 37 |
|
| 38 |
t.notOk(depValid({ |
| 39 |
isLink: true, |
| 40 |
realpath: '/some/other/path', |
| 41 |
}, 'file:/some/path', null, emptyRequestor), 'links must point at intended target') |
| 42 |
|
| 43 |
t.notOk(depValid({ |
| 44 |
realpath: '/some/path', |
| 45 |
}, 'file:/some/path', null, emptyRequestor), 'file:// must be a link') |
| 46 |
|
| 47 |
t.ok(depValid({ |
| 48 |
name: 'foo', |
| 49 |
resolved: 'git://host/repo#somebranch', |
| 50 |
package: { |
| 51 |
version: '1.2.3', |
| 52 |
}, |
| 53 |
get version () { |
| 54 |
return this.package.version |
| 55 |
}, |
| 56 |
}, 'git://host/repo#semver:1.x', null, emptyRequestor), 'git url with semver range') |
| 57 |
|
| 58 |
t.ok(depValid({ |
| 59 |
name: 'foo', |
| 60 |
package: { |
| 61 |
name: 'bar', |
| 62 |
version: '1.2.3', |
| 63 |
}, |
| 64 |
get version () { |
| 65 |
return this.package.version |
| 66 |
}, |
| 67 |
}, 'npm:[email protected]', null, emptyRequestor), 'alias is ok') |
| 68 |
|
| 69 |
t.ok(depValid({ |
| 70 |
resolved: 'https://registry/abbrev-1.1.1.tgz', |
| 71 |
package: {}, |
| 72 |
get version () { |
| 73 |
return this.package.version |
| 74 |
}, |
| 75 |
}, 'https://registry/abbrev-1.1.1.tgz', null, emptyRequestor), 'remote url match') |
| 76 |
|
| 77 |
t.ok(depValid({ |
| 78 |
resolved: 'git+ssh://[email protected]/foo/bar', |
| 79 |
package: {}, |
| 80 |
get version () { |
| 81 |
return this.package.version |
| 82 |
}, |
| 83 |
}, 'git+ssh://[email protected]/foo/bar.git', null, emptyRequestor), 'matching _from saveSpec') |
| 84 |
|
| 85 |
t.notOk(depValid({ |
| 86 |
resolved: 'git+ssh://[email protected]/foo/bar', |
| 87 |
package: {}, |
| 88 |
get version () { |
| 89 |
return this.package.version |
| 90 |
}, |
| 91 |
}, 'git+ssh://[email protected]/bar/foo.git', null, emptyRequestor), 'different repo') |
| 92 |
|
| 93 |
t.notOk(depValid({ |
| 94 |
package: {}, |
| 95 |
get version () { |
| 96 |
return this.package.version |
| 97 |
}, |
| 98 |
}, 'git+ssh://[email protected]/bar/foo.git', null, emptyRequestor), 'missing repo') |
| 99 |
|
| 100 |
t.ok(depValid({ |
| 101 |
resolved: `file:${resolve('/path/to/tarball.tgz')}`, |
| 102 |
}, resolve('/path/to/tarball.tgz'), null, emptyRequestor), 'same tarball') |
| 103 |
|
| 104 |
t.notOk(depValid({ |
| 105 |
resolved: 'file:/path/to/other/tarball.tgz', |
| 106 |
}, '/path/to/tarball.tgz', null, emptyRequestor), 'different tarball') |
| 107 |
|
| 108 |
t.notOk(depValid({ |
| 109 |
isLink: true, |
| 110 |
}, '/path/to/tarball.tgz', null, emptyRequestor), 'links are not tarballs') |
| 111 |
|
| 112 |
t.ok(depValid({ |
| 113 |
package: { |
| 114 |
_requested: { |
| 115 |
saveSpec: 'file:tarball.tgz', |
| 116 |
}, |
| 117 |
}, |
| 118 |
get version () { |
| 119 |
return this.package.version |
| 120 |
}, |
| 121 |
}, './tarball.tgz', null, emptyRequestor), 'probably the same-ish, hopefully') |
| 122 |
|
| 123 |
t.notOk(depValid({ |
| 124 |
package: {}, |
| 125 |
get version () { |
| 126 |
return this.package.version |
| 127 |
}, |
| 128 |
}, './tarball.tgz', null, emptyRequestor), 'too uncertain, nope') |
| 129 |
|
| 130 |
t.ok(depValid({ |
| 131 |
resolved: 'https://registry.npmjs.org/foo/foo-1.2.3.tgz', |
| 132 |
}, 'latest', null, emptyRequestor), 'tagged registry version needs remote tarball') |
| 133 |
|
| 134 |
t.notOk(depValid({ |
| 135 |
resolved: 'git+https://registry.npmjs.org/foo/foo-1.2.3.git', |
| 136 |
}, 'latest', null, emptyRequestor), 'tagged registry version needs remote tarball, not git') |
| 137 |
|
| 138 |
t.notOk(depValid({}, 'latest', null, emptyRequestor), |
| 139 |
'tagged registry version needs remote tarball resolution') |
| 140 |
t.end() |
| 141 |
}) |
| 142 |
|
| 143 |
t.test('unsupported dependency type', t => { |
| 144 |
const requestor = { errors: [], edgesOut: new Map() } |
| 145 |
const child = { name: 'kid' } |
| 146 |
const request = { type: 'not a type' } |
| 147 |
t.notOk(depValid(child, request, null, requestor)) |
| 148 |
t.match(requestor, { |
| 149 |
errors: [{ |
| 150 |
message: 'Unsupported dependency type', |
| 151 |
dependency: 'kid', |
| 152 |
requested: { type: 'not a type' }, |
| 153 |
}], |
| 154 |
}, 'parent got an error for their unsupported request') |
| 155 |
t.end() |
| 156 |
}) |
| 157 |
|
| 158 |
t.test('invalid tag name', t => { |
| 159 |
const requestor = { errors: [], edgesOut: new Map() } |
| 160 |
const child = { name: 'kid' } |
| 161 |
const request = '!!@#$%!#@$!' |
| 162 |
t.notOk(depValid(child, request, null, requestor)) |
| 163 |
t.match(requestor, { |
| 164 |
errors: [{ |
| 165 |
message: 'Invalid tag name "!!@#$%!#@$!"', |
| 166 |
dependency: 'kid', |
| 167 |
requested: '!!@#$%!#@$!', |
| 168 |
}], |
| 169 |
}, 'parent got an error for their invalid request') |
| 170 |
t.end() |
| 171 |
}) |
| 172 |
|
| 173 |
t.test('invalid request all together', t => { |
| 174 |
const requestor = { errors: [], edgesOut: new Map() } |
| 175 |
const child = { name: 'kid' } |
| 176 |
const request = null |
| 177 |
t.notOk(depValid(child, request, null, requestor)) |
| 178 |
t.match(requestor, { |
| 179 |
errors: [{ |
| 180 |
message: 'Invalid dependency specifier', |
| 181 |
requested: null, |
| 182 |
dependency: 'kid', |
| 183 |
}], |
| 184 |
}, 'parent got an error for their invalid request') |
| 185 |
t.end() |
| 186 |
}) |
| 187 |
|
| 188 |
t.test('installLinks makes Link nodes invalid', t => { |
| 189 |
const requestor = { errors: [], installLinks: true, edgesOut: new Map() } |
| 190 |
const child = { isLink: true, isWorkspace: false, name: 'kid' } |
| 191 |
const request = { type: 'directory' } |
| 192 |
t.notOk(depValid(child, request, null, requestor)) |
| 193 |
t.end() |
| 194 |
}) |
| 195 |
|
| 196 |
t.test('installLinks does not make workspace nodes invalid', t => { |
| 197 |
const requestor = { errors: [], installLinks: true, edgesOut: new Map() } |
| 198 |
const child = { |
| 199 |
isLink: true, |
| 200 |
isWorkspace: true, |
| 201 |
name: 'kid', |
| 202 |
realpath: '/some/path', |
| 203 |
} |
| 204 |
const request = normalizePaths(npa('file:/some/path')) |
| 205 |
t.ok(depValid(child, request, null, requestor)) |
| 206 |
t.end() |
| 207 |
}) |
| 208 |
|
| 209 |
t.test('sha-1 and sha-256', t => { |
| 210 |
t.ok(depValid({ |
| 211 |
name: 'foo', |
| 212 |
resolved: 'npm/repo#0d7bd85a85fa2571fa532d2fc842ed099b236ad2', |
| 213 |
package: { |
| 214 |
version: '1.2.3', |
| 215 |
}, |
| 216 |
get version () { |
| 217 |
return this.package.version |
| 218 |
}, |
| 219 |
}, 'npm/repo#0d7bd85a85fa2571fa532d2fc842ed099b236ad2', null, emptyRequestor), 'git url with full sha-1 hash match') |
| 220 |
|
| 221 |
t.notOk(depValid({ |
| 222 |
name: 'foo', |
| 223 |
resolved: 'npm/repo#0d7bd85a85fa2571fa532d2fc842ed099b236ad2', |
| 224 |
package: { |
| 225 |
version: '1.2.3', |
| 226 |
}, |
| 227 |
get version () { |
| 228 |
return this.package.version |
| 229 |
}, |
| 230 |
}, 'npm/repo#1d7bd85a85fa2571fa532d2fc842ed099b236ad2', null, emptyRequestor), 'git url with full sha-1 hash mismatch') |
| 231 |
|
| 232 |
t.ok(depValid({ |
| 233 |
name: 'foo', |
| 234 |
resolved: 'npm/repo#8e3a9b3579ab330238c06b761e7f1b5dc5b4ac6e5a96da4dd2fb3b7411009df8', |
| 235 |
package: { |
| 236 |
version: '1.2.3', |
| 237 |
}, |
| 238 |
get version () { |
| 239 |
return this.package.version |
| 240 |
}, |
| 241 |
}, 'npm/repo#8e3a9b3579ab330238c06b761e7f1b5dc5b4ac6e5a96da4dd2fb3b7411009df8', null, emptyRequestor), 'git url with full sha-256 hash match') |
| 242 |
|
| 243 |
t.notOk(depValid({ |
| 244 |
name: 'foo', |
| 245 |
resolved: 'npm/repo#8e3a9b3579ab330238c06b761e7f1b5dc5b4ac6e5a96da4dd2fb3b7411009df8', |
| 246 |
package: { |
| 247 |
version: '1.2.3', |
| 248 |
}, |
| 249 |
get version () { |
| 250 |
return this.package.version |
| 251 |
}, |
| 252 |
}, 'npm/repo#9e3a9b3579ab330238c06b761e7f1b5dc5b4ac6e5a96da4dd2fb3b7411009df8', null, emptyRequestor), 'git url with full sha-256 hash mismatch') |
| 253 |
|
| 254 |
t.end() |
| 255 |
}) |