mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Move project to separate folder
This commit is contained in:
parent
dc3a7bf6e2
commit
b1ca2d2dc5
62 changed files with 4 additions and 1 deletions
|
|
@ -1,44 +0,0 @@
|
|||
import { spawnSync } from "child_process";
|
||||
import * as os from "os";
|
||||
import fs from "fs";
|
||||
|
||||
export const knownAikidoTools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
{ tool: "pnpm", aikidoCommand: "aikido-pnpm" },
|
||||
{ tool: "pnpx", aikidoCommand: "aikido-pnpx" },
|
||||
// When adding a new tool here, also update the expected alias in the tests (setup.spec.js, teardown.spec.js)
|
||||
// and add the documentation for the new tool in the README.md
|
||||
];
|
||||
|
||||
export function doesExecutableExistOnSystem(executableName) {
|
||||
if (os.platform() === "win32") {
|
||||
const result = spawnSync("where", [executableName], { stdio: "ignore" });
|
||||
return result.status === 0;
|
||||
} else {
|
||||
const result = spawnSync("which", [executableName], { stdio: "ignore" });
|
||||
return result.status === 0;
|
||||
}
|
||||
}
|
||||
|
||||
export function removeLinesMatchingPattern(filePath, pattern) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
return;
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = fileContent.split(os.EOL);
|
||||
const updatedLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, updatedLines.join(os.EOL), "utf-8");
|
||||
}
|
||||
|
||||
export function addLineToFile(filePath, line) {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const updatedContent = fileContent + os.EOL + line;
|
||||
fs.writeFileSync(filePath, updatedContent, "utf-8");
|
||||
}
|
||||
|
|
@ -1,75 +0,0 @@
|
|||
import chalk from "chalk";
|
||||
import { ui } from "../environment/userInteraction.js";
|
||||
import { detectShells } from "./shellDetection.js";
|
||||
import { knownAikidoTools } from "./helpers.js";
|
||||
|
||||
/**
|
||||
* Loops over the detected shells and calls the setup function for each.
|
||||
*/
|
||||
export async function setup() {
|
||||
ui.writeInformation(
|
||||
chalk.bold("Setting up shell aliases.") +
|
||||
" This will wrap safe-chain around npm, npx, and yarn commands."
|
||||
);
|
||||
ui.emptyLine();
|
||||
|
||||
try {
|
||||
const shells = detectShells();
|
||||
if (shells.length === 0) {
|
||||
ui.writeError("No supported shells detected. Cannot set up aliases.");
|
||||
return;
|
||||
}
|
||||
|
||||
ui.writeInformation(
|
||||
`Detected ${shells.length} supported shell(s): ${shells
|
||||
.map((shell) => chalk.bold(shell.name))
|
||||
.join(", ")}.`
|
||||
);
|
||||
|
||||
let updatedCount = 0;
|
||||
for (const shell of shells) {
|
||||
if (setupShell(shell)) {
|
||||
updatedCount++;
|
||||
}
|
||||
}
|
||||
|
||||
if (updatedCount > 0) {
|
||||
ui.emptyLine();
|
||||
ui.writeInformation(`Please restart your terminal to apply the changes.`);
|
||||
}
|
||||
} catch (error) {
|
||||
ui.writeError(
|
||||
`Failed to set up shell aliases: ${error.message}. Please check your shell configuration.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calls the setup function for the given shell and reports the result.
|
||||
*/
|
||||
function setupShell(shell) {
|
||||
let success = false;
|
||||
try {
|
||||
shell.teardown(knownAikidoTools); // First, tear down to prevent duplicate aliases
|
||||
success = shell.setup(knownAikidoTools);
|
||||
} catch {
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ui.writeInformation(
|
||||
`${chalk.bold("- " + shell.name + ":")} ${chalk.green(
|
||||
"Setup successful"
|
||||
)}`
|
||||
);
|
||||
} else {
|
||||
ui.writeError(
|
||||
`${chalk.bold("- " + shell.name + ":")} ${chalk.red(
|
||||
"Setup failed"
|
||||
)}. Please check your ${shell.name} configuration.`
|
||||
);
|
||||
}
|
||||
|
||||
return success;
|
||||
}
|
||||
|
|
@ -1,26 +0,0 @@
|
|||
import zsh from "./supported-shells/zsh.js";
|
||||
import bash from "./supported-shells/bash.js";
|
||||
import powershell from "./supported-shells/powershell.js";
|
||||
import windowsPowershell from "./supported-shells/windowsPowershell.js";
|
||||
import fish from "./supported-shells/fish.js";
|
||||
import { ui } from "../environment/userInteraction.js";
|
||||
|
||||
export function detectShells() {
|
||||
let possibleShells = [zsh, bash, powershell, windowsPowershell, fish];
|
||||
let availableShells = [];
|
||||
|
||||
try {
|
||||
for (const shell of possibleShells) {
|
||||
if (shell.isInstalled()) {
|
||||
availableShells.push(shell);
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
ui.writeError(
|
||||
`We were not able to detect which shells are installed on your system. Please check your shell configuration. Error: ${error.message}`
|
||||
);
|
||||
return [];
|
||||
}
|
||||
|
||||
return availableShells;
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
import {
|
||||
addLineToFile,
|
||||
doesExecutableExistOnSystem,
|
||||
removeLinesMatchingPattern,
|
||||
} from "../helpers.js";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const shellName = "Bash";
|
||||
const executableName = "bash";
|
||||
const startupFileCommand = "echo ~/.bashrc";
|
||||
|
||||
function isInstalled() {
|
||||
return doesExecutableExistOnSystem(executableName);
|
||||
}
|
||||
|
||||
function teardown(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool } of tools) {
|
||||
// Remove any existing alias for the tool
|
||||
removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`alias ${tool}="${aikidoCommand}" # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStartupFile() {
|
||||
try {
|
||||
return execSync(startupFileCommand, {
|
||||
encoding: "utf8",
|
||||
shell: executableName,
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: shellName,
|
||||
isInstalled,
|
||||
setup,
|
||||
teardown,
|
||||
};
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { tmpdir } from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { knownAikidoTools } from "../helpers.js";
|
||||
|
||||
describe("Bash shell integration", () => {
|
||||
let mockStartupFile;
|
||||
let bash;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(tmpdir(), `test-bashrc-${Date.now()}`);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
doesExecutableExistOnSystem: () => true,
|
||||
addLineToFile: (filePath, line) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
fs.appendFileSync(filePath, line + "\n", "utf-8");
|
||||
},
|
||||
removeLinesMatchingPattern: (filePath, pattern) => {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
mock.module("child_process", {
|
||||
namedExports: {
|
||||
execSync: () => mockStartupFile,
|
||||
},
|
||||
});
|
||||
|
||||
// Import bash module after mocking
|
||||
bash = (await import("./bash.js")).default;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up test files
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("isInstalled", () => {
|
||||
it("should return true when bash is installed", () => {
|
||||
assert.strictEqual(bash.isInstalled(), true);
|
||||
});
|
||||
|
||||
it("should call doesExecutableExistOnSystem with correct parameter", () => {
|
||||
// Test that the method calls the helper with the right executable name
|
||||
assert.strictEqual(bash.isInstalled(), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup", () => {
|
||||
it("should add aliases for all provided tools", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
const result = bash.setup(tools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes('alias npm="aikido-npm" # Safe-chain alias for npm')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias npx="aikido-npx" # Safe-chain alias for npx')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias yarn="aikido-yarn" # Safe-chain alias for yarn')
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty tools array", () => {
|
||||
const result = bash.setup([]);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
// File should be created during teardown call even if no tools are provided
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.strictEqual(content.trim(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("teardown", () => {
|
||||
it("should remove npm, npx, and yarn aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/bash",
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias ls='ls --color=auto'",
|
||||
"alias grep='grep --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = bash.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("alias npx="));
|
||||
assert.ok(!content.includes("alias yarn="));
|
||||
assert.ok(content.includes("alias ls="));
|
||||
assert.ok(content.includes("alias grep="));
|
||||
});
|
||||
|
||||
it("should handle file that doesn't exist", () => {
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = bash.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/bash",
|
||||
"alias ls='ls --color=auto'",
|
||||
"export PATH=$PATH:~/bin",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = bash.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("alias ls="));
|
||||
assert.ok(content.includes("export PATH="));
|
||||
});
|
||||
});
|
||||
|
||||
describe("shell properties", () => {
|
||||
it("should have correct name", () => {
|
||||
assert.strictEqual(bash.name, "Bash");
|
||||
});
|
||||
|
||||
it("should expose all required methods", () => {
|
||||
assert.ok(typeof bash.isInstalled === "function");
|
||||
assert.ok(typeof bash.setup === "function");
|
||||
assert.ok(typeof bash.teardown === "function");
|
||||
assert.ok(typeof bash.name === "string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration tests", () => {
|
||||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
// Setup
|
||||
bash.setup(tools);
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes('alias npm="aikido-npm"'));
|
||||
assert.ok(content.includes('alias yarn="aikido-yarn"'));
|
||||
|
||||
// Teardown
|
||||
bash.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("alias yarn="));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
bash.setup(tools);
|
||||
bash.teardown(tools);
|
||||
bash.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/alias npm="/g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
import {
|
||||
addLineToFile,
|
||||
doesExecutableExistOnSystem,
|
||||
removeLinesMatchingPattern,
|
||||
} from "../helpers.js";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const shellName = "Fish";
|
||||
const executableName = "fish";
|
||||
const startupFileCommand = "echo ~/.config/fish/config.fish";
|
||||
|
||||
function isInstalled() {
|
||||
return doesExecutableExistOnSystem(executableName);
|
||||
}
|
||||
|
||||
function teardown(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool } of tools) {
|
||||
// Remove any existing alias for the tool
|
||||
removeLinesMatchingPattern(
|
||||
startupFile,
|
||||
new RegExp(`^alias\\s+${tool}\\s+`)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`alias ${tool} "${aikidoCommand}" # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStartupFile() {
|
||||
try {
|
||||
return execSync(startupFileCommand, {
|
||||
encoding: "utf8",
|
||||
shell: executableName,
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: shellName,
|
||||
isInstalled,
|
||||
setup,
|
||||
teardown,
|
||||
};
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { tmpdir } from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { knownAikidoTools } from "../helpers.js";
|
||||
|
||||
describe("Fish shell integration", () => {
|
||||
let mockStartupFile;
|
||||
let fish;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(tmpdir(), `test-fish-config-${Date.now()}`);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
doesExecutableExistOnSystem: () => true,
|
||||
addLineToFile: (filePath, line) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
fs.appendFileSync(filePath, line + "\n", "utf-8");
|
||||
},
|
||||
removeLinesMatchingPattern: (filePath, pattern) => {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
mock.module("child_process", {
|
||||
namedExports: {
|
||||
execSync: () => mockStartupFile,
|
||||
},
|
||||
});
|
||||
|
||||
// Import fish module after mocking
|
||||
fish = (await import("./fish.js")).default;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up test files
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("isInstalled", () => {
|
||||
it("should return true when fish is installed", () => {
|
||||
assert.strictEqual(fish.isInstalled(), true);
|
||||
});
|
||||
|
||||
it("should call doesExecutableExistOnSystem with correct parameter", () => {
|
||||
// Test that the method calls the helper with the right executable name
|
||||
assert.strictEqual(fish.isInstalled(), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup", () => {
|
||||
it("should add aliases for all provided tools", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
const result = fish.setup(tools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes('alias npm "aikido-npm" # Safe-chain alias for npm')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias npx "aikido-npx" # Safe-chain alias for npx')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias yarn "aikido-yarn" # Safe-chain alias for yarn')
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty tools array", () => {
|
||||
const result = fish.setup([]);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
// File should be created during teardown call even if no tools are provided
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.strictEqual(content.trim(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("teardown", () => {
|
||||
it("should remove npm, npx, and yarn aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/usr/bin/env fish",
|
||||
"alias npm 'aikido-npm'",
|
||||
"alias npx 'aikido-npx'",
|
||||
"alias yarn 'aikido-yarn'",
|
||||
"alias ls 'ls --color=auto'",
|
||||
"alias grep 'grep --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = fish.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm "));
|
||||
assert.ok(!content.includes("alias npx "));
|
||||
assert.ok(!content.includes("alias yarn "));
|
||||
assert.ok(content.includes("alias ls "));
|
||||
assert.ok(content.includes("alias grep "));
|
||||
});
|
||||
|
||||
it("should handle file that doesn't exist", () => {
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = fish.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/usr/bin/env fish",
|
||||
"alias ls 'ls --color=auto'",
|
||||
"set PATH $PATH ~/bin",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = fish.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("alias ls "));
|
||||
assert.ok(content.includes("set PATH "));
|
||||
});
|
||||
});
|
||||
|
||||
describe("shell properties", () => {
|
||||
it("should have correct name", () => {
|
||||
assert.strictEqual(fish.name, "Fish");
|
||||
});
|
||||
|
||||
it("should expose all required methods", () => {
|
||||
assert.ok(typeof fish.isInstalled === "function");
|
||||
assert.ok(typeof fish.setup === "function");
|
||||
assert.ok(typeof fish.teardown === "function");
|
||||
assert.ok(typeof fish.name === "string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration tests", () => {
|
||||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
// Setup
|
||||
fish.setup(tools);
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes('alias npm "aikido-npm"'));
|
||||
assert.ok(content.includes('alias yarn "aikido-yarn"'));
|
||||
|
||||
// Teardown
|
||||
fish.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm "));
|
||||
assert.ok(!content.includes("alias yarn "));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
fish.setup(tools);
|
||||
fish.teardown(tools);
|
||||
fish.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/alias npm "/g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
import {
|
||||
addLineToFile,
|
||||
doesExecutableExistOnSystem,
|
||||
removeLinesMatchingPattern,
|
||||
} from "../helpers.js";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const shellName = "PowerShell Core";
|
||||
const executableName = "pwsh";
|
||||
const startupFileCommand = "echo $PROFILE";
|
||||
|
||||
function isInstalled() {
|
||||
return doesExecutableExistOnSystem(executableName);
|
||||
}
|
||||
|
||||
function teardown(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool } of tools) {
|
||||
// Remove any existing alias for the tool
|
||||
removeLinesMatchingPattern(
|
||||
startupFile,
|
||||
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`Set-Alias ${tool} ${aikidoCommand} # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStartupFile() {
|
||||
try {
|
||||
return execSync(startupFileCommand, {
|
||||
encoding: "utf8",
|
||||
shell: executableName,
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: shellName,
|
||||
isInstalled,
|
||||
setup,
|
||||
teardown,
|
||||
};
|
||||
|
|
@ -1,204 +0,0 @@
|
|||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { tmpdir } from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { knownAikidoTools } from "../helpers.js";
|
||||
|
||||
describe("PowerShell Core shell integration", () => {
|
||||
let mockStartupFile;
|
||||
let powershell;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(
|
||||
tmpdir(),
|
||||
`test-powershell-profile-${Date.now()}.ps1`
|
||||
);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
doesExecutableExistOnSystem: () => true,
|
||||
addLineToFile: (filePath, line) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
fs.appendFileSync(filePath, line + "\n", "utf-8");
|
||||
},
|
||||
removeLinesMatchingPattern: (filePath, pattern) => {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
mock.module("child_process", {
|
||||
namedExports: {
|
||||
execSync: () => mockStartupFile,
|
||||
},
|
||||
});
|
||||
|
||||
// Import powershell module after mocking
|
||||
powershell = (await import("./powershell.js")).default;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up test files
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("isInstalled", () => {
|
||||
it("should return true when powershell is installed", () => {
|
||||
assert.strictEqual(powershell.isInstalled(), true);
|
||||
});
|
||||
|
||||
it("should call doesExecutableExistOnSystem with correct parameter", () => {
|
||||
// Test that the method calls the helper with the right executable name
|
||||
assert.strictEqual(powershell.isInstalled(), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup", () => {
|
||||
it("should add aliases for all provided tools", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
const result = powershell.setup(tools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes("Set-Alias npm aikido-npm # Safe-chain alias for npm")
|
||||
);
|
||||
assert.ok(
|
||||
content.includes("Set-Alias npx aikido-npx # Safe-chain alias for npx")
|
||||
);
|
||||
assert.ok(
|
||||
content.includes(
|
||||
"Set-Alias yarn aikido-yarn # Safe-chain alias for yarn"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty tools array", () => {
|
||||
const result = powershell.setup([]);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
// File should be created during teardown call even if no tools are provided
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.strictEqual(content.trim(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("teardown", () => {
|
||||
it("should remove npm, npx, and yarn aliases", () => {
|
||||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias npm aikido-npm",
|
||||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"Set-Alias grep Select-String",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias npx "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
assert.ok(content.includes("Set-Alias ls "));
|
||||
assert.ok(content.includes("Set-Alias grep "));
|
||||
});
|
||||
|
||||
it("should handle file that doesn't exist", () => {
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"$env:PATH += ';C:\\Tools'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("Set-Alias ls "));
|
||||
assert.ok(content.includes("$env:PATH "));
|
||||
});
|
||||
});
|
||||
|
||||
describe("shell properties", () => {
|
||||
it("should have correct name", () => {
|
||||
assert.strictEqual(powershell.name, "PowerShell Core");
|
||||
});
|
||||
|
||||
it("should expose all required methods", () => {
|
||||
assert.ok(typeof powershell.isInstalled === "function");
|
||||
assert.ok(typeof powershell.setup === "function");
|
||||
assert.ok(typeof powershell.teardown === "function");
|
||||
assert.ok(typeof powershell.name === "string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration tests", () => {
|
||||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
// Setup
|
||||
powershell.setup(tools);
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("Set-Alias npm aikido-npm"));
|
||||
assert.ok(content.includes("Set-Alias yarn aikido-yarn"));
|
||||
|
||||
// Teardown
|
||||
powershell.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
powershell.setup(tools);
|
||||
powershell.teardown(tools);
|
||||
powershell.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/Set-Alias npm /g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
import {
|
||||
addLineToFile,
|
||||
doesExecutableExistOnSystem,
|
||||
removeLinesMatchingPattern,
|
||||
} from "../helpers.js";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const shellName = "Windows PowerShell";
|
||||
const executableName = "powershell";
|
||||
const startupFileCommand = "echo $PROFILE";
|
||||
|
||||
function isInstalled() {
|
||||
return doesExecutableExistOnSystem(executableName);
|
||||
}
|
||||
|
||||
function teardown(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool } of tools) {
|
||||
// Remove any existing alias for the tool
|
||||
removeLinesMatchingPattern(
|
||||
startupFile,
|
||||
new RegExp(`^Set-Alias\\s+${tool}\\s+`)
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`Set-Alias ${tool} ${aikidoCommand} # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStartupFile() {
|
||||
try {
|
||||
return execSync(startupFileCommand, {
|
||||
encoding: "utf8",
|
||||
shell: executableName,
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: shellName,
|
||||
isInstalled,
|
||||
setup,
|
||||
teardown,
|
||||
};
|
||||
|
|
@ -1,204 +0,0 @@
|
|||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { tmpdir } from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { knownAikidoTools } from "../helpers.js";
|
||||
|
||||
describe("Windows PowerShell shell integration", () => {
|
||||
let mockStartupFile;
|
||||
let windowsPowershell;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(
|
||||
tmpdir(),
|
||||
`test-windows-powershell-profile-${Date.now()}.ps1`
|
||||
);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
doesExecutableExistOnSystem: () => true,
|
||||
addLineToFile: (filePath, line) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
fs.appendFileSync(filePath, line + "\n", "utf-8");
|
||||
},
|
||||
removeLinesMatchingPattern: (filePath, pattern) => {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
mock.module("child_process", {
|
||||
namedExports: {
|
||||
execSync: () => mockStartupFile,
|
||||
},
|
||||
});
|
||||
|
||||
// Import windowsPowershell module after mocking
|
||||
windowsPowershell = (await import("./windowsPowershell.js")).default;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up test files
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("isInstalled", () => {
|
||||
it("should return true when windows powershell is installed", () => {
|
||||
assert.strictEqual(windowsPowershell.isInstalled(), true);
|
||||
});
|
||||
|
||||
it("should call doesExecutableExistOnSystem with correct parameter", () => {
|
||||
// Test that the method calls the helper with the right executable name
|
||||
assert.strictEqual(windowsPowershell.isInstalled(), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup", () => {
|
||||
it("should add aliases for all provided tools", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
const result = windowsPowershell.setup(tools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes("Set-Alias npm aikido-npm # Safe-chain alias for npm")
|
||||
);
|
||||
assert.ok(
|
||||
content.includes("Set-Alias npx aikido-npx # Safe-chain alias for npx")
|
||||
);
|
||||
assert.ok(
|
||||
content.includes(
|
||||
"Set-Alias yarn aikido-yarn # Safe-chain alias for yarn"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty tools array", () => {
|
||||
const result = windowsPowershell.setup([]);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
// File should be created during teardown call even if no tools are provided
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.strictEqual(content.trim(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("teardown", () => {
|
||||
it("should remove npm, npx, and yarn aliases", () => {
|
||||
const initialContent = [
|
||||
"# Windows PowerShell profile",
|
||||
"Set-Alias npm aikido-npm",
|
||||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"Set-Alias grep Select-String",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = windowsPowershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias npx "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
assert.ok(content.includes("Set-Alias ls "));
|
||||
assert.ok(content.includes("Set-Alias grep "));
|
||||
});
|
||||
|
||||
it("should handle file that doesn't exist", () => {
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = windowsPowershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
const initialContent = [
|
||||
"# Windows PowerShell profile",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"$env:PATH += ';C:\\Tools'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = windowsPowershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("Set-Alias ls "));
|
||||
assert.ok(content.includes("$env:PATH "));
|
||||
});
|
||||
});
|
||||
|
||||
describe("shell properties", () => {
|
||||
it("should have correct name", () => {
|
||||
assert.strictEqual(windowsPowershell.name, "Windows PowerShell");
|
||||
});
|
||||
|
||||
it("should expose all required methods", () => {
|
||||
assert.ok(typeof windowsPowershell.isInstalled === "function");
|
||||
assert.ok(typeof windowsPowershell.setup === "function");
|
||||
assert.ok(typeof windowsPowershell.teardown === "function");
|
||||
assert.ok(typeof windowsPowershell.name === "string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration tests", () => {
|
||||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
// Setup
|
||||
windowsPowershell.setup(tools);
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("Set-Alias npm aikido-npm"));
|
||||
assert.ok(content.includes("Set-Alias yarn aikido-yarn"));
|
||||
|
||||
// Teardown
|
||||
windowsPowershell.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
windowsPowershell.setup(tools);
|
||||
windowsPowershell.teardown(tools);
|
||||
windowsPowershell.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/Set-Alias npm /g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
import {
|
||||
addLineToFile,
|
||||
doesExecutableExistOnSystem,
|
||||
removeLinesMatchingPattern,
|
||||
} from "../helpers.js";
|
||||
import { execSync } from "child_process";
|
||||
|
||||
const shellName = "Zsh";
|
||||
const executableName = "zsh";
|
||||
const startupFileCommand = "echo ${ZDOTDIR:-$HOME}/.zshrc";
|
||||
|
||||
function isInstalled() {
|
||||
return doesExecutableExistOnSystem(executableName);
|
||||
}
|
||||
|
||||
function teardown(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool } of tools) {
|
||||
// Remove any existing alias for the tool
|
||||
removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`alias ${tool}="${aikidoCommand}" # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function getStartupFile() {
|
||||
try {
|
||||
return execSync(startupFileCommand, {
|
||||
encoding: "utf8",
|
||||
shell: executableName,
|
||||
}).trim();
|
||||
} catch (error) {
|
||||
throw new Error(
|
||||
`Command failed: ${startupFileCommand}. Error: ${error.message}`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
name: shellName,
|
||||
isInstalled,
|
||||
setup,
|
||||
teardown,
|
||||
};
|
||||
|
|
@ -1,199 +0,0 @@
|
|||
import { describe, it, beforeEach, afterEach, mock } from "node:test";
|
||||
import assert from "node:assert";
|
||||
import { tmpdir } from "node:os";
|
||||
import fs from "node:fs";
|
||||
import path from "path";
|
||||
import { knownAikidoTools } from "../helpers.js";
|
||||
|
||||
describe("Zsh shell integration", () => {
|
||||
let mockStartupFile;
|
||||
let zsh;
|
||||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(tmpdir(), `test-zshrc-${Date.now()}`);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
doesExecutableExistOnSystem: () => true,
|
||||
addLineToFile: (filePath, line) => {
|
||||
if (!fs.existsSync(filePath)) {
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
}
|
||||
fs.appendFileSync(filePath, line + "\n", "utf-8");
|
||||
},
|
||||
removeLinesMatchingPattern: (filePath, pattern) => {
|
||||
if (!fs.existsSync(filePath)) return;
|
||||
const content = fs.readFileSync(filePath, "utf-8");
|
||||
const lines = content.split("\n");
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
mock.module("child_process", {
|
||||
namedExports: {
|
||||
execSync: () => mockStartupFile,
|
||||
},
|
||||
});
|
||||
|
||||
// Import zsh module after mocking
|
||||
zsh = (await import("./zsh.js")).default;
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
// Clean up test files
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
||||
describe("isInstalled", () => {
|
||||
it("should return true when zsh is installed", () => {
|
||||
assert.strictEqual(zsh.isInstalled(), true);
|
||||
});
|
||||
|
||||
it("should call doesExecutableExistOnSystem with correct parameter", () => {
|
||||
// Test that the method calls the helper with the right executable name
|
||||
assert.strictEqual(zsh.isInstalled(), true);
|
||||
});
|
||||
});
|
||||
|
||||
describe("setup", () => {
|
||||
it("should add aliases for all provided tools", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
const result = zsh.setup(tools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes('alias npm="aikido-npm" # Safe-chain alias for npm')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias npx="aikido-npx" # Safe-chain alias for npx')
|
||||
);
|
||||
assert.ok(
|
||||
content.includes('alias yarn="aikido-yarn" # Safe-chain alias for yarn')
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle empty tools array", () => {
|
||||
const result = zsh.setup([]);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
// File should be created during teardown call even if no tools are provided
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.strictEqual(content.trim(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("teardown", () => {
|
||||
it("should remove npm, npx, and yarn aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/zsh",
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias ls='ls --color=auto'",
|
||||
"alias grep='grep --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = zsh.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("alias npx="));
|
||||
assert.ok(!content.includes("alias yarn="));
|
||||
assert.ok(content.includes("alias ls="));
|
||||
assert.ok(content.includes("alias grep="));
|
||||
});
|
||||
|
||||
it("should handle file that doesn't exist", () => {
|
||||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = zsh.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/zsh",
|
||||
"alias ls='ls --color=auto'",
|
||||
"export PATH=$PATH:~/bin",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = zsh.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("alias ls="));
|
||||
assert.ok(content.includes("export PATH="));
|
||||
});
|
||||
});
|
||||
|
||||
describe("shell properties", () => {
|
||||
it("should have correct name", () => {
|
||||
assert.strictEqual(zsh.name, "Zsh");
|
||||
});
|
||||
|
||||
it("should expose all required methods", () => {
|
||||
assert.ok(typeof zsh.isInstalled === "function");
|
||||
assert.ok(typeof zsh.setup === "function");
|
||||
assert.ok(typeof zsh.teardown === "function");
|
||||
assert.ok(typeof zsh.name === "string");
|
||||
});
|
||||
});
|
||||
|
||||
describe("integration tests", () => {
|
||||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
];
|
||||
|
||||
// Setup
|
||||
zsh.setup(tools);
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes('alias npm="aikido-npm"'));
|
||||
assert.ok(content.includes('alias yarn="aikido-yarn"'));
|
||||
|
||||
// Teardown
|
||||
zsh.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("alias yarn="));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
zsh.setup(tools);
|
||||
zsh.teardown(tools);
|
||||
zsh.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/alias npm="/g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -1,61 +0,0 @@
|
|||
import chalk from "chalk";
|
||||
import { ui } from "../environment/userInteraction.js";
|
||||
import { detectShells } from "./shellDetection.js";
|
||||
import { knownAikidoTools } from "./helpers.js";
|
||||
|
||||
export async function teardown() {
|
||||
ui.writeInformation(
|
||||
chalk.bold("Removing shell aliases.") +
|
||||
" This will remove safe-chain aliases for npm, npx, and yarn commands."
|
||||
);
|
||||
ui.emptyLine();
|
||||
|
||||
try {
|
||||
const shells = detectShells();
|
||||
if (shells.length === 0) {
|
||||
ui.writeError("No supported shells detected. Cannot remove aliases.");
|
||||
return;
|
||||
}
|
||||
|
||||
ui.writeInformation(
|
||||
`Detected ${shells.length} supported shell(s): ${shells
|
||||
.map((shell) => chalk.bold(shell.name))
|
||||
.join(", ")}.`
|
||||
);
|
||||
|
||||
let updatedCount = 0;
|
||||
for (const shell of shells) {
|
||||
let success = false;
|
||||
try {
|
||||
success = shell.teardown(knownAikidoTools);
|
||||
} catch {
|
||||
success = false;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
ui.writeInformation(
|
||||
`${chalk.bold("- " + shell.name + ":")} ${chalk.green(
|
||||
"Teardown successful"
|
||||
)}`
|
||||
);
|
||||
updatedCount++;
|
||||
} else {
|
||||
ui.writeError(
|
||||
`${chalk.bold("- " + shell.name + ":")} ${chalk.red(
|
||||
"Teardown failed"
|
||||
)}. Please check your ${shell.name} configuration.`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if (updatedCount > 0) {
|
||||
ui.emptyLine();
|
||||
ui.writeInformation(`Please restart your terminal to apply the changes.`);
|
||||
}
|
||||
} catch (error) {
|
||||
ui.writeError(
|
||||
`Failed to remove shell aliases: ${error.message}. Please check your shell configuration.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue