Files
renovate/lib/modules/manager/cargo/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

998 lines
31 KiB
TypeScript

import type { Stats } from 'node:fs';
import upath from 'upath';
import { envMock, mockExecAll, mockExecSequence } from '~test/exec-util.ts';
import { hostRules } from '~test/host-rules.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 * as docker from '../../../util/exec/docker/index.ts';
import { ExecError } from '../../../util/exec/exec-error.ts';
import { CrateDatasource } from '../../datasource/crate/index.ts';
import type { UpdateArtifactsConfig } from '../types.ts';
import * as cargo from './index.ts';
vi.mock('../../../util/exec/env.ts');
vi.mock('../../../util/http/index.ts');
vi.mock('../../../util/fs/index.ts');
process.env.CONTAINERBASE = 'true';
const config: UpdateArtifactsConfig = {};
const adminConfig: RepoGlobalConfig & InternalGlobalConfigOptions = {
// `join` fixes Windows CI
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',
};
describe('modules/manager/cargo/artifacts', () => {
beforeEach(() => {
env.getChildProcessEnv.mockReturnValue(envMock.basic);
GlobalConfig.set(adminConfig);
docker.resetPrefetchedImages();
});
afterEach(() => {
GlobalConfig.reset();
});
it('returns null if no Cargo.lock found', async () => {
fs.statLocalFile.mockRejectedValue(new Error('not found!'));
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '',
config,
}),
).resolves.toBeNull();
});
it('returns null if updatedDeps is empty', async () => {
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps: [],
newPackageFileContent: '',
config,
}),
).resolves.toBeNull();
});
it('returns null if unchanged', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '',
config,
}),
).resolves.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace',
},
]);
});
it('returns updated Cargo.lock', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.not.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace',
},
]);
});
it('returns updated Cargo.lock with precise version update', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
packageName: 'dep1',
lockedVersion: '1.0.0',
newVersion: '1.0.1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{ file: { contents: undefined, path: 'Cargo.lock', type: 'addition' } },
]);
expect(execSnapshots).toMatchObject([
{
cmd:
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --package dep1@1.0.0 --precise 1.0.1',
},
{
cmd:
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --workspace',
},
]);
});
it('skips precise update when manifest range has changed', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
const updatedDeps = [
{
depName: 'pprof',
packageName: 'pprof',
lockedVersion: '0.13.0',
currentValue: '0.13',
newVersion: '0.14.0',
newValue: '0.14',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{ file: { contents: undefined, path: 'Cargo.lock', type: 'addition' } },
]);
expect(execSnapshots).toMatchObject([
{
cmd:
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --workspace',
},
]);
});
it('handles mixed deps where some have range changes and some do not', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
const updatedDeps = [
{
depName: 'pprof',
packageName: 'pprof',
lockedVersion: '0.13.0',
currentValue: '0.13',
newVersion: '0.14.0',
newValue: '0.14',
datasource: CrateDatasource.id,
},
{
depName: 'serde',
packageName: 'serde',
lockedVersion: '1.0.0',
newVersion: '1.0.1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{ file: { contents: undefined, path: 'Cargo.lock', type: 'addition' } },
]);
expect(execSnapshots).toMatchObject([
{
cmd:
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --package serde@1.0.0 --precise 1.0.1',
},
{
cmd:
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --workspace',
},
]);
});
it('returns an artifact error when cargo update fails', async () => {
const cmd =
'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --package dep1@1.0.0 --precise 1.0.1';
const execError = new ExecError('Exec error', {
cmd,
stdout: '',
stderr: '',
options: {},
});
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll(execError);
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
packageName: 'dep1',
lockedVersion: '1.0.0',
newVersion: '1.0.1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{ artifactError: { fileName: 'Cargo.lock', stderr: 'Exec error' } },
]);
expect(execSnapshots).toMatchObject([{ cmd }]);
});
it('returns updated Cargo.lock when a preceding dependency triggers an update in a later dependency', async () => {
// Sometimes `cargo update` will fail when a preceding dependency update
// causes another dependency to update. In this case we can no longer
// reference the dependency by its old version, so we filter it out
// and retry recursively.
// mock updateArtifacts iter 1
const lockfileOrig = `
[[package]]
name = "dep1"
version = "1.0.0"
[[package]]
name = "dep2"
version = "1.0.0"
[[package]]
name = "dep3"
version = "1.0.0"
`;
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce(lockfileOrig);
const workspaceCmd =
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml --workspace';
const packageDep1Cmd =
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --package dep1@1.0.0 --precise 1.0.1';
const packageDep2Cmd =
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --package dep2@1.0.0 --precise 1.0.2';
const packageDep3Cmd =
'cargo update --config net.git-fetch-with-cli=true' +
' --manifest-path Cargo.toml' +
' --package dep3@1.0.0 --precise 1.0.3';
const execSnapshots = mockExecSequence([
// Iter 1: packageDep1Cmd (updates dep1 and dep2)
{ stdout: '', stderr: '' },
// Iter 1: packageDep2Cmd (fails to update dep2 - updated by previous command)
new ExecError('Exec error', {
cmd: packageDep2Cmd,
stdout: '',
stderr: '... error: package ID specification ...',
options: {},
}),
// Iter 2: packageDep3Cmd (updates dep3)
{ stdout: '', stderr: '' },
// Iter 2: workspaceCmd
{ stdout: '', stderr: '' },
]);
const lockfileAfterIter1 = `
[[package]]
name = "dep1"
version = "1.0.1"
[[package]]
name = "dep2"
version = "1.0.2"
[[package]]
name = "dep3"
version = "1.0.0"
`;
fs.readLocalFile.mockResolvedValueOnce(lockfileAfterIter1);
// mock updateArtifacts iter 2
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce(lockfileAfterIter1);
// run updateArtifacts
const updatedDeps = [
{
depName: 'dep1',
packageName: 'dep1',
lockedVersion: '1.0.0',
newVersion: '1.0.1',
datasource: CrateDatasource.id,
},
{
depName: 'dep2',
packageName: 'dep2',
lockedVersion: '1.0.0',
newVersion: '1.0.2',
datasource: CrateDatasource.id,
},
{
depName: 'dep3',
packageName: 'dep3',
lockedVersion: '1.0.0',
newVersion: '1.0.3',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{ file: { contents: undefined, path: 'Cargo.lock', type: 'addition' } },
]);
expect(execSnapshots.slice(0, 2)).toMatchObject([
{ cmd: packageDep1Cmd },
{ cmd: packageDep2Cmd },
]);
expect(execSnapshots.slice(2, 4)).toMatchObject([
{ cmd: packageDep3Cmd },
{ cmd: workspaceCmd },
]);
});
it('returns updated Cargo.lock when there are no more dependencies to update', async () => {
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps: [],
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{
file: {
contents: 'New Cargo.lock',
path: 'Cargo.lock',
type: 'addition',
},
},
]);
});
it('updates Cargo.lock based on the packageName, when given', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'renamedDep1',
packageName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.not.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace',
},
]);
});
it('returns updated workspace Cargo.lock', async () => {
fs.statLocalFile.mockRejectedValueOnce(
new Error('crates/one/Cargo.lock not found'),
);
fs.statLocalFile.mockRejectedValueOnce(
new Error('crates/Cargo.lock not found'),
);
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'crates/one/Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.not.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path crates/one/Cargo.toml --workspace',
},
]);
});
it('returns updated Cargo.lock for lockfile maintenance', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps: [],
newPackageFileContent: '{}',
config: {
...config,
isLockFileMaintenance: true,
constraints: { rust: '1.65.0' },
},
}),
).resolves.not.toBeNull();
expect(execSnapshots).toMatchObject([
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml',
},
]);
});
it('supports docker mode', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: 'docker pull ghcr.io/renovatebot/base-image' },
{},
{
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 rust 1.65.0' +
' && ' +
'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace' +
"'",
options: {
cwd: '/tmp/github/some/repo',
env: {
CONTAINERBASE_CACHE_DIR: '/tmp/cache/containerbase',
},
},
},
]);
});
it('supports docker mode with credentials', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
hostRules.add({
token: 'some-token',
hostType: 'github',
matchHost: 'api.github.com',
});
hostRules.add({
token: 'some-other-token',
matchHost: 'https://gitea.com',
});
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{ cmd: 'docker pull ghcr.io/renovatebot/base-image' },
{},
{
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 GIT_CONFIG_KEY_0 ' +
'-e GIT_CONFIG_VALUE_0 ' +
'-e GIT_CONFIG_KEY_1 ' +
'-e GIT_CONFIG_VALUE_1 ' +
'-e GIT_CONFIG_KEY_2 ' +
'-e GIT_CONFIG_VALUE_2 ' +
'-e GIT_CONFIG_COUNT ' +
'-e GIT_CONFIG_KEY_3 ' +
'-e GIT_CONFIG_VALUE_3 ' +
'-e GIT_CONFIG_KEY_4 ' +
'-e GIT_CONFIG_VALUE_4 ' +
'-e GIT_CONFIG_KEY_5 ' +
'-e GIT_CONFIG_VALUE_5 ' +
'-e CONTAINERBASE_CACHE_DIR ' +
'-w "/tmp/github/some/repo" ' +
'ghcr.io/renovatebot/base-image ' +
"bash -l -c '" +
'install-tool rust 1.65.0' +
' && ' +
'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace' +
"'",
options: {
cwd: '/tmp/github/some/repo',
env: {
CONTAINERBASE_CACHE_DIR: '/tmp/cache/containerbase',
GIT_CONFIG_COUNT: '6',
GIT_CONFIG_KEY_0:
'url.https://ssh:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_2: 'url.https://some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://ssh:some-other-token@gitea.com/.insteadOf',
GIT_CONFIG_KEY_4:
'url.https://git:some-other-token@gitea.com/.insteadOf',
GIT_CONFIG_KEY_5:
'url.https://some-other-token@gitea.com/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@github.com/',
GIT_CONFIG_VALUE_1: 'git@github.com:',
GIT_CONFIG_VALUE_2: 'https://github.com/',
GIT_CONFIG_VALUE_3: 'ssh://git@gitea.com/',
GIT_CONFIG_VALUE_4: 'git@gitea.com:',
GIT_CONFIG_VALUE_5: 'https://gitea.com/',
},
},
},
]);
});
it('supports docker mode with many credentials', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
hostRules.add({
token: 'some-token',
matchHost: 'api.github.com',
hostType: 'github',
});
hostRules.add({
token: 'some-enterprise-token',
matchHost: 'github.enterprise.com',
hostType: 'github',
});
hostRules.add({
token: 'some-gitlab-token',
matchHost: 'gitlab.enterprise.com',
hostType: 'gitlab',
});
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toEqual(
expect.arrayContaining([
expect.objectContaining({
options: expect.objectContaining({
env: expect.objectContaining({
GIT_CONFIG_COUNT: '9',
GIT_CONFIG_KEY_0:
'url.https://ssh:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_2: 'url.https://some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://ssh:some-enterprise-token@github.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_4:
'url.https://git:some-enterprise-token@github.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_5:
'url.https://some-enterprise-token@github.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_6:
'url.https://gitlab-ci-token:some-gitlab-token@gitlab.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_7:
'url.https://gitlab-ci-token:some-gitlab-token@gitlab.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_8:
'url.https://gitlab-ci-token:some-gitlab-token@gitlab.enterprise.com/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@github.com/',
GIT_CONFIG_VALUE_1: 'git@github.com:',
GIT_CONFIG_VALUE_2: 'https://github.com/',
GIT_CONFIG_VALUE_3: 'ssh://git@github.enterprise.com/',
GIT_CONFIG_VALUE_4: 'git@github.enterprise.com:',
GIT_CONFIG_VALUE_5: 'https://github.enterprise.com/',
GIT_CONFIG_VALUE_6: 'ssh://git@gitlab.enterprise.com/',
GIT_CONFIG_VALUE_7: 'git@gitlab.enterprise.com:',
GIT_CONFIG_VALUE_8: 'https://gitlab.enterprise.com/',
}),
}),
}),
]),
);
});
it('supports docker mode and ignores non git credentials', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
hostRules.add({
token: 'some-token',
matchHost: 'api.github.com',
hostType: 'github',
});
hostRules.add({
token: 'some-enterprise-token',
matchHost: 'github.enterprise.com',
hostType: 'npm',
});
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toEqual(
expect.arrayContaining([
expect.objectContaining({
options: expect.objectContaining({
env: expect.objectContaining({
GIT_CONFIG_COUNT: '3',
GIT_CONFIG_KEY_0:
'url.https://ssh:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_2: 'url.https://some-token@github.com/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@github.com/',
GIT_CONFIG_VALUE_1: 'git@github.com:',
GIT_CONFIG_VALUE_2: 'https://github.com/',
}),
}),
}),
]),
);
});
it('supports docker mode with Cargo specific credential', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'docker' });
hostRules.add({
token: 'some-token',
matchHost: 'api.github.com',
hostType: 'github',
});
hostRules.add({
token: 'some-enterprise-token-cargo',
matchHost: 'github.enterprise.com',
hostType: 'cargo',
});
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toEqual(
expect.arrayContaining([
expect.objectContaining({
options: expect.objectContaining({
env: expect.objectContaining({
GIT_CONFIG_COUNT: '6',
GIT_CONFIG_KEY_0:
'url.https://ssh:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_1:
'url.https://git:some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_2: 'url.https://some-token@github.com/.insteadOf',
GIT_CONFIG_KEY_3:
'url.https://ssh:some-enterprise-token-cargo@github.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_4:
'url.https://git:some-enterprise-token-cargo@github.enterprise.com/.insteadOf',
GIT_CONFIG_KEY_5:
'url.https://some-enterprise-token-cargo@github.enterprise.com/.insteadOf',
GIT_CONFIG_VALUE_0: 'ssh://git@github.com/',
GIT_CONFIG_VALUE_1: 'git@github.com:',
GIT_CONFIG_VALUE_2: 'https://github.com/',
GIT_CONFIG_VALUE_3: 'ssh://git@github.enterprise.com/',
GIT_CONFIG_VALUE_4: 'git@github.enterprise.com:',
GIT_CONFIG_VALUE_5: 'https://github.enterprise.com/',
}),
}),
}),
]),
);
});
it('supports install mode', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, constraints: { rust: '1.65.0' } },
}),
).resolves.toEqual([
{
file: {
contents: undefined,
path: 'Cargo.lock',
type: 'addition',
},
},
]);
expect(execSnapshots).toMatchObject([
{
cmd: 'install-tool rust 1.65.0',
options: {
cwd: '/tmp/github/some/repo',
env: {
CONTAINERBASE_CACHE_DIR: '/tmp/cache/containerbase',
},
},
},
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace',
options: {
cwd: '/tmp/github/some/repo',
env: {
CONTAINERBASE_CACHE_DIR: '/tmp/cache/containerbase',
},
},
},
]);
});
it('falls back to the extracted rust constraint', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
GlobalConfig.set({ ...adminConfig, binarySource: 'install' });
git.getFile.mockResolvedValueOnce('Old Cargo.lock');
const execSnapshots = mockExecAll();
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('New Cargo.lock');
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config: { ...config, extractedConstraints: { rust: '1.65.0' } },
}),
).resolves.not.toBeNull();
expect(execSnapshots).toMatchObject([
{ cmd: 'install-tool rust 1.65.0' },
{
cmd: 'cargo update --config net.git-fetch-with-cli=true --manifest-path Cargo.toml --workspace',
},
]);
});
it('catches errors', async () => {
fs.statLocalFile.mockResolvedValueOnce(partial<Stats>());
fs.findLocalSiblingOrParent.mockResolvedValueOnce('Cargo.lock');
fs.readLocalFile.mockResolvedValueOnce('Current Cargo.lock');
fs.writeLocalFile.mockImplementationOnce(() => {
throw new Error('not found');
});
const updatedDeps = [
{
depName: 'dep1',
datasource: CrateDatasource.id,
},
];
await expect(
cargo.updateArtifacts({
packageFileName: 'Cargo.toml',
updatedDeps,
newPackageFileContent: '{}',
config,
}),
).resolves.toEqual([
{ artifactError: { fileName: 'Cargo.lock', stderr: 'not found' } },
]);
});
});