mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 12:10: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() {
|
function copyStartupFiles() {
|
||||||
const startupFiles = ["init-zsh.sh"];
|
const startupFiles = ["init-posix.sh"];
|
||||||
|
|
||||||
for (const file of startupFiles) {
|
for (const file of startupFiles) {
|
||||||
const targetPath = path.join(os.homedir(), ".safe-chain", "scripts", file);
|
const targetPath = path.join(os.homedir(), ".safe-chain", "scripts", file);
|
||||||
|
|
|
||||||
|
|
@ -21,18 +21,22 @@ function teardown(tools) {
|
||||||
removeLinesMatchingPattern(startupFile, new RegExp(`^alias\\s+${tool}=`));
|
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;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup(tools) {
|
function setup() {
|
||||||
const startupFile = getStartupFile();
|
const startupFile = getStartupFile();
|
||||||
|
|
||||||
for (const { tool, aikidoCommand } of tools) {
|
|
||||||
addLineToFile(
|
addLineToFile(
|
||||||
startupFile,
|
startupFile,
|
||||||
`alias ${tool}="${aikidoCommand}" # Safe-chain alias for ${tool}`
|
`source ~/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script`
|
||||||
);
|
);
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -66,37 +66,16 @@ describe("Bash shell integration", () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
describe("setup", () => {
|
describe("setup", () => {
|
||||||
it("should add aliases for all provided tools", () => {
|
it("should add source line for bash initialization script", () => {
|
||||||
const tools = [
|
const result = bash.setup();
|
||||||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
|
||||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
|
||||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
|
||||||
];
|
|
||||||
|
|
||||||
const result = bash.setup(tools);
|
|
||||||
assert.strictEqual(result, true);
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(
|
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
|
// Setup
|
||||||
bash.setup(tools);
|
bash.setup(tools);
|
||||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(content.includes('alias npm="aikido-npm"'));
|
assert.ok(content.includes("source ~/.safe-chain/scripts/init-posix.sh"));
|
||||||
assert.ok(content.includes('alias yarn="aikido-yarn"'));
|
|
||||||
|
|
||||||
// Teardown
|
// Teardown
|
||||||
bash.teardown(tools);
|
bash.teardown(tools);
|
||||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(!content.includes("alias npm="));
|
assert.ok(
|
||||||
assert.ok(!content.includes("alias yarn="));
|
!content.includes("source ~/.safe-chain/scripts/init-posix.sh")
|
||||||
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
it("should handle multiple setup calls", () => {
|
it("should handle multiple setup calls", () => {
|
||||||
|
|
@ -192,8 +171,29 @@ describe("Bash shell integration", () => {
|
||||||
bash.setup(tools);
|
bash.setup(tools);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
const npmMatches = (content.match(/alias npm="/g) || []).length;
|
const sourceMatches = (content.match(/source.*init-posix\.sh/g) || [])
|
||||||
assert.strictEqual(npmMatches, 1, "Should not duplicate aliases");
|
.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}=`));
|
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(
|
removeLinesMatchingPattern(
|
||||||
startupFile,
|
startupFile,
|
||||||
/^source\s+~\/\.safe-chain\/scripts\/init-zsh\.sh/
|
/^source\s+~\/\.safe-chain\/scripts\/init-posix\.sh/
|
||||||
);
|
);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setup(tools) {
|
function setup() {
|
||||||
const startupFile = getStartupFile();
|
const startupFile = getStartupFile();
|
||||||
|
|
||||||
addLineToFile(
|
addLineToFile(
|
||||||
startupFile,
|
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;
|
return true;
|
||||||
|
|
|
||||||
|
|
@ -73,7 +73,7 @@ describe("Zsh shell integration", () => {
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(
|
assert.ok(
|
||||||
content.includes(
|
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);
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
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", () => {
|
it("should remove zsh initialization script source line", () => {
|
||||||
const initialContent = [
|
const initialContent = [
|
||||||
"#!/bin/zsh",
|
"#!/bin/zsh",
|
||||||
"source ~/.safe-chain/scripts/init-zsh.sh",
|
"source ~/.safe-chain/scripts/init-posix.sh",
|
||||||
"alias ls='ls --color=auto'",
|
"alias ls='ls --color=auto'",
|
||||||
].join("\n");
|
].join("\n");
|
||||||
|
|
||||||
|
|
@ -124,7 +124,9 @@ describe("Zsh shell integration", () => {
|
||||||
assert.strictEqual(result, true);
|
assert.strictEqual(result, true);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
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="));
|
assert.ok(content.includes("alias ls="));
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -178,12 +180,14 @@ describe("Zsh shell integration", () => {
|
||||||
// Setup
|
// Setup
|
||||||
zsh.setup();
|
zsh.setup();
|
||||||
let content = fs.readFileSync(mockStartupFile, "utf-8");
|
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
|
// Teardown
|
||||||
zsh.teardown(tools);
|
zsh.teardown(tools);
|
||||||
content = fs.readFileSync(mockStartupFile, "utf-8");
|
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", () => {
|
it("should handle multiple setup calls", () => {
|
||||||
|
|
@ -194,7 +198,7 @@ describe("Zsh shell integration", () => {
|
||||||
zsh.setup(tools);
|
zsh.setup(tools);
|
||||||
|
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
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;
|
.length;
|
||||||
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");
|
||||||
});
|
});
|
||||||
|
|
@ -203,7 +207,7 @@ describe("Zsh shell integration", () => {
|
||||||
const initialContent = [
|
const initialContent = [
|
||||||
"#!/bin/zsh",
|
"#!/bin/zsh",
|
||||||
"alias npm='old-npm'",
|
"alias npm='old-npm'",
|
||||||
"source ~/.safe-chain/scripts/init-zsh.sh",
|
"source ~/.safe-chain/scripts/init-posix.sh",
|
||||||
"alias ls='ls --color=auto'",
|
"alias ls='ls --color=auto'",
|
||||||
].join("\n");
|
].join("\n");
|
||||||
|
|
||||||
|
|
@ -213,7 +217,9 @@ describe("Zsh shell integration", () => {
|
||||||
zsh.teardown(knownAikidoTools);
|
zsh.teardown(knownAikidoTools);
|
||||||
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
const content = fs.readFileSync(mockStartupFile, "utf-8");
|
||||||
assert.ok(!content.includes("alias npm="));
|
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="));
|
assert.ok(content.includes("alias ls="));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue