Skip to content

Commit

Permalink
🐛 making tests work on wsl
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Jan 20, 2025
1 parent 8968284 commit f2e2505
Show file tree
Hide file tree
Showing 14 changed files with 64 additions and 51 deletions.
4 changes: 2 additions & 2 deletions libraries.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ function core::setShellOptions() {
# +nocasematch: disable case-insensitive matching
shopt -s extquote globasciiranges huponexit interactive_comments progcomp
shopt -u assoc_expand_once cdable_vars checkhash checkjobs checkwinsize compat31 compat32 compat40 compat41 compat42 compat43 compat44 globstar localvar_inherit localvar_unset nocaseglob nocasematch progcomp_alias shift_verbose xpg_echo nullglob direxpand dirspell dotglob extglob failglob execfail expand_aliases inherit_errexit
if (( ${BASH_VERSINFO[1]} >= 2)); then
if (( BASH_VERSINFO[1] >= 2)); then
shopt -s globskipdots patsub_replacement
shopt -u varredir_close
fi
Expand Down Expand Up @@ -1154,7 +1154,7 @@ function string::highlight() {
#
# > - All characters in the pattern must be found in the same order in the matched line.
# > - Use `shopt -s nocasematch` to make this function is case insensitive.
# > - This function is not appropriate for large arrays (>10k elements), see `array::fuzzyFilterSortFileWithGrepAndAwk` for large arrays.
# > - This function is not appropriate for large arrays (>10k elements), see `array::fuzzyFilterSortFileWithGrepAndGawk` for large arrays.
function array::fuzzyFilterSort() {
local pattern="${1}"
local -n array="${2}"
Expand Down
10 changes: 5 additions & 5 deletions libraries.d/lib-array
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function array::makeArraysSameSize() {
done
}

# ## array::fuzzyFilterSortFileWithGrepAndAwk
# ## array::fuzzyFilterSortFileWithGrepAndGawk
#
# Allows to fuzzy sort a file against a given pattern.
# Outputs a file containing only the lines matching the pattern.
Expand All @@ -338,12 +338,12 @@ function array::makeArraysSameSize() {
# The output file containing the indexes of the matched lines in the original file.
#
# ```bash
# array::fuzzyFilterSortFileWithGrepAndAwk file.txt filtered.txt correspondences.txt
# array::fuzzyFilterSortFileWithGrepAndGawk file.txt filtered.txt correspondences.txt
# ```
#
# > This is not a pure bash function! Use `array::fuzzyFilterSort` for pure bash alternative.
# > This function is useful for very large arrays.
function array::fuzzyFilterSortFileWithGrepAndAwk() {
function array::fuzzyFilterSortFileWithGrepAndGawk() {
local pattern="${1}"
local fileToFilter="${2}"
local outputFilteredFile="${3}"
Expand All @@ -368,10 +368,10 @@ function array::fuzzyFilterSortFileWithGrepAndAwk() {
patternRegex+=")"

# we use grep to pre-filter the lines.
# We could do everything in awk but it would be slower for hugh files.
# We could do everything in gawk but it would be slower for hugh files.
grep --ignore-case --line-number "${patternRegex//[()]/}" "${fileToFilter}" >"${_ARRAY_TEMP_FILTER_FILE}"
patternRegex="${patternRegex}(.?)"
awk "
gawk "
BEGIN {
# Ignore case when matching the pattern
IGNORECASE = 1;
Expand Down
4 changes: 2 additions & 2 deletions libraries.d/lib-prompt
Original file line number Diff line number Diff line change
Expand Up @@ -486,7 +486,7 @@ function prompt_setItemsListVariables() {
_PROMPT_ITEMS_BOX_FILTER_SYNCHRONOUSLY="false"
fi
if [[ -z ${_PROMPT_ITEMS_BOX_FILTER_USING_GREP} && _PROMPT_ORIGINAL_ITEMS_COUNT -gt 1000 ]]; then
if command -v grep &>/dev/null && command -v awk &>/dev/null; then
if command -v grep &>/dev/null && command -v gawk &>/dev/null; then
_PROMPT_ITEMS_BOX_FILTER_USING_GREP="true"
# shellcheck source=lib-array
source array
Expand Down Expand Up @@ -970,7 +970,7 @@ function prompt_filterItemsAsync() {
: >"${_PROMPT_ITEMS_BOX_ASYNC_FILTERED_ITEMS_CORRESPONDENCES_FILE}"
rm "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_TEMP1}" &>/dev/null || :

array::fuzzyFilterSortFileWithGrepAndAwk "${_PROMPT_ITEMS_BOX_FILTER_STRING}" "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_ORIGINAL_ITEMS}" "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_TEMP1}" "${_PROMPT_ITEMS_BOX_ASYNC_FILTERED_ITEMS_CORRESPONDENCES_FILE}"
array::fuzzyFilterSortFileWithGrepAndGawk "${_PROMPT_ITEMS_BOX_FILTER_STRING}" "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_ORIGINAL_ITEMS}" "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_TEMP1}" "${_PROMPT_ITEMS_BOX_ASYNC_FILTERED_ITEMS_CORRESPONDENCES_FILE}"

if [[ -f ${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_TEMP1} ]]; then
mv "${_PROMPT_ITEMS_BOX_FILTER_USING_GREP_TEMP1}" "${_PROMPT_ITEMS_BOX_ASYNC_FILTERED_ITEMS_FILE}"
Expand Down
20 changes: 14 additions & 6 deletions libraries.d/lib-test
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,18 @@ function test_exec() {
test::fail "No command provided to ⌜${FUNCNAME[1]:-}⌝ in ⌜${BASH_SOURCE[2]:-}:${BASH_LINENO[1]:-}⌝."
fi

local IFS=' '
local IFS=' ' compoundCommand
# compute the command to run, escaping special characters
local compoundCommand="${*//[\\ $'\t']/\\&}"
if (( BASH_VERSINFO[1] >= 2)); then
compoundCommand="${*//[\\ $'\t']/\\&}"
else
local params=("${@//\\/\\\\}")
params=("${params[@]// /\\ }")
compoundCommand="${params[*]//$'\t'/\\$'\t'}"
fi
compoundCommand="${compoundCommand//$'\n'/"$'\n'"}"


_TEST_OUTPUT="${compoundCommand}"
if declare -f test::transformTextBeforeFlushing &>/dev/null; then
test::transformTextBeforeFlushing
Expand Down Expand Up @@ -221,7 +228,7 @@ function test::printReturnedVars() {
_TEST_OUTPUT+=")"$'\n'
fi
done
if [[ -v RETURNED_ASSOCIATIVE_ARRAY || -v RETURNED_ASSOCIATIVE_ARRAY[@] ]]; then
if [[ -v RETURNED_ASSOCIATIVE_ARRAY && -v RETURNED_ASSOCIATIVE_ARRAY[@] ]]; then
_TEST_OUTPUT+="RETURNED_ASSOCIATIVE_ARRAY=("$'\n'
for key in "${!RETURNED_ASSOCIATIVE_ARRAY[@]}"; do
_TEST_OUTPUT+="[${key}]=${RETURNED_ASSOCIATIVE_ARRAY[${key}]@Q}"$'\n'
Expand Down Expand Up @@ -287,10 +294,11 @@ function test::printVars() {
_TEST_OUTPUT+="[${key}]=${array[${key}]@Q}"$'\n'
done
_TEST_OUTPUT+=")"$'\n'
elif [[ ${chunk} =~ "declare -"[^[:space:]]+" " ]]; then
elif [[ ${chunk} =~ "declare -"[^[:space:]]+" "([^=]+)"=" ]]; then
# we print a simple variable
_TEST_OUTPUT+="${chunk//"${BASH_REMATCH[0]}"/}"$'\n'
else
varName="${BASH_REMATCH[1]}"
_TEST_OUTPUT+="${varName}='${!varName//\'/\'\"\'\"\'}'"$'\n'
elif [[ ${chunk} == "declare -"* ]]; then
test::fail "Could not parse variable declaration: ⌜${chunk}⌝."
fi
done
Expand Down
4 changes: 2 additions & 2 deletions tests.d/cli-profiler/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ Notice that the profiling file is cleaned up after the command execution to maxi
**Exported variables:**

```text
VALET_CONFIG_COMMAND_PROFILING_FILE="/tmp/valet.d/f1-2"
VALET_CONFIG_STARTUP_PROFILING_FILE="/tmp/valet.d/f2-2"
VALET_CONFIG_COMMAND_PROFILING_FILE='/tmp/valet.d/f1-2'
VALET_CONFIG_STARTUP_PROFILING_FILE='/tmp/valet.d/f2-2'
```

### ✅ Testing the profiler cli option
Expand Down
12 changes: 6 additions & 6 deletions tests.d/lib-array/00.lib-array.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function main() {
test_array::isInArray
test_array::makeArraysSameSize
test_array::fuzzyFilterSort
test_array::fuzzyFilterSortFileWithGrepAndAwk
test_array::fuzzyFilterSortFileWithGrepAndGawk
}

function test_array::sort() {
Expand Down Expand Up @@ -130,15 +130,15 @@ function test_array::fuzzyFilterSort() {
shopt -u nocasematch
}

function test_array::fuzzyFilterSortFileWithGrepAndAwk() {
test::title "✅ Testing array::fuzzyFilterSortFileWithGrepAndAwk"
function test_array::fuzzyFilterSortFileWithGrepAndGawk() {
test::title "✅ Testing array::fuzzyFilterSortFileWithGrepAndGawk"

mapfile -t _MY_ARRAY <words
shopt -s nocasematch
array::fuzzyFilterSort ea _MY_ARRAY
shopt -u nocasematch

test::prompt "array::fuzzyFilterSortFileWithGrepAndAwk /words /out1 /out2"
test::prompt "array::fuzzyFilterSortFileWithGrepAndGawk /words /out1 /out2"
test::prompt "io::head /out1 10"
local value
local -i nb=0
Expand All @@ -157,12 +157,12 @@ function test_array::fuzzyFilterSortFileWithGrepAndAwk() {
io::createTempFile
local outputCorrespondenceFile="${RETURNED_VALUE}"

if ! command -v grep &>/dev/null || ! command -v awk &>/dev/null; then
if ! command -v grep &>/dev/null || ! command -v gawk &>/dev/null; then
test::markdown "> The result is the same as the pure bash implementation."
return 0
fi

array::fuzzyFilterSortFileWithGrepAndAwk ea words "${outputFilteredFile}" "${outputCorrespondenceFile}"
array::fuzzyFilterSortFileWithGrepAndGawk ea words "${outputFilteredFile}" "${outputCorrespondenceFile}"

io::readFile "${outputFilteredFile}"
local awkLines="${RETURNED_VALUE%$'\n'}"
Expand Down
4 changes: 2 additions & 2 deletions tests.d/lib-array/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -284,9 +284,9 @@ RETURNED_ARRAY2=(
)
```

### ✅ Testing array::fuzzyFilterSortFileWithGrepAndAwk
### ✅ Testing array::fuzzyFilterSortFileWithGrepAndGawk

`array::fuzzyFilterSortFileWithGrepAndAwk /words /out1 /out2`
`array::fuzzyFilterSortFileWithGrepAndGawk /words /out1 /out2`

`io::head /out1 10`

Expand Down
12 changes: 6 additions & 6 deletions tests.d/lib-interactive/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ Exited with code: `1`
`printf '\e[%sR' '123;456' | interactive::getCursorPosition`

```text
GLOBAL_CURSOR_LINE="123"
GLOBAL_CURSOR_COLUMN="456"
GLOBAL_CURSOR_LINE='123'
GLOBAL_CURSOR_COLUMN='456'
```

### ✅ Testing interactiveGetProgressBarString
Expand Down Expand Up @@ -135,8 +135,8 @@ RETURNED_VALUE='█████████████████████
### ✅ Testing interactive::clearBox

```text
GLOBAL_CURSOR_LINE="42"
GLOBAL_CURSOR_COLUMN="42"
GLOBAL_CURSOR_LINE='42'
GLOBAL_CURSOR_COLUMN='42'
```

`interactive::clearBox 1 1 10 10`
Expand All @@ -158,8 +158,8 @@ GLOBAL_CURSOR_COLUMN="42"
### ✅ Testing interactive::getBestAutocompleteBox

```text
GLOBAL_LINES="10"
GLOBAL_COLUMNS="10"
GLOBAL_LINES='10'
GLOBAL_COLUMNS='10'
```

`interactive::getBestAutocompleteBox 1 1 20 20`
Expand Down
14 changes: 6 additions & 8 deletions tests.d/lib-io/00.tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -177,15 +177,13 @@ function test_io::convertToWindowsPath() {
}

function test_io::convertFromWindowsPath() {
printf "%s\n" "→ io::convertFromWindowsPath 'C:\\Users\\username'"
io::convertFromWindowsPath 'C:\Users\username'
echo "${RETURNED_VALUE}"

printf "%s\n" "→ io::convertFromWindowsPath 'D:\\data\\file'"
io::convertFromWindowsPath 'D:\data\file'
echo "${RETURNED_VALUE}"
test::title "✅ Testing io::convertFromWindowsPath"

test::endTest "Testing io::convertFromWindowsPath" 0
# shellcheck disable=SC2317
function test::transformTextBeforeFlushing() { _TEST_OUTPUT="${_TEST_OUTPUT//\/mnt}"; }
test::func io::convertFromWindowsPath 'C:\Users\username'
test::func io::convertFromWindowsPath 'D:\data\file'
unset test::transformTextBeforeFlushing
}

function test_io::createLink() {
Expand Down
17 changes: 10 additions & 7 deletions tests.d/lib-io/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,19 +259,22 @@ ln: -s $GLOBAL_VALET_HOME/tests.d/lib-io/resources/gitignored/try $GLOBAL_VALET_
$GLOBAL_VALET_HOME/tests.d/lib-io/resources/gitignored
```

### Testing io::convertFromWindowsPath
### Testing io::convertFromWindowsPath

`io::convertFromWindowsPath C:\\Users\\username`

Returned variables:

Exit code: `0`
```text
RETURNED_VALUE='/c/Users/username'
```

Standard output
`io::convertFromWindowsPath D:\\data\\file`

Returned variables:

```text
→ io::convertFromWindowsPath 'C:\Users\username'
/c/Users/username
→ io::convertFromWindowsPath 'D:\data\file'
/d/data/file
RETURNED_VALUE='/d/data/file'
```

### Testing io::head limited to 10 lines
Expand Down
6 changes: 5 additions & 1 deletion tests.d/lib-string/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,11 @@ This is a text to highlight.
**Global variables:**

```text
_TEST_MULTILINE_STRING=$'The first line.\nThe second line.\nThe third line.\nThe fourth line.\nThe fifth line.'
_TEST_MULTILINE_STRING='The first line.
The second line.
The third line.
The fourth line.
The fifth line.'
```

Testing string::head with 3 lines
Expand Down
4 changes: 2 additions & 2 deletions tests.d/lib-test/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ You can manually report the definition of any variable using the `test::printVar
`test::printVars GLOBAL_VAR1 GLOBAL_VAR2 GLOBAL_VAR3`

```text
GLOBAL_VAR1="This is the value of a global string"
GLOBAL_VAR1='This is the value of a global string'
GLOBAL_VAR2=(
[0]='This'
[1]='is'
Expand Down Expand Up @@ -280,6 +280,6 @@ The variable TESTING_HOOKS shows that the hooks [before-tests](../before-tests),
This test is written in the [after-each-test](after-each-test) script. The `after-each-test-suites` and `after-tests` are executed after the test script exits, thus we can't output their results in this report.

```text
TESTING_HOOKS="before-tests,before-each-test-suite,after-each-test"
TESTING_HOOKS='before-tests,before-each-test-suite,after-each-test'
```

2 changes: 1 addition & 1 deletion tests.d/self-release/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ DEBUG Found function: ⌜array::sortWithCriteria⌝
DEBUG Found function: ⌜array::appendIfNotPresent⌝
DEBUG Found function: ⌜array::isInArray⌝
DEBUG Found function: ⌜array::makeArraysSameSize⌝
DEBUG Found function: ⌜array::fuzzyFilterSortFileWithGrepAndAwk
DEBUG Found function: ⌜array::fuzzyFilterSortFileWithGrepAndGawk
DEBUG Found function: ⌜bash::countJobs⌝
DEBUG Found function: ⌜bash::runInParallel⌝
DEBUG Found function: ⌜bash::injectCodeInFunction⌝
Expand Down
2 changes: 1 addition & 1 deletion version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.28.2118
0.28.2164

0 comments on commit f2e2505

Please sign in to comment.