mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Handle comments from the PR
This commit is contained in:
parent
6c269a1bb5
commit
36c195f5a9
14 changed files with 166 additions and 181 deletions
|
|
@ -3,6 +3,7 @@ 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;
|
||||
|
|
@ -10,8 +11,11 @@ describe("PowerShell Core shell integration", () => {
|
|||
|
||||
beforeEach(async () => {
|
||||
// Create temporary startup file for testing
|
||||
mockStartupFile = path.join(tmpdir(), `test-powershell-profile-${Date.now()}.ps1`);
|
||||
|
||||
mockStartupFile = path.join(
|
||||
tmpdir(),
|
||||
`test-powershell-profile-${Date.now()}.ps1`
|
||||
);
|
||||
|
||||
// Mock the helpers module
|
||||
mock.module("../helpers.js", {
|
||||
namedExports: {
|
||||
|
|
@ -26,10 +30,10 @@ describe("PowerShell Core shell integration", () => {
|
|||
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));
|
||||
const filteredLines = lines.filter((line) => !pattern.test(line));
|
||||
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
|
||||
}
|
||||
}
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
// Mock child_process execSync
|
||||
|
|
@ -48,7 +52,7 @@ describe("PowerShell Core shell integration", () => {
|
|||
if (fs.existsSync(mockStartupFile)) {
|
||||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
|
||||
// Reset mocks
|
||||
mock.reset();
|
||||
});
|
||||
|
|
@ -69,34 +73,30 @@ describe("PowerShell Core shell integration", () => {
|
|||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" }
|
||||
{ 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'));
|
||||
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");
|
||||
|
|
@ -113,12 +113,12 @@ describe("PowerShell Core shell integration", () => {
|
|||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"Set-Alias grep Select-String"
|
||||
"Set-Alias grep Select-String",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = powershell.teardown();
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
|
|
@ -134,7 +134,7 @@ describe("PowerShell Core shell integration", () => {
|
|||
fs.unlinkSync(mockStartupFile);
|
||||
}
|
||||
|
||||
const result = powershell.teardown();
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
|
|
@ -142,12 +142,12 @@ describe("PowerShell Core shell integration", () => {
|
|||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"$env:PATH += ';C:\\Tools'"
|
||||
"$env:PATH += ';C:\\Tools'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
const result = powershell.teardown();
|
||||
const result = powershell.teardown(knownAikidoTools);
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
|
|
@ -173,17 +173,17 @@ describe("PowerShell Core shell integration", () => {
|
|||
it("should handle complete setup and teardown cycle", () => {
|
||||
const tools = [
|
||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" }
|
||||
{ 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'));
|
||||
assert.ok(content.includes("Set-Alias npm aikido-npm"));
|
||||
assert.ok(content.includes("Set-Alias yarn aikido-yarn"));
|
||||
|
||||
// Teardown
|
||||
powershell.teardown();
|
||||
powershell.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
|
|
@ -193,8 +193,9 @@ describe("PowerShell Core shell integration", () => {
|
|||
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");
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue