From 4f22d820e9d2ed229471d25928755b707f2898da Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?David=20Morcillo=20Mu=C3=B1oz?= Date: Thu, 12 Dec 2024 11:41:05 +0100 Subject: [PATCH] Add env var to configure local cache path This adds the option to override the local cache path using an env var. This is useful to override this in specific environments without changing the config file. --- src/plugins/shadowdog-local-cache.test.ts | 40 ++++++++++++++++++++++- src/plugins/shadowdog-local-cache.ts | 2 ++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/src/plugins/shadowdog-local-cache.test.ts b/src/plugins/shadowdog-local-cache.test.ts index f083e31..7bc0537 100644 --- a/src/plugins/shadowdog-local-cache.test.ts +++ b/src/plugins/shadowdog-local-cache.test.ts @@ -1,5 +1,5 @@ import fs from 'fs-extra' -import { describe, it, beforeEach, afterEach, vi, expect } from 'vitest' +import { describe, it, beforeEach, afterEach, vi, expect, afterAll } from 'vitest' import shadowdogLocalCache, { compressArtifact } from './shadowdog-local-cache' describe('shadowdog local cache', () => { @@ -117,4 +117,42 @@ describe('shadowdog local cache', () => { }) }) }) + + describe('when local cache path is overriden by env var', () => { + beforeEach(async () => { + vi.stubEnv('SHADOWDOG_LOCAL_CACHE_PATH', 'tmp/tests/cache_overriden') + fs.writeFileSync('tmp/tests/artifacts/foo', 'foo') + }) + + afterAll(() => { + vi.unstubAllEnvs() + }) + + it('stores the cache in the defined path', async () => { + await shadowdogLocalCache.middleware({ + config: { + command: 'echo foo', + artifacts: [ + { + output: 'tmp/tests/artifacts/foo', + }, + ], + tags: [], + workingDirectory: '', + }, + files: [], + invalidators: { + environment: [], + files: [], + }, + next, + abort: () => {}, + options: { + path: 'tmp/tests/cache', + }, + }) + expect(fs.existsSync('tmp/tests/cache/0adeca2ac6.tar.gz')).toBe(false) + expect(fs.existsSync('tmp/tests/cache_overriden/0adeca2ac6.tar.gz')).toBe(true) + }) + }) }) diff --git a/src/plugins/shadowdog-local-cache.ts b/src/plugins/shadowdog-local-cache.ts index 3915b71..0b38c9d 100644 --- a/src/plugins/shadowdog-local-cache.ts +++ b/src/plugins/shadowdog-local-cache.ts @@ -184,6 +184,8 @@ const middleware: Middleware = async ({ const mergedOptions = pluginOptionsSchema.parse(options) + mergedOptions.path = process.env.SHADOWDOG_LOCAL_CACHE_PATH ?? mergedOptions.path + const currentCache = computeCache([...files, ...invalidators.files], invalidators.environment) fs.mkdirpSync(mergedOptions.path)