UNPKG

@factorialco/shadowdog

Version:

<img src="https://raw.githubusercontent.com/factorialco/shadowdog/refs/heads/main/logo.png" alt="drawing" width="100"/>

211 lines (210 loc) 8.96 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const fs_extra_1 = __importDefault(require("fs-extra")); const vitest_1 = require("vitest"); const shadowdog_local_cache_1 = __importStar(require("./shadowdog-local-cache")); const events_1 = require("../events"); vitest_1.vi.mock('../utils', async () => { const utils = await vitest_1.vi.importActual('../utils'); return { ...utils, computeCache: vitest_1.vi.fn(() => '0adeca2ac6'), computeFileCacheName: vitest_1.vi.fn(() => '0adeca2ac6'), readShadowdogVersion: vitest_1.vi.fn(() => ''), }; }); (0, vitest_1.describe)('shadowdog local cache', () => { const next = vitest_1.vi.fn(() => fs_extra_1.default.writeFile('tmp/tests/artifacts/foo', 'foo')); const eventEmitter = new events_1.ShadowdogEventEmitter(); (0, vitest_1.beforeEach)(() => { fs_extra_1.default.mkdirpSync('tmp/tests/cache'); fs_extra_1.default.mkdirpSync('tmp/tests/artifacts'); }); (0, vitest_1.afterEach)(() => { fs_extra_1.default.rmSync('tmp', { recursive: true }); return next.mockClear(); }); (0, vitest_1.describe)('when cache is not present', () => { (0, vitest_1.it)('executes the next middleware', async () => { await shadowdog_local_cache_1.default.middleware({ config: { command: 'echo foo', artifacts: [ { output: 'tmp/tests/artifacts/foo', }, ], tags: [], workingDirectory: '', }, files: [], invalidators: { environment: [], files: [], }, next, abort: () => { }, options: { path: 'tmp/tests/cache', read: true, write: true, }, eventEmitter, }); (0, vitest_1.expect)(next).toHaveBeenCalled(); }); }); (0, vitest_1.describe)('when cache is present', () => { (0, vitest_1.describe)('when the artifact is a single file', () => { (0, vitest_1.beforeEach)(async () => { fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'foo'); await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/foo', 'tmp/tests/cache/0adeca2ac6.tar.gz'); fs_extra_1.default.rmSync('tmp/tests/artifacts', { recursive: true }); }); (0, vitest_1.it)('does not execute the next middleware', async () => { await shadowdog_local_cache_1.default.middleware({ config: { command: 'echo foo', artifacts: [ { output: 'tmp/tests/artifacts/foo', }, ], tags: [], workingDirectory: '', }, files: [], invalidators: { environment: [], files: [], }, next, abort: () => { }, options: { path: 'tmp/tests/cache', read: true, write: true, }, eventEmitter, }); (0, vitest_1.expect)(next).not.toHaveBeenCalled(); (0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8')).toBe('foo'); }); }); (0, vitest_1.describe)('when the artifact is a folder with some files to ignore', () => { (0, vitest_1.beforeEach)(async () => { fs_extra_1.default.mkdirpSync('tmp/tests/artifacts'); fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'foo'); fs_extra_1.default.writeFileSync('tmp/tests/artifacts/bar', 'bar'); await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts', 'tmp/tests/cache/0adeca2ac6.tar.gz'); fs_extra_1.default.rmSync('tmp/tests/artifacts', { recursive: true }); }); (0, vitest_1.it)('does not execute the next middleware', async () => { await shadowdog_local_cache_1.default.middleware({ config: { command: 'echo foo', artifacts: [ { output: 'tmp/tests/artifacts', ignore: ['tmp/tests/artifacts/bar'], }, ], tags: [], workingDirectory: '', }, files: [], invalidators: { environment: [], files: [], }, next, abort: () => { }, options: { path: 'tmp/tests/cache', read: true, write: true, }, eventEmitter, }); fs_extra_1.default.mkdirpSync('tmp/tests/artifacts'); (0, vitest_1.expect)(next).not.toHaveBeenCalled(); (0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8')).toBe('foo'); (0, vitest_1.expect)(fs_extra_1.default.existsSync('tmp/tests/artifacts/bar')).toBe(false); }); }); }); (0, vitest_1.describe)('when local cache path is overriden by env var', () => { (0, vitest_1.beforeEach)(async () => { vitest_1.vi.stubEnv('SHADOWDOG_LOCAL_CACHE_PATH', 'tmp/tests/cache_overriden'); fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'foo'); }); (0, vitest_1.afterAll)(() => { vitest_1.vi.unstubAllEnvs(); }); (0, vitest_1.it)('stores the cache in the defined path', async () => { await shadowdog_local_cache_1.default.middleware({ config: { command: 'echo foo', artifacts: [ { output: 'tmp/tests/artifacts/foo', }, ], tags: [], workingDirectory: '', }, files: [], invalidators: { environment: [], files: [], }, next, abort: () => { }, options: { path: 'tmp/tests/cache', read: true, write: true, }, eventEmitter, }); (0, vitest_1.expect)(fs_extra_1.default.existsSync('tmp/tests/cache/0adeca2ac6.tar.gz')).toBe(false); (0, vitest_1.expect)(fs_extra_1.default.existsSync('tmp/tests/cache_overriden/0adeca2ac6.tar.gz')).toBe(true); }); }); });