Skip to content

Commit

Permalink
feat(manager/npm): support buildpack
Browse files Browse the repository at this point in the history
  • Loading branch information
viceice committed Aug 5, 2022
1 parent 4b0b49e commit e90cdd6
Show file tree
Hide file tree
Showing 8 changed files with 155 additions and 40 deletions.
100 changes: 98 additions & 2 deletions lib/modules/manager/npm/post-update/lerna.spec.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { envMock, mockExecAll } from '../../../../../test/exec-util';
import { env, partial } from '../../../../../test/util';
import { env, logger, mockedFunction, partial } from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import type { RepoGlobalConfig } from '../../../../config/types';
import type { PackageFile, PostUpdateConfig } from '../../types';
import * as lernaHelper from './lerna';
import { getNodeToolConstraint } from './node-version';

jest.mock('../../../../util/exec/env');
jest.mock('../../npm/post-update/node-version');

process.env.BUILDPACK = 'true';

function lernaPkgFile(lernaClient: string): Partial<PackageFile> {
return {
lernaClient,
Expand All @@ -23,17 +26,29 @@ function lernaPkgFileWithoutLernaDep(
};
}

mockedFunction(logger.logger.debug).mockImplementation((...args) =>
// eslint-disable-next-line no-console
console.log(...args)
);

const config = partial<PostUpdateConfig>({});

describe('modules/manager/npm/post-update/lerna', () => {
const globalConfig: RepoGlobalConfig = { localDir: '' };
const globalConfig: RepoGlobalConfig = {
localDir: '',
cacheDir: '/tmp/cache',
};

describe('generateLockFiles()', () => {
beforeEach(() => {
jest.resetAllMocks();
jest.resetModules();
env.getChildProcessEnv.mockReturnValue(envMock.basic);
GlobalConfig.set(globalConfig);
mockedFunction(getNodeToolConstraint).mockResolvedValueOnce({
toolName: 'node',
constraint: '16.16.0',
});
});

it('returns if no lernaClient', async () => {
Expand Down Expand Up @@ -120,6 +135,87 @@ describe('modules/manager/npm/post-update/lerna', () => {
expect(res.error).toBeFalse();
expect(execSnapshots).toMatchSnapshot();
});

it('suppports docker', async () => {
const execSnapshots = mockExecAll();
GlobalConfig.set({ ...globalConfig, binarySource: 'docker' });
const res = await lernaHelper.generateLockFiles(
lernaPkgFile('npm'),
'some-dir',
{ ...config, constraints: { npm: '^6.0.0' } },
{}
);
expect(execSnapshots).toMatchObject([
{
cmd: 'docker pull renovate/sidecar',
},
{
cmd: 'docker ps --filter name=renovate_sidecar -aq',
},
{
cmd:
'docker run --rm --name=renovate_sidecar --label=renovate_child ' +
'-v "/tmp/cache":"/tmp/cache" ' +
'-e BUILDPACK_CACHE_DIR ' +
'-w "some-dir" renovate/sidecar ' +
'bash -l -c "' +
'install-tool node 16.16.0 ' +
'&& ' +
"npm i -g npm@'^6.0.0' || true " +
'&& ' +
'hash -d npm 2>/dev/null || true ' +
'&& npm i -g [email protected] ' +
'&& lerna info || echo \\"Ignoring lerna info failure\\" ' +
'&& ' +
'npm install --ignore-scripts --no-audit --package-lock-only ' +
'&& ' +
'lerna bootstrap --no-ci --ignore-scripts -- --ignore-scripts --no-audit --package-lock-only' +
'"',
options: {
cwd: 'some-dir',
},
},
]);
expect(res.error).toBeFalse();
});

it('suppports binarySource=install', async () => {
const execSnapshots = mockExecAll();
GlobalConfig.set({ ...globalConfig, binarySource: 'install' });
const res = await lernaHelper.generateLockFiles(
lernaPkgFile('npm'),
'some-dir',
{ ...config, constraints: { npm: '^6.0.0' } },
{}
);
expect(res.error).toBeFalse();
expect(execSnapshots).toMatchObject([
{
cmd: 'install-tool node 16.16.0',
options: {
cwd: 'some-dir',
},
},
{
cmd: 'lerna info || echo "Ignoring lerna info failure"',
options: {
cwd: 'some-dir',
},
},
{
cmd: 'npm install --ignore-scripts --no-audit --package-lock-only',
options: {
cwd: 'some-dir',
},
},
{
cmd: 'lerna bootstrap --no-ci --ignore-scripts -- --ignore-scripts --no-audit --package-lock-only',
options: {
cwd: 'some-dir',
},
},
]);
});
});

describe('getLernaVersion()', () => {
Expand Down
8 changes: 3 additions & 5 deletions lib/modules/manager/npm/post-update/lerna.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { logger } from '../../../../logger';
import { exec } from '../../../../util/exec';
import type { ExecOptions, ExtraEnv } from '../../../../util/exec/types';
import type { PackageFile, PostUpdateConfig } from '../../types';
import { getNodeConstraint } from './node-version';
import { getNodeToolConstraint } from './node-version';
import type { GenerateLockFileResult } from './types';
import { getOptimizeCommand } from './yarn';

Expand Down Expand Up @@ -77,7 +77,6 @@ export async function generateLockFiles(
lernaCommand = lernaCommand.replace('--ignore-scripts ', '');
}
lernaCommand += cmdOptions;
const tagConstraint = await getNodeConstraint(config);
const extraEnv: ExtraEnv = {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
Expand All @@ -86,11 +85,10 @@ export async function generateLockFiles(
cwdFile: upath.join(lockFileDir, 'package.json'),
extraEnv,
docker: {
image: 'node',
tagScheme: 'node',
tagConstraint,
image: 'sidecar',
},
preCommands,
toolConstraints: [await getNodeToolConstraint(config, [])],
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
3 changes: 3 additions & 0 deletions lib/modules/manager/npm/post-update/npm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,8 @@ describe('modules/manager/npm/post-update/npm', () => {
'-w "some-dir" ' +
'renovate/node ' +
'bash -l -c "' +
'install-tool node 16.16.0 ' +
'&& ' +
'install-tool npm 6.0.0 ' +
'&& ' +
'hash -d npm 2>/dev/null || true ' +
Expand Down Expand Up @@ -296,6 +298,7 @@ describe('modules/manager/npm/post-update/npm', () => {
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool node 16.16.0' },
{ cmd: 'install-tool npm 6.0.0' },
{ cmd: 'hash -d npm 2>/dev/null || true' },
{
Expand Down
13 changes: 6 additions & 7 deletions lib/modules/manager/npm/post-update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {
} from '../../../../util/fs';
import type { PostUpdateConfig, Upgrade } from '../../types';
import { composeLockFile, parseLockFile } from '../utils';
import { getNodeConstraint, getNodeUpdate } from './node-version';
import { getNodeToolConstraint } from './node-version';
import type { GenerateLockFileResult } from './types';

export async function generateLockFile(
Expand Down Expand Up @@ -55,20 +55,19 @@ export async function generateLockFile(
cmdOptions += ' --ignore-scripts';
}

const tagConstraint =
getNodeUpdate(upgrades) ?? (await getNodeConstraint(config));
const extraEnv: ExtraEnv = {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
};
const execOptions: ExecOptions = {
cwdFile: lockFileName,
extraEnv,
toolConstraints: [npmToolConstraint],
toolConstraints: [
await getNodeToolConstraint(config, upgrades),
npmToolConstraint,
],
docker: {
image: 'node',
tagScheme: 'node',
tagConstraint,
image: 'sidecar',
},
};
// istanbul ignore if
Expand Down
27 changes: 19 additions & 8 deletions lib/modules/manager/npm/post-update/pnpm.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
import { envMock, mockExecAll } from '../../../../../test/exec-util';
import { Fixtures } from '../../../../../test/fixtures';
import { env, fs, partial } from '../../../../../test/util';
import {
env,
fs,
mockedFunction,
partial,
} from '../../../../../test/util';
import { GlobalConfig } from '../../../../config/global';
import type { PostUpdateConfig } from '../../types';
import { getNodeToolConstraint } from './node-version';
import * as pnpmHelper from './pnpm';

jest.mock('../../../../util/exec/env');
Expand All @@ -20,6 +26,10 @@ describe('modules/manager/npm/post-update/pnpm', () => {
config = partial<PostUpdateConfig>({ constraints: { pnpm: '^2.0.0' } });
env.getChildProcessEnv.mockReturnValue(envMock.basic);
GlobalConfig.set({ localDir: '' });
mockedFunction(getNodeToolConstraint).mockResolvedValueOnce({
toolName: 'node',
constraint: '16.16.0',
});
});

it('generates lock files', async () => {
Expand Down Expand Up @@ -197,19 +207,19 @@ describe('modules/manager/npm/post-update/pnpm', () => {
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchObject([
{ cmd: 'docker pull renovate/node' },
{ cmd: 'docker ps --filter name=renovate_node -aq' },
{ cmd: 'docker pull renovate/sidecar' },
{ cmd: 'docker ps --filter name=renovate_sidecar -aq' },
{
cmd:
'docker run --rm --name=renovate_node --label=renovate_child ' +
'docker run --rm --name=renovate_sidecar --label=renovate_child ' +
'-v "/tmp":"/tmp" ' +
'-e BUILDPACK_CACHE_DIR ' +
'-w "some-dir" ' +
'renovate/node ' +
'renovate/sidecar ' +
'bash -l -c "' +
'install-tool pnpm 6.0.0 ' +
'&& ' +
'pnpm install --recursive --lockfile-only' +
'install-tool node 16.16.0 ' +
'&& install-tool pnpm 6.0.0 ' +
'&& pnpm install --recursive --lockfile-only' +
'"',
},
]);
Expand All @@ -231,6 +241,7 @@ describe('modules/manager/npm/post-update/pnpm', () => {
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool node 16.16.0' },
{ cmd: 'install-tool pnpm 6.0.0' },
{
cmd: 'pnpm install --recursive --lockfile-only --ignore-scripts --ignore-pnpmfile',
Expand Down
14 changes: 7 additions & 7 deletions lib/modules/manager/npm/post-update/pnpm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import type {
import { deleteLocalFile, readLocalFile } from '../../../../util/fs';
import type { PostUpdateConfig, Upgrade } from '../../types';
import type { NpmPackage } from '../extract/types';
import { getNodeConstraint, getNodeUpdate } from './node-version';
import { getNodeToolConstraint } from './node-version';
import type { GenerateLockFileResult, PnpmLockFile } from './types';

export async function generateLockFile(
Expand All @@ -34,8 +34,7 @@ export async function generateLockFile(
constraint:
config.constraints?.pnpm ?? (await getPnpmConstraint(lockFileDir)),
};
const tagConstraint =
getNodeUpdate(upgrades) ?? (await getNodeConstraint(config));

const extraEnv: ExtraEnv = {
NPM_CONFIG_CACHE: env.NPM_CONFIG_CACHE,
npm_config_store: env.npm_config_store,
Expand All @@ -44,11 +43,12 @@ export async function generateLockFile(
cwdFile: lockFileName,
extraEnv,
docker: {
image: 'node',
tagScheme: 'node',
tagConstraint,
image: 'sidecar',
},
toolConstraints: [pnpmToolConstraint],
toolConstraints: [
await getNodeToolConstraint(config, upgrades),
pnpmToolConstraint,
],
};
// istanbul ignore if
if (GlobalConfig.get('exposeAllEnv')) {
Expand Down
17 changes: 13 additions & 4 deletions lib/modules/manager/npm/post-update/yarn.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import * as docker from '../../../../util/exec/docker';
import { getPkgReleases } from '../../../datasource';
import type { PostUpdateConfig } from '../../types';
import type { NpmManagerData } from '../types';
import { getNodeToolContraint } from './node-version';
import * as yarnHelper from './yarn';

jest.mock('fs-extra', () =>
Expand Down Expand Up @@ -41,8 +42,12 @@ describe('modules/manager/npm/post-update/yarn', () => {
delete process.env.BUILDPACK;
jest.clearAllMocks();
Fixtures.reset();
docker.resetPrefetchedImages();
GlobalConfig.set({ localDir: '.', cacheDir: '/tmp/cache' });
docker.resetPrefetchedImages();
mockedFunction(getNodeToolContraint).mockResolvedValueOnce({
toolName: 'node',
constraint: '16.16.0',
});
});

it.each([
Expand Down Expand Up @@ -380,8 +385,8 @@ describe('modules/manager/npm/post-update/yarn', () => {
},
});
const res = await yarnHelper.generateLockFile('some-dir', {}, config);
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool node 16.16.0', options: { cwd: 'some-dir' } },
{ cmd: 'install-tool corepack 0.10.0', options: { cwd: 'some-dir' } },
{
cmd: 'yarn install --mode=update-lockfile',
Expand All @@ -395,6 +400,7 @@ describe('modules/manager/npm/post-update/yarn', () => {
},
},
]);
expect(res.lockFile).toBe('package-lock-contents');
});

it('uses slim yarn instead of corepack', async () => {
Expand Down Expand Up @@ -427,6 +433,7 @@ describe('modules/manager/npm/post-update/yarn', () => {
const res = await yarnHelper.generateLockFile('some-dir', {}, config);
expect(res.lockFile).toBe(plocktest1YarnLockV1);
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool node 16.16.0', options: { cwd: 'some-dir' } },
{ cmd: 'install-tool yarn-slim 1.22.18', options: { cwd: 'some-dir' } },
{
cmd: 'yarn install --ignore-engines --ignore-platform --network-timeout 100000 --ignore-scripts',
Expand Down Expand Up @@ -500,11 +507,13 @@ describe('modules/manager/npm/post-update/yarn', () => {
expect(res.lockFile).toBe(plocktest1YarnLockV1);
const options = { encoding: 'utf-8' };
expect(execSnapshots).toMatchObject([
{ cmd: 'docker pull renovate/node', options },
{ cmd: 'docker pull renovate/sidecar', options },
{
cmd:
`docker run --rm --name=renovate_node --label=renovate_child -v ".":"." -v "/tmp/cache":"/tmp/cache" -e CI -e BUILDPACK_CACHE_DIR -w "some-dir" renovate/node ` +
`docker run --rm --name=renovate_sidecar --label=renovate_child -v ".":"." -v "/tmp/cache":"/tmp/cache" -e CI -e BUILDPACK_CACHE_DIR -w "some-dir" renovate/sidecar ` +
`bash -l -c "` +
`install-tool node 16.16.0` +
` && ` +
`install-tool yarn-slim 1.22.18` +
` && ` +
`sed -i 's/ steps,/ steps.slice(0,1),/' some-dir/.yarn/cli.js || true` +
Expand Down
Loading

0 comments on commit e90cdd6

Please sign in to comment.