Welcome to mirror list, hosted at ThFree Co, Russian Federation.

fund.js « tap « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 97b414bf6e0173b5222b784b998555621d6bc3d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
'use strict'

const fs = require('fs')
const path = require('path')

const test = require('tap').test
const Tacks = require('tacks')
const Dir = Tacks.Dir
const File = Tacks.File
const common = require('../common-tap.js')
const isWindows = require('../../lib/utils/is-windows.js')

const base = common.pkg
const noFunding = path.join(base, 'no-funding-package')
const maintainerOwnsAllDeps = path.join(base, 'maintainer-owns-all-deps')
const nestedNoFundingPackages = path.join(base, 'nested-no-funding-packages')
const fundingStringShorthand = path.join(base, 'funding-string-shorthand')

function getFixturePackage ({ name, version, dependencies, funding }, extras) {
  const getDeps = () => Object
    .keys(dependencies)
    .reduce((res, dep) => (Object.assign({}, res, {
      [dep]: '*'
    })), {})

  return Dir(Object.assign({
    'package.json': File({
      name,
      version: version || '1.0.0',
      funding: (funding === undefined) ? {
        type: 'individual',
        url: 'http://example.com/donate'
      } : funding,
      dependencies: dependencies && getDeps(dependencies)
    })
  }, extras))
}

const fixture = new Tacks(Dir({
  'funding-string-shorthand': Dir({
    'package.json': File({
      name: 'funding-string-shorthand',
      version: '0.0.0',
      funding: 'https://example.com/sponsor'
    })
  }),
  'no-funding-package': Dir({
    'package.json': File({
      name: 'no-funding-package',
      version: '0.0.0'
    })
  }),
  'maintainer-owns-all-deps': getFixturePackage({
    name: 'maintainer-owns-all-deps',
    dependencies: {
      'dep-foo': '*',
      'dep-bar': '*'
    }
  }, {
    node_modules: Dir({
      'dep-foo': getFixturePackage({
        name: 'dep-foo',
        dependencies: {
          'dep-sub-foo': '*'
        }
      }, {
        node_modules: Dir({
          'dep-sub-foo': getFixturePackage({
            name: 'dep-sub-foo'
          })
        })
      }),
      'dep-bar': getFixturePackage({
        name: 'dep-bar'
      })
    })
  }),
  'nested-no-funding-packages': getFixturePackage({
    name: 'nested-no-funding-packages',
    funding: null,
    dependencies: {
      foo: '*'
    },
    devDependencies: {
      lorem: '*'
    }
  }, {
    node_modules: Dir({
      foo: getFixturePackage({
        name: 'foo',
        dependencies: {
          bar: '*'
        },
        funding: null
      }, {
        node_modules: Dir({
          bar: getFixturePackage({
            name: 'bar'
          }, {
            node_modules: Dir({
              'sub-bar': getFixturePackage({
                name: 'sub-bar',
                funding: 'https://example.com/sponsor'
              })
            })
          })
        })
      }),
      lorem: getFixturePackage({
        name: 'lorem',
        funding: {
          url: 'https://example.com/lorem'
        }
      })
    })
  })
}))

function checkOutput (t, { code, stdout, stderr }) {
  t.is(code, 0, `exited code 0`)
  t.is(stderr, '', 'no warnings')
}

function jsonTest (t, { assertionMsg, expected, stdout }) {
  let parsed = JSON.parse(stdout)
  t.deepEqual(parsed, expected, assertionMsg)
}

function snapshotTest (t, { stdout, assertionMsg }) {
  t.matchSnapshot(stdout, assertionMsg)
}

function testFundCmd ({ title, assertionMsg, args = [], opts = {}, output = checkOutput, assertion = snapshotTest, expected }) {
  const validate = (t) => (err, code, stdout, stderr) => {
    if (err) throw err

    output(t, { code, stdout, stderr })
    assertion(t, { assertionMsg, expected, stdout })
  }

  return test(title, (t) => {
    t.plan(3)
    common.npm(['fund', '--unicode=false'].concat(args), opts, validate(t))
  })
}

test('setup', function (t) {
  fixture.remove(base)
  fixture.create(base)
  t.end()
})

testFundCmd({
  title: 'fund with no package containing funding',
  assertionMsg: 'should print empty funding info',
  opts: { cwd: noFunding }
})

testFundCmd({
  title: 'fund in which same maintainer owns all its deps',
  assertionMsg: 'should print stack packages together',
  opts: { cwd: maintainerOwnsAllDeps }
})

testFundCmd({
  title: 'fund in which same maintainer owns all its deps, using --json option',
  assertionMsg: 'should print stack packages together',
  args: ['--json'],
  opts: { cwd: maintainerOwnsAllDeps },
  assertion: jsonTest,
  expected: {
    length: 3,
    name: 'maintainer-owns-all-deps',
    version: '1.0.0',
    funding: { type: 'individual', url: 'http://example.com/donate' },
    dependencies: {
      'dep-bar': {
        version: '1.0.0',
        funding: { type: 'individual', url: 'http://example.com/donate' }
      },
      'dep-foo': {
        version: '1.0.0',
        funding: { type: 'individual', url: 'http://example.com/donate' },
        dependencies: {
          'dep-sub-foo': {
            version: '1.0.0',
            funding: { type: 'individual', url: 'http://example.com/donate' }
          }
        }
      }
    }
  }
})

testFundCmd({
  title: 'fund containing multi-level nested deps with no funding',
  assertionMsg: 'should omit dependencies with no funding declared',
  opts: { cwd: nestedNoFundingPackages }
})

testFundCmd({
  title: 'fund containing multi-level nested deps with no funding, using --json option',
  assertionMsg: 'should omit dependencies with no funding declared',
  args: ['--json'],
  opts: { cwd: nestedNoFundingPackages },
  assertion: jsonTest,
  expected: {
    length: 3,
    name: 'nested-no-funding-packages',
    version: '1.0.0',
    dependencies: {
      lorem: { version: '1.0.0', funding: { url: 'https://example.com/lorem' } },
      bar: {
        version: '1.0.0',
        funding: { type: 'individual', url: 'http://example.com/donate' },
        dependencies: {
          'sub-bar': {
            version: '1.0.0',
            funding: { url: 'https://example.com/sponsor' }
          }
        }
      }
    }
  }
})

testFundCmd({
  title: 'fund does not support global',
  assertionMsg: 'should throw EFUNDGLOBAL error',
  args: ['--global'],
  output: (t, { code, stdout, stderr }) => {
    t.is(code, 1, `exited code 0`)
    const [ errCode, errCmd ] = stderr.split('\n')
    t.matchSnapshot(`${errCode}\n${errCmd}`, 'should write error msgs to stderr')
  }
})

testFundCmd({
  title: 'fund does not support global, using --json option',
  assertionMsg: 'should throw EFUNDGLOBAL error',
  args: ['--global', '--json'],
  output: (t, { code, stdout, stderr }) => {
    t.is(code, 1, `exited code 0`)
    const [ errCode, errCmd ] = stderr.split('\n')
    t.matchSnapshot(`${errCode}\n${errCmd}`, 'should write error msgs to stderr')
  },
  assertion: jsonTest,
  expected: {
    error: {
      code: 'EFUNDGLOBAL',
      summary: '`npm fund` does not support globals',
      detail: ''
    }
  }
})

testFundCmd({
  title: 'fund using package argument with no browser',
  assertionMsg: 'should open funding url',
  args: ['.', '--no-browser'],
  opts: { cwd: maintainerOwnsAllDeps }
})

testFundCmd({
  title: 'fund using string shorthand',
  assertionMsg: 'should open string-only url',
  args: ['.', '--no-browser'],
  opts: { cwd: fundingStringShorthand }
})

testFundCmd({
  title: 'fund using package argument with no browser, using --json option',
  assertionMsg: 'should open funding url',
  args: ['.', '--json', '--no-browser'],
  opts: { cwd: maintainerOwnsAllDeps },
  assertion: jsonTest,
  expected: {
    title: 'individual funding available at the following URL',
    url: 'http://example.com/donate'
  }
})

if (!isWindows) {
  test('fund using package argument', function (t) {
    const fakeBrowser = path.join(common.pkg, '_script.sh')
    const outFile = path.join(common.pkg, '_output')

    const s = '#!/usr/bin/env bash\n' +
            'echo "$@" > ' + JSON.stringify(common.pkg) + '/_output\n'
    fs.writeFileSync(fakeBrowser, s)
    fs.chmodSync(fakeBrowser, '0755')

    common.npm([
      'fund', '.',
      '--loglevel=silent',
      '--browser=' + fakeBrowser
    ], { cwd: maintainerOwnsAllDeps }, function (err, code, stdout, stderr) {
      t.ifError(err, 'repo command ran without error')
      t.equal(code, 0, 'exit ok')
      var res = fs.readFileSync(outFile, 'utf8')
      t.equal(res, 'http://example.com/donate\n')
      t.end()
    })
  })
}

test('cleanup', function (t) {
  t.pass(base)
  fixture.remove(base)
  t.end()
})