Files
nvm/test/fast/Unit tests/nvm_dir_has_entries
T
Jordan Harband b0a377a524 [Perf] nvm_ls: check whether a version directory has entries without forking
Each check ran `ls` piped into `grep` in a subshell,
which cost more than the `find` it guards;
a glob finds the same entries in the shell.
2026-11-19 16:05:28 -08:00

39 lines
863 B
Bash
Executable File

#!/bin/sh
cleanup() {
rm -rf "${TEST_DIR}"
}
die() {
echo "$@"
cleanup
exit 1
}
: nvm.sh
\. ../../../nvm.sh
TEST_DIR="$(mktemp -d)"
# glob characters and a space in the path are matched literally
DIR="${TEST_DIR}/a [b] *"
! nvm_dir_has_entries "${DIR}" || die 'a missing directory has no entries'
mkdir "${DIR}"
! nvm_dir_has_entries "${DIR}" || die 'an empty directory has no entries'
touch "${DIR}/.hidden"
! nvm_dir_has_entries "${DIR}" || die 'a directory with only dotfiles has no entries, as with `ls`'
ln -s "${TEST_DIR}/nonexistent" "${DIR}/broken"
nvm_dir_has_entries "${DIR}" || die 'a broken symlink is an entry'
rm "${DIR}/broken"
mkdir "${DIR}/v1.0.0"
nvm_dir_has_entries "${DIR}" || die 'a directory with a subdirectory has entries'
! nvm_dir_has_entries "${TEST_DIR}/a [b] " || die 'the directory name is not a pattern'
cleanup