Skip to content

Commit e651f5a

Browse files
Add yarnBerry to test suite
1 parent 47c83cb commit e651f5a

File tree

2 files changed

+97
-19
lines changed

2 files changed

+97
-19
lines changed

src/test/cli-options.test.ts

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import * as assert from 'uvu/assert';
99
import {suite} from 'uvu';
1010
import {rigTest} from './util/rig-test.js';
1111
import {WireitTestRig} from './util/test-rig.js';
12-
import {Options} from '../cli-options.js';
12+
import {Agent, Options} from '../cli-options.js';
1313
import {Result} from '../error.js';
1414

1515
const test = suite<object>();
@@ -29,6 +29,58 @@ async function getOptionsResult(
2929
extraScripts?: Record<string, string>,
3030
): Promise<Result<Options>> {
3131
rig.env.WIREIT_DEBUG_LOG_FILE = '';
32+
33+
if (command.startsWith('yarnBerry')) {
34+
// `yarn` chooses whether to use "classic" or "berry" based on the presence
35+
// of `yarnPath` in `.yarnrc.yml`.
36+
//
37+
// To closest simulate a user's environment, set `yarnPath` and let the
38+
// global `yarn` command pick it when testing `yarnBerry`.
39+
command = command.replace(/yarnBerry/g, 'yarn');
40+
41+
extraScripts = Object.fromEntries(
42+
Object.entries(extraScripts || {}).map(([scriptName, scriptCommand]) => [
43+
scriptName,
44+
scriptCommand.replace(/yarnBerry/g, 'yarn'),
45+
]),
46+
);
47+
48+
await rig.write({
49+
'.yarnrc.yml': `
50+
nodeLinker: node-modules
51+
yarnPath: ${pathlib.join(
52+
process.cwd(),
53+
'third_party',
54+
'.yarn',
55+
'releases',
56+
'yarn-4.0.1.cjs',
57+
)}
58+
`,
59+
});
60+
61+
// `yarn.lock` tells `yarn` that this test is meant to be its own workspace
62+
// root, even though there are higher `package.json` files in the file tree.
63+
//
64+
// This is the `yarn.lock` you get if you initialize `yarn` in an empty
65+
// folder.
66+
await rig.write({
67+
'yarn.lock': `
68+
# This file is generated by running "yarn install" inside your project.
69+
# Manual changes might be lost - proceed with caution!
70+
71+
__metadata:
72+
version: 8
73+
cacheKey: 10c0
74+
75+
"root-workspace-0b6124@workspace:.":
76+
version: 0.0.0-use.local
77+
resolution: "root-workspace-0b6124@workspace:."
78+
languageName: unknown
79+
linkType: soft
80+
`,
81+
});
82+
}
83+
3284
await rig.write({
3385
'package.json': {
3486
scripts: {
@@ -39,7 +91,12 @@ async function getOptionsResult(
3991
},
4092
},
4193
});
42-
env = {...env, WIREIT_DEBUG_LOG_FILE: ''};
94+
95+
env = {
96+
...env,
97+
WIREIT_DEBUG_LOG_FILE: '',
98+
};
99+
43100
assert.equal((await rig.exec(command, {env}).exit).code, 0);
44101
return JSON.parse(await rig.read('options.json')) as Result<Options>;
45102
}
@@ -67,8 +124,8 @@ async function assertOptions(
67124
});
68125
}
69126

70-
for (const command of ['npm', 'yarn', 'pnpm'] as const) {
71-
const agent = command === 'yarn' ? 'yarnClassic' : command;
127+
for (const command of ['npm', 'yarn', 'pnpm', 'yarnBerry'] as const) {
128+
const agent: Agent = command === 'yarn' ? 'yarnClassic' : command;
72129
// eslint-disable-next-line @typescript-eslint/unbound-method
73130
const skipIfYarn = command === 'yarn' ? test.skip : test;
74131
// eslint-disable-next-line @typescript-eslint/unbound-method
@@ -87,6 +144,19 @@ for (const command of ['npm', 'yarn', 'pnpm'] as const) {
87144
}),
88145
);
89146

147+
test(
148+
`${command} run main`,
149+
rigTest(async ({rig}) => {
150+
await assertOptions(rig, `${command} run main`, {
151+
agent,
152+
script: {
153+
packageDir: rig.temp,
154+
name: 'main',
155+
},
156+
});
157+
}),
158+
);
159+
90160
test(
91161
`${command} test`,
92162
rigTest(async ({rig}) => {

src/test/util/test-rig.ts

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,30 @@ export class WireitTestRig
6464
*/
6565
override async setup() {
6666
await super.setup();
67-
const absWireitBinaryPath = pathlib.resolve(repoRoot, 'bin', 'wireit.js');
68-
const absWireitTempInstallPath = pathlib.resolve(
69-
this.temp,
70-
'node_modules',
71-
'.bin',
72-
'wireit',
67+
68+
await Promise.all(
69+
[['wireit', ['bin', 'wireit.js']] as const].map(
70+
async ([name, pathParts]) => {
71+
const binaryPath = pathlib.resolve(repoRoot, ...pathParts);
72+
const tempInstallPath = pathlib.resolve(
73+
this.temp,
74+
'node_modules',
75+
'.bin',
76+
name,
77+
);
78+
79+
if (IS_WINDOWS) {
80+
// Npm install works differently on Windows, since it won't recognize a
81+
// shebang like "#!/usr/bin/env node". Npm instead uses the cmd-shim
82+
// package to generate Windows shell wrappers for each binary, so we do
83+
// that here too.
84+
await cmdShim(binaryPath, tempInstallPath);
85+
} else {
86+
await this.symlink(binaryPath, tempInstallPath, 'file');
87+
}
88+
},
89+
),
7390
);
74-
if (IS_WINDOWS) {
75-
// Npm install works differently on Windows, since it won't recognize a
76-
// shebang like "#!/usr/bin/env node". Npm instead uses the cmd-shim
77-
// package to generate Windows shell wrappers for each binary, so we do
78-
// that here too.
79-
await cmdShim(absWireitBinaryPath, absWireitTempInstallPath);
80-
} else {
81-
await this.symlink(absWireitBinaryPath, absWireitTempInstallPath, 'file');
82-
}
8391
}
8492

8593
/**

0 commit comments

Comments
 (0)