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