-
-
Notifications
You must be signed in to change notification settings - Fork 931
/
index.ts
163 lines (144 loc) · 4.64 KB
/
index.ts
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
/// <reference path="../../../__typings__/index.d.ts" />
import path from 'path'
import { licenses } from '@pnpm/plugin-commands-licenses'
import { install } from '@pnpm/plugin-commands-installation'
import { REGISTRY_MOCK_PORT } from '@pnpm/registry-mock'
import stripAnsi from 'strip-ansi'
import { DEFAULT_OPTS } from './utils'
import tempy from 'tempy'
const REGISTRY_URL = `http://localhost:${REGISTRY_MOCK_PORT}`
const LICENSES_OPTIONS = {
cacheDir: 'cache',
fetchRetries: 1,
fetchRetryFactor: 1,
fetchRetryMaxtimeout: 60,
fetchRetryMintimeout: 10,
global: false,
networkConcurrency: 16,
offline: false,
rawConfig: { registry: REGISTRY_URL },
registries: { default: REGISTRY_URL },
strictSsl: false,
tag: 'latest',
userAgent: '',
userConfig: {},
}
test('pnpm licenses', async () => {
const workspaceDir = path.resolve('./test/fixtures/complex-licenses')
const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})
// Attempt to run the licenses command now
const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: false,
// we need to prefix it with v3 otherwise licenses tool can't find anything
// in the content-addressable directory
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])
expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages')
})
test('pnpm licenses: show details', async () => {
const workspaceDir = path.resolve('./test/fixtures/simple-licenses')
const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})
// Attempt to run the licenses command now
const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: true,
// we need to prefix it with v3 otherwise licenses tool can't find anything
// in the content-addressable directory
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])
expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages-details')
})
test('pnpm licenses: output as json', async () => {
const workspaceDir = path.resolve('./test/fixtures/simple-licenses')
const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})
// Attempt to run the licenses command now
const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: false,
json: true,
// we need to prefix it with v3 otherwise licenses tool can't find anything
// in the content-addressable directory
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])
expect(exitCode).toBe(0)
expect(output).not.toHaveLength(0)
expect(output).not.toBe('No licenses in packages found')
const parsedOutput = JSON.parse(output)
expect(Object.keys(parsedOutput)).toMatchSnapshot('found-license-types')
const packagesWithMIT = parsedOutput['MIT']
expect(packagesWithMIT.length).toBeGreaterThan(0)
expect(Object.keys(packagesWithMIT[0])).toEqual([
'name',
'version',
'path',
'license',
'author',
'homepage',
])
expect(packagesWithMIT[0].name).toBe('is-positive')
})
test('pnpm licenses: fails when lockfile is missing', async () => {
await expect(
licenses.handler({
...LICENSES_OPTIONS,
dir: path.resolve('./test/fixtures/invalid'),
pnpmHomeDir: '',
long: true,
}, ['list'])
).rejects.toThrowErrorMatchingInlineSnapshot(
'"No pnpm-lock.yaml found: Cannot check a project without a lockfile"'
)
})
test('pnpm licenses: should correctly read LICENSE file with executable file mode', async () => {
const workspaceDir = path.resolve('./test/fixtures/file-mode-test')
const tmp = tempy.directory()
const storeDir = path.join(tmp, 'store')
await install.handler({
...DEFAULT_OPTS,
dir: workspaceDir,
pnpmHomeDir: '',
storeDir,
})
// Attempt to run the licenses command now
const { output, exitCode } = await licenses.handler({
...LICENSES_OPTIONS,
dir: workspaceDir,
pnpmHomeDir: '',
long: true,
// we need to prefix it with v3 otherwise licenses tool can't find anything
// in the content-addressable directory
storeDir: path.resolve(storeDir, 'v3'),
}, ['list'])
expect(exitCode).toBe(0)
expect(stripAnsi(output)).toMatchSnapshot('show-packages-details')
})