Add regular setup support

This commit is contained in:
Reinier Criel 2026-04-10 12:09:40 -07:00
parent 1635bee387
commit 24af6f21eb
23 changed files with 575 additions and 48 deletions

View file

@ -2,9 +2,11 @@ import {
addLineToFile, addLineToFile,
doesExecutableExistOnSystem, doesExecutableExistOnSystem,
removeLinesMatchingPattern, removeLinesMatchingPattern,
getScriptsDir,
} from "../helpers.js"; } from "../helpers.js";
import { execSync, spawnSync } from "child_process"; import { execSync, spawnSync } from "child_process";
import * as os from "os"; import * as os from "os";
import path from "path";
const shellName = "Bash"; const shellName = "Bash";
const executableName = "bash"; const executableName = "bash";
@ -32,10 +34,10 @@ function teardown(tools) {
); );
} }
// Removes the line that sources the safe-chain bash initialization script (~/.safe-chain/scripts/init-posix.sh) // Removes the line that sources the safe-chain bash initialization script (any path, requires safe-chain comment)
removeLinesMatchingPattern( removeLinesMatchingPattern(
startupFile, startupFile,
/^source\s+~\/\.safe-chain\/scripts\/init-posix\.sh/, /^source\s+.*init-posix\.sh.*#\s*Safe-chain/,
eol eol
); );
@ -47,7 +49,7 @@ function setup() {
addLineToFile( addLineToFile(
startupFile, startupFile,
`source ~/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script`, `source ${path.join(getScriptsDir(), "init-posix.sh")} # Safe-chain bash initialization script`,
eol eol
); );

View file

@ -19,6 +19,7 @@ describe("Bash shell integration", () => {
mock.module("../helpers.js", { mock.module("../helpers.js", {
namedExports: { namedExports: {
doesExecutableExistOnSystem: () => true, doesExecutableExistOnSystem: () => true,
getScriptsDir: () => "/test-home/.safe-chain/scripts",
addLineToFile: (filePath, line) => { addLineToFile: (filePath, line) => {
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, "", "utf-8"); fs.writeFileSync(filePath, "", "utf-8");
@ -109,7 +110,7 @@ describe("Bash 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-posix.sh # Safe-chain bash initialization script" "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script"
) )
); );
}); });
@ -129,7 +130,7 @@ describe("Bash shell integration", () => {
const content = fs.readFileSync(windowsCygwinPath, "utf-8"); const content = fs.readFileSync(windowsCygwinPath, "utf-8");
assert.ok( assert.ok(
content.includes( content.includes(
"source ~/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script" "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script"
) )
); );
}); });
@ -209,13 +210,13 @@ 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("source ~/.safe-chain/scripts/init-posix.sh")); assert.ok(content.includes("source /test-home/.safe-chain/scripts/init-posix.sh"));
// Teardown // Teardown
bash.teardown(tools); bash.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
!content.includes("source ~/.safe-chain/scripts/init-posix.sh") !content.includes("source /test-home/.safe-chain/scripts/init-posix.sh")
); );
}); });
@ -236,7 +237,7 @@ describe("Bash shell integration", () => {
const initialContent = [ const initialContent = [
"#!/bin/bash", "#!/bin/bash",
"alias npm='old-npm'", "alias npm='old-npm'",
"source ~/.safe-chain/scripts/init-posix.sh", "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script",
"alias ls='ls --color=auto'", "alias ls='ls --color=auto'",
].join("\n"); ].join("\n");
@ -247,7 +248,7 @@ describe("Bash shell integration", () => {
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( assert.ok(
!content.includes("source ~/.safe-chain/scripts/init-posix.sh") !content.includes("source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain bash initialization script")
); );
assert.ok(content.includes("alias ls=")); assert.ok(content.includes("alias ls="));
}); });

View file

@ -2,8 +2,10 @@ import {
addLineToFile, addLineToFile,
doesExecutableExistOnSystem, doesExecutableExistOnSystem,
removeLinesMatchingPattern, removeLinesMatchingPattern,
getScriptsDir,
} from "../helpers.js"; } from "../helpers.js";
import { execSync } from "child_process"; import { execSync } from "child_process";
import path from "path";
const shellName = "Fish"; const shellName = "Fish";
const executableName = "fish"; const executableName = "fish";
@ -31,10 +33,10 @@ function teardown(tools) {
); );
} }
// Removes the line that sources the safe-chain fish initialization script (~/.safe-chain/scripts/init-fish.fish) // Removes the line that sources the safe-chain fish initialization script (any path, requires safe-chain comment)
removeLinesMatchingPattern( removeLinesMatchingPattern(
startupFile, startupFile,
/^source\s+~\/\.safe-chain\/scripts\/init-fish\.fish/, /^source\s+.*init-fish\.fish.*#\s*Safe-chain/,
eol eol
); );
@ -46,7 +48,7 @@ function setup() {
addLineToFile( addLineToFile(
startupFile, startupFile,
`source ~/.safe-chain/scripts/init-fish.fish # Safe-chain Fish initialization script`, `source ${path.join(getScriptsDir(), "init-fish.fish")} # Safe-chain Fish initialization script`,
eol eol
); );

View file

@ -17,6 +17,7 @@ describe("Fish shell integration", () => {
mock.module("../helpers.js", { mock.module("../helpers.js", {
namedExports: { namedExports: {
doesExecutableExistOnSystem: () => true, doesExecutableExistOnSystem: () => true,
getScriptsDir: () => "/test-home/.safe-chain/scripts",
addLineToFile: (filePath, line) => { addLineToFile: (filePath, line) => {
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, "", "utf-8"); fs.writeFileSync(filePath, "", "utf-8");
@ -72,7 +73,7 @@ describe("Fish shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
content.includes('source ~/.safe-chain/scripts/init-fish.fish # Safe-chain Fish initialization script') content.includes('source /test-home/.safe-chain/scripts/init-fish.fish # Safe-chain Fish initialization script')
); );
}); });
@ -81,7 +82,7 @@ describe("Fish shell integration", () => {
fish.setup(); fish.setup();
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = (content.match(/source ~\/\.safe-chain\/scripts\/init-fish\.fish/g) || []).length; const sourceMatches = (content.match(/source \/test-home\/\.safe-chain\/scripts\/init-fish\.fish/g) || []).length;
assert.strictEqual(sourceMatches, 2, "Should allow multiple source lines (helper doesn't dedupe)"); assert.strictEqual(sourceMatches, 2, "Should allow multiple source lines (helper doesn't dedupe)");
}); });
}); });
@ -93,7 +94,7 @@ describe("Fish shell integration", () => {
"alias npm 'aikido-npm'", "alias npm 'aikido-npm'",
"alias npx 'aikido-npx'", "alias npx 'aikido-npx'",
"alias yarn 'aikido-yarn'", "alias yarn 'aikido-yarn'",
"source ~/.safe-chain/scripts/init-fish.fish # Safe-chain Fish initialization script", "source /test-home/.safe-chain/scripts/init-fish.fish # Safe-chain Fish initialization script",
"alias ls 'ls --color=auto'", "alias ls 'ls --color=auto'",
"alias grep 'grep --color=auto'", "alias grep 'grep --color=auto'",
].join("\n"); ].join("\n");
@ -107,7 +108,7 @@ describe("Fish shell integration", () => {
assert.ok(!content.includes("alias npm ")); assert.ok(!content.includes("alias npm "));
assert.ok(!content.includes("alias npx ")); assert.ok(!content.includes("alias npx "));
assert.ok(!content.includes("alias yarn ")); assert.ok(!content.includes("alias yarn "));
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-fish.fish")); assert.ok(!content.includes("source /test-home/.safe-chain/scripts/init-fish.fish"));
assert.ok(content.includes("alias ls ")); assert.ok(content.includes("alias ls "));
assert.ok(content.includes("alias grep ")); assert.ok(content.includes("alias grep "));
}); });
@ -162,12 +163,12 @@ describe("Fish shell integration", () => {
// Setup // Setup
fish.setup(); fish.setup();
let content = fs.readFileSync(mockStartupFile, "utf-8"); let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(content.includes('source ~/.safe-chain/scripts/init-fish.fish')); assert.ok(content.includes('source /test-home/.safe-chain/scripts/init-fish.fish'));
// Teardown // Teardown
fish.teardown(tools); fish.teardown(tools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok(!content.includes("source ~/.safe-chain/scripts/init-fish.fish")); assert.ok(!content.includes("source /test-home/.safe-chain/scripts/init-fish.fish"));
}); });
it("should handle multiple setup calls", () => { it("should handle multiple setup calls", () => {
@ -176,7 +177,7 @@ describe("Fish shell integration", () => {
fish.setup(); fish.setup();
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = (content.match(/source ~\/\.safe-chain\/scripts\/init-fish\.fish/g) || []).length; const sourceMatches = (content.match(/source \/test-home\/\.safe-chain\/scripts\/init-fish\.fish/g) || []).length;
assert.strictEqual(sourceMatches, 1, "Should have exactly one source line after setup-teardown-setup cycle"); assert.strictEqual(sourceMatches, 1, "Should have exactly one source line after setup-teardown-setup cycle");
}); });
}); });

View file

@ -3,8 +3,10 @@ import {
doesExecutableExistOnSystem, doesExecutableExistOnSystem,
removeLinesMatchingPattern, removeLinesMatchingPattern,
validatePowerShellExecutionPolicy, validatePowerShellExecutionPolicy,
getScriptsDir,
} from "../helpers.js"; } from "../helpers.js";
import { execSync } from "child_process"; import { execSync } from "child_process";
import path from "path";
const shellName = "PowerShell Core"; const shellName = "PowerShell Core";
const executableName = "pwsh"; const executableName = "pwsh";
@ -30,10 +32,10 @@ function teardown(tools) {
); );
} }
// Remove the line that sources the safe-chain PowerShell initialization script // Remove the line that sources the safe-chain PowerShell initialization script (any path, requires safe-chain comment)
removeLinesMatchingPattern( removeLinesMatchingPattern(
startupFile, startupFile,
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/, /^\.\s+["']?.*init-pwsh\.ps1["']?.*#\s*Safe-chain/,
); );
return true; return true;
@ -52,7 +54,7 @@ async function setup() {
addLineToFile( addLineToFile(
startupFile, startupFile,
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`, `. "${path.join(getScriptsDir(), "init-pwsh.ps1")}" # Safe-chain PowerShell initialization script`,
); );
return true; return true;

View file

@ -40,6 +40,7 @@ describe("PowerShell Core shell integration", () => {
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8"); fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
}, },
validatePowerShellExecutionPolicy: () => executionPolicyResult, validatePowerShellExecutionPolicy: () => executionPolicyResult,
getScriptsDir: () => "/test-home/.safe-chain/scripts",
}, },
}); });
@ -83,7 +84,7 @@ describe("PowerShell Core shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
content.includes( content.includes(
'. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script', '. "/test-home/.safe-chain/scripts/init-pwsh.ps1" # Safe-chain PowerShell initialization script',
), ),
); );
}); });
@ -93,7 +94,7 @@ describe("PowerShell Core shell integration", () => {
it("should remove init-pwsh.ps1 source line", () => { it("should remove init-pwsh.ps1 source line", () => {
const initialContent = [ const initialContent = [
"# PowerShell profile", "# PowerShell profile",
'. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script', '. "/test-home/.safe-chain/scripts/init-pwsh.ps1" # Safe-chain PowerShell initialization script',
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"Set-Alias grep Select-String", "Set-Alias grep Select-String",
].join("\n"); ].join("\n");
@ -105,7 +106,7 @@ describe("PowerShell Core shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
!content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), !content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
assert.ok(content.includes("Set-Alias ls ")); assert.ok(content.includes("Set-Alias ls "));
assert.ok(content.includes("Set-Alias grep ")); assert.ok(content.includes("Set-Alias grep "));
@ -180,14 +181,14 @@ describe("PowerShell Core shell integration", () => {
await powershell.setup(); await powershell.setup();
let content = fs.readFileSync(mockStartupFile, "utf-8"); let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
// Teardown // Teardown
powershell.teardown(knownAikidoTools); powershell.teardown(knownAikidoTools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
!content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), !content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
}); });
@ -198,7 +199,7 @@ describe("PowerShell Core shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = ( const sourceMatches = (
content.match(/\. "\$HOME\\.safe-chain\\scripts\\init-pwsh\.ps1"/g) || content.match(/\. "\/test-home\/\.safe-chain\/scripts\/init-pwsh\.ps1"/g) ||
[] []
).length; ).length;
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines"); assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");

View file

@ -3,8 +3,10 @@ import {
doesExecutableExistOnSystem, doesExecutableExistOnSystem,
removeLinesMatchingPattern, removeLinesMatchingPattern,
validatePowerShellExecutionPolicy, validatePowerShellExecutionPolicy,
getScriptsDir,
} from "../helpers.js"; } from "../helpers.js";
import { execSync } from "child_process"; import { execSync } from "child_process";
import path from "path";
const shellName = "Windows PowerShell"; const shellName = "Windows PowerShell";
const executableName = "powershell"; const executableName = "powershell";
@ -30,10 +32,10 @@ function teardown(tools) {
); );
} }
// Remove the line that sources the safe-chain PowerShell initialization script // Remove the line that sources the safe-chain PowerShell initialization script (any path, requires safe-chain comment)
removeLinesMatchingPattern( removeLinesMatchingPattern(
startupFile, startupFile,
/^\.\s+["']?\$HOME[/\\].safe-chain[/\\]scripts[/\\]init-pwsh\.ps1["']?/, /^\.\s+["']?.*init-pwsh\.ps1["']?.*#\s*Safe-chain/,
); );
return true; return true;
@ -52,7 +54,7 @@ async function setup() {
addLineToFile( addLineToFile(
startupFile, startupFile,
`. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script`, `. "${path.join(getScriptsDir(), "init-pwsh.ps1")}" # Safe-chain PowerShell initialization script`,
); );
return true; return true;

View file

@ -40,6 +40,7 @@ describe("Windows PowerShell shell integration", () => {
fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8"); fs.writeFileSync(filePath, filteredLines.join("\n"), "utf-8");
}, },
validatePowerShellExecutionPolicy: () => executionPolicyResult, validatePowerShellExecutionPolicy: () => executionPolicyResult,
getScriptsDir: () => "/test-home/.safe-chain/scripts",
}, },
}); });
@ -83,7 +84,7 @@ describe("Windows PowerShell shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
content.includes( content.includes(
'. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script', '. "/test-home/.safe-chain/scripts/init-pwsh.ps1" # Safe-chain PowerShell initialization script',
), ),
); );
}); });
@ -93,7 +94,7 @@ describe("Windows PowerShell shell integration", () => {
it("should remove init-pwsh.ps1 source line", () => { it("should remove init-pwsh.ps1 source line", () => {
const initialContent = [ const initialContent = [
"# Windows PowerShell profile", "# Windows PowerShell profile",
'. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1" # Safe-chain PowerShell initialization script', '. "/test-home/.safe-chain/scripts/init-pwsh.ps1" # Safe-chain PowerShell initialization script',
"Set-Alias ls Get-ChildItem", "Set-Alias ls Get-ChildItem",
"Set-Alias grep Select-String", "Set-Alias grep Select-String",
].join("\n"); ].join("\n");
@ -105,7 +106,7 @@ describe("Windows PowerShell shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
!content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), !content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
assert.ok(content.includes("Set-Alias ls ")); assert.ok(content.includes("Set-Alias ls "));
assert.ok(content.includes("Set-Alias grep ")); assert.ok(content.includes("Set-Alias grep "));
@ -180,14 +181,14 @@ describe("Windows PowerShell shell integration", () => {
await windowsPowershell.setup(); await windowsPowershell.setup();
let content = fs.readFileSync(mockStartupFile, "utf-8"); let content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
// Teardown // Teardown
windowsPowershell.teardown(knownAikidoTools); windowsPowershell.teardown(knownAikidoTools);
content = fs.readFileSync(mockStartupFile, "utf-8"); content = fs.readFileSync(mockStartupFile, "utf-8");
assert.ok( assert.ok(
!content.includes('. "$HOME\\.safe-chain\\scripts\\init-pwsh.ps1"'), !content.includes('. "/test-home/.safe-chain/scripts/init-pwsh.ps1"'),
); );
}); });
@ -198,7 +199,7 @@ describe("Windows PowerShell shell integration", () => {
const content = fs.readFileSync(mockStartupFile, "utf-8"); const content = fs.readFileSync(mockStartupFile, "utf-8");
const sourceMatches = ( const sourceMatches = (
content.match(/\. "\$HOME\\.safe-chain\\scripts\\init-pwsh\.ps1"/g) || content.match(/\. "\/test-home\/\.safe-chain\/scripts\/init-pwsh\.ps1"/g) ||
[] []
).length; ).length;
assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines"); assert.strictEqual(sourceMatches, 1, "Should not duplicate source lines");

View file

@ -2,8 +2,10 @@ import {
addLineToFile, addLineToFile,
doesExecutableExistOnSystem, doesExecutableExistOnSystem,
removeLinesMatchingPattern, removeLinesMatchingPattern,
getScriptsDir,
} from "../helpers.js"; } from "../helpers.js";
import { execSync } from "child_process"; import { execSync } from "child_process";
import path from "path";
const shellName = "Zsh"; const shellName = "Zsh";
const executableName = "zsh"; const executableName = "zsh";
@ -31,10 +33,10 @@ function teardown(tools) {
); );
} }
// Removes the line that sources the safe-chain zsh initialization script (~/.safe-chain/scripts/init-posix.sh) // Removes the line that sources the safe-chain zsh initialization script (any path, requires safe-chain comment)
removeLinesMatchingPattern( removeLinesMatchingPattern(
startupFile, startupFile,
/^source\s+~\/\.safe-chain\/scripts\/init-posix\.sh/, /^source\s+.*init-posix\.sh.*#\s*Safe-chain/,
eol eol
); );
@ -46,7 +48,7 @@ function setup() {
addLineToFile( addLineToFile(
startupFile, startupFile,
`source ~/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script`, `source ${path.join(getScriptsDir(), "init-posix.sh")} # Safe-chain Zsh initialization script`,
eol eol
); );

View file

@ -17,6 +17,7 @@ describe("Zsh shell integration", () => {
mock.module("../helpers.js", { mock.module("../helpers.js", {
namedExports: { namedExports: {
doesExecutableExistOnSystem: () => true, doesExecutableExistOnSystem: () => true,
getScriptsDir: () => "/test-home/.safe-chain/scripts",
addLineToFile: (filePath, line) => { addLineToFile: (filePath, line) => {
if (!fs.existsSync(filePath)) { if (!fs.existsSync(filePath)) {
fs.writeFileSync(filePath, "", "utf-8"); fs.writeFileSync(filePath, "", "utf-8");
@ -73,7 +74,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-posix.sh # Safe-chain Zsh initialization script" "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script"
) )
); );
}); });
@ -83,7 +84,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-posix.sh")); assert.ok(content.includes("source /test-home/.safe-chain/scripts/init-posix.sh"));
}); });
}); });
@ -114,7 +115,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-posix.sh", "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script",
"alias ls='ls --color=auto'", "alias ls='ls --color=auto'",
].join("\n"); ].join("\n");
@ -125,7 +126,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("source ~/.safe-chain/scripts/init-posix.sh") !content.includes("source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script")
); );
assert.ok(content.includes("alias ls=")); assert.ok(content.includes("alias ls="));
}); });
@ -180,13 +181,13 @@ 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-posix.sh")); assert.ok(content.includes("source /test-home/.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( assert.ok(
!content.includes("source ~/.safe-chain/scripts/init-posix.sh") !content.includes("source /test-home/.safe-chain/scripts/init-posix.sh")
); );
}); });
@ -207,7 +208,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-posix.sh", "source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script",
"alias ls='ls --color=auto'", "alias ls='ls --color=auto'",
].join("\n"); ].join("\n");
@ -218,7 +219,7 @@ describe("Zsh shell integration", () => {
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( assert.ok(
!content.includes("source ~/.safe-chain/scripts/init-posix.sh") !content.includes("source /test-home/.safe-chain/scripts/init-posix.sh # Safe-chain Zsh initialization script")
); );
assert.ok(content.includes("alias ls=")); assert.ok(content.includes("alias ls="));
}); });

View file

@ -78,4 +78,39 @@ describe("E2E: bun coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("bash");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious bun packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("bash");
const result = await shell.runCommand("bunx safe-chain-test");
assert.ok(
result.output.includes("blocked 1 malicious package downloads"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -102,4 +102,47 @@ describe("E2E: npm coverage using PATH", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup-ci");
// Persist SAFE_CHAIN_DIR and the custom shims dir in .zshrc so new shells
// inherit both (shims need SAFE_CHAIN_DIR to strip themselves from PATH)
await setupShell.runCommand(
`echo 'export SAFE_CHAIN_DIR=${CUSTOM_DIR}' >> ~/.zshrc`
);
await setupShell.runCommand(
`echo 'export PATH="${CUSTOM_DIR}/shims:$PATH"' >> ~/.zshrc`
);
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious npm packages when shims are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("npm i safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -119,4 +119,39 @@ describe("E2E: npm coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious npm packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("npm i safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -204,4 +204,44 @@ describe("E2E: safe-chain setup-ci command for pip/pip3", () => {
); );
}); });
} }
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand("pip3 cache purge");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup-ci");
await setupShell.runCommand(
`echo 'export SAFE_CHAIN_DIR=${CUSTOM_DIR}' >> ~/.zshrc`
);
await setupShell.runCommand(
`echo 'export PATH="${CUSTOM_DIR}/shims:$PATH"' >> ~/.zshrc`
);
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("intercepts pip3 install when shims are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand(
"pip3 install --break-system-packages certifi --safe-chain-logging=verbose"
);
assert.ok(
result.output.includes("no malware found."),
`Expected pip3 to be protected with SAFE_CHAIN_DIR. Output:\n${result.output}`
);
});
});
}); });

View file

@ -844,4 +844,40 @@ describe("E2E: pip coverage", () => {
`python -m pip SHOULD go through safe-chain. Output was:\n${result.output}` `python -m pip SHOULD go through safe-chain. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
await setupShell.runCommand("pip3 cache purge");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("intercepts pip3 install when scripts are in a custom directory", async () => {
// New shell sources ~/.zshrc → sources init-posix.sh from custom dir
// → defines pip3() shell function that routes through safe-chain
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand(
"pip3 install --break-system-packages requests --safe-chain-logging=verbose"
);
assert.ok(
result.output.includes("no malware found."),
`Expected pip3 to be protected with SAFE_CHAIN_DIR. Output:\n${result.output}`
);
});
});
}); });

View file

@ -197,4 +197,39 @@ describe("E2E: pipx coverage", () => {
`Expected exit message. Output was:\n${result.output}` `Expected exit message. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious pipx packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("pipx install safe-chain-pi-test");
assert.ok(
result.output.includes("blocked by safe-chain"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -122,4 +122,45 @@ describe("E2E: pnpm coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup-ci");
await setupShell.runCommand(
`echo 'export SAFE_CHAIN_DIR=${CUSTOM_DIR}' >> ~/.zshrc`
);
await setupShell.runCommand(
`echo 'export PATH="${CUSTOM_DIR}/shims:$PATH"' >> ~/.zshrc`
);
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious pnpm packages when shims are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("pnpm add safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -139,4 +139,39 @@ describe("E2E: pnpm coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious pnpm packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("pnpm add safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -422,4 +422,46 @@ describe("E2E: poetry coverage", () => {
`Expected env list output. Output was:\n${envListResult.output}` `Expected env list output. Output was:\n${envListResult.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
await setupShell.runCommand("command poetry cache clear pypi --all -n");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious poetry packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
await shell.runCommand("mkdir /tmp/test-poetry-custom-dir");
await shell.runCommand(
"cd /tmp/test-poetry-custom-dir && poetry init --no-interaction"
);
const result = await shell.runCommand(
"cd /tmp/test-poetry-custom-dir && poetry add safe-chain-pi-test"
);
assert.ok(
result.output.includes("blocked by safe-chain"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -64,6 +64,57 @@ describe("E2E: SAFE_CHAIN_DIR support", () => {
); );
}); });
it("setup writes the custom path to ~/.bashrc when SAFE_CHAIN_DIR is set", async () => {
const shell = await container.openShell("bash");
await shell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await shell.runCommand("safe-chain setup");
const result = await shell.runCommand("cat ~/.bashrc");
assert.ok(
result.output.includes(`source ${CUSTOM_DIR}/scripts/init-posix.sh`),
`Expected ~/.bashrc to contain custom scripts path. Output:\n${result.output}`
);
assert.ok(
!result.output.includes("source ~/.safe-chain/scripts/init-posix.sh"),
`Expected ~/.bashrc to NOT contain default path. Output:\n${result.output}`
);
});
it("setup with SAFE_CHAIN_DIR still protects npm in a new shell session", async () => {
// Run setup with the custom dir
const setupShell = await container.openShell("bash");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
// Open a fresh shell — it will source ~/.bashrc which sources init-posix.sh
// from the custom dir, defining the npm wrapper function
const projectShell = await container.openShell("bash");
await projectShell.runCommand("cd /testapp");
const result = await projectShell.runCommand(
"npm i axios@1.13.0 --safe-chain-logging=verbose"
);
// "Safe-chain: Package" appears before npm downloads — confirms interception happened
assert.ok(
result.output.includes("Safe-chain: Package"),
`Expected npm to be protected after setup with SAFE_CHAIN_DIR. Output:\n${result.output}`
);
});
it("teardown removes the custom SAFE_CHAIN_DIR source line from ~/.bashrc", async () => {
const shell = await container.openShell("bash");
await shell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await shell.runCommand("safe-chain setup");
await shell.runCommand("safe-chain teardown");
const result = await shell.runCommand("cat ~/.bashrc");
assert.ok(
!result.output.includes(`source ${CUSTOM_DIR}/scripts/init-posix.sh`),
`Expected custom source line to be removed from ~/.bashrc. Output:\n${result.output}`
);
});
it("safe-chain protects a non-root user when installed to a shared dir with SAFE_CHAIN_DIR", async () => { it("safe-chain protects a non-root user when installed to a shared dir with SAFE_CHAIN_DIR", async () => {
// Step 1: create a non-root user inside the container // Step 1: create a non-root user inside the container
container.dockerExec("useradd -m safeuser"); container.dockerExec("useradd -m safeuser");

View file

@ -569,4 +569,43 @@ describe("E2E: uv coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
await setupShell.runCommand("uv cache clean");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious uv packages when scripts are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
await shell.runCommand("uv init test-project-custom-dir");
const result = await shell.runCommand(
"cd test-project-custom-dir && uv add safe-chain-pi-test"
);
assert.ok(
result.output.includes("blocked 1 malicious package downloads:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -84,4 +84,45 @@ describe("E2E: yarn coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup-ci");
await setupShell.runCommand(
`echo 'export SAFE_CHAIN_DIR=${CUSTOM_DIR}' >> ~/.zshrc`
);
await setupShell.runCommand(
`echo 'export PATH="${CUSTOM_DIR}/shims:$PATH"' >> ~/.zshrc`
);
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious yarn packages when shims are in a custom directory", async () => {
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("yarn add safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });

View file

@ -125,4 +125,43 @@ describe("E2E: yarn coverage", () => {
`Output did not include expected text. Output was:\n${result.output}` `Output did not include expected text. Output was:\n${result.output}`
); );
}); });
describe("with SAFE_CHAIN_DIR (custom install directory)", () => {
const CUSTOM_DIR = "/usr/local/.safe-chain";
let customContainer;
beforeEach(async () => {
customContainer = new DockerTestContainer();
await customContainer.start();
// Run setup with the custom dir — init-posix.sh is copied to the custom
// scripts dir, and ~/.zshrc gets a source line pointing there
const setupShell = await customContainer.openShell("zsh");
await setupShell.runCommand(`export SAFE_CHAIN_DIR=${CUSTOM_DIR}`);
await setupShell.runCommand("safe-chain setup");
});
afterEach(async () => {
if (customContainer) {
await customContainer.stop();
customContainer = null;
}
});
it("blocks malicious yarn packages when scripts are in a custom directory", async () => {
// New shell sources ~/.zshrc → sources init-posix.sh from custom dir
// → defines yarn() shell function that routes through safe-chain
const shell = await customContainer.openShell("zsh");
const result = await shell.runCommand("yarn add safe-chain-test");
assert.ok(
result.output.includes("Malicious changes detected:"),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
assert.ok(
result.output.includes("Exiting without installing malicious packages."),
`Expected malicious package to be blocked. Output:\n${result.output}`
);
});
});
}); });