Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src/cli/utils/safe-delete-install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ export function generateSafeDeleteBlock(
` for arg in "$@"; do`,
` [[ "$arg" =~ ^- ]] || files+=("$arg")`,
` done`,
` if (( \${#files[@]} > 0 )); then`,
` ${cmd} "\${files[@]}"`,
` local existing=()`,
` for f in "\${files[@]}"; do`,
` [ -e "$f" ] || [ -L "$f" ] && existing+=("$f")`,
` done`,
` if (( \${#existing[@]} > 0 )); then`,
` ${cmd} "\${existing[@]}"`,
` fi`,
`}`,
`command() {`,
Expand All @@ -51,8 +55,14 @@ export function generateSafeDeleteBlock(
` set files $files $arg`,
` end`,
` end`,
` if test (count $files) -gt 0`,
` ${cmd} $files`,
` set -l existing`,
` for f in $files`,
` if test -e $f; or test -L $f`,
` set existing $existing $f`,
` end`,
` end`,
` if test (count $existing) -gt 0`,
` ${cmd} $existing`,
` end`,
`end`,
END_MARKER,
Expand Down Expand Up @@ -96,7 +106,8 @@ export function generateSafeDeleteBlock(
`}`,
`function rm {`,
` $files = $args | Where-Object { $_ -notlike '-*' }`,
` if ($files) { & ${cmd} @files }`,
` $existing = $files | Where-Object { Test-Path $_ }`,
` if ($existing) { & ${cmd} @existing }`,
`}`,
END_MARKER,
].join('\n');
Expand Down
14 changes: 14 additions & 0 deletions src/templates/managed-settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@
"Bash(rm -rf ~*)",
"Bash(rm -rf .*)",
"Bash(* rm -rf /*)",
"Bash(rm -r /*)",
"Bash(rm -r ~*)",
"Bash(rm -r .*)",
"Bash(rm -fr /*)",
"Bash(rm -fr ~*)",
"Bash(rm -fr .*)",
"Bash(rm -f /*)",
"Bash(rm -f ~*)",
"Bash(rm -f .*)",
"Bash(dd if=*)",
"Bash(dd*of=/dev/*)",
"Bash(mkfs*)",
Expand Down Expand Up @@ -85,12 +94,17 @@
"Bash(crontab*)",
"Bash(rm /var/log*)",
"Bash(rm -rf /var/log*)",
"Bash(rm -r /var/log*)",
"Bash(rm -f /var/log*)",
"Bash(rm -fr /var/log*)",
"Bash(> /var/log*)",
"Bash(truncate /var/log*)",
"Bash(history -c*)",
"Bash(history -w*)",
"Bash(rm ~/.bash_history*)",
"Bash(rm -f ~/.bash_history*)",
"Bash(rm ~/.zsh_history*)",
"Bash(rm -f ~/.zsh_history*)",
"Bash(unset HISTFILE*)",
"Bash(curl 169.254.169.254*)",
"Bash(wget 169.254.169.254*)",
Expand Down
26 changes: 19 additions & 7 deletions tests/safe-delete-install.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,31 @@ import {
} from '../src/cli/utils/safe-delete-install.js';

describe('generateSafeDeleteBlock', () => {
it('generates bash/zsh block with markers and both functions', () => {
it('generates bash/zsh block with markers, existence check, and both functions', () => {
const block = generateSafeDeleteBlock('zsh', 'darwin', 'trash');
expect(block).not.toBeNull();
expect(block).toContain('# >>> DevFlow safe-delete >>>');
expect(block).toContain('# <<< DevFlow safe-delete <<<');
expect(block).toContain('rm() {');
expect(block).toContain('command() {');
expect(block).toContain('trash "${files[@]}"');
expect(block).toContain('[ -e "$f" ] || [ -L "$f" ]');
expect(block).toContain('existing+=("$f")');
expect(block).toContain('trash "${existing[@]}"');
});

it('generates bash block with trash-put command', () => {
const block = generateSafeDeleteBlock('bash', 'linux', 'trash-put');
expect(block).toContain('trash-put "${files[@]}"');
expect(block).toContain('trash-put "${existing[@]}"');
});

it('generates fish block with fish syntax', () => {
it('generates fish block with fish syntax and existence check', () => {
const block = generateSafeDeleteBlock('fish', 'darwin', 'trash');
expect(block).not.toBeNull();
expect(block).toContain('# >>> DevFlow safe-delete >>>');
expect(block).toContain('function rm --description "Safe delete via trash"');
expect(block).toContain('trash $files');
expect(block).toContain('test -e $f; or test -L $f');
expect(block).toContain('set existing $existing $f');
expect(block).toContain('trash $existing');
expect(block).not.toContain('command()');
});

Expand All @@ -42,13 +46,21 @@ describe('generateSafeDeleteBlock', () => {
expect(block).toContain('Remove-Alias rm');
});

it('generates PowerShell macOS/Linux block with trash command', () => {
it('generates PowerShell macOS/Linux block with trash command and existence check', () => {
const block = generateSafeDeleteBlock('powershell', 'darwin', 'trash');
expect(block).not.toBeNull();
expect(block).toContain('& trash @files');
expect(block).toContain('Test-Path $_');
expect(block).toContain('& trash @existing');
expect(block).not.toContain('Microsoft.VisualBasic');
});

it('generates PowerShell Windows block with Resolve-Path existence check', () => {
const block = generateSafeDeleteBlock('powershell', 'win32', null);
expect(block).not.toBeNull();
expect(block).toContain('Resolve-Path $f -ErrorAction SilentlyContinue');
expect(block).toContain('Test-Path $p -PathType Container');
});

it('returns null for unknown shell', () => {
expect(generateSafeDeleteBlock('unknown', 'darwin', 'trash')).toBeNull();
});
Expand Down