mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Use powershell functions to wrap npm, npx, yarn, pnpm and pnpx
This commit is contained in:
parent
eba1e9cc8e
commit
577b09bd39
6 changed files with 217 additions and 117 deletions
|
|
@ -69,49 +69,43 @@ describe("PowerShell Core shell integration", () => {
|
|||
});
|
||||
|
||||
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);
|
||||
it("should add init-pwsh.ps1 source line", () => {
|
||||
const result = powershell.setup();
|
||||
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")
|
||||
content.includes(". \"$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh.ps1\" # Safe-chain PowerShell initialization script")
|
||||
);
|
||||
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", () => {
|
||||
it("should remove init-pwsh.ps1 source line", () => {
|
||||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias npm aikido-npm",
|
||||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
". \"$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh.ps1\" # Safe-chain PowerShell initialization script",
|
||||
"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(". \"$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh.ps1\""));
|
||||
assert.ok(content.includes("Set-Alias ls "));
|
||||
assert.ok(content.includes("Set-Alias grep "));
|
||||
});
|
||||
|
||||
it("should remove old-style aliases from earlier versions", () => {
|
||||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias npm aikido-npm # Safe-chain alias for npm",
|
||||
"Set-Alias npx aikido-npx # Safe-chain alias for npx",
|
||||
"Set-Alias yarn aikido-yarn # Safe-chain alias for yarn",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
"Set-Alias grep Select-String",
|
||||
].join("\n");
|
||||
|
|
@ -138,7 +132,7 @@ describe("PowerShell Core shell integration", () => {
|
|||
assert.strictEqual(result, true);
|
||||
});
|
||||
|
||||
it("should handle file with no relevant aliases", () => {
|
||||
it("should handle file with no relevant content", () => {
|
||||
const initialContent = [
|
||||
"# PowerShell profile",
|
||||
"Set-Alias ls Get-ChildItem",
|
||||
|
|
@ -171,34 +165,25 @@ describe("PowerShell Core shell integration", () => {
|
|||
|
||||
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);
|
||||
powershell.setup();
|
||||
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(". \"$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh.ps1\""));
|
||||
|
||||
// Teardown
|
||||
powershell.teardown(tools);
|
||||
powershell.teardown(knownAikidoTools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("Set-Alias npm "));
|
||||
assert.ok(!content.includes("Set-Alias yarn "));
|
||||
assert.ok(!content.includes(". \"$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh.ps1\""));
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
const tools = [{ tool: "npm", aikidoCommand: "aikido-npm" }];
|
||||
|
||||
powershell.setup(tools);
|
||||
powershell.teardown(tools);
|
||||
powershell.setup(tools);
|
||||
powershell.setup();
|
||||
powershell.teardown(knownAikidoTools);
|
||||
powershell.setup();
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const npmMatches = (content.match(/Set-Alias npm /g) || []).length;
|
||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
||||
const sourceMatches = (content.match(/\. "\$HOME\\\\.safe-chain\\\\scripts\\\\init-pwsh\.ps1"/g) || []).length;
|
||||
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue