Skip to content

Commit 1c65d26

Browse files
ruyadornoMichael Perrotte
authored and
Michael Perrotte
committedDec 3, 2019
fix(fund): open url for string shorthand
Trying to open url for a package that is using the string shorthand is currently broken using: `npm fund <pkg>` This commit fixes the issue and adds the missing unit and integration tests covering that usecase. Fixes #498 PR-URL: #501 Credit: @ruyadorno Close: #501 Reviewed-by: @mikemimik
1 parent e4b9796 commit 1c65d26

File tree

5 files changed

+102
-11
lines changed

5 files changed

+102
-11
lines changed
 

‎lib/fund.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const readShrinkwrap = require('./install/read-shrinkwrap.js')
1414
const mutateIntoLogicalTree = require('./install/mutate-into-logical-tree.js')
1515
const output = require('./utils/output.js')
1616
const openUrl = require('./utils/open-url.js')
17-
const { getFundingInfo, validFundingUrl } = require('./utils/funding.js')
17+
const { getFundingInfo, retrieveFunding, validFundingUrl } = require('./utils/funding.js')
1818

1919
const FundConfig = figgyPudding({
2020
browser: {}, // used by ./utils/open-url
@@ -132,7 +132,7 @@ function printHuman (fundingInfo, opts) {
132132
function openFundingUrl (packageName, cb) {
133133
function getUrlAndOpen (packageMetadata) {
134134
const { funding } = packageMetadata
135-
const { type, url } = funding || {}
135+
const { type, url } = retrieveFunding(funding) || {}
136136
const noFundingError =
137137
new Error(`No funding method available for: ${packageName}`)
138138
noFundingError.code = 'ENOFUND'

‎lib/utils/funding.js

+10-8
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,18 @@
33
const URL = require('url').URL
44

55
exports.getFundingInfo = getFundingInfo
6+
exports.retrieveFunding = retrieveFunding
67
exports.validFundingUrl = validFundingUrl
78

9+
// supports both object funding and string shorthand
10+
function retrieveFunding (funding) {
11+
return typeof funding === 'string'
12+
? {
13+
url: funding
14+
}
15+
: funding
16+
}
17+
818
// Is the value of a `funding` property of a `package.json`
919
// a valid type+url for `npm fund` to display?
1020
function validFundingUrl (funding) {
@@ -60,14 +70,6 @@ function getFundingInfo (idealTree, opts) {
6070
)
6171
}
6272

63-
function retrieveFunding (funding) {
64-
return typeof funding === 'string'
65-
? {
66-
url: funding
67-
}
68-
: funding
69-
}
70-
7173
function getFundingDependencies (tree) {
7274
const deps = tree && tree.dependencies
7375
if (!deps) return empty()

‎tap-snapshots/test-tap-fund.js-TAP.test.js

+7
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,13 @@ http://example.com/donate
4747
4848
`
4949

50+
exports[`test/tap/fund.js TAP fund using string shorthand > should open string-only url 1`] = `
51+
Funding available at the following URL:
52+
53+
https://example.com/sponsor
54+
55+
`
56+
5057
exports[`test/tap/fund.js TAP fund with no package containing funding > should print empty funding info 1`] = `
5158
no-funding-package@0.0.0
5259

‎test/tap/fund.js

+15
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const base = common.pkg
1414
const noFunding = path.join(base, 'no-funding-package')
1515
const maintainerOwnsAllDeps = path.join(base, 'maintainer-owns-all-deps')
1616
const nestedNoFundingPackages = path.join(base, 'nested-no-funding-packages')
17+
const fundingStringShorthand = path.join(base, 'funding-string-shorthand')
1718

1819
function getFixturePackage ({ name, version, dependencies, funding }, extras) {
1920
const getDeps = () => Object
@@ -36,6 +37,13 @@ function getFixturePackage ({ name, version, dependencies, funding }, extras) {
3637
}
3738

3839
const fixture = new Tacks(Dir({
40+
'funding-string-shorthand': Dir({
41+
'package.json': File({
42+
name: 'funding-string-shorthand',
43+
version: '0.0.0',
44+
funding: 'https://example.com/sponsor'
45+
})
46+
}),
3947
'no-funding-package': Dir({
4048
'package.json': File({
4149
name: 'no-funding-package',
@@ -253,6 +261,13 @@ testFundCmd({
253261
opts: { cwd: maintainerOwnsAllDeps }
254262
})
255263

264+
testFundCmd({
265+
title: 'fund using string shorthand',
266+
assertionMsg: 'should open string-only url',
267+
args: ['.', '--no-browser'],
268+
opts: { cwd: fundingStringShorthand }
269+
})
270+
256271
testFundCmd({
257272
title: 'fund using package argument with no browser, using --json option',
258273
assertionMsg: 'should open funding url',

‎test/tap/utils.funding.js

+68-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22

33
const { test } = require('tap')
4-
const { getFundingInfo } = require('../../lib/utils/funding')
4+
const { retrieveFunding, getFundingInfo } = require('../../lib/utils/funding')
55

66
test('empty tree', (t) => {
77
t.deepEqual(
@@ -545,3 +545,70 @@ test('handle different versions', (t) => {
545545
)
546546
t.end()
547547
})
548+
549+
test('retrieve funding info from valid objects', (t) => {
550+
t.deepEqual(
551+
retrieveFunding({
552+
url: 'http://example.com',
553+
type: 'Foo'
554+
}),
555+
{
556+
url: 'http://example.com',
557+
type: 'Foo'
558+
},
559+
'should return standard object fields'
560+
)
561+
t.deepEqual(
562+
retrieveFunding({
563+
extra: 'Foo',
564+
url: 'http://example.com',
565+
type: 'Foo'
566+
}),
567+
{
568+
extra: 'Foo',
569+
url: 'http://example.com',
570+
type: 'Foo'
571+
},
572+
'should leave untouched extra fields'
573+
)
574+
t.deepEqual(
575+
retrieveFunding({
576+
url: 'http://example.com'
577+
}),
578+
{
579+
url: 'http://example.com'
580+
},
581+
'should accept url-only objects'
582+
)
583+
t.end()
584+
})
585+
586+
test('retrieve funding info from invalid objects', (t) => {
587+
t.deepEqual(
588+
retrieveFunding({}),
589+
{},
590+
'should passthrough empty objects'
591+
)
592+
t.deepEqual(
593+
retrieveFunding(),
594+
undefined,
595+
'should not care about undefined'
596+
)
597+
t.deepEqual(
598+
retrieveFunding(),
599+
null,
600+
'should not care about null'
601+
)
602+
t.end()
603+
})
604+
605+
test('retrieve funding info string shorthand', (t) => {
606+
t.deepEqual(
607+
retrieveFunding('http://example.com'),
608+
{
609+
url: 'http://example.com'
610+
},
611+
'should accept string shorthand'
612+
)
613+
t.end()
614+
})

0 commit comments

Comments
 (0)
Please sign in to comment.