Skip to content

Commit

Permalink
scripts: add & use get_filelist()
Browse files Browse the repository at this point in the history
This does not support filenames which contain newlines, as that would
make the scripts significantly more complicated.

Newlines aren't part of the POSIX Portable Filename Character Set.
The GNU style of using null-separated lists is interesting, but it's not
part of POSIX so it's not portable.

References:
- https://dwheeler.com/essays/fixing-unix-linux-filenames.html
- Use `find` instead of `ls` to better handle non-alphanumeric filenames.
  https://www.shellcheck.net/wiki/SC2012
  • Loading branch information
gperciva committed Jun 30, 2023
1 parent 635bf6a commit d2ef6a3
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 27 additions & 2 deletions tests/shared_test_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
# Set up the below variables.
# - expected_exitcode(expected, actual):
# Check if ${expected} matches ${actual}.
# - get_filelist (pattern): return a sorted list of files corresponding to
# ${pattern}, separated by newlines.
# - run_scenarios():
# Run scenarios in the test directory.
#
Expand Down Expand Up @@ -205,6 +207,29 @@ expected_exitcode() {
fi
}

## get_filelist (pattern):
# Return a sorted list of files corresponding to ${pattern}, separated by
# newlines.
get_filelist() {
_get_filelist_pattern=$1

# Separate the pattern into directory and basename.
_get_filelist_dir=$(dirname "${_get_filelist_pattern}")
_get_filelist_base=$(basename "${_get_filelist_pattern}")

# Find all files matching the pattern. Don't complain if the
# directory does not exist (i.e. checking for non-existent valgrind
# logfiles).
_get_filelist_unsorted=$(find "${_get_filelist_dir}" \
-name "${_get_filelist_base}" -prune -print 2>/dev/null)

# Sort the list.
_get_filelist_filelist=$(printf "%s" "${_get_filelist_unsorted}" | sort)

# Return the sorted list.
printf "%s" "${_get_filelist_filelist}"
}

## notify_success_or_fail (log_basename, val_log_basename):
# Examine all "exit code" files beginning with ${log_basename} and
# print "SUCCESS!", "FAILED!", "SKIP!", or "PARTIAL SUCCESS / SKIP!"
Expand All @@ -217,7 +242,7 @@ notify_success_or_fail() {
val_log_basename=$2

# Bail if there's no exitfiles.
exitfiles=$(ls "${log_basename}"-*.exit) || true
exitfiles=$(set -o noglob; get_filelist "${log_basename}"-*.exit)
if [ -z "${exitfiles}" ]; then
echo "FAILED" 1>&2
s_retval=1
Expand All @@ -229,7 +254,7 @@ notify_success_or_fail() {
skip_exitfiles=0

# Check each exitfile.
for exitfile in $(echo "${exitfiles}" | sort); do
for exitfile in ${exitfiles}; do
ret=$(cat "${exitfile}")
total_exitfiles=$(( total_exitfiles + 1 ))
if [ "${ret}" -lt 0 ]; then
Expand Down
2 changes: 1 addition & 1 deletion tests/shared_valgrind_functions.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ valgrind_check_basenames() {
val_basename=$(valgrind_get_basename "${exitfile}")

# Get list of files to check. (Yes, the star goes outside the quotes.)
logfiles=$(ls "${val_basename}"* 2>/dev/null)
logfiles=$(set -o noglob; get_filelist "${val_basename}"*)
num_logfiles=$(echo "${logfiles}" | wc -w)

# Bail if we don't have any valgrind logfiles to check.
Expand Down

0 comments on commit d2ef6a3

Please sign in to comment.