diff --git a/packages/safe-chain/bin/safe-chain.js b/packages/safe-chain/bin/safe-chain.js index 2793987..ad43104 100755 --- a/packages/safe-chain/bin/safe-chain.js +++ b/packages/safe-chain/bin/safe-chain.js @@ -3,7 +3,7 @@ import chalk from "chalk"; import { ui } from "../src/environment/userInteraction.js"; import { setup } from "../src/shell-integration/setup.js"; -import { teardown } from "../src/shell-integration/teardown.js"; +import { teardown, teardownCi } from "../src/shell-integration/teardown.js"; import { setupCi } from "../src/shell-integration/setup-ci.js"; import { initializeCliArguments } from "../src/config/cliArguments.js"; import { setEcoSystem } from "../src/config/settings.js"; @@ -61,6 +61,7 @@ if (tool) { setup(); } else if (command === "teardown") { teardown(); + teardownCi(); } else if (command === "setup-ci") { setupCi(); } else if (command === "--version" || command === "-v" || command === "-v") { diff --git a/packages/safe-chain/src/shell-integration/helpers.js b/packages/safe-chain/src/shell-integration/helpers.js index 50cea5d..844b48e 100644 --- a/packages/safe-chain/src/shell-integration/helpers.js +++ b/packages/safe-chain/src/shell-integration/helpers.js @@ -113,6 +113,13 @@ export function getPackageManagerList() { return `${tools.join(", ")}, and ${lastTool} commands`; } +/** + * @returns {string} + */ +export function getShimsDir() { + return path.join(os.homedir(), ".safe-chain", "shims"); +} + /** * @param {string} executableName * diff --git a/packages/safe-chain/src/shell-integration/setup-ci.js b/packages/safe-chain/src/shell-integration/setup-ci.js index bc5c5e6..b0a8c83 100644 --- a/packages/safe-chain/src/shell-integration/setup-ci.js +++ b/packages/safe-chain/src/shell-integration/setup-ci.js @@ -1,6 +1,6 @@ import chalk from "chalk"; import { ui } from "../environment/userInteraction.js"; -import { getPackageManagerList, knownAikidoTools } from "./helpers.js"; +import { getPackageManagerList, knownAikidoTools, getShimsDir } from "./helpers.js"; import fs from "fs"; import os from "os"; import path from "path"; @@ -32,7 +32,7 @@ export async function setupCi() { ); ui.emptyLine(); - const shimsDir = path.join(os.homedir(), ".safe-chain", "shims"); + const shimsDir = getShimsDir(); const binDir = path.join(os.homedir(), ".safe-chain", "bin"); // Create the shims directory if it doesn't exist if (!fs.existsSync(shimsDir)) { diff --git a/packages/safe-chain/src/shell-integration/setup-ci.spec.js b/packages/safe-chain/src/shell-integration/setup-ci.spec.js index 92ef82e..b437157 100644 --- a/packages/safe-chain/src/shell-integration/setup-ci.spec.js +++ b/packages/safe-chain/src/shell-integration/setup-ci.spec.js @@ -50,6 +50,7 @@ describe("Setup CI shell integration", () => { { tool: "yarn", aikidoCommand: "aikido-yarn" }, ], getPackageManagerList: () => "npm, yarn", + getShimsDir: () => mockShimsDir, }, }); diff --git a/packages/safe-chain/src/shell-integration/teardown.js b/packages/safe-chain/src/shell-integration/teardown.js index bc83b48..f5f86a9 100644 --- a/packages/safe-chain/src/shell-integration/teardown.js +++ b/packages/safe-chain/src/shell-integration/teardown.js @@ -1,7 +1,8 @@ import chalk from "chalk"; import { ui } from "../environment/userInteraction.js"; import { detectShells } from "./shellDetection.js"; -import { knownAikidoTools, getPackageManagerList } from "./helpers.js"; +import { knownAikidoTools, getPackageManagerList, getShimsDir, } from "./helpers.js"; +import fs from "fs"; /** * @returns {Promise} @@ -62,3 +63,24 @@ export async function teardown() { return; } } + +/** + * @returns {Promise} + */ +export async function teardownCi() { + const shimsDir = getShimsDir(); + if (fs.existsSync(shimsDir)) { + try { + fs.rmSync(shimsDir, { recursive: true, force: true }); + ui.writeInformation( + `${chalk.bold("- CI Shims:")} ${chalk.green("Removed successfully")}` + ); + } catch (/** @type {any} */ error) { + ui.writeError( + `${chalk.bold("- CI Shims:")} ${chalk.red( + "Failed to remove" + )}. Error: ${error.message}` + ); + } + } +} diff --git a/test/e2e/teardown-ci.e2e.spec.js b/test/e2e/teardown-ci.e2e.spec.js new file mode 100644 index 0000000..fe97d5e --- /dev/null +++ b/test/e2e/teardown-ci.e2e.spec.js @@ -0,0 +1,41 @@ +import { describe, it, before, beforeEach, afterEach } from "node:test"; +import { DockerTestContainer } from "./DockerTestContainer.js"; +import assert from "node:assert"; + +describe("E2E: safe-chain teardown command (CI)", () => { + let container; + + before(async () => { + DockerTestContainer.buildImage(); + }); + + beforeEach(async () => { + container = new DockerTestContainer(); + await container.start(); + }); + + afterEach(async () => { + if (container) { + await container.stop(); + container = null; + } + }); + + it("safe-chain teardown removes shims directory created by setup-ci", async () => { + const shell = await container.openShell("bash"); + + // Run setup-ci + await shell.runCommand("safe-chain setup-ci"); + + // Verify shims directory exists + const checkShimsExist = await shell.runCommand("test -d ~/.safe-chain/shims && echo 'exists' || echo 'missing'"); + assert.ok(checkShimsExist.output.includes("exists"), "Shims directory should exist after setup-ci"); + + // Run teardown + await shell.runCommand("safe-chain teardown"); + + // Verify shims directory is gone + const checkShimsGone = await shell.runCommand("test -d ~/.safe-chain/shims && echo 'exists' || echo 'missing'"); + assert.ok(checkShimsGone.output.includes("missing"), "Shims directory should be removed after teardown"); + }); +});