|
| 1 | +import { |
| 2 | + existsSync, |
| 3 | + mkdtempSync, |
| 4 | + mkdirSync, |
| 5 | + readFileSync, |
| 6 | + rmSync, |
| 7 | + writeFileSync, |
| 8 | +} from "node:fs"; |
| 9 | +import os from "node:os"; |
| 10 | +import path from "node:path"; |
| 11 | +import test from "node:test"; |
| 12 | +import assert from "node:assert/strict"; |
| 13 | + |
| 14 | +import { LocalActionsManager } from "./LocalActionsManager.js"; |
| 15 | + |
| 16 | +const createFixture = () => { |
| 17 | + const rootDirectory = mkdtempSync(path.join(os.tmpdir(), "local-actions-")); |
| 18 | + const actionsDirectory = path.join(rootDirectory, "actions"); |
| 19 | + const currentActionPath = path.join( |
| 20 | + actionsDirectory, |
| 21 | + "create-or-update-comment", |
| 22 | + ); |
| 23 | + const siblingActionPath = path.join(actionsDirectory, "get-issue-number"); |
| 24 | + |
| 25 | + mkdirSync(currentActionPath, { recursive: true }); |
| 26 | + mkdirSync(siblingActionPath, { recursive: true }); |
| 27 | + writeFileSync( |
| 28 | + path.join(currentActionPath, "action.yml"), |
| 29 | + "name: current\n", |
| 30 | + "utf8", |
| 31 | + ); |
| 32 | + writeFileSync( |
| 33 | + path.join(siblingActionPath, "action.yml"), |
| 34 | + "name: sibling\n", |
| 35 | + "utf8", |
| 36 | + ); |
| 37 | + |
| 38 | + return { |
| 39 | + actionsDirectory, |
| 40 | + currentActionPath, |
| 41 | + rootDirectory, |
| 42 | + selfActionsPath: path.join(actionsDirectory, "self-actions"), |
| 43 | + teardown() { |
| 44 | + rmSync(rootDirectory, { force: true, recursive: true }); |
| 45 | + }, |
| 46 | + }; |
| 47 | +}; |
| 48 | + |
| 49 | +test("prepare copies sibling actions into the destination directory", async () => { |
| 50 | + const fixture = createFixture(); |
| 51 | + const manager = new LocalActionsManager(); |
| 52 | + |
| 53 | + try { |
| 54 | + const result = await manager.prepare({ |
| 55 | + destination: "../self-actions", |
| 56 | + sourcePath: fixture.currentActionPath, |
| 57 | + }); |
| 58 | + |
| 59 | + assert.equal(result.created, true); |
| 60 | + assert.equal(result.destinationPath, fixture.selfActionsPath); |
| 61 | + assert.equal( |
| 62 | + readFileSync( |
| 63 | + path.join(fixture.selfActionsPath, "get-issue-number", "action.yml"), |
| 64 | + "utf8", |
| 65 | + ), |
| 66 | + "name: sibling\n", |
| 67 | + ); |
| 68 | + assert.equal( |
| 69 | + readFileSync( |
| 70 | + path.join( |
| 71 | + fixture.selfActionsPath, |
| 72 | + "create-or-update-comment", |
| 73 | + "action.yml", |
| 74 | + ), |
| 75 | + "utf8", |
| 76 | + ), |
| 77 | + "name: current\n", |
| 78 | + ); |
| 79 | + assert.equal( |
| 80 | + existsSync(path.join(fixture.selfActionsPath, "self-actions")), |
| 81 | + false, |
| 82 | + ); |
| 83 | + } finally { |
| 84 | + fixture.teardown(); |
| 85 | + } |
| 86 | +}); |
| 87 | + |
| 88 | +test("prepare reuses an existing destination without marking it for cleanup", async () => { |
| 89 | + const fixture = createFixture(); |
| 90 | + const manager = new LocalActionsManager(); |
| 91 | + |
| 92 | + try { |
| 93 | + mkdirSync(fixture.selfActionsPath, { recursive: true }); |
| 94 | + writeFileSync( |
| 95 | + path.join(fixture.selfActionsPath, "marker.txt"), |
| 96 | + "existing\n", |
| 97 | + "utf8", |
| 98 | + ); |
| 99 | + |
| 100 | + const result = await manager.prepare({ |
| 101 | + destination: "../self-actions", |
| 102 | + sourcePath: fixture.currentActionPath, |
| 103 | + }); |
| 104 | + |
| 105 | + assert.equal(result.created, false); |
| 106 | + assert.equal( |
| 107 | + readFileSync(path.join(fixture.selfActionsPath, "marker.txt"), "utf8"), |
| 108 | + "existing\n", |
| 109 | + ); |
| 110 | + } finally { |
| 111 | + fixture.teardown(); |
| 112 | + } |
| 113 | +}); |
| 114 | + |
| 115 | +test("cleanup removes the destination only when it was created by the action", async () => { |
| 116 | + const fixture = createFixture(); |
| 117 | + const manager = new LocalActionsManager(); |
| 118 | + |
| 119 | + try { |
| 120 | + await manager.prepare({ |
| 121 | + destination: "../self-actions", |
| 122 | + sourcePath: fixture.currentActionPath, |
| 123 | + }); |
| 124 | + |
| 125 | + assert.equal( |
| 126 | + await manager.cleanup({ |
| 127 | + created: true, |
| 128 | + destinationPath: fixture.selfActionsPath, |
| 129 | + }), |
| 130 | + true, |
| 131 | + ); |
| 132 | + assert.equal( |
| 133 | + await manager.cleanup({ |
| 134 | + created: false, |
| 135 | + destinationPath: fixture.selfActionsPath, |
| 136 | + }), |
| 137 | + false, |
| 138 | + ); |
| 139 | + } finally { |
| 140 | + fixture.teardown(); |
| 141 | + } |
| 142 | +}); |
0 commit comments