Skip to content

Commit 48f6de2

Browse files
committed
test: add tests for aliases
1 parent 0b8c482 commit 48f6de2

File tree

2 files changed

+183
-0
lines changed

2 files changed

+183
-0
lines changed

test/tap/aliases.js

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
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+
})

test/util/mock-tarball.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const BB = require('bluebird')
4+
5+
const getStream = require('get-stream')
6+
const pipeline = require('mississippi').pipeline
7+
const tar = require('tar-stream')
8+
const zlib = require('zlib')
9+
10+
module.exports = makeTarball
11+
function makeTarball (files, opts) {
12+
opts = opts || {}
13+
const pack = tar.pack()
14+
Object.keys(files).forEach(function (filename) {
15+
const entry = files[filename]
16+
pack.entry({
17+
name: (opts.noPrefix ? '' : 'package/') + filename,
18+
type: entry.type,
19+
size: entry.size,
20+
mode: entry.mode,
21+
mtime: entry.mtime || new Date(0),
22+
linkname: entry.linkname,
23+
uid: entry.uid,
24+
gid: entry.gid,
25+
uname: entry.uname,
26+
gname: entry.gname
27+
}, typeof files[filename] === 'string'
28+
? files[filename]
29+
: files[filename].data)
30+
})
31+
pack.finalize()
32+
return BB.try(() => {
33+
if (opts.stream && opts.gzip) {
34+
return pipeline(pack, zlib.createGzip())
35+
} else if (opts.stream) {
36+
return pack
37+
} else {
38+
return getStream.buffer(pack).then(ret => {
39+
if (opts.gzip) {
40+
return BB.fromNode(cb => zlib.gzip(ret, cb))
41+
} else {
42+
return ret
43+
}
44+
})
45+
}
46+
})
47+
}

0 commit comments

Comments
 (0)