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

arborist-cmd.js « lib « test - github.com/npm/cli.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 91d8a7b333bf9d650779547c6fcfa2550448e4a4 (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
const { resolve } = require('path')
const t = require('tap')
const ArboristCmd = require('../../lib/arborist-cmd.js')

t.test('arborist-cmd', async t => {
  const path = t.testdir({
    'package.json': JSON.stringify({
      name: 'simple-workspaces-list',
      version: '1.1.1',
      workspaces: [
        'a',
        'b',
        'group/*',
      ],
    }),
    node_modules: {
      abbrev: {
        'package.json': JSON.stringify({ name: 'abbrev', version: '1.1.1' }),
      },
      a: t.fixture('symlink', '../a'),
      b: t.fixture('symlink', '../b'),
    },
    a: {
      'package.json': JSON.stringify({ name: 'a', version: '1.0.0' }),
    },
    b: {
      'package.json': JSON.stringify({ name: 'b', version: '1.0.0' }),
    },
    group: {
      c: {
        'package.json': JSON.stringify({
          name: 'c',
          version: '1.0.0',
          dependencies: {
            abbrev: '^1.1.1',
          },
        }),
      },
      d: {
        'package.json': JSON.stringify({ name: 'd', version: '1.0.0' }),
      },
    },
  })

  class TestCmd extends ArboristCmd {}

  const cmd = new TestCmd()
  cmd.npm = { localPrefix: path }

  // check filtering for a single workspace name
  cmd.exec = async function (args) {
    t.same(this.workspaceNames, ['a'], 'should set array with single ws name')
    t.same(args, ['foo'], 'should get received args')
  }
  await cmd.execWorkspaces(['foo'], ['a'])

  // check filtering single workspace by path
  cmd.exec = async function (args) {
    t.same(this.workspaceNames, ['a'],
      'should set array with single ws name from path')
  }
  await cmd.execWorkspaces([], ['./a'])

  // check filtering single workspace by full path
  cmd.exec = function (args) {
    t.same(this.workspaceNames, ['a'],
      'should set array with single ws name from full path')
  }
  await cmd.execWorkspaces([], [resolve(path, './a')])

  // filtering multiple workspaces by name
  cmd.exec = async function (args) {
    t.same(this.workspaceNames, ['a', 'c'],
      'should set array with multiple listed ws names')
  }
  await cmd.execWorkspaces([], ['a', 'c'])

  // filtering multiple workspaces by path names
  cmd.exec = async function (args) {
    t.same(this.workspaceNames, ['a', 'c'],
      'should set array with multiple ws names from paths')
  }
  await cmd.execWorkspaces([], ['./a', 'group/c'])

  // filtering multiple workspaces by parent path name
  cmd.exec = async function (args) {
    t.same(this.workspaceNames, ['c', 'd'],
      'should set array with multiple ws names from a parent folder name')
  }
  await cmd.execWorkspaces([], ['./group'])
})

t.test('handle getWorkspaces raising an error', async t => {
  const ArboristCmd = t.mock('../../lib/arborist-cmd.js', {
    '../../lib/workspaces/get-workspaces.js': async () => {
      throw new Error('oopsie')
    },
  })
  class TestCmd extends ArboristCmd {}
  const cmd = new TestCmd()
  cmd.npm = { localPrefix: t.testdir() }

  await t.rejects(
    cmd.execWorkspaces(['foo'], ['a']),
    { message: 'oopsie' }
  )
})