Skip to content

Commit

Permalink
fixes #37: rm.sh: supports -d
Browse files Browse the repository at this point in the history
  • Loading branch information
kaelzhang committed Nov 28, 2024
1 parent 10828b2 commit 9113015
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
14 changes: 13 additions & 1 deletion bin/rm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,7 @@ OPT_INTERACTIVE=
OPT_INTERACTIVE_ONCE=
OPT_RECURSIVE=
OPT_VERBOSE=
OPT_EMPTY_DIR=

# parse options
for arg in ${ARG[@]}; do
Expand Down Expand Up @@ -270,6 +271,10 @@ for arg in ${ARG[@]}; do
OPT_VERBOSE=1; debug "$LINENO: verbose : $arg"
;;

-d|--directory)
OPT_EMPTY_DIR=1; debug "$LINENO: empty dir : $arg"
;;

*)
invalid_option $arg
;;
Expand Down Expand Up @@ -303,8 +308,15 @@ remove(){

# if a directory, and without '-r' option
if [[ ! -n $OPT_RECURSIVE ]]; then
# if with '-d' option, and is an empty dir
if [[ -n $OPT_EMPTY_DIR && ! $(ls -A "$file") ]]; then
debug "$LINENO: trash an empty directory $file"
trash "$file"
return
fi

debug "$LINENO: $file: is a directory"
echo "$COMMAND: $file: is a directory"
error "$COMMAND: $file: is a directory"
return 1
fi

Expand Down
19 changes: 19 additions & 0 deletions test/cases.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,23 @@ module.exports = (
result_no_f.stderr
}`)
})

test(`${des_prefix}: removes an empty directory: -d`, async t => {
const {
createDir,
runRm,
pathExists
} = t.context

const dirpath = await createDir()
const result1 = await runRm([dirpath])

t.is(result1.code, 1, 'exit code should be 1')
t.true(result1.stderr.includes('is a directory'), 'stderr should include "is a directory"')

const result2 = await runRm(['-d', dirpath])

assertEmptySuccess(t, result2)
t.false(await pathExists(dirpath), 'directory should be removed')
})
}

0 comments on commit 9113015

Please sign in to comment.