Skip to content

Commit c4f97bf

Browse files
fix: empty results (#21)
* fix: {} not being treated as no-op * fix: bugs with addVariablesToPath and ParsedResourceSettings * chore: add change set tests * chore: bump package version
1 parent 79683e1 commit c4f97bf

4 files changed

Lines changed: 39 additions & 5 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@codifycli/plugin-core",
3-
"version": "1.2.0",
3+
"version": "1.2.1",
44
"description": "TypeScript library for building Codify plugins to manage system resources (applications, CLI tools, settings) through infrastructure-as-code",
55
"main": "dist/index.js",
66
"typings": "dist/index.d.ts",

src/plan/change-set.test.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,33 @@ describe('Change set tests', () => {
304304
expect(cs.operation).to.eq(ResourceOperation.DESTROY);
305305
})
306306

307+
it('treats empty objects {} as no-op', () => {
308+
const desired = {
309+
keyboard: {},
310+
dock: {},
311+
}
312+
313+
const current = {
314+
keyboard: {},
315+
dock: {},
316+
}
317+
318+
const cs = ChangeSet.calculateModification(desired, current);
319+
expect(cs.parameterChanges.length).to.eq(0);
320+
expect(cs.operation).to.eq(ResourceOperation.NOOP);
321+
})
322+
323+
it('treats empty object {} as absent when current has no value', () => {
324+
const desired = {
325+
keyboard: {},
326+
dock: {},
327+
}
328+
329+
const cs = ChangeSet.calculateModification(desired, {});
330+
expect(cs.parameterChanges.length).to.eq(0);
331+
expect(cs.operation).to.eq(ResourceOperation.NOOP);
332+
})
333+
307334
it('correctly determines array equality 5', () => {
308335
const arrA = [{ key1: 'b' }, { key1: 'a' }, { key1: 'a' }];
309336
const arrB = [{ key1: 'a' }, { key1: 'a' }, { key1: 'b' }];

src/plan/change-set.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,13 +154,13 @@ export class ChangeSet<T extends StringIndexedObject> {
154154
): ParameterChange<T>[] {
155155
const parameterChangeSet = new Array<ParameterChange<T>>();
156156

157-
// Filter out null and undefined values or else the diff below will not work
157+
// Filter out null, undefined, [], and {} — all treated as "no value"
158158
const desired = Object.fromEntries(
159-
Object.entries(desiredParameters).filter(([, v]) => v !== null && v !== undefined)
159+
Object.entries(desiredParameters).filter(([, v]) => !ChangeSet.isAbsent(v))
160160
) as Partial<T>
161161

162162
const current = Object.fromEntries(
163-
Object.entries(currentParameters).filter(([, v]) => v !== null && v !== undefined)
163+
Object.entries(currentParameters).filter(([, v]) => !ChangeSet.isAbsent(v))
164164
) as Partial<T>
165165

166166
for (const k of new Set([...Object.keys(current), ...Object.keys(desired)])) {
@@ -227,6 +227,13 @@ export class ChangeSet<T extends StringIndexedObject> {
227227
return orderOfOperations[Math.max(indexPrev, indexNext)];
228228
}
229229

230+
private static isAbsent(v: unknown): boolean {
231+
if (v === null || v === undefined) return true;
232+
if (Array.isArray(v)) return v.length === 0;
233+
if (typeof v === 'object') return Object.keys(v as object).length === 0;
234+
return false;
235+
}
236+
230237
private static isSame(
231238
desired: unknown,
232239
current: unknown,

src/utils/functions.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ export function resolvePathWithVariables(pathWithVariables: string) {
4343
export function addVariablesToPath(pathWithoutVariables: string) {
4444
let result = pathWithoutVariables;
4545
for (const [key, value] of Object.entries(process.env)) {
46-
if (!value || !path.isAbsolute(value) || value === '/' || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
46+
if (!value || !path.isAbsolute(value) || value === '/' || value === homeDirectory || key === 'HOME' || key === 'PATH' || key === 'SHELL' || key === 'PWD') {
4747
continue;
4848
}
4949

0 commit comments

Comments
 (0)