Skip to content

Commit ea19669

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

File tree

2 files changed

+229
-0
lines changed

2 files changed

+229
-0
lines changed

test/tap/aliases.js

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
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 readdirAsync = BB.promisify(fs.readdir)
16+
const readFileAsync = BB.promisify(fs.readFile)
17+
18+
const testDir = path.join(__dirname, path.basename(__filename, '.js'))
19+
20+
let server
21+
test('setup', t => {
22+
mr({}, (err, s) => {
23+
t.ifError(err, 'registry mocked successfully')
24+
server = s
25+
t.end()
26+
})
27+
})
28+
29+
test('installs an npm: protocol alias package', t => {
30+
const fixture = new Tacks(Dir({
31+
'package.json': File({})
32+
}))
33+
fixture.create(testDir)
34+
const packument = {
35+
name: 'foo',
36+
'dist-tags': { latest: '1.2.4' },
37+
versions: {
38+
'1.2.3': {
39+
name: 'foo',
40+
version: '1.2.3',
41+
dist: {
42+
tarball: `${server.registry}/foo/-/foo-1.2.3.tgz`
43+
}
44+
},
45+
'1.2.4': {
46+
name: 'foo',
47+
version: '1.2.4',
48+
dist: {
49+
tarball: `${server.registry}/foo/-/foo-1.2.4.tgz`
50+
}
51+
}
52+
}
53+
}
54+
server.get('/foo').reply(200, packument)
55+
return mockTar({
56+
'package.json': JSON.stringify({
57+
name: 'foo',
58+
version: '1.2.3'
59+
})
60+
}).then(tarball => {
61+
server.get('/foo/-/foo-1.2.3.tgz').reply(200, tarball)
62+
server.get('/foo/-/foo-1.2.4.tgz').reply(200, tarball)
63+
return common.npm([
64+
'install', 'bar@npm:foo@1.2.3', 'foo@1.2.4',
65+
'--cache', path.join(testDir, 'npmcache'),
66+
'--registry', server.registry
67+
], { cwd: testDir })
68+
}).then(([code, stdout, stderr]) => {
69+
t.equal(code, 0)
70+
t.comment(stdout)
71+
t.comment(stderr)
72+
t.match(stdout, /\+ foo@1\.2\.3 \(as bar\)/, 'useful message')
73+
return readFileAsync(
74+
path.join(testDir, 'node_modules', 'bar', 'package.json'),
75+
'utf8'
76+
)
77+
}).then(JSON.parse).then(pkg => {
78+
t.similar(pkg, {
79+
name: 'foo',
80+
version: '1.2.3'
81+
}, 'successfully installed foo as bar in node_modules')
82+
return common.npm(['ls'], { cwd: testDir })
83+
}).then(([code, stdout, stderr]) => {
84+
t.comment(stdout)
85+
t.comment(stderr)
86+
t.match(stdout, /bar@npm:foo@1.2.3/, 'npm ls prints it correctly')
87+
return common.npm([
88+
'outdated', '--json',
89+
'--registry', server.registry
90+
], { cwd: testDir })
91+
}).then(([code, stdout, stderr]) => {
92+
t.equal(code, 0, 'outdated succeeded')
93+
t.comment(stdout)
94+
t.comment(stderr)
95+
return common.npm([
96+
'rm', 'bar',
97+
'--registry', server.registry
98+
], { cwd: testDir })
99+
}).then(([code, stdout, stderr]) => {
100+
t.equal(code, 0, 'rm succeeded')
101+
t.comment(stdout)
102+
t.comment(stderr)
103+
t.match(stdout, 'removed 1 package', 'notified of removed package')
104+
return readdirAsync(path.join(testDir, 'node_modules'))
105+
}).then(dir => {
106+
t.deepEqual(dir, ['foo'], 'regular foo left in place')
107+
}).then(() => rimraf(testDir))
108+
})
109+
110+
test('installs a tarball dep as a different name than package.json', t => {
111+
return mockTar({
112+
'package.json': JSON.stringify({
113+
name: 'foo',
114+
version: '1.2.3'
115+
})
116+
}).then(tarball => {
117+
const fixture = new Tacks(Dir({
118+
'package.json': File({}),
119+
'foo.tgz': File(tarball)
120+
}))
121+
fixture.create(testDir)
122+
return common.npm([
123+
'install', 'bar@file:foo.tgz',
124+
'--cache', path.join(testDir, 'npmcache'),
125+
'--registry', server.registry
126+
], { cwd: testDir })
127+
}).then(([code, stdout, stderr]) => {
128+
t.comment(stdout)
129+
t.comment(stderr)
130+
t.match(stdout, /^\+ foo@1\.2\.3 \(as bar\)/, 'useful message')
131+
return readFileAsync(
132+
path.join(testDir, 'node_modules', 'bar', 'package.json'),
133+
'utf8'
134+
)
135+
}).then(JSON.parse).then(pkg => {
136+
t.similar(pkg, {
137+
name: 'foo',
138+
version: '1.2.3'
139+
}, 'successfully installed foo as bar in node_modules')
140+
return common.npm(['ls'], { cwd: testDir })
141+
}).then(([code, stdout, stderr]) => {
142+
t.comment(stdout)
143+
t.comment(stderr)
144+
t.match(stdout, /bar@file:foo.tgz/, 'npm ls prints it correctly')
145+
}).then(() => rimraf(testDir))
146+
})
147+
148+
test('installs a symlink dep as a different name than package.json', t => {
149+
const fixture = new Tacks(Dir({
150+
'package.json': File({}),
151+
'foo': Dir({
152+
'package.json': File({
153+
name: 'foo',
154+
version: '1.2.3'
155+
})
156+
})
157+
}))
158+
fixture.create(testDir)
159+
return common.npm([
160+
'install', 'bar@file:foo',
161+
'--cache', path.join(testDir, 'npmcache'),
162+
'--registry', server.registry
163+
], { cwd: testDir }).then(([code, stdout, stderr]) => {
164+
t.comment(stdout)
165+
t.comment(stderr)
166+
t.match(stdout, /^\+ foo@1\.2\.3 \(as bar\)/, 'useful message')
167+
return readFileAsync(
168+
path.join(testDir, 'node_modules', 'bar', 'package.json'),
169+
'utf8'
170+
)
171+
}).then(JSON.parse).then(pkg => {
172+
t.similar(pkg, {
173+
name: 'foo',
174+
version: '1.2.3'
175+
}, 'successfully installed foo as bar in node_modules')
176+
}).then(() => rimraf(testDir))
177+
})
178+
179+
test('cleanup', t => {
180+
server.close()
181+
return rimraf(testDir)
182+
})

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)