Skip to content

Commit ebf7ac9

Browse files
ybiquitousisaacs
authored andcommitted
feat: npm repo support repository.directory field
Related: #140 PR-URL: #163 Credit: @ybiquitous Close: #163 Reviewd-By: @isaacs
1 parent 92fda43 commit ebf7ac9

File tree

3 files changed

+88
-42
lines changed

3 files changed

+88
-42
lines changed

lib/repo.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ const getRepo = async pkg => {
3636
}
3737

3838
const info = hostedFromMani(mani)
39-
const url = info ? info.browse() : unknownHostedUrl(rurl)
39+
const url = info ? info.browse(mani.repository.directory) : unknownHostedUrl(rurl)
4040

4141
if (!url) {
4242
throw Object.assign(new Error('no repository: could not get url'), {

test/lib/repo.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ const pacote = {
8989
repository: { url: 'git+https://gothib.com/foo/unhostedgithttpsobj' }
9090
}
9191

92+
: spec === 'directory' ? {
93+
repository: {
94+
type: 'git',
95+
url: 'git+https://github.com/foo/test-repo-with-directory.git',
96+
directory: 'some/directory'
97+
}
98+
}
99+
92100
: spec === '.' ? {
93101
name: 'thispkg',
94102
version: '1.2.3',
@@ -143,6 +151,7 @@ t.test('open repo urls', t => {
143151
unhostedgitsshobj: 'https://gothib.com/foo/unhostedgitsshobj',
144152
unhostedgithttpobj: 'http://gothib.com/foo/unhostedgithttpobj',
145153
unhostedgithttpsobj: 'https://gothib.com/foo/unhostedgithttpsobj',
154+
directory: 'https://github.com/foo/test-repo-with-directory/tree/master/some/directory',
146155
'.': 'https://example.com/thispkg'
147156
}
148157
const keys = Object.keys(expect)

test/tap/repo.js

Lines changed: 78 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,16 @@ var test = require('tap').test
55
var rimraf = require('rimraf')
66
var fs = require('fs')
77
var path = require('path')
8-
var fakeBrowser = path.join(common.pkg, '_script.sh')
9-
var outFile = path.join(common.pkg, '_output')
10-
var opts = { cwd: common.pkg }
11-
var mkdirp = require('mkdirp')
8+
var fakeBrowser = path.join(__dirname, '_script.sh')
9+
var outFile = path.join(__dirname, '/_output')
10+
11+
var opts = { cwd: __dirname }
1212

1313
common.pendIfWindows('This is trickier to convert without opening new shells')
1414

1515
test('setup', function (t) {
16-
mkdirp.sync(common.pkg)
1716
var s = '#!/usr/bin/env bash\n' +
18-
'echo "$@" > ' + JSON.stringify(common.pkg) + '/_output\n'
17+
'echo "$@" > ' + JSON.stringify(__dirname) + '/_output\n'
1918
fs.writeFileSync(fakeBrowser, s, 'ascii')
2019
fs.chmodSync(fakeBrowser, '0755')
2120
t.pass('made script')
@@ -41,41 +40,6 @@ test('npm repo underscore', function (t) {
4140
})
4241
})
4342

44-
test('npm repo underscore --json', function (t) {
45-
mr({ port: common.port }, function (er, s) {
46-
common.npm([
47-
'repo', 'underscore',
48-
'--json',
49-
'--registry=' + common.registry,
50-
'--loglevel=silent',
51-
'--no-browser'
52-
], opts, function (err, code, stdout, stderr) {
53-
t.ifError(err, 'repo command ran without error')
54-
t.equal(code, 0, 'exit ok')
55-
t.matchSnapshot(stdout, 'should print json result')
56-
s.close()
57-
t.end()
58-
})
59-
})
60-
})
61-
62-
test('npm repo underscore --no-browser', function (t) {
63-
mr({ port: common.port }, function (er, s) {
64-
common.npm([
65-
'repo', 'underscore',
66-
'--no-browser',
67-
'--registry=' + common.registry,
68-
'--loglevel=silent'
69-
], opts, function (err, code, stdout, stderr) {
70-
t.ifError(err, 'repo command ran without error')
71-
t.equal(code, 0, 'exit ok')
72-
t.matchSnapshot(stdout, 'should print alternative msg')
73-
s.close()
74-
t.end()
75-
})
76-
})
77-
})
78-
7943
test('npm repo optimist - github (https://)', function (t) {
8044
mr({ port: common.port }, function (er, s) {
8145
common.npm([
@@ -168,6 +132,79 @@ test('npm repo test-repo-url-ssh - non-github (ssh://)', function (t) {
168132
})
169133
})
170134

135+
/* ----- Test by new mock registry: BEGIN ----- */
136+
137+
const Tacks = require('tacks')
138+
const mockTar = require('../util/mock-tarball.js')
139+
140+
const { Dir, File } = Tacks
141+
const testDir = path.join(__dirname, path.basename(__filename, '.js'))
142+
143+
let server
144+
test('setup mocked registry', t => {
145+
common.fakeRegistry.compat({}, (err, s) => {
146+
t.ifError(err, 'registry mocked successfully')
147+
server = s
148+
t.end()
149+
})
150+
})
151+
152+
test('npm repo test-repo-with-directory', t => {
153+
const fixture = new Tacks(Dir({
154+
'package.json': File({})
155+
}))
156+
fixture.create(testDir)
157+
const packument = {
158+
name: 'test-repo-with-directory',
159+
'dist-tags': { latest: '1.2.3' },
160+
versions: {
161+
'1.2.3': {
162+
name: 'test-repo-with-directory',
163+
version: '1.2.3',
164+
dist: {
165+
tarball: `${server.registry}/test-repo-with-directory/-/test-repo-with-directory-1.2.3.tgz`
166+
},
167+
repository: {
168+
type: 'git',
169+
url: 'git+https://github.com/foo/test-repo-with-directory.git',
170+
directory: 'some/directory'
171+
}
172+
}
173+
}
174+
}
175+
server.get('/test-repo-with-directory').reply(200, packument)
176+
return mockTar({
177+
'package.json': JSON.stringify({
178+
name: 'test-repo-with-directory',
179+
version: '1.2.3'
180+
})
181+
}).then(tarball => {
182+
server.get('/test-repo-with-directory/-/test-repo-with-directory-1.2.3.tgz').reply(200, tarball)
183+
return common.npm([
184+
'repo', 'test-repo-with-directory',
185+
'--registry=' + server.registry,
186+
'--loglevel=silent',
187+
'--browser=' + fakeBrowser
188+
])
189+
}).then(([code, stdout, stderr]) => {
190+
t.equal(code, 0)
191+
t.comment(stdout)
192+
t.comment(stderr)
193+
194+
const res = fs.readFileSync(outFile, 'ascii')
195+
t.equal(res, 'https://github.com/foo/test-repo-with-directory/tree/master/some/directory\n')
196+
rimraf.sync(outFile)
197+
})
198+
})
199+
200+
test('cleanup mocked registry', t => {
201+
server.close()
202+
rimraf.sync(testDir)
203+
t.end()
204+
})
205+
206+
/* ----- Test by new mock registry: END ----- */
207+
171208
test('cleanup', function (t) {
172209
fs.unlinkSync(fakeBrowser)
173210
t.pass('cleaned up')

0 commit comments

Comments
 (0)