Skip to content

Commit

Permalink
Add env var to configure local cache path
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
beagleknight committed Dec 12, 2024
1 parent e7c0944 commit 4f22d82
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/plugins/shadowdog-local-cache.test.ts
Original file line number Diff line number Diff line change
@@ -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', () => {
Expand Down Expand Up @@ -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)
})
})
})
2 changes: 2 additions & 0 deletions src/plugins/shadowdog-local-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,8 @@ const middleware: Middleware<PluginOptions> = 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)
Expand Down

0 comments on commit 4f22d82

Please # to comment.