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