|
| 1 | +const t = require('tap') |
| 2 | +const BB = require('bluebird') |
| 3 | +const binLinks = BB.promisify(require('../')) |
| 4 | + |
| 5 | +// forking between cmd-shims and symlinks is already handled by |
| 6 | +// the gentle-fs.binLink module. just test the unix handling here. |
| 7 | +const _FAKE_PLATFORM_ = process.platform === 'win32' ? 'unix' : null |
| 8 | + |
| 9 | +const fs = require('fs') |
| 10 | +const mkdirp = require('mkdirp').sync |
| 11 | +const rimraf = require('rimraf').sync |
| 12 | +const {basename, resolve} = require('path') |
| 13 | +const me = resolve(__dirname, basename(__filename, '.js')) |
| 14 | +rimraf(me) |
| 15 | +mkdirp(me) |
| 16 | +const globalDir = resolve(me, 'node_modules') |
| 17 | +t.teardown(() => rimraf(me)) |
| 18 | + |
| 19 | +const log = { |
| 20 | + clearProgress () {}, |
| 21 | + info () {}, |
| 22 | + showProgress () {}, |
| 23 | + silly () {}, |
| 24 | + verbose () {} |
| 25 | +} |
| 26 | + |
| 27 | +// create some stuff that's already in the bin/man folders |
| 28 | +const globalBin = resolve(me, 'bin') |
| 29 | +mkdirp(globalBin) |
| 30 | +fs.writeFileSync(globalBin + '/foo', 'pre-existing foo bin') |
| 31 | +mkdirp(me + '/node_modules/bar/bin') |
| 32 | +fs.writeFileSync(me + '/node_modules/bar/bin/bar.js', 'original bar') |
| 33 | +fs.symlinkSync(me + '/node_modules/bar/bin/bar.js', me + '/bin/bar') |
| 34 | + |
| 35 | +const prefixes = [ |
| 36 | + me, |
| 37 | + globalBin, |
| 38 | + globalDir, |
| 39 | + resolve(me, 'share/man/man1') |
| 40 | +] |
| 41 | + |
| 42 | +mkdirp(me + '/share/man/man1') |
| 43 | +fs.writeFileSync(me + '/share/man/man1/foo.1', 'pre-existing foo man') |
| 44 | +mkdirp(me + '/node_modules/bar/man') |
| 45 | +fs.writeFileSync(me + '/node_modules/bar/man/bar.1', 'original bar man') |
| 46 | +fs.symlinkSync(me + '/node_modules/bar/man/bar.1', me + '/share/man/man1/bar.1') |
| 47 | + |
| 48 | +t.test('foo package cannot link, pre-existing stuff there', t => { |
| 49 | + const foo = resolve(me, 'node_modules/foo') |
| 50 | + mkdirp(foo) |
| 51 | + const pkg = { |
| 52 | + name: 'foo', |
| 53 | + version: '1.2.3', |
| 54 | + bin: 'foo.js', |
| 55 | + man: ['foo.1', { not: 'a manpage string' }] |
| 56 | + } |
| 57 | + fs.writeFileSync(foo + '/package.json', JSON.stringify(pkg)) |
| 58 | + fs.writeFileSync(foo + '/foo.1', 'how to foo\n') |
| 59 | + fs.writeFileSync(foo + '/foo.js', '#!/usr/bin/env node\nconsole.log("foo")') |
| 60 | + |
| 61 | + // might get it on the bin or the man, but one of these will EEXIST |
| 62 | + return t.rejects(binLinks(pkg, foo, true, { |
| 63 | + prefix: me, |
| 64 | + global: true, |
| 65 | + globalBin, |
| 66 | + globalDir, |
| 67 | + log, |
| 68 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 69 | + name: pkg.name, |
| 70 | + _FAKE_PLATFORM_, |
| 71 | + prefixes: prefixes.concat(foo) |
| 72 | + }), { code: 'EEXIST' }).then(() => { |
| 73 | + // ensure we still have our preexisting bits |
| 74 | + t.equal(fs.readFileSync(me + '/bin/foo', 'utf8'), 'pre-existing foo bin') |
| 75 | + t.equal(fs.readFileSync(me + '/share/man/man1/foo.1', 'utf8'), 'pre-existing foo man') |
| 76 | + }) |
| 77 | +}) |
| 78 | + |
| 79 | +t.test('foo package can link with --force link', t => { |
| 80 | + const foo = resolve(me, 'node_modules/foo') |
| 81 | + mkdirp(foo) |
| 82 | + const pkg = { |
| 83 | + name: 'foo', |
| 84 | + version: '1.2.3', |
| 85 | + bin: 'foo.js', |
| 86 | + man: ['foo.1'] |
| 87 | + } |
| 88 | + fs.writeFileSync(foo + '/package.json', JSON.stringify(pkg)) |
| 89 | + fs.writeFileSync(foo + '/foo.1', 'how to foo\n') |
| 90 | + fs.writeFileSync(foo + '/foo.js', '#!/usr/bin/env node\nconsole.log("foo")') |
| 91 | + |
| 92 | + return binLinks(pkg, foo, true, { |
| 93 | + prefix: me, |
| 94 | + global: true, |
| 95 | + globalBin, |
| 96 | + globalDir, |
| 97 | + log, |
| 98 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 99 | + name: pkg.name, |
| 100 | + _FAKE_PLATFORM_, |
| 101 | + force: true, |
| 102 | + prefixes: prefixes.concat(foo) |
| 103 | + }).then(() => { |
| 104 | + // ensure we got our links made |
| 105 | + t.equal(fs.readFileSync(me + '/bin/foo', 'utf8'), '#!/usr/bin/env node\nconsole.log("foo")') |
| 106 | + if (process.platform !== 'win32') { |
| 107 | + t.equal(fs.readFileSync(me + '/share/man/man1/foo.1', 'utf8'), 'how to foo\n') |
| 108 | + } |
| 109 | + }) |
| 110 | +}) |
| 111 | + |
| 112 | +t.test('bar package can update, links are ours', t => { |
| 113 | + const bar = resolve(me, 'node_modules/bar') |
| 114 | + mkdirp(bar) |
| 115 | + const pkg = { |
| 116 | + name: 'bar', |
| 117 | + version: '1.2.3', |
| 118 | + bin: 'bar.js', |
| 119 | + man: ['bar.1'] |
| 120 | + } |
| 121 | + fs.writeFileSync(bar + '/package.json', JSON.stringify(pkg)) |
| 122 | + fs.writeFileSync(bar + '/bar.1', 'how to bar\n') |
| 123 | + fs.writeFileSync(bar + '/bar.js', '#!/usr/bin/env node\nconsole.log("bar")') |
| 124 | + |
| 125 | + return binLinks(pkg, bar, true, { |
| 126 | + prefix: me, |
| 127 | + parseable: true, |
| 128 | + global: true, |
| 129 | + globalBin, |
| 130 | + globalDir, |
| 131 | + log, |
| 132 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 133 | + name: pkg.name, |
| 134 | + _FAKE_PLATFORM_, |
| 135 | + prefixes: prefixes.concat(bar, bar + '/man') |
| 136 | + }).then(() => { |
| 137 | + // ensure we got our links made |
| 138 | + t.equal(fs.readFileSync(me + '/bin/bar', 'utf8'), '#!/usr/bin/env node\nconsole.log("bar")') |
| 139 | + if (process.platform !== 'win32') { |
| 140 | + t.equal(fs.readFileSync(me + '/share/man/man1/bar.1', 'utf8'), 'how to bar\n') |
| 141 | + } |
| 142 | + }) |
| 143 | +}) |
| 144 | + |
| 145 | +t.test('cannot overwrite with another package with the same bin', t => { |
| 146 | + const baz = resolve(me, 'node_modules/@scope/baz') |
| 147 | + mkdirp(baz) |
| 148 | + const pkg = { |
| 149 | + name: '@scope/baz', |
| 150 | + version: '1.2.3', |
| 151 | + bin: { bar: 'baz.js' } |
| 152 | + } |
| 153 | + fs.writeFileSync(baz + '/package.json', JSON.stringify(pkg)) |
| 154 | + fs.writeFileSync(baz + '/baz.js', '#!/usr/bin/env node\nconsole.log("baz")') |
| 155 | + |
| 156 | + return t.rejects(binLinks(pkg, baz, true, { |
| 157 | + global: true, |
| 158 | + prefix: me, |
| 159 | + globalBin, |
| 160 | + globalDir, |
| 161 | + log, |
| 162 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 163 | + _FAKE_PLATFORM_, |
| 164 | + name: pkg.name, |
| 165 | + prefixes: prefixes.concat(baz) |
| 166 | + }), { code: 'EEXIST' }).then(() => { |
| 167 | + // ensure bar is still intact |
| 168 | + t.equal(fs.readFileSync(me + '/bin/bar', 'utf8'), '#!/usr/bin/env node\nconsole.log("bar")') |
| 169 | + }) |
| 170 | +}) |
| 171 | + |
| 172 | +t.test('nothing to link', t => { |
| 173 | + const qux = resolve(me, 'node_modules/qux') |
| 174 | + mkdirp(qux) |
| 175 | + const pkg = { |
| 176 | + name: 'qux', |
| 177 | + version: '1.2.3' |
| 178 | + } |
| 179 | + fs.writeFileSync(qux + '/package.json', JSON.stringify(pkg)) |
| 180 | + |
| 181 | + return binLinks(pkg, qux, true, { |
| 182 | + global: true, |
| 183 | + prefix: me, |
| 184 | + globalBin, |
| 185 | + globalDir, |
| 186 | + log, |
| 187 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 188 | + _FAKE_PLATFORM_, |
| 189 | + name: pkg.name, |
| 190 | + prefixes: prefixes.concat(qux) |
| 191 | + }) |
| 192 | +}) |
| 193 | + |
| 194 | +t.test('invalid man page name', t => { |
| 195 | + const badman = resolve(me, 'node_modules/badman') |
| 196 | + mkdirp(badman) |
| 197 | + const pkg = { |
| 198 | + name: 'badman', |
| 199 | + version: '1.2.3', |
| 200 | + man: ['manpage'] |
| 201 | + } |
| 202 | + fs.writeFileSync(badman + '/package.json', JSON.stringify(pkg)) |
| 203 | + fs.writeFileSync(badman + '/manpage', JSON.stringify(pkg)) |
| 204 | + |
| 205 | + return t.rejects(binLinks(pkg, badman, true, { |
| 206 | + global: true, |
| 207 | + prefix: me, |
| 208 | + globalBin, |
| 209 | + globalDir, |
| 210 | + log, |
| 211 | + pkgId: `${pkg.name}@${pkg.version}`, |
| 212 | + _FAKE_PLATFORM_, |
| 213 | + name: pkg.name, |
| 214 | + prefixes: prefixes.concat(badman) |
| 215 | + }), { message: 'manpage is not a valid name for a man file.' }) |
| 216 | +}) |
0 commit comments