Skip to content

Commit

Permalink
eval_capture: solve failing tests on linux
Browse files Browse the repository at this point in the history
/improves #283
/fixes https://github.com/bevry/dorothy/actions/runs/13254558267/job/36998982191#step:2:502

update tests in `dorothy-config`, `echo-lines-after`, `echo-lines-before` to correctly detect and report a regression here
  • Loading branch information
balupton committed Feb 12, 2025
1 parent 91e1f66 commit 331cca2
Show file tree
Hide file tree
Showing 4 changed files with 306 additions and 77 deletions.
198 changes: 173 additions & 25 deletions commands/dorothy-config
Original file line number Diff line number Diff line change
Expand Up @@ -4,50 +4,197 @@

function dorothy_config_test() (
source "$DOROTHY/sources/bash.bash"
source "$(type -P eval-tester)"
echo-style --h1="TEST: $0"

# remove
fs-rm --quiet --no-confirm -- "$DOROTHY/user/config/dorothy-config-testing.bash"

local invented_packages util_packages expected_stdout expected_stderr expected_file
# prepare
local context expected_stdout expected_stderr expected_first_result invented_packages \
filename='setup.bash' \
filepath="$DOROTHY/user/config/setup.bash" \
util_packages=(
curl
ripgrep
sd
)
context="$(get-random-number)"
invented_packages=(
# 0 and 1 prefixes for sorting consistency
"dorothy-config-testing-value-0$(get-random-number)"
"dorothy-config-testing-value-1$(get-random-number)"
)
util_packages=(
curl
ripgrep
sd
)
expected_file="$DOROTHY/user/config/setup.bash"
"dorothy-config-testing-value-0-$context"
"dorothy-config-testing-value-1-$context"
) \
expected_stderr="Updated configuration file: $filepath"
expected_stdout="$(__print_lines "${invented_packages[@]}")"
expected_first_result="$(
cat <<EOF
#!/usr/bin/env bash
# do not use \`export\` keyword in this file:
# shellcheck disable=SC2034
# load the default configuration
source "\$DOROTHY/config/setup.bash"
# APK
# Used by \`setup-linux\`
# APK_INSTALL=()
# Apt / apt-get
# Used by \`setup-linux\`
# APT_UNINSTALL=()
# APT_INSTALL=()
# AUR / pamac / pacman / yay / paru / pakku / aurutils
# Used by \`setup-linux\`
# AUR_INSTALL=()
# Flatpak
# Used by \`setup-linux\`
# FLATPAK_INSTALL=()
# RPM / dnf / yum
# Used by \`setup-linux\`
# RPM_INSTALL=()
# Snap
# Used by \`setup-linux\`
# SNAP_INSTALL=()
# Zypper
# Used by \`setup-linux\`
# ZYPPER_INSTALL=()
# macOS App Store / mas / https://github.com/mas-cli/mas
# Used by \`setup-mac-appstore\`
# You can use \`mas list\` and \`mas search\` to find apps
# MAS_INSTALL=() # tuple array of id, label
# MAS_UPGRADE='no'
# Homebrew / brew / https://brew.sh
# Used by \`setup-mac-brew\`
# You can use \`setup-mac-brew --configure\` to configure some of these.
# HOMEBREW_UNTAPS=()
# HOMEBREW_TAPS=()
# HOMEBREW_FORMULAS=()
# HOMEBREW_SLOW_FORMULAS=()
# HOMEBREW_CASKS=()
HOMEBREW_FONTS=(
'font-cantarell'
'font-cascadia-code'
'font-fira-code'
'font-fira-code-nerd-font'
'font-fira-mono'
'font-fira-mono-nerd-font'
'font-hack' # many editors require this
'font-hasklig'
'font-ibm-plex' # many editors require this
'font-inconsolata-go-nerd-font'
'font-inter' # many apps require this
'font-jetbrains-mono'
'font-jetbrains-mono-nerd-font'
'font-lato'
'font-maven-pro'
'font-monoid'
'font-montserrat'
'font-open-sans'
'font-oxygen'
'font-oxygen-mono'
'font-roboto'
'font-roboto-mono'
'font-source-code-pro' # many editors require this
'font-ubuntu'
)
# HOMEBREW_UNINSTALL=() # for casks and formulas
# HOMEBREW_ENCODING_INSTALL='' # '', 'yes', 'no'
# Golang / go
# Used by \`setup-go\`
# GO_LINTING_INSTALL='' # '', 'yes', 'no'
# GO_INSTALL=()
# Node.js
# Used by \`setup-node\`
# NPM_INSTALL=()
# NODE_VERSIONS=()
# Python
# Used by \`setup-python\`
# PYTHON_INSTALL=()
# PIP_INSTALL=()
# PYTHON2_PIP_INSTALL=()
# PYTHON3_PIP_INSTALL=()
# PIPX_INSTALL=()
# Ruby
# Used by \`setup-ruby\`
# GEM_INSTALL=()
# Rust / Cargo / Crates.io
# Used by \`setup-rust\`
# CARGO_INSTALL=()
# Utilities to install, these are the [setup-util-*] scripts
# Used by \`setup-utils\`
# You can use \`setup-utils --configure\` to configure these.
SETUP_UTILS=(
curl
nano
ripgrep
sd
vim
)
DOROTHY_CONFIG_TESTING_LIST=(
dorothy-config-testing-value-0-$context
dorothy-config-testing-value-1-$context
)
EOF
)"

# cleanup
fs-rm --quiet --no-confirm -- "$filepath"

# eval_tester --stderr="$expected_stderr" -- \
# dorothy-config \
# setup.bash \
# --prefer=public \
# -- \
# --field=DOROTHY_CONFIG_TESTING_LIST $'--array=dorothy-config-testing-value-06589\ndorothy-config-testing-value-122058' \
# --field=SETUP_UTILS $'--array=curl\nnano\nripgrep\nsd\nvim'
# echo-file -- "$filepath" || :

# eval_tester --debug -- \
# config-helper \
# --file=/home/balupton/.local/share/dorothy/user/config/setup.bash \
# --multiple=warn-skip
# -- \
# --field=DOROTHY_CONFIG_TESTING_LIST $'--array=dorothy-config-testing-value-06589\ndorothy-config-testing-value-122058' \
# --field=SETUP_UTILS $'--array=curl\nnano\nripgrep\nsd\nvim'

# output the configuration file, so we can track its progress, in case something goes wrong, which on CI it might
echo-file -- "$expected_file" || :
echo-file -- "$filepath" || :

# add DOROTHY_CONFIG_TESTING_LIST, ignore stderr as it will vary based on whether choose was used or not
expected_stdout="$(__print_lines "${invented_packages[@]}")"
eval-tester --name='add and remove packages' --stdout="$expected_stdout" --ignore-stderr \
eval_tester --name='add and remove packages' --stdout="$expected_stdout" --ignore-stderr \
-- eval-no-color -- dorothy-config --packages-var='DOROTHY_CONFIG_TESTING_LIST' --prefer=public -- "${invented_packages[@]}" "${util_packages[@]}"

# output the configuration file, so we can track its progress, in case something goes wrong, which on CI it might
echo-file -- "$expected_file" || :
echo-file -- "$filepath" || :

# ensure it had the before lines, loader lines, and after lines
eval_tester --stdout="$expected_first_result" -- cat "$filepath"

# read DOROTHY_CONFIG_TESTING_LIST
expected_stdout=$'(\n'"$(echo-lines --quote=bash --indent=$'\t' -- "${invented_packages[@]}")"$'\n)'
eval-tester --name='read packages' --stdout="$expected_stdout" --ignore-stderr \
-- eval-no-color -- config-helper --file="$expected_file" -- --field='DOROTHY_CONFIG_TESTING_LIST'
eval_tester --name='read packages' --stdout="$expected_stdout" --ignore-stderr \
-- eval-no-color -- config-helper --file="$filepath" -- --field='DOROTHY_CONFIG_TESTING_LIST'

# output the configuration file, so we can track its progress, in case something goes wrong, which on CI it might
echo-file -- "$expected_file" || :
echo-file -- "$filepath" || :

# remove DOROTHY_CONFIG_TESTING_LIST
expected_stderr="Updated configuration file: $DOROTHY/user/config/setup.bash"
eval-tester --name='remove config' --stderr="$expected_stderr" \
-- eval-no-color -- dorothy-config 'setup.bash' --prefer=public -- --field='DOROTHY_CONFIG_TESTING_LIST' --replace=
eval_tester --name='remove config' --stderr="$expected_stderr" \
-- eval-no-color -- dorothy-config "$filename" --prefer=public -- --field='DOROTHY_CONFIG_TESTING_LIST' --replace=

# output the configuration file, so we can track its progress, in case something goes wrong, which on CI it might
echo-file -- "$expected_file" || :
echo-file -- "$filepath" || :

echo-style --g1="TEST: $0"
return 0
Expand Down Expand Up @@ -231,7 +378,7 @@ function dorothy_config() (
)"
if [[ -n $default_filepath ]]; then
# start with the header of the default configuration file
echo-lines-before --needle='' --stdin <"$default_filepath" >"$temp_filepath"
echo-lines-before --needle='' --stdin <"$default_filepath" >>"$temp_filepath" # >> because https://github.com/bevry/dorothy/issues/283
__print_line >>"$temp_filepath"

# inject the sourcing of the default configuration file
Expand Down Expand Up @@ -262,6 +409,7 @@ function dorothy_config() (
fi

# append the body of the default configuration file
# @note unless bash.bash:eval_capture removes [>/dev/stdout] or does [>>/dev/stdout] this will still overwrite instead of append
echo-lines-after --needle='' --stdin <"$default_filepath" >>"$temp_filepath"
else
# even the dorothy default is missing
Expand Down
78 changes: 59 additions & 19 deletions commands/echo-lines-after
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,64 @@

function echo_lines_after_test() (
source "$DOROTHY/sources/bash.bash"
source "$(type -P eval-tester)"
echo-style --h1="TEST: $0"

eval-tester --name='using arguments' --stdout=$'c\n\nb' \
-- echo-lines-after '' --needle='b' -- $'a\nb\n\nb\n\nc\n\nb'

{
# prepare
local expected=$'c\n\nb' needles=('' '--needle=b')
function pl {
__print_lines a b '' b '' c '' b
} | eval-tester --name='using stdin' --stdout=$'c\n\nb' \
-- echo-lines-after '' --needle='b'
}

{
__print_lines a b '' b '' c '' b
} | eval-tester --name='using --stdin' --stdout=$'c\n\nb' \
-- echo-lines-after '' --needle='b' --stdin
# standard tests
eval_tester --name='using arguments' --stdout="$expected" \
-- echo-lines-after "${needles[@]}" -- "$(pl)"

{
__print_lines a b '' b '' c '' b
} | eval-tester --name='using -' --stdout=$'c\n\nb' \
-- echo-lines-after '' --needle='b' -
pl | eval_tester --name='using stdin' --stdout="$expected" \
-- echo-lines-after "${needles[@]}"

pl | eval_tester --name='using --stdin' --stdout="$expected" \
-- echo-lines-after "${needles[@]}" --stdin

pl | eval_tester --name='using -' --stdout="$expected" \
-- echo-lines-after "${needles[@]}" -

eval-tester --name='using <<<' --stdout=$'c\n\nb' \
-- echo-lines-after '' --needle='b' - <<<"$(__print_lines a b '' b '' c '' b)"
eval_tester --name='using <<<' --stdout="$expected" \
-- echo-lines-after "${needles[@]}" - <<<"$(pl)"

# https://github.com/bevry/dorothy/issues/283
local in out
in="$(mktemp)"
out="$(mktemp)"
pl >"$in"
function test_a {
echo-lines-after "${needles[@]}" --stdin <"$in"
}
function test_b {
echo-lines-after "${needles[@]}" --stdin <"$in" >"$out"
cat "$out"
}
function test_c {
echo-lines-after "${needles[@]}" --stdin >"$out" <"$in"
cat "$out"
}
function test_aa {
__print_lines '0' >"$out"
echo-lines-after "${needles[@]}" --stdin >>"$out" <"$in"
cat "$out"
}
function test_bb {
__print_lines '0' >"$out"
echo-lines-after "${needles[@]}" --stdin <"$in" >>"$out"
cat "$out"
}
eval_tester --stdout="$expected" -- test_a
eval_tester --stdout="$expected" -- test_b
eval_tester --stdout="$expected" -- test_c
eval_tester --stdout=$'0\n'"$expected" -- test_aa
eval_tester --stdout=$'0\n'"$expected" -- test_bb

# done
echo-style --g1="TEST: $0"
return 0
)
Expand Down Expand Up @@ -58,13 +93,13 @@ function echo_lines_after() (
b
# exit status: 0
echo-lines -- a b b c b | echo-lines-after --needle='' --stdin
echo-lines -- a b b c b | echo-lines-after --needle='b' --stdin
c
b
# exit status: 0
echo-lines-after --needle='' --stdin <<< "\$(echo-lines -- a b b c b)"
echo-lines-after --needle='b' --stdin <<< "\$(echo-lines -- a b b c b)"
c
b
Expand Down Expand Up @@ -109,7 +144,7 @@ function echo_lines_after() (
# =====================================
# Action

local output='no' found='no'
local output='no' found='no' # buffer=''
function on_line {
local line="$1" match='no' needle
if [[ $output == 'no' ]]; then
Expand All @@ -131,8 +166,13 @@ function echo_lines_after() (
return 0
fi
fi
# buffer+="$line"$'\n'
__print_lines "$line"
}
# function on_finish {
# __print_value_strings_or_nothing "$buffer"
# }
# ^ buffering does not solve the tests on linux (non-bsd/macos), as on linux `>>` will still overwrite if something called still did `>/dev/stdout` instead of `>>/dev/stdout`

stdinargs "${option_args[@]}"
)
Expand Down
Loading

0 comments on commit 331cca2

Please sign in to comment.