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

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

const normalizePath = p => p
  .replace(/\\+/g, '/')
  .replace(/\r\n/g, '\n')

const cleanOutput = (str, path) => normalizePath(str)
  .replace(normalizePath(path), '{PATH}')

const clean = (res, path) => {
  const cleaned = new Map()
  for (const [key, value] of res.entries())
    cleaned.set(key, cleanOutput(value, path))
  return cleaned
}

t.test('get-workspaces', async t => {
  const path = t.testdir({
    packages: {
      a: {
        'package.json': JSON.stringify({
          name: 'a',
          version: '1.0.0',
          scripts: { glorp: 'echo a doing the glerp glop' },
        }),
      },
      b: {
        'package.json': JSON.stringify({
          name: 'b',
          version: '2.0.0',
          scripts: { glorp: 'echo b doing the glerp glop' },
        }),
      },
      c: {
        'package.json': JSON.stringify({
          name: 'c',
          version: '1.0.0',
          scripts: {
            test: 'exit 0',
            posttest: 'echo posttest',
            lorem: 'echo c lorem',
          },
        }),
      },
      d: {
        'package.json': JSON.stringify({
          name: 'd',
          version: '1.0.0',
          scripts: {
            test: 'exit 0',
            posttest: 'echo posttest',
          },
        }),
      },
      e: {
        'package.json': JSON.stringify({
          name: 'e',
          scripts: { test: 'exit 0', start: 'echo start something' },
        }),
      },
      noscripts: {
        'package.json': JSON.stringify({
          name: 'noscripts',
          version: '1.0.0',
        }),
      },
    },
    'package.json': JSON.stringify({
      name: 'x',
      version: '1.2.3',
      workspaces: ['packages/*'],
    }),
  })

  let workspaces

  workspaces = await getWorkspaces(['a', 'b'], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      a: '{PATH}/packages/a',
      b: '{PATH}/packages/b',
    })),
    'should filter by package name'
  )

  workspaces = await getWorkspaces(['./packages/c'], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      c: '{PATH}/packages/c',
    })),
    'should filter by package directory'
  )

  workspaces = await getWorkspaces(['packages/c'], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      c: '{PATH}/packages/c',
    })),
    'should filter by rel package directory'
  )

  workspaces = await getWorkspaces([resolve(path, 'packages/c')], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      c: '{PATH}/packages/c',
    })),
    'should filter by absolute package directory'
  )

  workspaces = await getWorkspaces(['packages'], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      a: '{PATH}/packages/a',
      b: '{PATH}/packages/b',
      c: '{PATH}/packages/c',
      d: '{PATH}/packages/d',
      e: '{PATH}/packages/e',
      noscripts: '{PATH}/packages/noscripts',
    })),
    'should filter by parent directory name'
  )

  workspaces = await getWorkspaces(['./packages/'], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      a: '{PATH}/packages/a',
      b: '{PATH}/packages/b',
      c: '{PATH}/packages/c',
      d: '{PATH}/packages/d',
      e: '{PATH}/packages/e',
      noscripts: '{PATH}/packages/noscripts',
    })),
    'should filter by parent directory path'
  )

  workspaces = await getWorkspaces([resolve(path, './packages')], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      a: '{PATH}/packages/a',
      b: '{PATH}/packages/b',
      c: '{PATH}/packages/c',
      d: '{PATH}/packages/d',
      e: '{PATH}/packages/e',
      noscripts: '{PATH}/packages/noscripts',
    })),
    'should filter by absolute parent directory path'
  )

  workspaces = await getWorkspaces([], { path })
  t.same(
    clean(workspaces, path),
    new Map(Object.entries({
      a: '{PATH}/packages/a',
      b: '{PATH}/packages/b',
      c: '{PATH}/packages/c',
      d: '{PATH}/packages/d',
      e: '{PATH}/packages/e',
      noscripts: '{PATH}/packages/noscripts',
    })),
    'should return all workspaces if no filter set'
  )

  try {
    await getWorkspaces(['missing'], { path })
    throw new Error('missed throw')
  } catch (err) {
    t.match(
      err,
      /No workspaces found/,
      'should throw no workspaces found error'
    )
  }

  const unconfiguredWorkspaces = t.testdir({
    'package.json': JSON.stringify({
      name: 'no-configured-workspaces',
      version: '1.0.0',
    }),
  })
  try {
    await getWorkspaces([], { path: unconfiguredWorkspaces })
    throw new Error('missed throw')
  } catch (err) {
    t.match(
      err,
      /No workspaces found/,
      'should throw no workspaces found error'
    )
  }
})