|
| 1 | +import * as fs from "fs"; |
| 2 | +import * as path from "path"; |
| 3 | + |
| 4 | +import test from "ava"; |
| 5 | +import * as sinon from "sinon"; |
| 6 | + |
| 7 | +import * as actionsUtil from "./actions-util"; |
| 8 | +import * as gitUtils from "./git-utils"; |
| 9 | +import { getRunnerLogger } from "./logging"; |
| 10 | +import { |
| 11 | + writeBaseDatabaseOidsFile, |
| 12 | + writeOverlayChangesFile, |
| 13 | +} from "./overlay-database-utils"; |
| 14 | +import { createTestConfig, setupTests } from "./testing-utils"; |
| 15 | +import { withTmpDir } from "./util"; |
| 16 | + |
| 17 | +setupTests(test); |
| 18 | + |
| 19 | +test("writeOverlayChangesFile generates correct changes file", async (t) => { |
| 20 | + await withTmpDir(async (tmpDir) => { |
| 21 | + const dbLocation = path.join(tmpDir, "db"); |
| 22 | + await fs.promises.mkdir(dbLocation, { recursive: true }); |
| 23 | + const sourceRoot = path.join(tmpDir, "src"); |
| 24 | + await fs.promises.mkdir(sourceRoot, { recursive: true }); |
| 25 | + const tempDir = path.join(tmpDir, "temp"); |
| 26 | + await fs.promises.mkdir(tempDir, { recursive: true }); |
| 27 | + |
| 28 | + const logger = getRunnerLogger(true); |
| 29 | + const config = createTestConfig({ dbLocation }); |
| 30 | + |
| 31 | + // Mock the getFileOidsUnderPath function to return base OIDs |
| 32 | + const baseOids = { |
| 33 | + "unchanged.js": "aaa111", |
| 34 | + "modified.js": "bbb222", |
| 35 | + "deleted.js": "ccc333", |
| 36 | + }; |
| 37 | + const getFileOidsStubForBase = sinon |
| 38 | + .stub(gitUtils, "getFileOidsUnderPath") |
| 39 | + .resolves(baseOids); |
| 40 | + |
| 41 | + // Write the base database OIDs file |
| 42 | + await writeBaseDatabaseOidsFile(config, sourceRoot); |
| 43 | + getFileOidsStubForBase.restore(); |
| 44 | + |
| 45 | + // Mock the getFileOidsUnderPath function to return overlay OIDs |
| 46 | + const currentOids = { |
| 47 | + "unchanged.js": "aaa111", |
| 48 | + "modified.js": "ddd444", // Changed OID |
| 49 | + "added.js": "eee555", // New file |
| 50 | + }; |
| 51 | + const getFileOidsStubForOverlay = sinon |
| 52 | + .stub(gitUtils, "getFileOidsUnderPath") |
| 53 | + .resolves(currentOids); |
| 54 | + |
| 55 | + // Write the overlay changes file, which uses the mocked overlay OIDs |
| 56 | + // and the base database OIDs file |
| 57 | + const getTempDirStub = sinon |
| 58 | + .stub(actionsUtil, "getTemporaryDirectory") |
| 59 | + .returns(tempDir); |
| 60 | + const changesFilePath = await writeOverlayChangesFile( |
| 61 | + config, |
| 62 | + sourceRoot, |
| 63 | + logger, |
| 64 | + ); |
| 65 | + getFileOidsStubForOverlay.restore(); |
| 66 | + getTempDirStub.restore(); |
| 67 | + |
| 68 | + const fileContent = await fs.promises.readFile(changesFilePath, "utf-8"); |
| 69 | + const parsedContent = JSON.parse(fileContent) as { changes: string[] }; |
| 70 | + |
| 71 | + t.deepEqual( |
| 72 | + parsedContent.changes.sort(), |
| 73 | + ["added.js", "deleted.js", "modified.js"], |
| 74 | + "Should identify added, deleted, and modified files", |
| 75 | + ); |
| 76 | + }); |
| 77 | +}); |
0 commit comments