feat(yarn): ignore yarn-path if binary does not exist (#12322)

Co-authored-by: Rhys Arkins <rhys@arkins.net>
This commit is contained in:
Derek 2021-10-28 00:19:20 -07:00 committed by GitHub
parent 48c5a3c5c3
commit 16aff7419f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 1 deletions

View file

@ -223,6 +223,12 @@ describe('manager/npm/post-update/yarn', () => {
describe('checkYarnrc()', () => { describe('checkYarnrc()', () => {
it('returns offline mirror and yarn path', async () => { it('returns offline mirror and yarn path', async () => {
fs.exists.mockImplementation((path) => {
if (path === './.yarn/cli.js') {
return new Promise<boolean>((resolve) => resolve(true));
}
return new Promise<boolean>((resolve) => resolve(false));
});
fs.readFile.mockImplementation((filename, encoding) => { fs.readFile.mockImplementation((filename, encoding) => {
if (filename.endsWith('.yarnrc')) { if (filename.endsWith('.yarnrc')) {
return new Promise<string>((resolve) => return new Promise<string>((resolve) =>
@ -238,6 +244,12 @@ describe('manager/npm/post-update/yarn', () => {
}); });
it('returns no offline mirror and unquoted yarn path', async () => { it('returns no offline mirror and unquoted yarn path', async () => {
fs.exists.mockImplementation((path) => {
if (path === './.yarn/cli.js') {
return new Promise<boolean>((resolve) => resolve(true));
}
return new Promise<boolean>((resolve) => resolve(false));
});
fs.readFile.mockImplementation((filename, encoding) => { fs.readFile.mockImplementation((filename, encoding) => {
if (filename.endsWith('.yarnrc')) { if (filename.endsWith('.yarnrc')) {
return new Promise<string>((resolve) => return new Promise<string>((resolve) =>
@ -249,5 +261,27 @@ describe('manager/npm/post-update/yarn', () => {
// FIXME: explicit assert condition // FIXME: explicit assert condition
expect(await _yarnHelper.checkYarnrc('/tmp/renovate')).toMatchSnapshot(); expect(await _yarnHelper.checkYarnrc('/tmp/renovate')).toMatchSnapshot();
}); });
it('returns offline mirror and no yarn path for non-existant yarn-path binary', async () => {
let yarnrcContents = 'yarn-path ./.yarn/cli.js\n';
fs.writeFile.mockImplementation((filename, fileContents) => {
if (filename.endsWith('.yarnrc')) {
yarnrcContents = fileContents;
}
return new Promise<void>((resolve) => resolve());
});
fs.readFile.mockImplementation((filename, encoding) => {
if (filename.endsWith('.yarnrc')) {
return new Promise<string>((resolve) => resolve(yarnrcContents));
}
return new Promise<string>((resolve) => resolve(''));
});
const { offlineMirror, yarnPath } = await _yarnHelper.checkYarnrc(
'/tmp/renovate'
);
expect(offlineMirror).toBeFalse();
expect(yarnPath).toBeNull();
expect(yarnrcContents).not.toContain('yarn-path');
});
}); });
}); });

View file

@ -11,7 +11,7 @@ import { id as npmId } from '../../../datasource/npm';
import { logger } from '../../../logger'; import { logger } from '../../../logger';
import { ExternalHostError } from '../../../types/errors/external-host-error'; import { ExternalHostError } from '../../../types/errors/external-host-error';
import { ExecOptions, exec } from '../../../util/exec'; import { ExecOptions, exec } from '../../../util/exec';
import { readFile, remove } from '../../../util/fs'; import { exists, readFile, remove, writeFile } from '../../../util/fs';
import { regEx } from '../../../util/regex'; import { regEx } from '../../../util/regex';
import type { PostUpdateConfig, Upgrade } from '../../types'; import type { PostUpdateConfig, Upgrade } from '../../types';
import { getNodeConstraint } from './node-version'; import { getNodeConstraint } from './node-version';
@ -35,6 +35,15 @@ export async function checkYarnrc(
if (pathLine) { if (pathLine) {
yarnPath = pathLine.replace(regEx(/^yarn-path\s+"?(.+?)"?$/), '$1'); yarnPath = pathLine.replace(regEx(/^yarn-path\s+"?(.+?)"?$/), '$1');
} }
const yarnBinaryExists = await exists(yarnPath);
if (!yarnBinaryExists) {
const scrubbedYarnrc = yarnrc.replace(
regEx(/^yarn-path\s+"?.+?"?$/gm),
''
);
await writeFile(`${cwd}/.yarnrc`, scrubbedYarnrc);
yarnPath = null;
}
} }
} catch (err) /* istanbul ignore next */ { } catch (err) /* istanbul ignore next */ {
// not found // not found