@factorialco/shadowdog
Version:
<img src="https://raw.githubusercontent.com/factorialco/shadowdog/refs/heads/main/logo.png" alt="drawing" width="100"/>
442 lines (441 loc) • 21.8 kB
JavaScript
"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)(() => {
// Clean up all test directories
if (fs_extra_1.default.existsSync('tmp')) {
fs_extra_1.default.rmSync('tmp', { recursive: true, force: true });
}
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: [],
environment: [],
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: [],
environment: [],
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: [],
environment: [],
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.mkdirpSync('tmp/tests/cache_overriden');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'foo');
});
(0, vitest_1.afterEach)(() => {
// Clean up the overridden cache path
if (fs_extra_1.default.existsSync('tmp/tests/cache_overriden')) {
fs_extra_1.default.rmSync('tmp/tests/cache_overriden', { recursive: true, force: true });
}
});
(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: [],
environment: [],
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);
});
});
(0, vitest_1.describe)('SHA verification before restoring from cache', () => {
(0, vitest_1.describe)('when artifact does not exist', () => {
(0, vitest_1.beforeEach)(async () => {
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'original content');
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)('restores from cache even when artifact does not exist', async () => {
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/foo',
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
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('original content');
});
});
(0, vitest_1.describe)('when artifact exists and SHA matches', () => {
(0, vitest_1.beforeEach)(async () => {
// Create cache with 'original content'
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'original content');
await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/foo', 'tmp/tests/cache/0adeca2ac6.tar.gz');
// Keep the file with same content (SHA should match)
});
(0, vitest_1.it)('skips restore when existing file SHA matches cached content', async () => {
// Record initial content to verify it doesn't change
const initialContent = fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8');
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/foo',
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
next,
abort: () => { },
options: {
path: 'tmp/tests/cache',
read: true,
write: true,
},
eventEmitter,
});
(0, vitest_1.expect)(next).not.toHaveBeenCalled();
// File content should remain unchanged (restore was skipped)
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8')).toBe(initialContent);
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8')).toBe('original content');
});
});
(0, vitest_1.describe)('when artifact exists but SHA does not match', () => {
(0, vitest_1.beforeEach)(async () => {
// Create cache with 'original content'
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'original content');
await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/foo', 'tmp/tests/cache/0adeca2ac6.tar.gz');
// Change the file content (SHA will not match)
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/foo', 'modified content');
});
(0, vitest_1.it)('restores from cache when existing file SHA does not match', async () => {
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/foo',
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
next,
abort: () => { },
options: {
path: 'tmp/tests/cache',
read: true,
write: true,
},
eventEmitter,
});
(0, vitest_1.expect)(next).not.toHaveBeenCalled();
// File should be restored to original content from cache
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/foo', 'utf8')).toBe('original content');
});
});
(0, vitest_1.describe)('when artifact is a directory and SHA matches', () => {
(0, vitest_1.beforeEach)(async () => {
// Create cache with directory containing files
fs_extra_1.default.mkdirpSync('tmp/tests/artifacts/dir');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file1.txt', 'content1');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file2.txt', 'content2');
await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/dir', 'tmp/tests/cache/0adeca2ac6.tar.gz');
// Keep the directory with same content
});
(0, vitest_1.it)('skips restore when existing directory SHA matches cached content', async () => {
// Record initial content to verify it doesn't change
const initialContent1 = fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file1.txt', 'utf8');
const initialContent2 = fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file2.txt', 'utf8');
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/dir',
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
next,
abort: () => { },
options: {
path: 'tmp/tests/cache',
read: true,
write: true,
},
eventEmitter,
});
(0, vitest_1.expect)(next).not.toHaveBeenCalled();
// Directory content should remain unchanged (restore was skipped)
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file1.txt', 'utf8')).toBe(initialContent1);
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file2.txt', 'utf8')).toBe(initialContent2);
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file1.txt', 'utf8')).toBe('content1');
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file2.txt', 'utf8')).toBe('content2');
});
});
(0, vitest_1.describe)('when artifact is a directory with stale files not in cache', () => {
(0, vitest_1.beforeEach)(async () => {
// Create cache with directory containing only file1 and file2
fs_extra_1.default.mkdirpSync('tmp/tests/artifacts/dir');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file1.txt', 'content1');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file2.txt', 'content2');
await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/dir', 'tmp/tests/cache/0adeca2ac6.tar.gz');
// Add a stale file that is NOT in the cache (simulates old gem RBI files)
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/stale-file.txt', 'stale content');
});
(0, vitest_1.it)('removes stale files that are not in the cached tarball', async () => {
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/dir',
replaceOnRestore: true,
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
next,
abort: () => { },
options: {
path: 'tmp/tests/cache',
read: true,
write: true,
},
eventEmitter,
});
(0, vitest_1.expect)(next).not.toHaveBeenCalled();
// Cached files should be present
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file1.txt', 'utf8')).toBe('content1');
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file2.txt', 'utf8')).toBe('content2');
// Stale file should be removed (not present in cache)
(0, vitest_1.expect)(fs_extra_1.default.existsSync('tmp/tests/artifacts/dir/stale-file.txt')).toBe(false);
});
});
(0, vitest_1.describe)('when artifact is a directory and SHA does not match', () => {
(0, vitest_1.beforeEach)(async () => {
// Create cache with directory containing files
fs_extra_1.default.mkdirpSync('tmp/tests/artifacts/dir');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file1.txt', 'content1');
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file2.txt', 'content2');
await (0, shadowdog_local_cache_1.compressArtifact)('tmp/tests/artifacts/dir', 'tmp/tests/cache/0adeca2ac6.tar.gz');
// Modify the directory content (SHA will not match)
fs_extra_1.default.writeFileSync('tmp/tests/artifacts/dir/file1.txt', 'modified content');
});
(0, vitest_1.it)('restores from cache when existing directory SHA does not match', async () => {
await shadowdog_local_cache_1.default.middleware({
config: {
command: 'echo foo',
artifacts: [
{
output: 'tmp/tests/artifacts/dir',
},
],
tags: [],
workingDirectory: '',
},
files: [],
environment: [],
next,
abort: () => { },
options: {
path: 'tmp/tests/cache',
read: true,
write: true,
},
eventEmitter,
});
(0, vitest_1.expect)(next).not.toHaveBeenCalled();
// Directory should be restored to original content from cache
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file1.txt', 'utf8')).toBe('content1');
(0, vitest_1.expect)(fs_extra_1.default.readFileSync('tmp/tests/artifacts/dir/file2.txt', 'utf8')).toBe('content2');
});
});
});
});