From 114dd526d5e4ffa8b7b7db52e4c66062e597c872 Mon Sep 17 00:00:00 2001 From: StephenHodgson Date: Sat, 13 Dec 2025 16:25:35 -0500 Subject: [PATCH] pull request feedback --- __tests__/actionUtils.test.ts | 22 +++++++++----- __tests__/save.test.ts | 51 +++++++++++++++++++++++++++++++ __tests__/saveImpl.test.ts | 56 ++++++++++++++++++++++++++++++++++- __tests__/saveOnly.test.ts | 36 ++++++++++++++++++++++ 4 files changed, 157 insertions(+), 8 deletions(-) diff --git a/__tests__/actionUtils.test.ts b/__tests__/actionUtils.test.ts index 4dc9d0c..f8612c4 100644 --- a/__tests__/actionUtils.test.ts +++ b/__tests__/actionUtils.test.ts @@ -197,6 +197,14 @@ test("getCompressionLevel allows zero for no compression", () => { expect(actionUtils.getCompressionLevel("foo")).toBe(0); }); +test("getCompressionLevel returns undefined for negative values", () => { + const infoMock = jest.spyOn(core, "info"); + + testUtils.setInput("foo", "-3"); + expect(actionUtils.getCompressionLevel("foo")).toBeUndefined(); + expect(infoMock).not.toHaveBeenCalledWith(expect.stringContaining("compression-level")); +}); + test("getCompressionLevel returns undefined and warns if input is too large", () => { const infoMock = jest.spyOn(core, "info"); testUtils.setInput("foo", "11"); @@ -218,15 +226,15 @@ test("setCompressionLevel sets no-compression flag when zero", () => { expect(process.env["GZIP"]).toBe("-0"); }); - test("higher compression level produces smaller gzip output", () => { - const data = Buffer.alloc(8 * 1024, "A"); +test("higher compression level produces smaller gzip output", () => { + const data = Buffer.alloc(8 * 1024, "A"); - const level0 = zlib.gzipSync(data, { level: 0 }); - const level9 = zlib.gzipSync(data, { level: 9 }); + const level0 = zlib.gzipSync(data, { level: 0 }); + const level9 = zlib.gzipSync(data, { level: 9 }); - expect(level0.byteLength).toBeGreaterThan(level9.byteLength); - expect(level0.byteLength - level9.byteLength).toBeGreaterThan(1000); - }); + expect(level0.byteLength).toBeGreaterThan(level9.byteLength); + expect(level0.byteLength - level9.byteLength).toBeGreaterThan(1000); +}); test("getInputAsInt throws if required and value missing", () => { expect(() => diff --git a/__tests__/save.test.ts b/__tests__/save.test.ts index f444df3..b189ab6 100644 --- a/__tests__/save.test.ts +++ b/__tests__/save.test.ts @@ -136,3 +136,54 @@ test("save with valid inputs uploads a cache", async () => { expect(failedMock).toHaveBeenCalledTimes(0); }); + +test("negative compression level leaves env unset in combined flow", async () => { + const failedMock = jest.spyOn(core, "setFailed"); + const setCompressionLevelMock = jest.spyOn( + actionUtils, + "setCompressionLevel" + ); + + const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; + const savedCacheKey = "Linux-node-"; + + jest.spyOn(core, "getState") + // Cache Entry State + .mockImplementationOnce(() => { + return primaryKey; + }) + // Cache Key State + .mockImplementationOnce(() => { + return savedCacheKey; + }); + + const inputPath = "node_modules"; + testUtils.setInput(Inputs.Path, inputPath); + testUtils.setInput(Inputs.UploadChunkSize, "4000000"); + testUtils.setInput(Inputs.CompressionLevel, "-5"); + + const cacheId = 4; + const saveCacheMock = jest + .spyOn(cache, "saveCache") + .mockImplementationOnce(() => { + return Promise.resolve(cacheId); + }); + + await saveRun(); + + expect(process.env["ZSTD_CLEVEL"]).toBeUndefined(); + expect(process.env["GZIP"]).toBeUndefined(); + expect(setCompressionLevelMock).not.toHaveBeenCalled(); + + expect(saveCacheMock).toHaveBeenCalledTimes(1); + expect(saveCacheMock).toHaveBeenCalledWith( + [inputPath], + primaryKey, + { + uploadChunkSize: 4000000 + }, + false + ); + + expect(failedMock).toHaveBeenCalledTimes(0); +}); diff --git a/__tests__/saveImpl.test.ts b/__tests__/saveImpl.test.ts index f91a215..b153f3c 100644 --- a/__tests__/saveImpl.test.ts +++ b/__tests__/saveImpl.test.ts @@ -473,7 +473,10 @@ test("save applies compression level when provided", async () => { test("save skips setting compression when value is out of range", async () => { const failedMock = jest.spyOn(core, "setFailed"); - const setCompressionLevelMock = jest.spyOn(actionUtils, "setCompressionLevel"); + const setCompressionLevelMock = jest.spyOn( + actionUtils, + "setCompressionLevel" + ); const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; const savedCacheKey = "Linux-node-"; @@ -518,3 +521,54 @@ test("save skips setting compression when value is out of range", async () => { expect(failedMock).toHaveBeenCalledTimes(0); }); + +test("save skips setting compression when value is negative", async () => { + const failedMock = jest.spyOn(core, "setFailed"); + const setCompressionLevelMock = jest.spyOn( + actionUtils, + "setCompressionLevel" + ); + + const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; + const savedCacheKey = "Linux-node-"; + + jest.spyOn(core, "getState") + // Cache Entry State + .mockImplementationOnce(() => { + return savedCacheKey; + }) + // Cache Key State + .mockImplementationOnce(() => { + return primaryKey; + }); + + const inputPath = "node_modules"; + testUtils.setInput(Inputs.Path, inputPath); + testUtils.setInput(Inputs.UploadChunkSize, "4000000"); + testUtils.setInput(Inputs.CompressionLevel, "-1"); + + const cacheId = 4; + const saveCacheMock = jest + .spyOn(cache, "saveCache") + .mockImplementationOnce(() => { + return Promise.resolve(cacheId); + }); + + await saveImpl(new StateProvider()); + + expect(process.env["ZSTD_CLEVEL"]).toBeUndefined(); + expect(process.env["GZIP"]).toBeUndefined(); + expect(setCompressionLevelMock).not.toHaveBeenCalled(); + + expect(saveCacheMock).toHaveBeenCalledTimes(1); + expect(saveCacheMock).toHaveBeenCalledWith( + [inputPath], + primaryKey, + { + uploadChunkSize: 4000000 + }, + false + ); + + expect(failedMock).toHaveBeenCalledTimes(0); +}); diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts index 6ac2358..2e31003 100644 --- a/__tests__/saveOnly.test.ts +++ b/__tests__/saveOnly.test.ts @@ -127,6 +127,42 @@ test("save with valid inputs uploads a cache", async () => { expect(failedMock).toHaveBeenCalledTimes(0); }); +test("negative compression level does not set env vars", async () => { + const failedMock = jest.spyOn(core, "setFailed"); + + const primaryKey = "Linux-node-bb828da54c148048dd17899ba9fda624811cfb43"; + + const inputPath = "node_modules"; + testUtils.setInput(Inputs.Key, primaryKey); + testUtils.setInput(Inputs.Path, inputPath); + testUtils.setInput(Inputs.UploadChunkSize, "4000000"); + testUtils.setInput(Inputs.CompressionLevel, "-2"); + + const cacheId = 4; + const saveCacheMock = jest + .spyOn(cache, "saveCache") + .mockImplementationOnce(() => { + return Promise.resolve(cacheId); + }); + + await saveOnlyRun(); + + expect(process.env["ZSTD_CLEVEL"]).toBeUndefined(); + expect(process.env["GZIP"]).toBeUndefined(); + + expect(saveCacheMock).toHaveBeenCalledTimes(1); + expect(saveCacheMock).toHaveBeenCalledWith( + [inputPath], + primaryKey, + { + uploadChunkSize: 4000000 + }, + false + ); + + expect(failedMock).toHaveBeenCalledTimes(0); +}); + test("save failing logs the warning message", async () => { const warningMock = jest.spyOn(core, "warning");