-
-
Notifications
You must be signed in to change notification settings - Fork 250
Expand file tree
/
Copy pathEnable.test.ts
More file actions
168 lines (133 loc) · 6.54 KB
/
Enable.test.ts
File metadata and controls
168 lines (133 loc) · 6.54 KB
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
import {Filename, ppath, xfs, npath} from '@yarnpkg/fslib';
import {delimiter} from 'node:path';
import process from 'node:process';
import {setTimeout} from 'node:timers/promises';
import {describe, beforeEach, it, expect, test} from 'vitest';
import {Engine} from '../sources/Engine';
import {SupportedPackageManagers, SupportedPackageManagerSetWithoutNpm} from '../sources/types';
import {makeBin, getBinaryNames} from './_binHelpers';
import {runCli} from './_runCli';
const engine = new Engine();
beforeEach(async () => {
// `process.env` is reset after each tests in setupTests.js.
process.env.COREPACK_HOME = npath.fromPortablePath(await xfs.mktempPromise());
process.env.COREPACK_DEFAULT_TO_LATEST = `0`;
});
describe(`EnableCommand`, () => {
it(`should add the binaries in the folder found in the PATH`, async () => {
await xfs.mktempPromise(async cwd => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);
process.env.PATH = `${npath.fromPortablePath(cwd)}${delimiter}${process.env.PATH}`;
await expect(runCli(cwd, [`enable`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
});
const expectedEntries: Array<string> = [ppath.basename(corepackBin)];
for (const packageManager of SupportedPackageManagerSetWithoutNpm)
for (const binName of engine.getBinariesFor(packageManager))
expectedEntries.push(...getBinaryNames(binName));
await expect(sortedEntries).resolves.toEqual(expectedEntries.sort());
});
});
it(`should add the binaries to the specified folder when using --install-directory`, async () => {
await xfs.mktempPromise(async cwd => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
});
const expectedEntries: Array<string> = [ppath.basename(corepackBin)];
for (const packageManager of SupportedPackageManagerSetWithoutNpm)
for (const binName of engine.getBinariesFor(packageManager))
expectedEntries.push(...getBinaryNames(binName));
await expect(sortedEntries).resolves.toEqual(expectedEntries.sort());
});
});
it(`should add binaries only for the requested package managers`, async () => {
await xfs.mktempPromise(async cwd => {
const corepackBin = await makeBin(cwd, `corepack` as Filename);
await expect(runCli(cwd, [`enable`, `--install-directory=${npath.fromPortablePath(cwd)}`, `yarn`])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const sortedEntries = xfs.readdirPromise(cwd).then(entries => {
return entries.sort();
});
const expectedEntries: Array<string> = [ppath.basename(corepackBin)];
for (const binName of engine.getBinariesFor(SupportedPackageManagers.Yarn))
expectedEntries.push(...getBinaryNames(binName));
await expect(sortedEntries).resolves.toEqual(expectedEntries.sort());
});
});
test.skipIf(process.platform === `win32`)(`should overwrite existing files`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.writeFilePromise(ppath.join(cwd, `yarn`), `hello`);
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const file = await xfs.readFilePromise(ppath.join(cwd, `yarn`), `utf8`);
expect(file).not.toBe(`hello`);
});
});
test.skipIf(process.platform === `win32`)(`shouldn't overwrite Yarn files if they are in a /switch/ folder`, async () => {
await xfs.mktempPromise(async cwd => {
await xfs.mkdirPromise(ppath.join(cwd, `switch/bin`), {recursive: true});
await xfs.writeFilePromise(ppath.join(cwd, `switch/bin/yarn`), `hello`);
await xfs.symlinkPromise(
ppath.join(cwd, `switch/bin/yarn`),
ppath.join(cwd, `yarn`),
);
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: expect.stringMatching(/^yarn is already installed in .+ and points to a Yarn Switch install - skipping\n$/),
exitCode: 0,
});
const file = await xfs.readFilePromise(ppath.join(cwd, `yarn`), `utf8`);
expect(file).toBe(`hello`);
});
});
test.skipIf(process.platform === `win32`)(`should not re-link if binaries are already correct`, async () => {
await xfs.mktempPromise(async cwd => {
await makeBin(cwd, `corepack` as Filename);
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const yarnStat1 = await xfs.lstatPromise(ppath.join(cwd, `yarn`));
await setTimeout(10);
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const yarnStat2 = await xfs.lstatPromise(ppath.join(cwd, `yarn`));
expect(yarnStat2.mtimeMs).toBe(yarnStat1.mtimeMs);
});
});
test.skipIf(process.platform === `win32`)(`should overwrite existing symlinks if they are incorrect`, async () => {
await xfs.mktempPromise(async cwd => {
await makeBin(cwd, `corepack` as Filename);
await xfs.writeFilePromise(ppath.join(cwd, `dummy-target`), `hello`);
await xfs.symlinkPromise(ppath.join(cwd, `dummy-target`), ppath.join(cwd, `yarn`));
await expect(runCli(cwd, [`enable`, `--install-directory`, npath.fromPortablePath(cwd)])).resolves.toMatchObject({
stdout: ``,
stderr: ``,
exitCode: 0,
});
const newLink = await xfs.readlinkPromise(ppath.join(cwd, `yarn`));
expect(newLink).toContain(`yarn.js`);
});
});
});