Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(npm): handle null lockfile after update #23658

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/modules/manager/npm/post-update/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,34 @@ describe('modules/manager/npm/post-update/index', () => {
]);
});

it('detects if lock file contents are unchanged', async () => {
spyNpm.mockResolvedValueOnce({ error: false, lockFile: '{}' });
fs.readLocalFile.mockImplementation((f): Promise<any> => {
if (f === 'package-lock.json') {
return Promise.resolve('{}');
}
return Promise.resolve(null);
});
git.getFile.mockImplementation((f) => {
if (f === 'package-lock.json') {
return Promise.resolve('{}');
}
return Promise.resolve(null);
});
expect(
(
await getAdditionalFiles(
{
...updateConfig,
updateLockFiles: true,
reuseExistingBranch: true,
},
additionalFiles
)
).updatedArtifacts.find((a) => a.path === 'package-lock.json')
).toBeUndefined();
});

it('works for yarn', async () => {
spyYarn.mockResolvedValueOnce({ error: false, lockFile: '{}' });
expect(
Expand Down
4 changes: 2 additions & 2 deletions lib/modules/manager/npm/post-update/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,7 @@ export async function getAdditionalFiles(
lockFile: npmLock,
stderr: res.stderr,
});
} else {
} else if (res.lockFile) {
const existingContent = await getFile(
npmLock,
config.reuseExistingBranch ? config.branchName : config.baseBranch
Expand All @@ -574,7 +574,7 @@ export async function getAdditionalFiles(
path: npmLock,
// TODO: can this be undefined? (#22198)

contents: res.lockFile!.replace(tokenRe, ''),
contents: res.lockFile.replace(tokenRe, ''),
});
}
}
Expand Down
18 changes: 9 additions & 9 deletions lib/modules/manager/npm/post-update/npm.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe('modules/manager/npm/post-update/npm', () => {
updates
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(2);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchSnapshot();
});
Expand All @@ -62,7 +62,7 @@ describe('modules/manager/npm/post-update/npm', () => {
updates
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toMatchSnapshot();
});
Expand Down Expand Up @@ -91,7 +91,7 @@ describe('modules/manager/npm/post-update/npm', () => {
updates
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toMatchSnapshot();
expect(execSnapshots).toMatchSnapshot();
});
Expand All @@ -117,7 +117,7 @@ describe('modules/manager/npm/post-update/npm', () => {
'some-dir/npm-shrinkwrap.json',
'utf8'
);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
// TODO: is that right?
expect(execSnapshots).toEqual([]);
Expand All @@ -140,7 +140,7 @@ describe('modules/manager/npm/post-update/npm', () => {
'some-dir/npm-shrinkwrap.json',
'utf8'
);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
// TODO: is that right?
expect(execSnapshots).toEqual([]);
Expand All @@ -158,7 +158,7 @@ describe('modules/manager/npm/post-update/npm', () => {
{ skipInstalls, binarySource, constraints: { npm: '^6.0.0' } }
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
// TODO: is that right?
expect(execSnapshots).toEqual([]);
Expand All @@ -176,7 +176,7 @@ describe('modules/manager/npm/post-update/npm', () => {
[{ isRemediation: true }]
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(1);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(res.lockFile).toBe('package-lock-contents');
expect(execSnapshots).toHaveLength(2);
});
Expand Down Expand Up @@ -421,7 +421,7 @@ describe('modules/manager/npm/post-update/npm', () => {
updates
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(2);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(execSnapshots).toMatchObject([
{
cmd: 'npm install --package-lock-only --no-audit --ignore-scripts --workspace=docs/a [email protected] [email protected]',
Expand Down Expand Up @@ -456,7 +456,7 @@ describe('modules/manager/npm/post-update/npm', () => {
modifiedUpdates
);
expect(fs.readLocalFile).toHaveBeenCalledTimes(2);
expect(res.error).toBeUndefined();
expect(res.error).toBeFalse();
expect(execSnapshots).toMatchObject([
{
cmd: 'npm install --package-lock-only --no-audit --ignore-scripts --workspace=docs/a [email protected] [email protected]',
Expand Down
2 changes: 1 addition & 1 deletion lib/modules/manager/npm/post-update/npm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ export async function generateLockFile(
}
return { error: true, stderr: err.stderr };
}
return { lockFile };
return { error: !lockFile, lockFile };
}

export function divideWorkspaceAndRootDeps(
Expand Down