/
globalBinDir.test.ts
109 lines (101 loc) · 2.86 KB
/
globalBinDir.test.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
/// <reference path="../../../typings/index.d.ts"/>
import fs from 'fs'
import { tempDir } from '@pnpm/prepare'
import path from 'path'
import pathName from 'path-name'
import symlinkDir from 'symlink-dir'
import { homedir } from 'os'
import getConfig from '@pnpm/config'
const globalBinDir = path.join(homedir(), '.local', 'pnpm')
const isWindows = process.platform === 'win32'
jest.mock('@pnpm/npm-conf/lib/conf', () => {
const originalModule = jest.requireActual('@pnpm/npm-conf/lib/conf')
class MockedConf extends originalModule {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
constructor (base: any, types: any) {
super(base, types)
const globalPrefixDirName = isWindows ? 'global-bin-dir-windows' : 'global-bin-dir'
this.prefix = this.globalPrefix = path.join(__dirname, globalPrefixDirName)
this.localPrefix = __dirname
}
get (name: string) {
if (name === 'prefix') {
return this.prefix
} else {
return super.get(name)
}
}
loadPrefix () {}
}
return MockedConf
})
test('respects global-bin-dir in npmrc', async () => {
const { config } = await getConfig({
cliOptions: {
global: true,
},
env: {
[pathName]: `${globalBinDir}${path.delimiter}${process.env[pathName]!}`,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.bin).toBe(globalBinDir)
})
test('respects global-bin-dir rather than dir', async () => {
const { config } = await getConfig({
cliOptions: {
global: true,
dir: __dirname,
},
env: {
[pathName]: `${globalBinDir}${path.delimiter}${process.env[pathName]!}`,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.bin).toBe(globalBinDir)
})
test('an exception is thrown when the global dir is not in PATH', async () => {
await expect(
getConfig({
cliOptions: {
global: true,
dir: __dirname,
},
env: {
[pathName]: process.env[pathName],
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
).rejects.toThrow(/is not in PATH/)
})
test('the global directory may be a symlink to a directory that is in PATH', async () => {
const tmp = tempDir()
const globalBinDirTarget = path.join(tmp, 'global-target')
fs.mkdirSync(globalBinDirTarget)
const globalBinDirSymlink = path.join(tmp, 'global-symlink')
await symlinkDir(globalBinDirTarget, globalBinDirSymlink)
const { config } = await getConfig({
cliOptions: {
global: true,
'global-bin-dir': globalBinDirSymlink,
dir: __dirname,
},
env: {
[pathName]: `${globalBinDirTarget}${path.delimiter}${process.env[pathName]!}`,
},
packageManager: {
name: 'pnpm',
version: '1.0.0',
},
})
expect(config.bin).toBe(globalBinDirSymlink)
})