From ea5981db97a19600eb6067502417b319723fe2ea Mon Sep 17 00:00:00 2001 From: Malo Bourgon Date: Mon, 3 Feb 2020 15:55:17 -0800 Subject: [PATCH 1/4] Add example for Haskell Stack --- README.md | 1 + examples.md | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/README.md b/README.md index a0180c3..98be465 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ See [Examples](examples.md) for a list of `actions/cache` implementations for us - [Elixir - Mix](./examples.md#elixir---mix) - [Go - Modules](./examples.md#go---modules) - [Haskell - Cabal](./examples.md#haskell---cabal) +- [Haskell - Stack](./examples.md#haskell---stack) - [Java - Gradle](./examples.md#java---gradle) - [Java - Maven](./examples.md#java---maven) - [Node - npm](./examples.md#node---npm) diff --git a/examples.md b/examples.md index 15da4e3..17b153a 100644 --- a/examples.md +++ b/examples.md @@ -6,6 +6,7 @@ - [Elixir - Mix](#elixir---mix) - [Go - Modules](#go---modules) - [Haskell - Cabal](#haskell---cabal) + - [Haskell - Stack](#haskell---stack) - [Java - Gradle](#java---gradle) - [Java - Maven](#java---maven) - [Node - npm](#node---npm) @@ -130,6 +131,25 @@ We cache the elements of the Cabal store separately, as the entirety of `~/.caba key: ${{ runner.os }}-${{ matrix.ghc }} ``` +## Haskell - Stack + +```yaml +- uses: actions/cache@v2 + name: Cache ~/.stack + with: + path: ~/.stack + key: ${{ runner.os }}-stack-global-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }} + restore-keys: | + ${{ runner.os }}-stack-global- +- uses: actions/cache@v2 + name: Cache .stack-work + with: + path: .stack-work + key: ${{ runner.os }}-stack-work-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }}-${{ hashFiles('**/*.hs') }} + restore-keys: | + ${{ runner.os }}-stack-work- +``` + ## Java - Gradle ```yaml From 669e7536d9d7f814487469a7bb832ed71dd3b601 Mon Sep 17 00:00:00 2001 From: Vipul Date: Tue, 22 Feb 2022 12:17:37 +0530 Subject: [PATCH 2/4] Revert "Add example for Haskell Stack" --- README.md | 1 - examples.md | 20 -------------------- 2 files changed, 21 deletions(-) diff --git a/README.md b/README.md index 98be465..a0180c3 100644 --- a/README.md +++ b/README.md @@ -92,7 +92,6 @@ See [Examples](examples.md) for a list of `actions/cache` implementations for us - [Elixir - Mix](./examples.md#elixir---mix) - [Go - Modules](./examples.md#go---modules) - [Haskell - Cabal](./examples.md#haskell---cabal) -- [Haskell - Stack](./examples.md#haskell---stack) - [Java - Gradle](./examples.md#java---gradle) - [Java - Maven](./examples.md#java---maven) - [Node - npm](./examples.md#node---npm) diff --git a/examples.md b/examples.md index 17b153a..15da4e3 100644 --- a/examples.md +++ b/examples.md @@ -6,7 +6,6 @@ - [Elixir - Mix](#elixir---mix) - [Go - Modules](#go---modules) - [Haskell - Cabal](#haskell---cabal) - - [Haskell - Stack](#haskell---stack) - [Java - Gradle](#java---gradle) - [Java - Maven](#java---maven) - [Node - npm](#node---npm) @@ -131,25 +130,6 @@ We cache the elements of the Cabal store separately, as the entirety of `~/.caba key: ${{ runner.os }}-${{ matrix.ghc }} ``` -## Haskell - Stack - -```yaml -- uses: actions/cache@v2 - name: Cache ~/.stack - with: - path: ~/.stack - key: ${{ runner.os }}-stack-global-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }} - restore-keys: | - ${{ runner.os }}-stack-global- -- uses: actions/cache@v2 - name: Cache .stack-work - with: - path: .stack-work - key: ${{ runner.os }}-stack-work-${{ hashFiles('stack.yaml') }}-${{ hashFiles('package.yaml') }}-${{ hashFiles('**/*.hs') }} - restore-keys: | - ${{ runner.os }}-stack-work- -``` - ## Java - Gradle ```yaml From 5b7eeecaebfc25e4ec267629647daa09be203c48 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Mon, 2 Jan 2023 07:29:20 +0000 Subject: [PATCH 3/4] stateinputprovider with pending test cases fix --- __tests__/saveOnly.test.ts | 8 ++------ __tests__/stateProvider.test.ts | 15 +++++++++++---- dist/restore-only/index.js | 8 ++++++-- dist/restore/index.js | 8 ++++++-- dist/save-only/index.js | 11 +++++++---- dist/save/index.js | 11 +++++++---- src/saveImpl.ts | 4 +--- src/stateProvider.ts | 12 +++++++++--- src/utils/testUtils.ts | 4 ++++ 9 files changed, 53 insertions(+), 28 deletions(-) diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts index 44bd732..b9bf246 100644 --- a/__tests__/saveOnly.test.ts +++ b/__tests__/saveOnly.test.ts @@ -11,12 +11,8 @@ jest.mock("@actions/cache"); jest.mock("../src/utils/actionUtils"); beforeAll(() => { - jest.spyOn(core, "getInput").mockImplementation((name, options) => { - return jest.requireActual("@actions/core").getInput(name, options); - }); - - jest.spyOn(core, "setOutput").mockImplementation((key, value) => { - return jest.requireActual("@actions/core").getInput(key, value); + jest.spyOn(core, "getInput").mockImplementation(name => { + return testUtils.getInput(name); }); jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( diff --git a/__tests__/stateProvider.test.ts b/__tests__/stateProvider.test.ts index 255b5fb..39ca350 100644 --- a/__tests__/stateProvider.test.ts +++ b/__tests__/stateProvider.test.ts @@ -58,11 +58,17 @@ test("StateProvider saves states", async () => { }); test("NullStateProvider saves outputs", async () => { + const states = new Map(); + + const getInputMock = jest + .spyOn(core, "getInput") + .mockImplementation(key => states.get(key) || ""); + const getStateMock = jest .spyOn(core, "getState") - .mockImplementation(name => - jest.requireActual("@actions/core").getState(name) - ); + .mockImplementation(key => { + return jest.requireActual("@actions/core").getState(key); + }); const setOutputMock = jest .spyOn(core, "setOutput") @@ -73,7 +79,7 @@ test("NullStateProvider saves outputs", async () => { const saveStateMock = jest .spyOn(core, "saveState") .mockImplementation((key, value) => { - return jest.requireActual("@actions/core").saveState(key, value); + states.set(key, value); }); const cacheMatchedKey = "node-cache"; @@ -84,6 +90,7 @@ test("NullStateProvider saves outputs", async () => { nullStateProvider.getCacheState(); expect(getStateMock).toHaveBeenCalledTimes(0); + expect(getInputMock).toHaveBeenCalledTimes(2); expect(setOutputMock).toHaveBeenCalledTimes(2); expect(saveStateMock).toHaveBeenCalledTimes(0); }); diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index 4f19308..bb5d187 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; diff --git a/dist/restore/index.js b/dist/restore/index.js index 81e33e9..c15e0f7 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; diff --git a/dist/save-only/index.js b/dist/save-only/index.js index 8573690..f9e20ed 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -9461,6 +9461,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9468,8 +9471,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; @@ -41059,8 +41063,7 @@ function saveImpl(stateProvider) { } // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) || - core.getInput(constants_1.Inputs.Key); + const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); return; diff --git a/dist/save/index.js b/dist/save/index.js index 9657785..065b00f 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -9405,6 +9405,9 @@ exports.StateProvider = StateProvider; class NullStateProvider extends StateProviderBase { constructor() { super(...arguments); + this.stateToInputMap = new Map([ + [constants_1.State.CachePrimaryKey, constants_1.Inputs.Key] + ]); this.stateToOutputMap = new Map([ [constants_1.State.CacheMatchedKey, constants_1.Outputs.CacheMatchedKey], [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] @@ -9412,8 +9415,9 @@ class NullStateProvider extends StateProviderBase { this.setState = (key, value) => { core.setOutput(this.stateToOutputMap.get(key), value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - this.getState = (key) => ""; + this.getState = (key) => { + return core.getInput(this.stateToInputMap.get(key)); + }; } } exports.NullStateProvider = NullStateProvider; @@ -41003,8 +41007,7 @@ function saveImpl(stateProvider) { } // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey) || - core.getInput(constants_1.Inputs.Key); + const primaryKey = stateProvider.getState(constants_1.State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); return; diff --git a/src/saveImpl.ts b/src/saveImpl.ts index d8d7674..e49a8ba 100644 --- a/src/saveImpl.ts +++ b/src/saveImpl.ts @@ -28,9 +28,7 @@ async function saveImpl(stateProvider: IStateProvider): Promise { // If restore has stored a primary key in state, reuse that // Else re-evaluate from inputs - const primaryKey = - stateProvider.getState(State.CachePrimaryKey) || - core.getInput(Inputs.Key); + const primaryKey = stateProvider.getState(State.CachePrimaryKey); if (!primaryKey) { utils.logWarning(`Key is not specified.`); diff --git a/src/stateProvider.ts b/src/stateProvider.ts index beb41e5..a2de728 100644 --- a/src/stateProvider.ts +++ b/src/stateProvider.ts @@ -1,6 +1,6 @@ import * as core from "@actions/core"; -import { Outputs, State } from "./constants"; +import { Inputs, Outputs, State } from "./constants"; export interface IStateProvider { setState(key: string, value: string): void; @@ -33,6 +33,10 @@ export class StateProvider extends StateProviderBase { } export class NullStateProvider extends StateProviderBase { + stateToInputMap = new Map([ + [State.CachePrimaryKey, Inputs.Key] + ]); + stateToOutputMap = new Map([ [State.CacheMatchedKey, Outputs.CacheMatchedKey], [State.CachePrimaryKey, Outputs.CachePrimaryKey] @@ -41,6 +45,8 @@ export class NullStateProvider extends StateProviderBase { setState = (key: string, value: string) => { core.setOutput(this.stateToOutputMap.get(key) as string, value); }; - // eslint-disable-next-line @typescript-eslint/no-unused-vars - getState = (key: string) => ""; + + getState = (key: string) => { + return core.getInput(this.stateToInputMap.get(key) as string); + }; } diff --git a/src/utils/testUtils.ts b/src/utils/testUtils.ts index 9e2134f..8edef15 100644 --- a/src/utils/testUtils.ts +++ b/src/utils/testUtils.ts @@ -9,6 +9,10 @@ export function setInput(name: string, value: string): void { process.env[getInputName(name)] = value; } +export function getInput(name: string): string { + return process.env[getInputName(name)] as string; +} + interface CacheInput { path: string; key: string; From 667e98af5a2108c90438fbbac17d0a57f6997784 Mon Sep 17 00:00:00 2001 From: Sankalp Kotewar <98868223+kotewar@users.noreply.github.com> Date: Tue, 3 Jan 2023 10:22:38 +0000 Subject: [PATCH 4/4] Fixed test cases --- __tests__/saveOnly.test.ts | 4 ++++ __tests__/stateProvider.test.ts | 21 +++++++++++++-------- dist/restore-only/index.js | 7 ++++++- dist/restore/index.js | 7 ++++++- dist/save-only/index.js | 7 ++++++- dist/save/index.js | 7 ++++++- src/stateProvider.ts | 7 ++++++- 7 files changed, 47 insertions(+), 13 deletions(-) diff --git a/__tests__/saveOnly.test.ts b/__tests__/saveOnly.test.ts index b9bf246..a431e73 100644 --- a/__tests__/saveOnly.test.ts +++ b/__tests__/saveOnly.test.ts @@ -15,6 +15,10 @@ beforeAll(() => { return testUtils.getInput(name); }); + jest.spyOn(core, "getState").mockImplementation(name => { + return jest.requireActual("@actions/core").getState(name); + }); + jest.spyOn(actionUtils, "getInputAsArray").mockImplementation( (name, options) => { return jest diff --git a/__tests__/stateProvider.test.ts b/__tests__/stateProvider.test.ts index 39ca350..540e065 100644 --- a/__tests__/stateProvider.test.ts +++ b/__tests__/stateProvider.test.ts @@ -1,11 +1,12 @@ import * as core from "@actions/core"; -import { Events, RefKey, State } from "../src/constants"; +import { Events, Inputs, RefKey, State } from "../src/constants"; import { IStateProvider, NullStateProvider, StateProvider } from "../src/stateProvider"; +import * as testUtils from "../src/utils/testUtils"; jest.mock("@actions/core"); @@ -62,7 +63,7 @@ test("NullStateProvider saves outputs", async () => { const getInputMock = jest .spyOn(core, "getInput") - .mockImplementation(key => states.get(key) || ""); + .mockImplementation(key => testUtils.getInput(key)); const getStateMock = jest .spyOn(core, "getState") @@ -73,7 +74,7 @@ test("NullStateProvider saves outputs", async () => { const setOutputMock = jest .spyOn(core, "setOutput") .mockImplementation((key, value) => { - return jest.requireActual("@actions/core").setOutput(key, value); + states.set(key, value); }); const saveStateMock = jest @@ -83,14 +84,18 @@ test("NullStateProvider saves outputs", async () => { }); const cacheMatchedKey = "node-cache"; + const cachePrimaryKey = "primary-key"; const nullStateProvider: IStateProvider = new NullStateProvider(); - nullStateProvider.setState(State.CacheMatchedKey, "outputValue"); - nullStateProvider.setState(State.CachePrimaryKey, cacheMatchedKey); - nullStateProvider.getState("outputKey"); - nullStateProvider.getCacheState(); + testUtils.setInput(Inputs.Key, cachePrimaryKey); + nullStateProvider.setState(State.CachePrimaryKey, cachePrimaryKey); + nullStateProvider.setState(State.CacheMatchedKey, cacheMatchedKey); + const output1 = nullStateProvider.getState(State.CachePrimaryKey); + const output2 = nullStateProvider.getCacheState(); expect(getStateMock).toHaveBeenCalledTimes(0); - expect(getInputMock).toHaveBeenCalledTimes(2); + expect(getInputMock).toHaveBeenCalledTimes(1); + expect(output1).toBe("primary-key"); + expect(output2).toBe(undefined); expect(setOutputMock).toHaveBeenCalledTimes(2); expect(saveStateMock).toHaveBeenCalledTimes(0); }); diff --git a/dist/restore-only/index.js b/dist/restore-only/index.js index bb5d187..f47996e 100644 --- a/dist/restore-only/index.js +++ b/dist/restore-only/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/restore/index.js b/dist/restore/index.js index c15e0f7..829985a 100644 --- a/dist/restore/index.js +++ b/dist/restore/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/save-only/index.js b/dist/save-only/index.js index f9e20ed..a2be0c1 100644 --- a/dist/save-only/index.js +++ b/dist/save-only/index.js @@ -9469,9 +9469,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/dist/save/index.js b/dist/save/index.js index 065b00f..41bf2d6 100644 --- a/dist/save/index.js +++ b/dist/save/index.js @@ -9413,9 +9413,14 @@ class NullStateProvider extends StateProviderBase { [constants_1.State.CachePrimaryKey, constants_1.Outputs.CachePrimaryKey] ]); this.setState = (key, value) => { - core.setOutput(this.stateToOutputMap.get(key), value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key), value); + } }; this.getState = (key) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key)); }; } diff --git a/src/stateProvider.ts b/src/stateProvider.ts index a2de728..131ec69 100644 --- a/src/stateProvider.ts +++ b/src/stateProvider.ts @@ -43,10 +43,15 @@ export class NullStateProvider extends StateProviderBase { ]); setState = (key: string, value: string) => { - core.setOutput(this.stateToOutputMap.get(key) as string, value); + if (this.stateToOutputMap.has(key)) { + core.setOutput(this.stateToOutputMap.get(key) as string, value); + } }; getState = (key: string) => { + if (!this.stateToInputMap.has(key)) { + return ""; + } return core.getInput(this.stateToInputMap.get(key) as string); }; }