|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const BB = require('bluebird') |
| 4 | + |
| 5 | +const common = require('../common-tap.js') |
| 6 | +const fs = require('graceful-fs') |
| 7 | +const mockTar = require('../util/mock-tarball.js') |
| 8 | +const mr = common.fakeRegistry.compat |
| 9 | +const path = require('path') |
| 10 | +const rimraf = BB.promisify(require('rimraf')) |
| 11 | +const Tacks = require('tacks') |
| 12 | +const { test } = require('tap') |
| 13 | + |
| 14 | +const { Dir, File } = Tacks |
| 15 | +const readFileAsync = BB.promisify(fs.readFile) |
| 16 | + |
| 17 | +const testDir = path.join(__dirname, path.basename(__filename, '.js')) |
| 18 | + |
| 19 | +let server |
| 20 | +test('setup', t => { |
| 21 | + mr({}, (err, s) => { |
| 22 | + t.ifError(err, 'registry mocked successfully') |
| 23 | + server = s |
| 24 | + t.end() |
| 25 | + }) |
| 26 | +}) |
| 27 | + |
| 28 | +test('installs an npm: protocol alias package', t => { |
| 29 | + const fixture = new Tacks(Dir({ |
| 30 | + 'package.json': File({}) |
| 31 | + })) |
| 32 | + fixture.create(testDir) |
| 33 | + const packument = { |
| 34 | + name: 'foo', |
| 35 | + 'dist-tags': { latest: '1.2.3' }, |
| 36 | + versions: { |
| 37 | + '1.2.3': { |
| 38 | + name: 'foo', |
| 39 | + version: '1.2.3', |
| 40 | + dist: { |
| 41 | + tarball: `${server.registry}/foo/-/foo-1.2.3.tgz` |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + } |
| 46 | + server.get('/foo').reply(200, packument) |
| 47 | + return mockTar({ |
| 48 | + 'package.json': JSON.stringify({ |
| 49 | + name: 'foo', |
| 50 | + version: '1.2.3' |
| 51 | + }) |
| 52 | + }).then(tarball => { |
| 53 | + server.get('/foo/-/foo-1.2.3.tgz').reply(200, tarball) |
| 54 | + return common.npm([ |
| 55 | + 'install', 'bar@npm:foo', |
| 56 | + '--cache', path.join(testDir, 'npmcache'), |
| 57 | + '--loglevel', 'silly', |
| 58 | + '--registry', server.registry |
| 59 | + ], { cwd: testDir }) |
| 60 | + }).then(() => { |
| 61 | + return readFileAsync( |
| 62 | + path.join(testDir, 'node_modules', 'bar', 'package.json'), |
| 63 | + 'utf8' |
| 64 | + ) |
| 65 | + }).then(JSON.parse).then(pkg => { |
| 66 | + t.similar(pkg, { |
| 67 | + name: 'foo', |
| 68 | + version: '1.2.3' |
| 69 | + }, 'successfully installed foo as bar in node_modules') |
| 70 | + }).then(() => rimraf(testDir)) |
| 71 | +}) |
| 72 | + |
| 73 | +test('installs a tarball dep as a different name than package.json', t => { |
| 74 | + return mockTar({ |
| 75 | + 'package.json': JSON.stringify({ |
| 76 | + name: 'foo', |
| 77 | + version: '1.2.3' |
| 78 | + }) |
| 79 | + }).then(tarball => { |
| 80 | + const fixture = new Tacks(Dir({ |
| 81 | + 'package.json': File({}), |
| 82 | + 'foo.tgz': File(tarball) |
| 83 | + })) |
| 84 | + fixture.create(testDir) |
| 85 | + return common.npm([ |
| 86 | + 'install', 'bar@file:foo.tgz', |
| 87 | + '--cache', path.join(testDir, 'npmcache'), |
| 88 | + '--loglevel', 'silly', |
| 89 | + '--registry', server.registry |
| 90 | + ], { cwd: testDir }) |
| 91 | + }).then(() => { |
| 92 | + return readFileAsync( |
| 93 | + path.join(testDir, 'node_modules', 'bar', 'package.json'), |
| 94 | + 'utf8' |
| 95 | + ) |
| 96 | + }).then(JSON.parse).then(pkg => { |
| 97 | + t.similar(pkg, { |
| 98 | + name: 'foo', |
| 99 | + version: '1.2.3' |
| 100 | + }, 'successfully installed foo as bar in node_modules') |
| 101 | + }).then(() => rimraf(testDir)) |
| 102 | +}) |
| 103 | + |
| 104 | +test('installs a symlink dep as a different name than package.json', t => { |
| 105 | + const fixture = new Tacks(Dir({ |
| 106 | + 'package.json': File({}), |
| 107 | + 'foo': Dir({ |
| 108 | + 'package.json': File({ |
| 109 | + name: 'foo', |
| 110 | + version: '1.2.3' |
| 111 | + }) |
| 112 | + }) |
| 113 | + })) |
| 114 | + fixture.create(testDir) |
| 115 | + return common.npm([ |
| 116 | + 'install', 'bar@file:foo', |
| 117 | + '--cache', path.join(testDir, 'npmcache'), |
| 118 | + '--loglevel', 'silly', |
| 119 | + '--registry', server.registry |
| 120 | + ], { cwd: testDir }).then(() => { |
| 121 | + return readFileAsync( |
| 122 | + path.join(testDir, 'node_modules', 'bar', 'package.json'), |
| 123 | + 'utf8' |
| 124 | + ) |
| 125 | + }).then(JSON.parse).then(pkg => { |
| 126 | + t.similar(pkg, { |
| 127 | + name: 'foo', |
| 128 | + version: '1.2.3' |
| 129 | + }, 'successfully installed foo as bar in node_modules') |
| 130 | + }).then(() => rimraf(testDir)) |
| 131 | +}) |
| 132 | + |
| 133 | +test('cleanup', t => { |
| 134 | + server.close() |
| 135 | + return rimraf(testDir) |
| 136 | +}) |
0 commit comments