Files
renovate/lib/modules/manager/apm/artifacts.spec.ts
Sebastian Poxhofer bf5ff94d28 refactor(manager): build artifact results with the shared helpers (#46092)
Migrate apm, bun, cargo, cocoapods, composer, conan, copier, deno,
devbox, gomod, helmv3, jsonnet-bundler, kustomize, mix, nuget and vendir
onto `fileAddition()`, `artifactError()` and `artifactErrorResult()`.
copier's private `artifactError()` is replaced by the shared one.

Behaviour change: artifact error text now comes from
`artifactErrorMessageFromExecError` everywhere, i.e. a non-blank
`err.stderr`, else a non-blank `err.stdout`, else `err.message`.
Previously apm used `${err}` ("Error: " prefix), cocoapods used a
nullish coalesce that kept an empty stderr, nuget preferred stdout over
stderr, and jsonnet-bundler emitted an undefined stderr with no
fallback. The rest used `err.message` only and are unchanged for errors
without output.
2026-09-16 13:13:41 +00:00

319 lines
11 KiB
TypeScript

import upath from 'upath';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { mockDeep } from 'vitest-mock-extended';
import { envMock, mockExecAll } from '~test/exec-util.ts';
import { env, fs, git, partial } from '~test/util.ts';
import { GlobalConfig } from '../../../config/global.ts';
import type {
InternalGlobalConfigOptions,
RepoGlobalConfig,
} from '../../../config/types.ts';
import { TEMPORARY_ERROR } from '../../../constants/error-messages.ts';
import * as docker from '../../../util/exec/docker/index.ts';
import type { StatusResult } from '../../../util/git/types.ts';
import * as _datasource from '../../datasource/index.ts';
import type { UpdateArtifactsConfig } from '../types.ts';
import { updateArtifacts } from './index.ts';
vi.mock('../../../util/exec/env.ts');
vi.mock('../../../util/fs/index.ts');
vi.mock('../../datasource/index.ts', () => mockDeep());
process.env.CONTAINERBASE = 'true';
const datasource = vi.mocked(_datasource);
const adminConfig: RepoGlobalConfig & InternalGlobalConfigOptions = {
localDir: upath.join('/tmp/github/some/repo'),
cacheDir: upath.join('/tmp/cache'),
containerbaseDir: upath.join('/tmp/cache/containerbase'),
dockerSidecarImage: 'ghcr.io/renovatebot/base-image',
binarySource: 'global',
};
const config: UpdateArtifactsConfig = {};
/** A `getRepoStatus` result with only the fields the manager reads. */
function repoStatus(status: Partial<StatusResult> = {}): StatusResult {
return partial<StatusResult>({
modified: [],
not_added: [],
deleted: [],
...status,
});
}
describe('modules/manager/apm/artifacts', () => {
describe('updateArtifacts', () => {
beforeEach(() => {
env.getChildProcessEnv.mockReturnValue(envMock.basic);
GlobalConfig.set(adminConfig);
docker.resetPrefetchedImages();
git.getRepoStatus.mockResolvedValue(repoStatus());
});
it('returns null if no updated deps and no lock file maintenance', async () => {
const execSnapshots = mockExecAll();
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [],
newPackageFileContent: '',
config,
}),
).resolves.toBeNull();
expect(execSnapshots).toEqual([]);
});
it('returns null if no lock file found', async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: '',
config,
}),
).resolves.toBeNull();
expect(execSnapshots).toEqual([]);
});
it('returns null if nothing changed after install', async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Current apm.lock.yaml');
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config,
}),
).resolves.toBeNull();
expect(execSnapshots).toMatchObject([{ cmd: 'apm install' }]);
});
it('returns the updated lockfile', async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
git.getRepoStatus.mockResolvedValueOnce(
repoStatus({ modified: ['apm.lock.yaml'] }),
);
fs.readLocalFile.mockResolvedValueOnce('New apm.lock.yaml');
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'apm.lock.yaml',
contents: 'New apm.lock.yaml',
},
},
]);
expect(execSnapshots).toMatchObject([{ cmd: 'apm install' }]);
});
it('returns regenerated harness files alongside the lockfile', async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
// `apm install` on a `targets: [claude, copilot]` manifest rewrites the
// lockfile and the deployed harness files, and removes a stale one.
git.getRepoStatus.mockResolvedValueOnce(
repoStatus({
modified: [
'apm.yml',
'apm.lock.yaml',
'.github/copilot-instructions.md',
],
not_added: ['.claude/commands/review.md'],
deleted: ['.claude/commands/old.md'],
}),
);
fs.readLocalFile.mockResolvedValueOnce('New apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('# copilot');
fs.readLocalFile.mockResolvedValueOnce('# review');
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config,
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'apm.lock.yaml',
contents: 'New apm.lock.yaml',
},
},
{
file: {
type: 'addition',
path: '.github/copilot-instructions.md',
contents: '# copilot',
},
},
{
file: {
type: 'addition',
path: '.claude/commands/review.md',
contents: '# review',
},
},
{
file: { type: 'deletion', path: '.claude/commands/old.md' },
},
]);
expect(execSnapshots).toMatchObject([{ cmd: 'apm install' }]);
});
it('deletes lock file on lockFileMaintenance', async () => {
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
git.getRepoStatus.mockResolvedValueOnce(
repoStatus({ modified: ['apm.lock.yaml'] }),
);
fs.readLocalFile.mockResolvedValueOnce('New apm.lock.yaml');
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [],
newPackageFileContent: 'new',
config: { ...config, isLockFileMaintenance: true },
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'apm.lock.yaml',
contents: 'New apm.lock.yaml',
},
},
]);
expect(fs.deleteLocalFile).toHaveBeenCalledWith('apm.lock.yaml');
expect(execSnapshots).toMatchObject([{ cmd: 'apm install' }]);
});
it('supports docker mode with tool constraint', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
git.getRepoStatus.mockResolvedValueOnce(
repoStatus({ modified: ['apm.lock.yaml'] }),
);
fs.readLocalFile.mockResolvedValueOnce('New apm.lock.yaml');
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '0.1.0' }, { version: '0.2.0' }],
});
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config: { ...config, constraints: { apm: '0.2.0' } },
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'apm.lock.yaml',
contents: 'New apm.lock.yaml',
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: 'docker pull ghcr.io/renovatebot/base-image' },
{ cmd: 'docker ps --filter name=renovate_sidecar -aq' },
{
cmd:
'docker run --rm --name=renovate_sidecar --label=renovate_child ' +
'-v "/tmp/github/some/repo":"/tmp/github/some/repo" ' +
'-v "/tmp/cache":"/tmp/cache" ' +
'-e CI ' +
'-e CONTAINERBASE_CACHE_DIR ' +
'-w "/tmp/github/some/repo" ' +
'ghcr.io/renovatebot/base-image ' +
"bash -l -c '" +
'install-tool apm 0.2.0 ' +
'&& ' +
'apm install' +
"'",
},
]);
});
it('falls back to the extracted apm constraint', async () => {
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
const execSnapshots = mockExecAll();
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
git.getRepoStatus.mockResolvedValueOnce(
repoStatus({ modified: ['apm.lock.yaml'] }),
);
fs.readLocalFile.mockResolvedValueOnce('New apm.lock.yaml');
datasource.getPkgReleases.mockResolvedValueOnce({
releases: [{ version: '0.1.0' }, { version: '0.2.0' }],
});
await updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config: { ...config, extractedConstraints: { apm: '0.2.0' } },
});
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool apm 0.2.0' },
{ cmd: 'apm install' },
]);
});
it('rethrows TEMPORARY_ERROR', async () => {
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
fs.writeLocalFile.mockRejectedValueOnce(new Error(TEMPORARY_ERROR));
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config,
}),
).rejects.toThrow(TEMPORARY_ERROR);
});
it('returns artifactError on failure', async () => {
fs.getSiblingFileName.mockReturnValueOnce('apm.lock.yaml');
fs.readLocalFile.mockResolvedValueOnce('Old apm.lock.yaml');
fs.writeLocalFile.mockRejectedValueOnce(new Error('write failed'));
await expect(
updateArtifacts({
packageFileName: 'apm.yml',
updatedDeps: [{ depName: 'owner/repo' }],
newPackageFileContent: 'new',
config,
}),
).resolves.toEqual([
{
artifactError: {
fileName: 'apm.lock.yaml',
stderr: 'write failed',
},
},
]);
});
});
});