mirror of
https://github.com/AikidoSec/safe-chain.git
synced 2026-05-26 20:20:49 +00:00
Implement pnpm and pnpx support
This commit is contained in:
parent
21cdefadde
commit
f10749923a
14 changed files with 754 additions and 145 deletions
|
|
@ -2,6 +2,8 @@ const knownAikidoTools = [
|
|||
{ tool: "npm", aikidoCommand: "aikido-npm" },
|
||||
{ tool: "npx", aikidoCommand: "aikido-npx" },
|
||||
{ tool: "yarn", aikidoCommand: "aikido-yarn" },
|
||||
{ tool: "pnpm", aikidoCommand: "aikido-pnpm" },
|
||||
{ tool: "pnpx", aikidoCommand: "aikido-pnpx" },
|
||||
// When adding a new tool here, also update the expected alias in the tests (shellIntegration.spec.js)
|
||||
// and add the documentation for the new tool in the README.md
|
||||
];
|
||||
|
|
|
|||
|
|
@ -6,45 +6,79 @@ import { getAliases } from "./helpers.js";
|
|||
import { readOrCreateStartupFile, appendAliasesToFile } from "./setup.js";
|
||||
|
||||
describe("setupShell", () => {
|
||||
function runSetupTestsForEnvironment(shell, startupExtension, expectedAliases) {
|
||||
function runSetupTestsForEnvironment(
|
||||
shell,
|
||||
startupExtension,
|
||||
expectedAliases
|
||||
) {
|
||||
describe(`${shell} shell setup`, () => {
|
||||
it(`should add aliases to ${shell} file`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, "", "alias cls='clear'"];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.addedCount, 3, "Should add 3 aliases");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find no existing aliases");
|
||||
assert.strictEqual(result.failedCount, 0, "Should have no failed aliases");
|
||||
|
||||
|
||||
assert.strictEqual(
|
||||
result.addedCount,
|
||||
expectedAliases.length,
|
||||
`Should add ${expectedAliases.length} aliases`
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find no existing aliases"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.failedCount,
|
||||
0,
|
||||
"Should have no failed aliases"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.ok(updatedContent.includes(alias), `Alias "${alias}" should be added`);
|
||||
assert.ok(
|
||||
updatedContent.includes(alias),
|
||||
`Alias "${alias}" should be added`
|
||||
);
|
||||
}
|
||||
assert.ok(updatedContent.includes("alias cls='clear'"), "Original aliases should remain");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias cls='clear'"),
|
||||
"Original aliases should remain"
|
||||
);
|
||||
});
|
||||
|
||||
it(`should not add aliases if they already exist in ${shell} file`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, "", ...expectedAliases];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.addedCount, 0, "Should add 0 aliases");
|
||||
assert.strictEqual(result.existingCount, 3, "Should find 3 existing aliases");
|
||||
assert.strictEqual(result.failedCount, 0, "Should have no failed aliases");
|
||||
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
expectedAliases.length,
|
||||
`Should find ${expectedAliases.length} existing aliases`
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.failedCount,
|
||||
0,
|
||||
"Should have no failed aliases"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
// Count occurrences to ensure no duplicates were added
|
||||
for (const alias of expectedAliases) {
|
||||
assert.strictEqual(countOccurrences(updatedContent, alias), 1, `Alias "${alias}" should appear exactly once`);
|
||||
assert.strictEqual(
|
||||
countOccurrences(updatedContent, alias),
|
||||
1,
|
||||
`Alias "${alias}" should appear exactly once`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -54,78 +88,144 @@ describe("setupShell", () => {
|
|||
if (fs.existsSync(filePath)) {
|
||||
fs.rmSync(filePath, { force: true });
|
||||
}
|
||||
|
||||
|
||||
// Test readOrCreateStartupFile function
|
||||
const fileContent = readOrCreateStartupFile(filePath);
|
||||
assert.strictEqual(fileContent, "", "Should return empty string for new file");
|
||||
assert.strictEqual(
|
||||
fileContent,
|
||||
"",
|
||||
"Should return empty string for new file"
|
||||
);
|
||||
assert.ok(fs.existsSync(filePath), "File should be created");
|
||||
|
||||
|
||||
// Test adding aliases to the newly created file
|
||||
const aliases = getAliases(filePath);
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.addedCount, 3, "Should add 3 aliases");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find no existing aliases");
|
||||
assert.strictEqual(result.failedCount, 0, "Should have no failed aliases");
|
||||
|
||||
|
||||
assert.strictEqual(
|
||||
result.addedCount,
|
||||
expectedAliases.length,
|
||||
`Should add ${expectedAliases.length} aliases`
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find no existing aliases"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.failedCount,
|
||||
0,
|
||||
"Should have no failed aliases"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.ok(updatedContent.includes(alias), `Alias "${alias}" should be added`);
|
||||
assert.ok(
|
||||
updatedContent.includes(alias),
|
||||
`Alias "${alias}" should be added`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it(`should add aliases only once when called multiple times for ${shell}`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, ""];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
|
||||
|
||||
// First call - should add aliases
|
||||
let fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const result1 = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
assert.strictEqual(result1.addedCount, 3, "First call should add 3 aliases");
|
||||
|
||||
assert.strictEqual(
|
||||
result1.addedCount,
|
||||
expectedAliases.length,
|
||||
`First call should add ${expectedAliases.length} aliases`
|
||||
);
|
||||
|
||||
// Second call - should detect existing aliases
|
||||
fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
const result2 = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
assert.strictEqual(result2.addedCount, 0, "Second call should add 0 aliases");
|
||||
assert.strictEqual(result2.existingCount, 3, "Second call should find 3 existing aliases");
|
||||
|
||||
assert.strictEqual(
|
||||
result2.addedCount,
|
||||
0,
|
||||
"Second call should add 0 aliases"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result2.existingCount,
|
||||
expectedAliases.length,
|
||||
`Second call should find ${expectedAliases.length} existing aliases`
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.strictEqual(countOccurrences(updatedContent, alias), 1, `Alias "${alias}" should appear exactly once`);
|
||||
assert.strictEqual(
|
||||
countOccurrences(updatedContent, alias),
|
||||
1,
|
||||
`Alias "${alias}" should appear exactly once`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it(`should use real getAliases() for ${shell} file`, () => {
|
||||
const filePath = `${tmpdir()}/test${startupExtension}`;
|
||||
const aliases = getAliases(filePath);
|
||||
|
||||
|
||||
// Verify we get the expected aliases for this shell type
|
||||
assert.strictEqual(aliases.length, 3, "Should get 3 aliases (npm, npx, yarn)");
|
||||
assert.strictEqual(
|
||||
aliases.length,
|
||||
expectedAliases.length,
|
||||
"Should get all aliases (npm, npx, yarn)"
|
||||
);
|
||||
for (let i = 0; i < aliases.length; i++) {
|
||||
assert.strictEqual(aliases[i], expectedAliases[i], `Alias ${i} should match expected format`);
|
||||
assert.strictEqual(
|
||||
aliases[i],
|
||||
expectedAliases[i],
|
||||
`Alias ${i} should match expected format`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it(`should handle mixed scenario - some existing, some new for ${shell}`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, "", expectedAliases[0], "alias other='command'"];
|
||||
const lines = [
|
||||
`#!/usr/bin/env ${shell}`,
|
||||
"",
|
||||
expectedAliases[0],
|
||||
"alias other='command'",
|
||||
];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.addedCount, 2, "Should add 2 new aliases");
|
||||
assert.strictEqual(result.existingCount, 1, "Should find 1 existing alias");
|
||||
assert.strictEqual(result.failedCount, 0, "Should have no failed aliases");
|
||||
|
||||
|
||||
assert.strictEqual(
|
||||
result.addedCount,
|
||||
expectedAliases.length - 1,
|
||||
`Should add ${expectedAliases.length - 1} aliases`
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
1,
|
||||
"Should find 1 existing alias"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.failedCount,
|
||||
0,
|
||||
"Should have no failed aliases"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.ok(updatedContent.includes(alias), `Alias "${alias}" should be present`);
|
||||
assert.ok(
|
||||
updatedContent.includes(alias),
|
||||
`Alias "${alias}" should be present`
|
||||
);
|
||||
}
|
||||
assert.ok(updatedContent.includes("alias other='command'"), "Other aliases should remain");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias other='command'"),
|
||||
"Other aliases should remain"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -134,65 +234,91 @@ describe("setupShell", () => {
|
|||
runSetupTestsForEnvironment("bash", ".bashrc", [
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'"
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias pnpm='aikido-pnpm'",
|
||||
"alias pnpx='aikido-pnpx'",
|
||||
]);
|
||||
|
||||
runSetupTestsForEnvironment("zsh", ".zshrc", [
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'"
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias pnpm='aikido-pnpm'",
|
||||
"alias pnpx='aikido-pnpx'",
|
||||
]);
|
||||
|
||||
runSetupTestsForEnvironment("fish", ".fish", [
|
||||
'alias npm "aikido-npm"',
|
||||
'alias npx "aikido-npx"',
|
||||
'alias yarn "aikido-yarn"'
|
||||
'alias yarn "aikido-yarn"',
|
||||
'alias pnpm "aikido-pnpm"',
|
||||
'alias pnpx "aikido-pnpx"',
|
||||
]);
|
||||
|
||||
runSetupTestsForEnvironment("pwsh", ".ps1", [
|
||||
"Set-Alias npm aikido-npm",
|
||||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn"
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
"Set-Alias pnpm aikido-pnpm",
|
||||
"Set-Alias pnpx aikido-pnpx",
|
||||
]);
|
||||
|
||||
describe("readOrCreateStartupFile", () => {
|
||||
it("should read existing file content", () => {
|
||||
const lines = ["#!/usr/bin/env bash", "", "alias test='echo test'"];
|
||||
const filePath = createShellStartupScript(lines, ".bashrc");
|
||||
|
||||
|
||||
const content = readOrCreateStartupFile(filePath);
|
||||
|
||||
assert.ok(content.includes("#!/usr/bin/env bash"), "Should contain shebang");
|
||||
assert.ok(content.includes("alias test='echo test'"), "Should contain existing aliases");
|
||||
|
||||
|
||||
assert.ok(
|
||||
content.includes("#!/usr/bin/env bash"),
|
||||
"Should contain shebang"
|
||||
);
|
||||
assert.ok(
|
||||
content.includes("alias test='echo test'"),
|
||||
"Should contain existing aliases"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
||||
it("should create file if it doesn't exist", () => {
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
if (fs.existsSync(filePath)) {
|
||||
fs.rmSync(filePath, { force: true });
|
||||
}
|
||||
|
||||
|
||||
const content = readOrCreateStartupFile(filePath);
|
||||
|
||||
assert.strictEqual(content, "", "Should return empty string for new file");
|
||||
|
||||
assert.strictEqual(
|
||||
content,
|
||||
"",
|
||||
"Should return empty string for new file"
|
||||
);
|
||||
assert.ok(fs.existsSync(filePath), "File should be created");
|
||||
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
||||
it("should handle empty existing file", () => {
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
fs.writeFileSync(filePath, "", "utf-8");
|
||||
|
||||
|
||||
const content = readOrCreateStartupFile(filePath);
|
||||
|
||||
assert.strictEqual(content, "", "Should return empty string for empty file");
|
||||
|
||||
assert.strictEqual(
|
||||
content,
|
||||
"",
|
||||
"Should return empty string for empty file"
|
||||
);
|
||||
assert.ok(fs.existsSync(filePath), "File should still exist");
|
||||
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
|
@ -203,15 +329,22 @@ describe("setupShell", () => {
|
|||
const lines = ["#!/usr/bin/env bash", "", "alias test='echo test'"];
|
||||
const filePath = createShellStartupScript(lines, ".bashrc");
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = appendAliasesToFile([], fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.addedCount, 0, "Should add 0 aliases");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find 0 existing aliases");
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find 0 existing aliases"
|
||||
);
|
||||
assert.strictEqual(result.failedCount, 0, "Should have 0 failed aliases");
|
||||
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
assert.ok(updatedContent.includes("alias test='echo test'"), "Original content should remain");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias test='echo test'"),
|
||||
"Original content should remain"
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle partial substring matches correctly", () => {
|
||||
|
|
@ -219,38 +352,57 @@ describe("setupShell", () => {
|
|||
"#!/usr/bin/env bash",
|
||||
"",
|
||||
"alias npmx='some-other-command'", // Contains 'npm' but shouldn't match 'alias npm='
|
||||
"alias test='echo test'"
|
||||
"alias test='echo test'",
|
||||
];
|
||||
const filePath = createShellStartupScript(lines, ".bashrc");
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const aliases = ["alias npm='aikido-npm'"];
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.addedCount, 1, "Should add 1 alias (npm)");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find 0 existing aliases");
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find 0 existing aliases"
|
||||
);
|
||||
assert.strictEqual(result.failedCount, 0, "Should have 0 failed aliases");
|
||||
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
assert.ok(updatedContent.includes("alias npm='aikido-npm'"), "npm alias should be added");
|
||||
assert.ok(updatedContent.includes("alias npmx='some-other-command'"), "npmx alias should remain");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias npm='aikido-npm'"),
|
||||
"npm alias should be added"
|
||||
);
|
||||
assert.ok(
|
||||
updatedContent.includes("alias npmx='some-other-command'"),
|
||||
"npmx alias should remain"
|
||||
);
|
||||
});
|
||||
|
||||
it("should handle file with only whitespace", () => {
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
const fileContent = `${EOL}${EOL} ${EOL}`;
|
||||
fs.writeFileSync(filePath, fileContent, "utf-8");
|
||||
|
||||
|
||||
const aliases = ["alias npm='aikido-npm'"];
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.addedCount, 1, "Should add 1 alias");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find 0 existing aliases");
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find 0 existing aliases"
|
||||
);
|
||||
assert.strictEqual(result.failedCount, 0, "Should have 0 failed aliases");
|
||||
|
||||
|
||||
const updatedContent = fs.readFileSync(filePath, "utf-8");
|
||||
assert.ok(updatedContent.includes("alias npm='aikido-npm'"), "Alias should be added");
|
||||
|
||||
assert.ok(
|
||||
updatedContent.includes("alias npm='aikido-npm'"),
|
||||
"Alias should be added"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
|
@ -258,21 +410,31 @@ describe("setupShell", () => {
|
|||
|
||||
describe("appendAliasesToFile error handling", () => {
|
||||
it("should handle file permission errors gracefully", () => {
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
fs.writeFileSync(filePath, "#!/usr/bin/env bash", "utf-8");
|
||||
|
||||
|
||||
// Make file read-only to simulate permission error
|
||||
fs.chmodSync(filePath, 0o444);
|
||||
|
||||
|
||||
const aliases = ["alias npm='aikido-npm'"];
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = appendAliasesToFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.addedCount, 0, "Should add 0 aliases due to permission error");
|
||||
assert.strictEqual(result.existingCount, 0, "Should find 0 existing aliases");
|
||||
|
||||
assert.strictEqual(
|
||||
result.addedCount,
|
||||
0,
|
||||
"Should add 0 aliases due to permission error"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.existingCount,
|
||||
0,
|
||||
"Should find 0 existing aliases"
|
||||
);
|
||||
assert.strictEqual(result.failedCount, 1, "Should have 1 failed alias");
|
||||
|
||||
|
||||
// Restore permissions and cleanup
|
||||
fs.chmodSync(filePath, 0o644);
|
||||
fs.rmSync(filePath, { force: true });
|
||||
|
|
@ -301,4 +463,4 @@ function countOccurrences(lines, searchString) {
|
|||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,41 +6,64 @@ import { getAliases } from "./helpers.js";
|
|||
import { removeAliasesFromFile } from "./teardown.js";
|
||||
|
||||
describe("teardown", () => {
|
||||
function runRemovalTestsForEnvironment(shell, startupExtension, expectedAliases) {
|
||||
function runRemovalTestsForEnvironment(
|
||||
shell,
|
||||
startupExtension,
|
||||
expectedAliases
|
||||
) {
|
||||
describe(`${shell} shell removal`, () => {
|
||||
it(`should remove aliases from ${shell} file`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, "", ...expectedAliases, ""];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
// Test the removeAliasesFromFile function directly
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.removedCount, 3, "Should remove 3 aliases");
|
||||
|
||||
assert.strictEqual(
|
||||
result.removedCount,
|
||||
expectedAliases.length,
|
||||
"Should remove all aliases"
|
||||
);
|
||||
assert.strictEqual(result.notFoundCount, 0, "Should find all aliases");
|
||||
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.ok(!updatedContent.includes(alias), `Alias "${alias}" should be removed`);
|
||||
assert.ok(
|
||||
!updatedContent.includes(alias),
|
||||
`Alias "${alias}" should be removed`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
it(`should handle file with no aliases for ${shell}`, () => {
|
||||
const lines = [`#!/usr/bin/env ${shell}`, "", "alias other='command'", ""];
|
||||
const lines = [
|
||||
`#!/usr/bin/env ${shell}`,
|
||||
"",
|
||||
"alias other='command'",
|
||||
"",
|
||||
];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.removedCount, 0, "Should remove 0 aliases");
|
||||
assert.strictEqual(result.notFoundCount, 3, "Should report 3 aliases not found");
|
||||
|
||||
assert.strictEqual(
|
||||
result.notFoundCount,
|
||||
expectedAliases.length,
|
||||
"Should report all aliases not found"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
assert.ok(updatedContent.includes("alias other='command'"), "Other aliases should remain unchanged");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias other='command'"),
|
||||
"Other aliases should remain unchanged"
|
||||
);
|
||||
});
|
||||
|
||||
it(`should remove duplicate aliases from ${shell} file`, () => {
|
||||
|
|
@ -50,33 +73,51 @@ describe("teardown", () => {
|
|||
...expectedAliases,
|
||||
"alias other='command'",
|
||||
...expectedAliases, // duplicates
|
||||
""
|
||||
"",
|
||||
];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.removedCount, 3, "Should remove 3 aliases (counting duplicates as single removal)");
|
||||
|
||||
assert.strictEqual(
|
||||
result.removedCount,
|
||||
expectedAliases.length,
|
||||
"Should remove all aliases (counting duplicates as single removal)"
|
||||
);
|
||||
assert.strictEqual(result.notFoundCount, 0, "Should find all aliases");
|
||||
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
for (const alias of expectedAliases) {
|
||||
assert.ok(!updatedContent.includes(alias), `Alias "${alias}" should be completely removed`);
|
||||
assert.ok(
|
||||
!updatedContent.includes(alias),
|
||||
`Alias "${alias}" should be completely removed`
|
||||
);
|
||||
}
|
||||
assert.ok(updatedContent.includes("alias other='command'"), "Other aliases should remain");
|
||||
assert.ok(
|
||||
updatedContent.includes("alias other='command'"),
|
||||
"Other aliases should remain"
|
||||
);
|
||||
});
|
||||
|
||||
it(`should use real getAliases() for ${shell} file`, () => {
|
||||
const filePath = `${tmpdir()}/test${startupExtension}`;
|
||||
const aliases = getAliases(filePath);
|
||||
|
||||
|
||||
// Verify we get the expected aliases for this shell type
|
||||
assert.strictEqual(aliases.length, 3, "Should get 3 aliases (npm, npx, yarn)");
|
||||
assert.strictEqual(
|
||||
aliases.length,
|
||||
expectedAliases.length,
|
||||
"Should get all aliases (npm, npx, yarn)"
|
||||
);
|
||||
for (let i = 0; i < aliases.length; i++) {
|
||||
assert.strictEqual(aliases[i], expectedAliases[i], `Alias ${i} should match expected format`);
|
||||
assert.strictEqual(
|
||||
aliases[i],
|
||||
expectedAliases[i],
|
||||
`Alias ${i} should match expected format`
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
|
|
@ -86,21 +127,31 @@ describe("teardown", () => {
|
|||
"",
|
||||
expectedAliases[0], // Only first alias
|
||||
"alias other='command'",
|
||||
""
|
||||
"",
|
||||
];
|
||||
const filePath = createShellStartupScript(lines, startupExtension);
|
||||
|
||||
|
||||
const aliases = getAliases(filePath);
|
||||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
|
||||
assert.strictEqual(result.removedCount, 1, "Should remove 1 alias");
|
||||
assert.strictEqual(result.notFoundCount, 2, "Should report 2 aliases not found");
|
||||
|
||||
assert.strictEqual(
|
||||
result.notFoundCount,
|
||||
expectedAliases.length - 1,
|
||||
"Should report all aliases not found"
|
||||
);
|
||||
|
||||
const updatedContent = readAndDeleteFile(filePath);
|
||||
assert.ok(!updatedContent.includes(expectedAliases[0]), "First alias should be removed");
|
||||
assert.ok(updatedContent.includes("alias other='command'"), "Other aliases should remain");
|
||||
assert.ok(
|
||||
!updatedContent.includes(expectedAliases[0]),
|
||||
"First alias should be removed"
|
||||
);
|
||||
assert.ok(
|
||||
updatedContent.includes("alias other='command'"),
|
||||
"Other aliases should remain"
|
||||
);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -109,39 +160,57 @@ describe("teardown", () => {
|
|||
runRemovalTestsForEnvironment("bash", ".bashrc", [
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'"
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias pnpm='aikido-pnpm'",
|
||||
"alias pnpx='aikido-pnpx'",
|
||||
]);
|
||||
|
||||
runRemovalTestsForEnvironment("zsh", ".zshrc", [
|
||||
"alias npm='aikido-npm'",
|
||||
"alias npx='aikido-npx'",
|
||||
"alias yarn='aikido-yarn'"
|
||||
"alias yarn='aikido-yarn'",
|
||||
"alias pnpm='aikido-pnpm'",
|
||||
"alias pnpx='aikido-pnpx'",
|
||||
]);
|
||||
|
||||
runRemovalTestsForEnvironment("fish", ".fish", [
|
||||
'alias npm "aikido-npm"',
|
||||
'alias npx "aikido-npx"',
|
||||
'alias yarn "aikido-yarn"'
|
||||
'alias yarn "aikido-yarn"',
|
||||
'alias pnpm "aikido-pnpm"',
|
||||
'alias pnpx "aikido-pnpx"',
|
||||
]);
|
||||
|
||||
runRemovalTestsForEnvironment("pwsh", ".ps1", [
|
||||
"Set-Alias npm aikido-npm",
|
||||
"Set-Alias npx aikido-npx",
|
||||
"Set-Alias yarn aikido-yarn"
|
||||
"Set-Alias yarn aikido-yarn",
|
||||
"Set-Alias pnpm aikido-pnpm",
|
||||
"Set-Alias pnpx aikido-pnpx",
|
||||
]);
|
||||
|
||||
describe("removeAliasesFromFile edge cases", () => {
|
||||
it("should handle empty file", () => {
|
||||
const aliases = ["alias npm='aikido-npm'"];
|
||||
const fileContent = "";
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
fs.writeFileSync(filePath, fileContent, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.removedCount, 0, "Should remove 0 aliases from empty file");
|
||||
assert.strictEqual(result.notFoundCount, 1, "Should report 1 alias not found");
|
||||
|
||||
|
||||
assert.strictEqual(
|
||||
result.removedCount,
|
||||
0,
|
||||
"Should remove 0 aliases from empty file"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.notFoundCount,
|
||||
1,
|
||||
"Should report 1 alias not found"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
|
@ -149,14 +218,24 @@ describe("teardown", () => {
|
|||
it("should handle file with only whitespace", () => {
|
||||
const aliases = ["alias npm='aikido-npm'"];
|
||||
const fileContent = `${EOL}${EOL} ${EOL}`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random().toString(36).substring(2, 15)}.bashrc`;
|
||||
const filePath = `${tmpdir()}/test-${Math.random()
|
||||
.toString(36)
|
||||
.substring(2, 15)}.bashrc`;
|
||||
fs.writeFileSync(filePath, fileContent, "utf-8");
|
||||
|
||||
|
||||
const result = removeAliasesFromFile(aliases, fileContent, filePath);
|
||||
|
||||
assert.strictEqual(result.removedCount, 0, "Should remove 0 aliases from whitespace-only file");
|
||||
assert.strictEqual(result.notFoundCount, 1, "Should report 1 alias not found");
|
||||
|
||||
|
||||
assert.strictEqual(
|
||||
result.removedCount,
|
||||
0,
|
||||
"Should remove 0 aliases from whitespace-only file"
|
||||
);
|
||||
assert.strictEqual(
|
||||
result.notFoundCount,
|
||||
1,
|
||||
"Should report 1 alias not found"
|
||||
);
|
||||
|
||||
// Cleanup
|
||||
fs.rmSync(filePath, { force: true });
|
||||
});
|
||||
|
|
@ -174,4 +253,4 @@ function readAndDeleteFile(filePath) {
|
|||
const fileContent = fs.readFileSync(filePath, "utf-8");
|
||||
fs.rmSync(filePath, { force: true });
|
||||
return fileContent.split(EOL);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue