| 1 |
const t = require('tap') |
| 2 |
const { normalizePath } = require('./fixtures/utils.js') |
| 3 |
const consistentResolve = require('../lib/consistent-resolve.js') |
| 4 |
const cr = (...args) => { |
| 5 |
const r = consistentResolve(...args) |
| 6 |
return r && normalizePath(r) |
| 7 |
} |
| 8 |
|
| 9 |
t.test('file and directories resolved to toPath', t => { |
| 10 |
const tp = '/foo' |
| 11 |
const fp = '/foo/bar' |
| 12 |
t.equal(cr('/foo/bar/baz', fp, tp, true), 'file:bar/baz') |
| 13 |
t.equal(cr('/foo/bar/baz', fp, tp), 'file:/foo/bar/baz') |
| 14 |
|
| 15 |
t.equal(cr('./baz', fp, tp, true), 'file:bar/baz') |
| 16 |
t.equal(cr('./baz', fp, tp), 'file:/foo/bar/baz') |
| 17 |
|
| 18 |
t.equal(cr('/foo/bar/baz.tgz', fp, tp, true), 'file:bar/baz.tgz') |
| 19 |
t.equal(cr('/foo/bar/baz.tgz', fp, tp), 'file:/foo/bar/baz.tgz') |
| 20 |
|
| 21 |
t.equal(cr('baz.tgz', fp, tp, true), 'file:bar/baz.tgz') |
| 22 |
t.equal(cr('baz.tgz', fp, tp), 'file:/foo/bar/baz.tgz') |
| 23 |
|
| 24 |
t.equal(cr('file:/foo/bar/baz', fp, tp, true), 'file:bar/baz') |
| 25 |
t.equal(cr('file:/foo/bar/baz', fp, tp), 'file:/foo/bar/baz') |
| 26 |
|
| 27 |
t.equal(cr('file:baz', fp, tp, true), 'file:bar/baz') |
| 28 |
t.equal(cr('file:baz', fp, tp), 'file:/foo/bar/baz') |
| 29 |
|
| 30 |
t.equal(cr('file:/foo/bar/baz.tgz', fp, tp, true), 'file:bar/baz.tgz') |
| 31 |
t.equal(cr('file:/foo/bar/baz.tgz', fp, tp), 'file:/foo/bar/baz.tgz') |
| 32 |
|
| 33 |
t.equal(cr('file:baz.tgz', fp, tp, true), 'file:bar/baz.tgz') |
| 34 |
t.equal(cr('file:baz.tgz', fp, tp), 'file:/foo/bar/baz.tgz') |
| 35 |
t.end() |
| 36 |
}) |
| 37 |
|
| 38 |
t.test('file and directories made consistent if toPath not set', t => { |
| 39 |
const fp = '/foo/bar' |
| 40 |
for (const rel of [true, false]) { |
| 41 |
// relative doesn't matter if toPath not set |
| 42 |
t.test(`rel=${rel}`, t => { |
| 43 |
t.equal(cr('/foo/bar/baz', fp, null, rel), 'file:/foo/bar/baz') |
| 44 |
t.equal(cr('./baz', fp, null, rel), 'file:/foo/bar/baz') |
| 45 |
t.equal(cr('/foo/bar/baz.tgz', fp, null, rel), 'file:/foo/bar/baz.tgz') |
| 46 |
t.equal(cr('baz.tgz', fp, null, rel), 'file:/foo/bar/baz.tgz') |
| 47 |
t.equal(cr('file:/foo/bar/baz', fp, null, rel), 'file:/foo/bar/baz') |
| 48 |
t.equal(cr('file:baz', fp, null, rel), 'file:/foo/bar/baz') |
| 49 |
t.equal(cr('file:/foo/bar/baz.tgz', fp, null, rel), 'file:/foo/bar/baz.tgz') |
| 50 |
t.equal(cr('file:baz.tgz', fp, null, rel), 'file:/foo/bar/baz.tgz') |
| 51 |
t.end() |
| 52 |
}) |
| 53 |
} |
| 54 |
t.end() |
| 55 |
}) |
| 56 |
|
| 57 |
t.test('consistent hosted git info urls', t => { |
| 58 |
const expect = 'git+ssh://[email protected]/a/b.git' |
| 59 |
const expectAuth = 'git+https://user:[email protected]/a/b.git' |
| 60 |
t.equal(cr('a/b'), expect) |
| 61 |
t.equal(cr('github:a/b'), expect) |
| 62 |
t.equal(cr('git+https://github.com/a/b'), expect) |
| 63 |
t.equal(cr('git://github.com/a/b'), expect) |
| 64 |
t.equal(cr('git+ssh://[email protected]/a/b'), expect) |
| 65 |
t.equal(cr('git+https://github.com/a/b.git'), expect) |
| 66 |
t.equal(cr('git://github.com/a/b.git'), expect) |
| 67 |
t.equal(cr('git+ssh://[email protected]/a/b.git'), expect) |
| 68 |
t.equal(cr('git+https://user:[email protected]/a/b.git'), expectAuth) |
| 69 |
|
| 70 |
const hash = '#0000000000000000000000000000000000000000' |
| 71 |
t.equal(cr('a/b' + hash), expect + hash) |
| 72 |
t.equal(cr('github:a/b' + hash), expect + hash) |
| 73 |
t.equal(cr('git+https://github.com/a/b' + hash), expect + hash) |
| 74 |
t.equal(cr('git://github.com/a/b' + hash), expect + hash) |
| 75 |
t.equal(cr('git+ssh://[email protected]/a/b' + hash), expect + hash) |
| 76 |
t.equal(cr('git+https://github.com/a/b.git' + hash), expect + hash) |
| 77 |
t.equal(cr('git://github.com/a/b.git' + hash), expect + hash) |
| 78 |
t.equal(cr('git+ssh://[email protected]/a/b.git' + hash), expect + hash) |
| 79 |
t.equal(cr('xyz@a/b' + hash), expect + hash) |
| 80 |
t.equal(cr('xyz@github:a/b' + hash), expect + hash) |
| 81 |
t.equal(cr('xyz@git+https://github.com/a/b' + hash), expect + hash) |
| 82 |
t.equal(cr('xyz@git://github.com/a/b' + hash), expect + hash) |
| 83 |
t.equal(cr('xyz@git+ssh://[email protected]/a/b' + hash), expect + hash) |
| 84 |
t.equal(cr('xyz@git+https://github.com/a/b.git' + hash), expect + hash) |
| 85 |
t.equal(cr('xyz@git://github.com/a/b.git' + hash), expect + hash) |
| 86 |
t.equal(cr('xyz@git+ssh://[email protected]/a/b.git' + hash), expect + hash) |
| 87 |
t.end() |
| 88 |
}) |
| 89 |
|
| 90 |
t.test('unhosted git returns saveSpec', t => { |
| 91 |
const r = 'git+https://x.com/y.git#0000000000000000000000000000000000000000' |
| 92 |
t.equal(cr(r), r) |
| 93 |
t.equal(cr('xyz@' + r), r) |
| 94 |
t.end() |
| 95 |
}) |
| 96 |
|
| 97 |
t.test('remotes returned as-is', t => { |
| 98 |
const r = 'http://x.com/y.tgz' |
| 99 |
t.equal(cr(r), r) |
| 100 |
t.equal(cr('xyz@' + r), r) |
| 101 |
t.end() |
| 102 |
}) |
| 103 |
|
| 104 |
t.test('falsey resolved returns null', t => { |
| 105 |
t.equal(cr(null), null) |
| 106 |
t.equal(cr(0), null) |
| 107 |
t.equal(cr(false), null) |
| 108 |
t.equal(cr(undefined), null) |
| 109 |
t.end() |
| 110 |
}) |
| 111 |
|
| 112 |
t.test('tag returns tag', t => { |
| 113 |
t.equal(cr('foo@latest'), 'latest') |
| 114 |
t.end() |
| 115 |
}) |
| 116 |
|
| 117 |
t.test('package name returns package name', t => { |
| 118 |
t.equal(cr('foo'), 'foo') |
| 119 |
t.end() |
| 120 |
}) |
| 121 |
|
| 122 |
t.test('invalid resolved returns as-is', t => { |
| 123 |
t.equal(cr('not ! a : v@lid t*A*g'), 'not ! a : v@lid t*A*g') |
| 124 |
t.end() |
| 125 |
}) |