Skip to content

Commit 8f5d746

Browse files
authored
fix: handle empty array (#731)
* fix: handle empty array * fix: lint
1 parent a1d462a commit 8f5d746

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

lib/extractor/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ export function getUserInstructionLayersFromConfig(imageConfig) {
201201
const maxDiffInHours = 5;
202202

203203
const history = imageConfig.history;
204-
if (!history) {
204+
if (!history || history.length === 0) {
205205
return [];
206206
}
207207
const lastInstructionTime = new Date(history.slice(-1)[0].created);

test/system/detected-layers/detected-layers.spec.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ describe("correctly picks user instruction layers from manifest config", () => {
6666
);
6767
expect(layers.length).toBe(0);
6868
});
69+
70+
it("handles empty history array)", async () => {
71+
const configWithEmptyHistory = {
72+
architecture: "arm64",
73+
os: "linux",
74+
history: [],
75+
rootfs: { diff_ids: [] },
76+
config: { Labels: {} },
77+
};
78+
const layers = getUserInstructionLayersFromConfig(configWithEmptyHistory);
79+
expect(layers).toEqual([]);
80+
});
81+
82+
it("handles undefined history without error", async () => {
83+
const configWithNoHistory = {
84+
architecture: "arm64",
85+
os: "linux",
86+
history: undefined,
87+
rootfs: { diff_ids: [] },
88+
config: { Labels: {} },
89+
};
90+
const layers = getUserInstructionLayersFromConfig(configWithNoHistory);
91+
expect(layers).toEqual([]);
92+
});
6993
});
7094

7195
describe("layers are extracted", () => {

0 commit comments

Comments
 (0)