Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add tests for yarn berry #963

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 72 additions & 8 deletions src/test/cli-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
* SPDX-License-Identifier: Apache-2.0
*/

import * as pathlib from 'path';
import * as assert from 'uvu/assert';
import {suite} from 'uvu';
import {rigTest} from './util/rig-test.js';
import {WireitTestRig} from './util/test-rig.js';
import {Options} from '../cli-options.js';
import * as pathlib from 'path';
import {Agent, Options} from '../cli-options.js';
import {IS_WINDOWS} from '../util/windows.js';
import {Result} from '../error.js';
import {WireitTestRig} from './util/test-rig.js';
import {rigTest} from './util/rig-test.js';
import {suite} from 'uvu';

const test = suite<object>();

Expand All @@ -29,6 +30,58 @@ async function getOptionsResult(
extraScripts?: Record<string, string>,
): Promise<Result<Options>> {
rig.env.WIREIT_DEBUG_LOG_FILE = '';

if (command.startsWith('yarnBerry')) {
// `yarn` chooses whether to use "classic" or "berry" based on the presence
// of `yarnPath` in `.yarnrc.yml`.
//
// To closest simulate a user's environment, set `yarnPath` and let the
// global `yarn` command pick it when testing `yarnBerry`.
command = command.replace(/yarnBerry/g, 'yarn');

extraScripts = Object.fromEntries(
Object.entries(extraScripts || {}).map(([scriptName, scriptCommand]) => [
scriptName,
scriptCommand.replace(/yarnBerry/g, 'yarn'),
]),
);

await rig.write({
'.yarnrc.yml': `
nodeLinker: node-modules
yarnPath: ${pathlib.join(
process.cwd(),
'third_party',
'.yarn',
'releases',
'yarn-4.0.1.cjs',
)}
`,
});

// `yarn.lock` tells `yarn` that this test is meant to be its own workspace
// root, even though there are higher `package.json` files in the file tree.
//
// This is the `yarn.lock` you get if you initialize `yarn` in an empty
// folder.
await rig.write({
'yarn.lock': `
# This file is generated by running "yarn install" inside your project.
# Manual changes might be lost - proceed with caution!

__metadata:
version: 8
cacheKey: 10c0

"root-workspace-0b6124@workspace:.":
version: 0.0.0-use.local
resolution: "root-workspace-0b6124@workspace:."
languageName: unknown
linkType: soft
`,
});
}

await rig.write({
'package.json': {
scripts: {
Expand All @@ -39,7 +92,12 @@ async function getOptionsResult(
},
},
});
env = {...env, WIREIT_DEBUG_LOG_FILE: ''};

env = {
...env,
WIREIT_DEBUG_LOG_FILE: '',
};

assert.equal((await rig.exec(command, {env}).exit).code, 0);
return JSON.parse(await rig.read('options.json')) as Result<Options>;
}
Expand Down Expand Up @@ -67,8 +125,14 @@ async function assertOptions(
});
}

for (const command of ['npm', 'yarn', 'pnpm'] as const) {
const agent = command === 'yarn' ? 'yarnClassic' : command;
// yarnBerry tests work everywhere but Windows (#1014). Until Windows testing
// is fixed, skip yarnBerry tests on Windows to prevent CI failures.
const commands = (['npm', 'yarn', 'pnpm', 'yarnBerry'] as const).filter(
(command) => !IS_WINDOWS || command !== 'yarnBerry',
);

for (const command of commands) {
const agent: Agent = command === 'yarn' ? 'yarnClassic' : command;
// eslint-disable-next-line @typescript-eslint/unbound-method
const skipIfYarn = command === 'yarn' ? test.skip : test;
// eslint-disable-next-line @typescript-eslint/unbound-method
Expand Down
38 changes: 23 additions & 15 deletions src/test/util/test-rig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,30 @@ export class WireitTestRig
*/
override async setup() {
await super.setup();
const absWireitBinaryPath = pathlib.resolve(repoRoot, 'bin', 'wireit.js');
const absWireitTempInstallPath = pathlib.resolve(
this.temp,
'node_modules',
'.bin',
'wireit',

await Promise.all(
[['wireit', ['bin', 'wireit.js']] as const].map(
async ([name, pathParts]) => {
const binaryPath = pathlib.resolve(repoRoot, ...pathParts);
const tempInstallPath = pathlib.resolve(
this.temp,
'node_modules',
'.bin',
name,
);

if (IS_WINDOWS) {
// Npm install works differently on Windows, since it won't recognize a
// shebang like "#!/usr/bin/env node". Npm instead uses the cmd-shim
// package to generate Windows shell wrappers for each binary, so we do
// that here too.
await cmdShim(binaryPath, tempInstallPath);
} else {
await this.symlink(binaryPath, tempInstallPath, 'file');
}
},
),
);
if (IS_WINDOWS) {
// Npm install works differently on Windows, since it won't recognize a
// shebang like "#!/usr/bin/env node". Npm instead uses the cmd-shim
// package to generate Windows shell wrappers for each binary, so we do
// that here too.
await cmdShim(absWireitBinaryPath, absWireitTempInstallPath);
} else {
await this.symlink(absWireitBinaryPath, absWireitTempInstallPath, 'file');
}
}

/**
Expand Down
25 changes: 25 additions & 0 deletions third_party/.yarn/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
BSD 2-Clause License

Copyright (c) 2016-present, Yarn Contributors.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Loading