Skip to content

Commit

Permalink
refactor: use records for artifactsUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
secustor committed Feb 10, 2025
1 parent 91fd1b1 commit 74adf52
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 120 deletions.
36 changes: 18 additions & 18 deletions lib/modules/manager/npm/post-update/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ describe('modules/manager/npm/post-update/index', () => {
'',
);
expect(yarnrcYmlContent).toBe('');
expect(updatedArtifacts).toEqual([]);
expect(updatedArtifacts).toEqual({});
expect(logger.logger.debug).not.toHaveBeenCalled();
expect(logger.logger.error).not.toHaveBeenCalled();
});
Expand Down Expand Up @@ -397,7 +397,7 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -417,13 +417,13 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [
{
updatedArtifacts: {
'package-lock.json': {
type: 'addition',
path: 'package-lock.json',
contents: '{}',
},
],
},
});

expect(fs.readLocalFile).toHaveBeenCalledWith('.npmrc', 'utf8');
Expand Down Expand Up @@ -501,13 +501,13 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [
{
updatedArtifacts: {
'yarn.lock': {
type: 'addition',
path: 'yarn.lock',
contents: '{}',
},
],
},
});
expect(fs.deleteLocalFile).toHaveBeenCalled();
});
Expand All @@ -534,21 +534,21 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [
{
updatedArtifacts: {
'packages/pnpm/pnpm-lock.yaml': {
type: 'addition',
path: 'packages/pnpm/pnpm-lock.yaml',
contents: 'some-contents:',
},
],
},
});
expect(fs.deleteLocalFile).toHaveBeenCalled();
});

it('no npm files', async () => {
expect(await getAdditionalFiles(baseConfig, {})).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -557,7 +557,7 @@ describe('modules/manager/npm/post-update/index', () => {
await getAdditionalFiles(baseConfig, additionalFiles),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -574,7 +574,7 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -594,7 +594,7 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -609,7 +609,7 @@ describe('modules/manager/npm/post-update/index', () => {
artifactErrors: [
{ lockFile: 'package-lock.json', stderr: 'some-error' },
],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -622,7 +622,7 @@ describe('modules/manager/npm/post-update/index', () => {
),
).toStrictEqual({
artifactErrors: [{ lockFile: 'yarn.lock', stderr: 'some-error' }],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand All @@ -646,7 +646,7 @@ describe('modules/manager/npm/post-update/index', () => {
artifactErrors: [
{ lockFile: 'packages/pnpm/pnpm-lock.yaml', stderr: 'some-error' },
],
updatedArtifacts: [],
updatedArtifacts: {},
});
});

Expand Down
15 changes: 0 additions & 15 deletions lib/util/records.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,13 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() h
"artifactErrors": [],
"artifactNotices": [],
"reuseExistingBranch": true,
"updatedArtifacts": [
{
"updatedArtifacts": {
"composer.json": {
"contents": "some contents",
"path": "composer.json",
"type": "addition",
},
],
},
"updatedPackageFiles": [
{
"contents": "some new content",
Expand All @@ -176,13 +176,13 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() h
"artifactErrors": [],
"artifactNotices": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [
{
"updatedArtifacts": {
"composer.json": {
"contents": "some contents",
"path": "composer.json",
"type": "addition",
},
],
},
"updatedPackageFiles": [],
}
`;
Expand All @@ -207,13 +207,13 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() u
"artifactErrors": [],
"artifactNotices": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [
{
"updatedArtifacts": {
"composer.lock": {
"contents": "some contents",
"path": "composer.lock",
"type": "addition",
},
],
},
"updatedPackageFiles": [
{
"contents": "existing content",
Expand All @@ -229,13 +229,13 @@ exports[`workers/repository/update/branch/get-updated getUpdatedPackageFiles() u
"artifactErrors": [],
"artifactNotices": [],
"reuseExistingBranch": undefined,
"updatedArtifacts": [
{
"updatedArtifacts": {
"terraform.lock": {
"contents": "some contents",
"path": "terraform.lock",
"type": "addition",
},
],
},
"updatedPackageFiles": [
{
"contents": "existing content",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,24 @@ describe('workers/repository/update/branch/execute-post-upgrade-commands', () =>
config,
);

expect(res.updatedArtifacts).toHaveLength(3);
expect(res.updatedArtifacts).toEqual({
artifact: {
contents: '',
path: 'artifact',
type: 'addition',
},
'some-existing-dir': {
contents: '',
path: 'some-existing-dir',
type: 'addition',
},
symlink: {
contents: 'dest',
isSymlink: true,
path: 'symlink',
type: 'addition',
},
});
expect(fs.writeLocalFile).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -119,7 +136,7 @@ describe('workers/repository/update/branch/execute-post-upgrade-commands', () =>
config,
);

expect(res.updatedArtifacts).toHaveLength(0);
expect(res.updatedArtifacts).toEqual({});
expect(fs.writeLocalFile).toHaveBeenCalledTimes(1);
});

Expand Down Expand Up @@ -168,7 +185,7 @@ describe('workers/repository/update/branch/execute-post-upgrade-commands', () =>
config,
);

expect(res.updatedArtifacts).toHaveLength(0);
expect(res.updatedArtifacts).toEqual({});
expect(fs.writeLocalFile).toHaveBeenCalledTimes(1);
expect(logger.logger.debug).toHaveBeenCalledWith(
{ file: 'not-a-txt-file' },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import { getRepoStatus } from '../../../../util/git';
import type { FileChange } from '../../../../util/git/types';
import { minimatch } from '../../../../util/minimatch';
import { filterEntries } from '../../../../util/records';
import { regEx } from '../../../../util/regex';
import { sanitize } from '../../../../util/sanitize';
import { compile } from '../../../../util/template';
Expand All @@ -29,7 +28,7 @@ export async function postUpgradeCommandsExecutor(
filteredUpgradeCommands: BranchUpgradeConfig[],
config: BranchConfig,
): Promise<PostUpgradeCommandsExecutionResult> {
let updatedArtifacts = {
const updatedArtifacts = {
...config.updatedArtifacts,
};
const artifactErrors = [...(config.artifactErrors ?? [])];
Expand Down Expand Up @@ -147,12 +146,6 @@ export async function postUpgradeCommandsExecutor(
contents: existingContent,
};
}
// If the file is deleted by a previous post-update command, remove the deletion from updatedArtifacts
// remove all entries with the type 'deletion'
updatedArtifacts = filterEntries(
updatedArtifacts,
([, value]) => value.type !== 'deletion',
);
}
}
if (!fileMatched) {
Expand All @@ -174,11 +167,6 @@ export async function postUpgradeCommandsExecutor(
type: 'deletion',
path: relativePath,
};
// If the file is created or modified by a previous post-update command, remove the modification from updatedArtifacts
updatedArtifacts = filterEntries(
updatedArtifacts,
([, value]) => value.type !== 'addition',
);
}
}
}
Expand All @@ -193,7 +181,7 @@ export default async function executePostUpgradeCommands(
const hasChangedFiles =
(is.array(config.updatedPackageFiles) &&
config.updatedPackageFiles.length > 0) ||
(is.array(config.updatedArtifacts) && config.updatedArtifacts.length > 0);
Object.keys(config.updatedArtifacts ?? {}).length > 0;

if (!hasChangedFiles) {
/* Only run post-upgrade tasks if there are changes to package files... */
Expand Down
Loading

0 comments on commit 74adf52

Please sign in to comment.