fix(pip-compile): Correctly report errors when a lock file is unchanged (#29363)

This commit is contained in:
Miles Budnek 2024-05-31 01:19:04 -04:00 committed by GitHub
parent 4313b9b329
commit 635854e321
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 42 additions and 12 deletions

View file

@ -1,7 +1,11 @@
import { codeBlock } from 'common-tags';
import { mockDeep } from 'jest-mock-extended';
import { join } from 'upath';
import { envMock, mockExecAll } from '../../../../test/exec-util';
import {
envMock,
mockExecAll,
mockExecSequence,
} from '../../../../test/exec-util';
import { Fixtures } from '../../../../test/fixtures';
import { env, fs, git, mocked, partial } from '../../../../test/util';
import { GlobalConfig } from '../../../config/global';
@ -80,7 +84,7 @@ describe('modules/manager/pip-compile/artifacts', () => {
expect(execSnapshots).toEqual([]);
});
it('returns null if unchanged', async () => {
it('returns null if all unchanged', async () => {
fs.readLocalFile.mockResolvedValueOnce(simpleHeader);
const execSnapshots = mockExecAll();
fs.readLocalFile.mockResolvedValueOnce('new lock');
@ -615,5 +619,32 @@ describe('modules/manager/pip-compile/artifacts', () => {
'pip-compile --output-file=requirements.txt requirements.in --upgrade-package=foo==1.0.2 --upgrade-package=bar==2.0.0',
);
});
it('reports errors when a lock file is unchanged', async () => {
fs.readLocalFile.mockResolvedValue(simpleHeader);
mockExecSequence([
new Error('Oh noes!'),
{ stdout: 'This one worked', stderr: '' },
]);
git.getRepoStatus.mockResolvedValue(
partial<StatusResult>({
modified: [],
}),
);
const results = await updateArtifacts({
packageFileName: 'requirements.in',
updatedDeps: [],
newPackageFileContent: 'some new content',
config: {
...config,
lockFiles: ['requirements1.txt', 'requirements2.txt'],
},
});
expect(results).toMatchObject([
{
artifactError: { lockFile: 'requirements1.txt', stderr: 'Oh noes!' },
},
]);
});
});
});

View file

@ -125,9 +125,7 @@ export async function updateArtifacts({
logger.trace({ env: execOptions.extraEnv }, 'pip-compile extra env vars');
await exec(cmd, execOptions);
const status = await getRepoStatus();
if (!status?.modified.includes(outputFileName)) {
return null;
}
if (status?.modified.includes(outputFileName)) {
result.push({
file: {
type: 'addition',
@ -135,6 +133,7 @@ export async function updateArtifacts({
contents: await readLocalFile(outputFileName, 'utf8'),
},
});
}
} catch (err) {
// istanbul ignore if
if (err.message === TEMPORARY_ERROR) {
@ -150,5 +149,5 @@ export async function updateArtifacts({
}
}
logger.debug('pip-compile: Returning updated output file(s)');
return result;
return result.length === 0 ? null : result;
}