• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict'
2const path = require('path')
3const test = require('tap').test
4const Tacks = require('tacks')
5const Dir = Tacks.Dir
6const File = Tacks.File
7const common = require('../common-tap.js')
8
9const base = common.pkg
10const singlePackage = path.join(base, 'single-funding-package')
11const multiplePackages = path.join(base, 'top-level-funding')
12
13function getFixturePackage ({ name, version, dependencies, funding }) {
14  return Dir({
15    'package.json': File({
16      name,
17      version: version || '1.0.0',
18      funding: funding || {
19        type: 'individual',
20        url: 'http://example.com/donate'
21      },
22      dependencies: dependencies || {}
23    })
24  })
25}
26
27const fixture = new Tacks(Dir({
28  'package.json': File({}),
29  'single-funding-package': getFixturePackage({
30    name: 'single-funding-package'
31  }),
32  'top-level-funding': getFixturePackage({
33    name: 'top-level-funding',
34    dependencies: {
35      'dep-foo': 'file:../dep-foo',
36      'dep-bar': 'file:../dep-bar'
37    }
38  }),
39  'dep-foo': getFixturePackage({
40    name: 'dep-foo',
41    funding: {
42      type: 'corporate',
43      url: 'https://corp.example.com/sponsor'
44    },
45    dependencies: {
46      'sub-dep-bar': 'file:../sub-dep-bar'
47    }
48  }),
49  'dep-bar': getFixturePackage({
50    name: 'dep-bar',
51    version: '2.1.0',
52    dependencies: {
53      'sub-dep-bar': 'file:../sub-dep-bar'
54    }
55  }),
56  'sub-dep-bar': getFixturePackage({
57    name: 'sub-dep-bar',
58    funding: {
59      type: 'foo',
60      url: 'http://example.com/foo'
61    }
62  })
63}))
64
65test('mention npm fund upon installing single dependency', function (t) {
66  setup(t)
67  common.npm(['install', '--no-save', singlePackage], {cwd: base}, function (err, code, stdout, stderr) {
68    if (err) throw err
69    t.is(code, 0, 'installed successfully')
70    t.is(stderr, '', 'no warnings')
71    t.includes(stdout, '1 package is looking for funding', 'should print amount of packages needing funding')
72    t.includes(stdout, '  run `npm fund` for details', 'should print npm fund mention')
73    t.end()
74  })
75})
76
77test('mention npm fund upon installing multiple dependencies', function (t) {
78  setup(t)
79  common.npm(['install', '--no-save', multiplePackages], {cwd: base}, function (err, code, stdout, stderr) {
80    if (err) throw err
81    t.is(code, 0, 'installed successfully')
82    t.is(stderr, '', 'no warnings')
83    t.includes(stdout, '4 packages are looking for funding', 'should print amount of packages needing funding')
84    t.includes(stdout, '  run `npm fund` for details', 'should print npm fund mention')
85    t.end()
86  })
87})
88
89test('skips mention npm fund using --no-fund option', function (t) {
90  setup(t)
91  common.npm(['install', '--no-save', '--no-fund', multiplePackages], {cwd: base}, function (err, code, stdout, stderr) {
92    if (err) throw err
93    t.is(code, 0, 'installed successfully')
94    t.is(stderr, '', 'no warnings')
95    t.doesNotHave(stdout, '4 packages are looking for funding', 'should print amount of packages needing funding')
96    t.doesNotHave(stdout, '  run `npm fund` for details', 'should print npm fund mention')
97    t.end()
98  })
99})
100
101test('mention packages looking for funding using --json', function (t) {
102  setup(t)
103  common.npm(['install', '--no-save', '--json', multiplePackages], {cwd: base}, function (err, code, stdout, stderr) {
104    if (err) throw err
105    t.is(code, 0, 'installed successfully')
106    t.is(stderr, '', 'no warnings')
107    const res = JSON.parse(stdout)
108    t.match(res.funding, '4 packages are looking for funding', 'should print amount of packages needing funding')
109    t.end()
110  })
111})
112
113test('cleanup', function (t) {
114  cleanup()
115  t.end()
116})
117
118function setup (t) {
119  fixture.create(base)
120  t.teardown(() => {
121    cleanup()
122  })
123}
124
125function cleanup () {
126  fixture.remove(base)
127}
128