Files
renovate/lib/modules/manager/devbox/artifacts.spec.ts
Sebastian Poxhofer 4b4d14ec00 feat(manager): pass extracted constraints to updateArtifacts (#45826)
* feat(manager): pass extracted constraints to updateArtifacts

`extractedConstraints` were collected during extraction but only ever read
by `fetch.ts` for the lookup. At artifact time every manager had to derive
the same tool constraints a second time, and when the updated package file
no longer carried the constraint there was nothing to fall back to.

`generateBranchConfig()` now merges the `extractedConstraints` of all
upgrades of a branch onto the branch config, which reaches `updateArtifacts()`
through `patchConfigForArtifactsUpdate()`. `UpdateArtifactsConfig` declares
the field and documents what it contains.

The new `resolveToolConstraint()` helper implements the ladder once:
user `constraints` > an optional artifact-time derivation > the extracted
value. The derivation is ranked above the extracted value on purpose: it
reads the package files as the branch changed them, so poetry (python is an
updatable dependency there), composer, pipenv (`pipenv` can be a dev
package) and pep621 (`requires-python` is an updatable dependency) would
otherwise regress to a stale constraint. pipenv, pep621 uv/pdm, poetry and
composer are migrated to the helper and keep their derivation.

Behaviour change: these managers now use the extract-time constraint as a
last resort instead of running unconstrained, for example on lock file
maintenance where pipenv deletes `Pipfile.lock` before deriving.

`execute-post-upgrade-commands.ts` is unaffected: it reads
`upgrade.constraints` per upgrade, and the extracted values are kept in a
separate field rather than folded into `constraints`, so post-upgrade task
tool constraints still come from user config only.

Not migrated, because their constraints are never extracted, so the
fallback would be dead code: bundler (`ruby`/`bundler`), pixi (`pixi`),
pep621 uv (`required-version`) and npm's node ladder, whose last step
already reads `package.json` engines.

* refactor(manager): resolve tool constraints through resolveToolConstraint()

Every manager now obtains the constraint of a tool it runs through
`resolveToolConstraint()` instead of reading `config.constraints` itself, so
the precedence "user config, then a value derived from the updated files, then
the constraints collected during extraction" applies everywhere. The helper
treats an empty string as not set at every step and accepts the
`PostUpdateConfig` shape, whose `constraints` may be `null`.

Behaviour changes that go beyond the new fallback to extracted constraints:

- `mix` applies its hardcoded erlang default only after the extracted
  constraint, so an erlang version collected at extraction can win over it.
- `kustomize` decides `HELM_EXPERIMENTAL_OCI` from the fully resolved helm
  constraint; `generateHelmEnvs()` takes that value instead of the config.
- `npm` (yarn): an upgrade of yarn without a new value now consults the user
  constraint before deriving one from package.json or the lock file.
- The "Using <tool> constraint from config" debug lines in bundler, composer,
  copier and pip-compile are gone, as the helper is the only reader of the
  config now.

Helpers that read the config synchronously became async: bundler
`getBundlerConstraint()`, copier `getPythonVersionConstraint()` and
`getCopierVersionConstraint()`, pip-compile `get*VersionConstraint()` and
`getToolVersionConstraint()`, gomod `deriveGoToolchainConstraints()` and
`getUpdateImportPathCmds()`, mise `getMiseLockToolConstraints()`. composer
`getPhpConstraint()` is removed.

Each converted manager has a test showing the extracted constraint reaching
the executed tool, and, where a derive callback exists, that the derived value
still wins over it.

* refactor(linters): add prefer-resolve-tool-constraint rule

Reports member reads and destructuring of `constraints` and
`extractedConstraints` from a manager config in `lib/modules/manager/**`, so
that tool constraints are resolved through `resolveToolConstraint()` in
`lib/modules/manager/util.ts` only. Writes and the same properties on package
files, which extraction has to populate, are not reported. The helper itself
and spec files are exempt in `.oxlintrc.json`.

* refactor(manager): inline thin resolveToolConstraint wrappers

Drop copier/utils.ts and the pip-compile getPipToolsVersionConstraint /
getUvVersionConstraint helpers, which only forwarded to
resolveToolConstraint(). Callers now call it directly.

* docs(development): note lock file maintenance in tool constraints section

* refactor(manager): address review on tool constraint resolution

composer only runs extractConstraints() when a user constraint is missing,
via Lazy. UpdateArtifactsConfig no longer re-declares constraints and
extractedConstraints, which ToolConstraintsConfig already provides.
2026-09-14 11:09:07 +00:00

515 lines
16 KiB
TypeScript

import { codeBlock } from 'common-tags';
import { mockExecAll } from '~test/exec-util.ts';
import { fs, git, partial } from '~test/util.ts';
import { GlobalConfig } from '../../../config/global.ts';
import type {
InternalGlobalConfigOptions,
RepoGlobalConfig,
} from '../../../config/types.ts';
import type { StatusResult } from '../../../util/git/types.ts';
import type { UpdateArtifact } from '../types.ts';
import { updateArtifacts } from './artifacts.ts';
vi.mock('../../../util/exec/env.ts');
vi.mock('../../../util/fs/index.ts');
const globalConfig: RepoGlobalConfig & InternalGlobalConfigOptions = {
localDir: '',
binarySource: 'global',
};
const devboxJson = codeBlock`
{
"$schema": "https://raw.githubusercontent.com/jetpack-io/devbox/0.10.1/.schema/devbox.schema.json",
"packages": ["nodejs@20", "metabase@0.49.1", "postgresql@latest", "gh@latest"],
}
`;
describe('modules/manager/devbox/artifacts', () => {
describe('updateArtifacts()', () => {
let updateArtifact: UpdateArtifact;
beforeEach(() => {
GlobalConfig.set(globalConfig);
updateArtifact = {
config: {},
newPackageFileContent: '',
packageFileName: '',
updatedDeps: [],
};
});
it('skips if no updatedDeps and no lockFileMaintenance', async () => {
await expect(updateArtifacts(updateArtifact)).resolves.toBeNull();
});
it('skips if no lock file in config', async () => {
updateArtifact.updatedDeps = [{}];
await expect(updateArtifacts(updateArtifact)).resolves.toBeNull();
});
it('skips if cannot read lock file', async () => {
updateArtifact.updatedDeps = [
{ manager: 'devbox', lockFiles: ['devbox.lock'] },
];
await expect(updateArtifacts(updateArtifact)).resolves.toBeNull();
});
it('returns installed devbox.lock', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
const oldLockFileContent = Buffer.from('Old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {},
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'devbox.lock',
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'devbox update nodejs --no-install',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
]);
});
it('calls install instead of update --no-install if an older version of devbox is constrained', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
const oldLockFileContent = Buffer.from('Old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {
constraints: {
devbox: '0.13.0',
},
},
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'devbox.lock',
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'devbox install',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
]);
});
it('falls back to the extracted devbox constraint', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
fs.readLocalFile.mockResolvedValueOnce(
Buffer.from('Old devbox.lock') as never,
);
fs.readLocalFile.mockResolvedValueOnce(
Buffer.from('New devbox.lock') as never,
);
await updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {
extractedConstraints: {
devbox: '0.13.0',
},
},
});
// the extracted constraint decides that `--no-install` is unsupported
expect(execSnapshots).toMatchObject([{ cmd: 'devbox install' }]);
});
it('returns installed devbox.lock with multiple updated deps', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
const oldLockFileContent = Buffer.from('Old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'ruby',
},
],
config: {},
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'devbox.lock',
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'devbox update nodejs --no-install',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
{
cmd: 'devbox update ruby --no-install',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
]);
});
it('returns null if no updatedDeps are passed', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const oldLockFileContent = Buffer.from('Old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [{}],
config: {},
}),
).resolves.toBeNull();
});
it('returns null if no updatedDeps have depNames', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce('{}');
const oldLockFileContent = Buffer.from('Old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
},
],
config: {},
}),
).resolves.toBeNull();
});
it('returns updated devbox.lock', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['devbox.lock'],
}),
);
const oldLockFileContent = Buffer.from('old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [{}],
config: {
isLockFileMaintenance: true,
},
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'devbox.lock',
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'devbox update --no-install',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
]);
});
it('calls update without --no-install flag if an older version of devbox is being used', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
const execSnapshots = mockExecAll();
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: ['devbox.lock'],
}),
);
const oldLockFileContent = Buffer.from('old devbox.lock');
const newLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [{}],
config: {
isLockFileMaintenance: true,
constraints: {
devbox: '< 0.14.0',
},
},
}),
).resolves.toEqual([
{
file: {
type: 'addition',
path: 'devbox.lock',
contents: newLockFileContent,
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'devbox update',
options: {
cwd: '.',
env: {},
maxBuffer: 10485760,
timeout: 900000,
stdin: 'pipe',
stdout: 'pipe',
stderr: 'pipe',
},
},
]);
});
it('returns null if no changes are found', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: [],
}),
);
mockExecAll();
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [],
config: {},
}),
).resolves.toBeNull();
});
it('returns null if devbox.lock not found after update', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: [],
}),
);
mockExecAll();
const oldLockFileContent = Buffer.from('Old devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {},
}),
).resolves.toBeNull();
});
it('returns null if devbox.lock not found', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: [],
}),
);
mockExecAll();
fs.readLocalFile.mockResolvedValueOnce(null);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {},
}),
).resolves.toBeNull();
});
it('returns null if no lock file changes are found', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(`{}`);
git.getRepoStatus.mockResolvedValueOnce(
partial<StatusResult>({
modified: [],
}),
);
mockExecAll();
const oldLockFileContent = Buffer.from('Old devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {},
}),
).resolves.toBeNull();
});
it('returns an artifact error on failure', async () => {
fs.getSiblingFileName.mockReturnValueOnce('devbox.lock');
const newLockFileContent = `{}`;
const oldLockFileContent = Buffer.from('New devbox.lock');
fs.readLocalFile.mockResolvedValueOnce(oldLockFileContent as never);
fs.readLocalFile.mockResolvedValueOnce(newLockFileContent);
await expect(
updateArtifacts({
packageFileName: 'devbox.json',
newPackageFileContent: devboxJson,
updatedDeps: [
{
manager: 'devbox',
lockFiles: ['devbox.lock'],
depName: 'nodejs',
},
],
config: {},
}),
).resolves.toEqual([
{
artifactError: {
fileName: 'devbox.lock',
stderr: "Cannot read properties of undefined (reading 'stdout')",
},
},
]);
});
});
});