mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Refactor to customize shell integration per shell
This commit is contained in:
parent
21cdefadde
commit
fe1ca396b4
16 changed files with 1302 additions and 780 deletions
197
src/shell-integration/supported-shells/powershell.spec.js
Normal file
197
src/shell-integration/supported-shells/powershell.spec.js
Normal file
|
|
@ -0,0 +1,197 @@
|
|||
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";
|
||||
|
||||
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: {
|
||||
execAndGetOutput: () => mockStartupFile,
|
||||
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");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// 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 call teardown before setup", () => {
|
||||
// Pre-populate file with existing aliases
|
||||
fs.writeFileSync(mockStartupFile, 'Set-Alias npm old-npm\nSet-Alias npx old-npx\n', "utf-8");
|
||||
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
powershell.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes('Set-Alias npm old-npm'));
|
||||
assert.ok(content.includes('Set-Alias npm aikido-npm'));
|
||||
});
|
||||
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
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();
|
||||
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.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");
|
||||
});
|
||||
});
|
||||
});
|
||||
Loading…
Add table
Add a link
Reference in a new issue