4141# This is good practice even if you call `remove_bats_test_dirs` in your
4242# `environment.bash` file.
4343
44+ . " ${BASH_SOURCE%/* } /helper-function"
45+
4446# A subdirectory of BATS_TMPDIR that contains a space.
4547#
4648# Using this path instead of BATS_TMPDIR directly helps ensure that shell
@@ -61,8 +63,9 @@ BATS_TEST_BINDIR="$BATS_TEST_ROOTDIR/bin"
6163# Arguments:
6264# $1: Path to the project's top-level test directory
6365set_bats_test_suite_name () {
64- local test_rootdir=" $( cd " $1 " && echo " $PWD " ) "
65- local relative_filename=" ${BATS_TEST_FILENAME# $test_rootdir / } "
66+ cd " $1 "
67+ local relative_filename=" ${BATS_TEST_FILENAME# $PWD / } "
68+ cd - & > /dev/null
6669 readonly SUITE=" ${relative_filename% .bats} "
6770}
6871
@@ -74,18 +77,9 @@ set_bats_test_suite_name() {
7477# Arguments:
7578# $@: Paths of subdirectories relative to BATS_TEST_ROOTDIR
7679create_bats_test_dirs () {
77- local dirs_to_create=()
78- local test_dir
79-
80- for test_dir in " ${@/#/ $BATS_TEST_ROOTDIR / } " ; do
81- if [[ ! -d " $test_dir " ]]; then
82- dirs_to_create+=(" $test_dir " )
83- fi
84- done
85-
86- if [[ " ${# dirs_to_create[@]} " -ne ' 0' ]]; then
87- mkdir -p " ${dirs_to_create[@]} "
88- fi
80+ set " $DISABLE_BATS_SHELL_OPTIONS "
81+ __create_bats_test_dirs " $@ "
82+ restore_bats_shell_options " $? "
8983}
9084
9185# Creates a test script relative to BATS_TEST_ROOTDIR
@@ -100,26 +94,9 @@ create_bats_test_dirs() {
10094# $1: Path of the script relative to BATS_TEST_ROOTDIR
10195# ...: Lines comprising the script
10296create_bats_test_script () {
103- local script=" $1 "
104- shift
105- local script_dir=" ${script%/* } "
106-
107- if [[ -z " $script " ]]; then
108- echo " No test script specified" >&2
109- exit 1
110- elif [[ " $script_dir " == " $script " ]]; then
111- script_dir=' '
112- fi
113-
114- create_bats_test_dirs " $script_dir "
115- script=" $BATS_TEST_ROOTDIR /$script "
116- rm -f " $script "
117-
118- if [[ " ${1: 0: 2} " != ' #!' ]]; then
119- echo " #! /usr/bin/env bash" > " $script "
120- fi
121- printf ' %s\n' " $@ " >> " $script "
122- chmod 700 " $script "
97+ set " $DISABLE_BATS_SHELL_OPTIONS "
98+ __create_bats_test_script " $@ "
99+ restore_bats_shell_options " $? "
123100}
124101
125102# Recursively removes `BATS_TEST_ROOTDIR` and its subdirectories
@@ -183,18 +160,15 @@ skip_if_cannot_trigger_file_permission_failure() {
183160# Arguments:
184161# ...: System programs that must be present for the test case to proceed
185162skip_if_system_missing () {
186- local missing=()
187- local program
163+ local __missing=()
188164
189- for program in " $@ " ; do
190- if ! command -v " $program " > /dev/null; then
191- missing+=(" $program " )
192- fi
193- done
165+ set " $DISABLE_BATS_SHELL_OPTIONS "
166+ __search_for_missing_programs " $@ "
167+ restore_bats_shell_options " $? "
194168
195- if [[ " ${# missing [@]} " -ne ' 0' ]]; then
196- printf -v missing ' %s, ' " ${missing [@]} "
197- skip " ${missing % , } not installed on the system"
169+ if [[ " ${# __missing [@]} " -ne ' 0' ]]; then
170+ printf -v __missing ' %s, ' " ${__missing [@]} "
171+ skip " ${__missing % , } not installed on the system"
198172 fi
199173}
200174
@@ -311,12 +285,9 @@ test_filter() {
311285# compare the exact lines of `output` using `assert_lines_equal` and other
312286# `lines`-based assertions.
313287split_bats_output_into_lines () {
314- local line
315- lines=()
316-
317- while IFS= read -r line; do
318- lines+=(" ${line% $' \r ' } " )
319- done <<< " $output"
288+ set " $DISABLE_BATS_SHELL_OPTIONS "
289+ __split_bats_output_into_lines
290+ restore_bats_shell_options " $? "
320291}
321292
322293# Creates a stub program in PATH for testing purposes
@@ -335,3 +306,80 @@ stub_program_in_path() {
335306 fi
336307 create_bats_test_script " ${BATS_TEST_BINDIR# $BATS_TEST_ROOTDIR / } /$1 " " ${@: 2} "
337308}
309+
310+ # --------------------------------
311+ # IMPLEMENTATION - HERE BE DRAGONS
312+ #
313+ # None of the functions below this line are part of the public interface.
314+ # --------------------------------
315+
316+ # Implementation for `create_bats_test_dirs`
317+ #
318+ # Arguments:
319+ # $@: Paths of subdirectories relative to BATS_TEST_ROOTDIR
320+ __create_bats_test_dirs () {
321+ local dirs_to_create=()
322+ local test_dir
323+
324+ for test_dir in " ${@/#/ $BATS_TEST_ROOTDIR / } " ; do
325+ if [[ ! -d " $test_dir " ]]; then
326+ dirs_to_create+=(" $test_dir " )
327+ fi
328+ done
329+
330+ if [[ " ${# dirs_to_create[@]} " -ne ' 0' ]]; then
331+ mkdir -p " ${dirs_to_create[@]} "
332+ fi
333+ }
334+
335+ # Implementation for `create_bats_test_script`
336+ #
337+ # Arguments:
338+ # $1: Path of the script relative to BATS_TEST_ROOTDIR
339+ # ...: Lines comprising the script
340+ __create_bats_test_script () {
341+ local script=" $1 "
342+ shift
343+ local script_dir=" ${script%/* } "
344+
345+ if [[ -z " $script " ]]; then
346+ echo " No test script specified" >&2
347+ exit 1
348+ elif [[ " $script_dir " == " $script " ]]; then
349+ script_dir=' '
350+ fi
351+
352+ create_bats_test_dirs " $script_dir "
353+ script=" $BATS_TEST_ROOTDIR /$script "
354+ rm -f " $script "
355+
356+ if [[ " ${1: 0: 2} " != ' #!' ]]; then
357+ echo " #! /usr/bin/env bash" > " $script "
358+ fi
359+ printf ' %s\n' " $@ " >> " $script "
360+ chmod 700 " $script "
361+ }
362+
363+ # Enumerates programs not installed on the system for `skip_if_system_missing`
364+ #
365+ # Arguments:
366+ # ...: System programs that must be present for the test case to proceed
367+ __search_for_missing_programs () {
368+ local program
369+
370+ for program in " $@ " ; do
371+ if ! command -v " $program " > /dev/null; then
372+ __missing+=(" $program " )
373+ fi
374+ done
375+ }
376+
377+ # Implementation for `split_bats_output_into_lines`
378+ __split_bats_output_into_lines () {
379+ local line
380+ lines=()
381+
382+ while IFS= read -r line; do
383+ lines+=(" ${line% $' \r ' } " )
384+ done <<< " $output"
385+ }
0 commit comments