From 870d3d8e130bd832a9226f7d7b5cd3d32ce40886 Mon Sep 17 00:00:00 2001
From: Alex Dunae <alex.dunae@bentley.com>
Date: Tue, 14 Jan 2025 10:40:03 -0800
Subject: [PATCH] Add cache-key as output

---
 __tests__/cache-restore.test.ts | 32 ++++++++++++++++++++++++++++++++
 __tests__/cache-save.test.ts    | 13 ++++++++++---
 action.yml                      |  2 ++
 src/cache-restore.ts            |  1 +
 src/cache-save.ts               |  1 +
 5 files changed, 46 insertions(+), 3 deletions(-)

diff --git a/__tests__/cache-restore.test.ts b/__tests__/cache-restore.test.ts
index 0bbf2812..56aabc6f 100644
--- a/__tests__/cache-restore.test.ts
+++ b/__tests__/cache-restore.test.ts
@@ -180,6 +180,38 @@ describe('cache-restore', () => {
     );
   });
 
+  describe('Cache key output', () => {
+    const packageManager = 'npm';
+    const cacheDependencyPath = 'package-lock.json';
+    const primaryKey = `node-cache-${platform}-${arch}-${packageManager}-${npmFileHash}`;
+    const cacheKey = `node-cache-${platform}-${arch}-${packageManager}-abc123`;
+
+    beforeEach(() => {
+      getCommandOutputSpy.mockImplementation(command => {
+        if (command.includes('npm config get cache')) return npmCachePath;
+      });
+    });
+
+    it('sets the cache-key output', async () => {
+      restoreCacheSpy.mockResolvedValue(cacheKey);
+      await restoreCache(packageManager, cacheDependencyPath);
+      expect(setOutputSpy).toHaveBeenCalledWith('cache-key', primaryKey);
+    });
+
+    it('sets the cache-hit output to true when cache is found', async () => {
+      restoreCacheSpy.mockResolvedValue(cacheKey);
+      await restoreCache(packageManager, cacheDependencyPath);
+      expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', true);
+    });
+
+    it('sets the cache-hit output to false when cache is not found', async () => {
+      restoreCacheSpy.mockResolvedValue(undefined);
+      await restoreCache(packageManager, cacheDependencyPath);
+
+      expect(setOutputSpy).toHaveBeenCalledWith('cache-hit', false);
+    });
+  });
+
   afterEach(() => {
     jest.resetAllMocks();
     jest.clearAllMocks();
diff --git a/__tests__/cache-save.test.ts b/__tests__/cache-save.test.ts
index 17899dfa..01dfe909 100644
--- a/__tests__/cache-save.test.ts
+++ b/__tests__/cache-save.test.ts
@@ -27,6 +27,7 @@ describe('run', () => {
   let setFailedSpy: jest.SpyInstance;
   let getStateSpy: jest.SpyInstance;
   let saveCacheSpy: jest.SpyInstance;
+  let setOutputSpy: jest.SpyInstance;
   let getCommandOutputSpy: jest.SpyInstance;
   let hashFilesSpy: jest.SpyInstance;
   let existsSpy: jest.SpyInstance;
@@ -53,6 +54,8 @@ describe('run', () => {
     saveCacheSpy = jest.spyOn(cache, 'saveCache');
     saveCacheSpy.mockImplementation(() => undefined);
 
+    setOutputSpy = jest.spyOn(core, 'setOutput');
+
     // glob
     hashFilesSpy = jest.spyOn(glob, 'hashFiles');
     hashFilesSpy.mockImplementation((pattern: string) => {
@@ -228,6 +231,7 @@ describe('run', () => {
       expect(infoSpy).toHaveBeenLastCalledWith(
         `Cache saved with the key: ${npmFileHash}`
       );
+      expect(core.setOutput).toHaveBeenCalledWith('cache-key', npmFileHash);
       expect(setFailedSpy).not.toHaveBeenCalled();
     });
 
@@ -258,6 +262,7 @@ describe('run', () => {
       expect(infoSpy).toHaveBeenLastCalledWith(
         `Cache saved with the key: ${npmFileHash}`
       );
+      expect(core.setOutput).toHaveBeenCalledWith('cache-key', npmFileHash);
       expect(setFailedSpy).not.toHaveBeenCalled();
     });
 
@@ -288,6 +293,7 @@ describe('run', () => {
       expect(infoSpy).toHaveBeenLastCalledWith(
         `Cache saved with the key: ${yarnFileHash}`
       );
+      expect(core.setOutput).toHaveBeenCalledWith('cache-key', yarnFileHash);
       expect(setFailedSpy).not.toHaveBeenCalled();
     });
 
@@ -297,9 +303,9 @@ describe('run', () => {
         key === State.CachePackageManager
           ? inputs['cache']
           : key === State.CacheMatchedKey
-          ? pnpmFileHash
+          ? 'no-match'
           : key === State.CachePrimaryKey
-          ? npmFileHash
+          ? pnpmFileHash
           : key === State.CachePaths
           ? '["/foo/bar"]'
           : 'not expected'
@@ -316,8 +322,9 @@ describe('run', () => {
       );
       expect(saveCacheSpy).toHaveBeenCalled();
       expect(infoSpy).toHaveBeenLastCalledWith(
-        `Cache saved with the key: ${npmFileHash}`
+        `Cache saved with the key: ${pnpmFileHash}`
       );
+      expect(core.setOutput).toHaveBeenCalledWith('cache-key', pnpmFileHash);
       expect(setFailedSpy).not.toHaveBeenCalled();
     });
 
diff --git a/action.yml b/action.yml
index 99db5869..99140912 100644
--- a/action.yml
+++ b/action.yml
@@ -30,6 +30,8 @@ inputs:
 outputs:
   cache-hit: 
     description: 'A boolean value to indicate if a cache was hit.'
+  cache-key:
+    description: 'The key used for the cache.'
   node-version:
     description: 'The installed node version.'
 runs:
diff --git a/src/cache-restore.ts b/src/cache-restore.ts
index af12ad83..65767510 100644
--- a/src/cache-restore.ts
+++ b/src/cache-restore.ts
@@ -45,6 +45,7 @@ export const restoreCache = async (
   core.debug(`primary key is ${primaryKey}`);
 
   core.saveState(State.CachePrimaryKey, primaryKey);
+  core.setOutput('cache-key', primaryKey);
 
   const isManagedByYarnBerry = await repoHasYarnBerryManagedDependencies(
     packageManagerInfo,
diff --git a/src/cache-save.ts b/src/cache-save.ts
index 2522127a..470b8457 100644
--- a/src/cache-save.ts
+++ b/src/cache-save.ts
@@ -66,6 +66,7 @@ const cachePackages = async (packageManager: string) => {
   }
 
   core.info(`Cache saved with the key: ${primaryKey}`);
+  core.setOutput('cache-key', primaryKey);
 };
 
 run(true);