Merge pull request #573 from crazy-max/test-cache-post

cache: allow to skip state to send cache directly to gha
This commit is contained in:
CrazyMax
2025-01-21 12:19:39 +01:00
committed by GitHub
2 changed files with 31 additions and 11 deletions

View File

@@ -20,13 +20,27 @@ import os from 'os';
import path from 'path'; import path from 'path';
import {Cache} from '../src/cache'; import {Cache} from '../src/cache';
import {Util} from '../src/util';
const fixturesDir = path.join(__dirname, '.fixtures'); const fixturesDir = path.join(__dirname, '.fixtures');
const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'cache-itg-')); const tmpDir = fs.mkdtempSync(path.join(process.env.TEMP || os.tmpdir(), 'cache-itg-'));
describe('cache', () => { describe('cache', () => {
it('github-repo', async () => { it('caches github-repo', async () => {
const r = (Math.random() + 1).toString(36).substring(7); const r = Util.generateRandomString();
const htcName = `cache-test-github-repo-${r}`;
const c = new Cache({
htcName: htcName,
htcVersion: `v1.0.0+${r}`,
baseCacheDir: path.join(tmpDir, '.cache-test'),
cacheFile: 'github-repo.json'
});
expect(await c.save(path.join(fixturesDir, 'github-repo.json'), true)).not.toEqual('');
expect(await c.find()).not.toEqual('');
});
it('caches github-repo with post state', async () => {
const r = Util.generateRandomString();
const htcName = `cache-test-github-repo-${r}`; const htcName = `cache-test-github-repo-${r}`;
const c = new Cache({ const c = new Cache({
htcName: htcName, htcName: htcName,
@@ -35,6 +49,7 @@ describe('cache', () => {
cacheFile: 'github-repo.json' cacheFile: 'github-repo.json'
}); });
expect(await c.save(path.join(fixturesDir, 'github-repo.json'))).not.toEqual(''); expect(await c.save(path.join(fixturesDir, 'github-repo.json'))).not.toEqual('');
expect(await Cache.post()).not.toBeNull();
expect(await c.find()).not.toEqual(''); expect(await c.find()).not.toEqual('');
}); });
}); });

View File

@@ -55,7 +55,7 @@ export class Cache {
} }
} }
public async save(file: string): Promise<string> { public async save(file: string, skipState?: boolean): Promise<string> {
core.debug(`Cache.save ${file}`); core.debug(`Cache.save ${file}`);
const cachePath = this.copyToCache(file); const cachePath = this.copyToCache(file);
@@ -63,14 +63,19 @@ export class Cache {
core.debug(`Cache.save cached to hosted tool cache ${htcPath}`); core.debug(`Cache.save cached to hosted tool cache ${htcPath}`);
if (!this.ghaNoCache && cache.isFeatureAvailable()) { if (!this.ghaNoCache && cache.isFeatureAvailable()) {
core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`); if (skipState) {
core.saveState( core.debug(`Cache.save caching ${this.ghaCacheKey} to GitHub Actions cache`);
Cache.POST_CACHE_KEY, await cache.saveCache([this.cacheDir], this.ghaCacheKey);
JSON.stringify({ } else {
dir: this.cacheDir, core.debug(`Cache.save sending ${this.ghaCacheKey} to post state`);
key: this.ghaCacheKey core.saveState(
} as CachePostState) Cache.POST_CACHE_KEY,
); JSON.stringify({
dir: this.cacheDir,
key: this.ghaCacheKey
} as CachePostState)
);
}
} }
return cachePath; return cachePath;