mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Fix broken shell integration with nvm for Zsh and Bash
This commit is contained in:
parent
4424ba2e5b
commit
24d4862dfd
6 changed files with 63 additions and 53 deletions
|
|
@ -81,7 +81,7 @@ function setupShell(shell) {
|
|||
}
|
||||
|
||||
function copyStartupFiles() {
|
||||
const startupFiles = ["init-zsh.sh"];
|
||||
const startupFiles = ["init-posix.sh"];
|
||||
|
||||
for (const file of startupFiles) {
|
||||
const targetPath = path.join(os.homedir(), ".safe-chain", "scripts", file);
|
||||
|
|
|
|||
|
|
@ -21,18 +21,22 @@ function teardown(tools) {
|
|||
removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
|
||||
}
|
||||
|
||||
// Removes the line that sources the safe-chain bash initialization script (~/.aikido/scripts/init-posix.sh)
|
||||
removeLinesMatchingPattern(
|
||||
startupFile,
|
||||
/^source\s+~\/\.safe-chain\/scripts\/init-posix\.sh/
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
function setup() {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
for (const { tool, aikidoCommand } of tools) {
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`alias ${tool}="${aikidoCommand}" # Safe-chain alias for ${tool}`
|
||||
);
|
||||
}
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`source ~/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script`
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -66,37 +66,16 @@ describe("Bash 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 = bash.setup(tools);
|
||||
it("should add source line for bash initialization script", () => {
|
||||
const result = bash.setup();
|
||||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes('alias npm="aikido-npm" # Safe-chain alias for npm')
|
||||
content.includes(
|
||||
"source ~/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script"
|
||||
)
|
||||
);
|
||||
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(), "");
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -174,14 +153,14 @@ describe("Bash shell integration", () => {
|
|||
// 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"'));
|
||||
assert.ok(content.includes("source ~/.safe-chain/scripts/init-posix.sh"));
|
||||
|
||||
// Teardown
|
||||
bash.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("alias yarn="));
|
||||
assert.ok(
|
||||
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
|
|
@ -192,8 +171,29 @@ describe("Bash shell integration", () => {
|
|||
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");
|
||||
const sourceMatches = (content.match(/source.*init-posix\.sh/g) || [])
|
||||
.length;
|
||||
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||
});
|
||||
|
||||
it("should handle mixed content with aliases and source lines", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/bash",
|
||||
"alias npm='old-npm'",
|
||||
"source ~/.safe-chain/scripts/init-posix.sh",
|
||||
"alias ls='ls --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
fs.writeFileSync(mockStartupFile, initialContent, "utf-8");
|
||||
|
||||
// Teardown should remove both aliases and source line
|
||||
bash.teardown(knownAikidoTools);
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(
|
||||
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||
);
|
||||
assert.ok(content.includes("alias ls="));
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -21,21 +21,21 @@ function teardown(tools) {
|
|||
removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
|
||||
}
|
||||
|
||||
// Removes the line that sources the safe-chain zsh initialization script (~/.aikido/scripts/init-zsh.sh)
|
||||
// Removes the line that sources the safe-chain zsh initialization script (~/.aikido/scripts/init-posix.sh)
|
||||
removeLinesMatchingPattern(
|
||||
startupFile,
|
||||
/^source\s+~\/\.safe-chain\/scripts\/init-zsh\.sh/
|
||||
/^source\s+~\/\.safe-chain\/scripts\/init-posix\.sh/
|
||||
);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function setup(tools) {
|
||||
function setup() {
|
||||
const startupFile = getStartupFile();
|
||||
|
||||
addLineToFile(
|
||||
startupFile,
|
||||
`source ~/.safe-chain/scripts/init-zsh.sh # Safe-chain Zsh initialization script`
|
||||
`source ~/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script`
|
||||
);
|
||||
|
||||
return true;
|
||||
|
|
|
|||
|
|
@ -73,7 +73,7 @@ describe("Zsh shell integration", () => {
|
|||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(
|
||||
content.includes(
|
||||
"source ~/.safe-chain/scripts/init-zsh.sh # Safe-chain Zsh initialization script"
|
||||
"source ~/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script"
|
||||
)
|
||||
);
|
||||
});
|
||||
|
|
@ -83,7 +83,7 @@ describe("Zsh shell integration", () => {
|
|||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||
assert.ok(content.includes("source ~/.safe-chain/scripts/init-posix.sh"));
|
||||
});
|
||||
});
|
||||
|
||||
|
|
@ -114,7 +114,7 @@ describe("Zsh shell integration", () => {
|
|||
it("should remove zsh initialization script source line", () => {
|
||||
const initialContent = [
|
||||
"#!/bin/zsh",
|
||||
"source ~/.safe-chain/scripts/init-zsh.sh",
|
||||
"source ~/.safe-chain/scripts/init-posix.sh",
|
||||
"alias ls='ls --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
|
|
@ -124,7 +124,9 @@ describe("Zsh shell integration", () => {
|
|||
assert.strictEqual(result, true);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||
assert.ok(
|
||||
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||
);
|
||||
assert.ok(content.includes("alias ls="));
|
||||
});
|
||||
|
||||
|
|
@ -178,12 +180,14 @@ describe("Zsh shell integration", () => {
|
|||
// Setup
|
||||
zsh.setup();
|
||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||
assert.ok(content.includes("source ~/.safe-chain/scripts/init-posix.sh"));
|
||||
|
||||
// Teardown
|
||||
zsh.teardown(tools);
|
||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||
assert.ok(
|
||||
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle multiple setup calls", () => {
|
||||
|
|
@ -194,7 +198,7 @@ describe("Zsh shell integration", () => {
|
|||
zsh.setup(tools);
|
||||
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
const sourceMatches = (content.match(/source.*init-zsh\.sh/g) || [])
|
||||
const sourceMatches = (content.match(/source.*init-posix\.sh/g) || [])
|
||||
.length;
|
||||
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||
});
|
||||
|
|
@ -203,7 +207,7 @@ describe("Zsh shell integration", () => {
|
|||
const initialContent = [
|
||||
"#!/bin/zsh",
|
||||
"alias npm='old-npm'",
|
||||
"source ~/.safe-chain/scripts/init-zsh.sh",
|
||||
"source ~/.safe-chain/scripts/init-posix.sh",
|
||||
"alias ls='ls --color=auto'",
|
||||
].join("\n");
|
||||
|
||||
|
|
@ -213,7 +217,9 @@ describe("Zsh shell integration", () => {
|
|||
zsh.teardown(knownAikidoTools);
|
||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||
assert.ok(!content.includes("alias npm="));
|
||||
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-zsh.sh"));
|
||||
assert.ok(
|
||||
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||
);
|
||||
assert.ok(content.includes("alias ls="));
|
||||
});
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue