Skip to content

Commit

Permalink
♻️ refacto tests lib-io
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Jan 21, 2025
1 parent 920abb1 commit 3f80715
Show file tree
Hide file tree
Showing 35 changed files with 2,420 additions and 2,445 deletions.
6 changes: 3 additions & 3 deletions commands.d/self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -335,22 +335,22 @@ VALET_CONFIG_FSFS_COUNT_RESET=\"\${VALET_CONFIG_FSFS_COUNT_RESET:-${EXPORTED_VAL
## -----------
# Change the default progress bar template.
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_PROGRESS_BAR_TEMPLATE=\"\${VALET_CONFIG_PROGRESS_BAR_TEMPLATE:-${EXPORTED_VALET_CONFIG_PROGRESS_BAR_TEMPLATE:-}}\"
# Change the default progress bar size
VALET_CONFIG_PROGRESS_BAR_SIZE=\"\${VALET_CONFIG_PROGRESS_BAR_SIZE:-${EXPORTED_VALET_CONFIG_PROGRESS_BAR_SIZE:-}}\"
# Change the default time between two frames for the animation of the spinner in the progress
# (in seconds, can be a float number).
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_PROGRESS_ANIMATION_DELAY=\"\${VALET_CONFIG_PROGRESS_ANIMATION_DELAY:-${EXPORTED_VALET_CONFIG_PROGRESS_ANIMATION_DELAY:-}}\"
# The default number of animation frames to wait between two updates of the progress bar.
VALET_CONFIG_PROGRESS_BAR_UPDATE_INTERVAL=\"\${VALET_CONFIG_PROGRESS_BAR_UPDATE_INTERVAL:-${EXPORTED_VALET_CONFIG_PROGRESS_BAR_UPDATE_INTERVAL:-}}\"
# Change the default spinner characters (one character represents one frame of the animation).
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_SPINNER_CHARACTERS=\"\${VALET_CONFIG_SPINNER_CHARACTERS:-${EXPORTED_VALET_CONFIG_SPINNER_CHARACTERS:-}}\"
# The default character to use to represent the selected item in the interactive mode.
Expand Down
18 changes: 9 additions & 9 deletions commands.d/self-extend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,9 @@ function selfExtend_downloadTarball() {

# download the tarball
log::info "Downloading the extension from the URL ⌜${tarballUrl}⌝ for sha1 ⌜${sha1}⌝."
interactive::startProgress "#spinner Download in progress, please wait..."
progress::start "#spinner Download in progress, please wait..."
curl::toFile true 200,302 "${tempDirectory}/${sha1}.tar.gz" "${tarballUrl}"
interactive::stopProgress
progress::stop

# untar
tar -xzf "${tempDirectory}/${sha1}.tar.gz" -C "${tempDirectory}" || core::fail "Could not untar the extension tarball ⌜${tempDirectory}/${sha1}.tar.gz⌝ using tar."
Expand Down Expand Up @@ -326,7 +326,7 @@ function selfExtend_getSha1() {

# get the sha1
RETURNED_VALUE=""
interactive::startProgress "#spinner Fetching reference information from GitHub..."
progress::start "#spinner Fetching reference information from GitHub..."
local url="https://api.github.com/repos/${owner}/${repo}/git/refs/heads/${reference}"
if ! curl::toVar false '200' -H "Accept: application/vnd.github.v3+json" "${url}"; then
url="https://api.github.com/repos/${owner}/${repo}/git/refs/tags/${reference}"
Expand All @@ -335,7 +335,7 @@ function selfExtend_getSha1() {
local response="${RETURNED_VALUE}"
local error="${RETURNED_VALUE2}"
local httpCode="${RETURNED_VALUE3}"
interactive::stopProgress
progress::stop

if [[ ${httpCode} == 404 ]]; then
core::fail "Could not find a branch or tag for the reference ⌜${reference}⌝ in repository ⌜${repositoryUrl}⌝: ${response}${error}."
Expand Down Expand Up @@ -374,9 +374,9 @@ function selfExtend_gitClone() {
rm -Rf "${targetDirectory}"

log::info "Cloning the git repository ⌜${url}⌝ with reference ⌜${version}⌝ in ⌜${targetDirectory}⌝."
interactive::startProgress "#spinner Cloning repo, please wait..."
progress::start "#spinner Cloning repo, please wait..."
io::invoke git "${args[@]}"
interactive::stopProgress
progress::stop
}

# Execute the `extension.setup.sh` script of the extension, if any.
Expand Down Expand Up @@ -554,14 +554,14 @@ function selfExtend_updateGitRepository() {
branch="${branch%%$'\n'*}"
log::debug "Fetching and merging branch ⌜${branch}⌝ from ⌜origin⌝ remote."
pushd "${repoPath}" &>/dev/null || core::fail "Could not change to the directory ⌜${repoPath}⌝."
interactive::startProgress "#spinner Fetching reference ${branch} for extension ${extensionName}..."
progress::start "#spinner Fetching reference ${branch} for extension ${extensionName}..."
if ! git fetch -q; then
popd &>/dev/null || :
interactive::stopProgress
progress::stop
log::warning "Failed to fetch from ⌜origin⌝ remote for the repo ⌜${path}⌝."
return 1
fi
interactive::stopProgress
progress::stop
if ! git merge -q --ff-only "origin/${branch}" &>/dev/null; then
popd &>/dev/null || :
log::warning "Failed to update the git repository ⌜${path}⌝, clean your workarea first (e.g. git stash, or git commit)."
Expand Down
14 changes: 8 additions & 6 deletions commands.d/self-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,9 @@ function selfUpdate_install() {
local releaseFile="${tempDirectory}/valet.tar.gz"

log::debug "Downloading the release from ⌜${releaseUrl}⌝."
interactive::startProgress "#spinner Downloading valet..."
progress::start "#spinner Downloading valet..."
selfUpdate_download "${releaseUrl}" "${releaseFile}"
interactive::stopProgress
progress::stop

log::debug "Unpacking the release in ⌜${GLOBAL_VALET_HOME}⌝."
tar -xzf "${releaseFile}" -C "${tempDirectory}" || core::fail "Could not unpack the release ⌜${releaseFile}⌝ using tar."
Expand Down Expand Up @@ -514,9 +514,9 @@ function selfUpdate_isDirectoryInPath() {
# Get the version number of the latest release on GitHub.
function selfUpdate_getLatestReleaseVersion() {
local jsonFile="${TMPDIR:-/tmp}/valet.latest.json"
interactive::startProgress "#spinner Fetching the latest version from GitHub API..."
progress::start "#spinner Fetching the latest version from GitHub API..."
selfUpdate_download "https://api.github.com/repos/jcaillon/valet/releases/latest" "${jsonFile}"
interactive::stopProgress
progress::stop
io::readFile "${jsonFile}"
rm -f "${jsonFile}" 1>/dev/null
if [[ ${RETURNED_VALUE} =~ "tag_name\":"([ ]?)"\"v"([^\"]+)"\"" ]]; then
Expand Down Expand Up @@ -589,6 +589,8 @@ function selfUpdate_sourceDependencies() {
source system
# shellcheck source=../libraries.d/lib-interactive
source interactive
# shellcheck source=../libraries.d/lib-progress
source progress
# shellcheck source=../libraries.d/lib-ansi-codes
source ansi-codes
# shellcheck source=../libraries.d/lib-io
Expand Down Expand Up @@ -752,8 +754,8 @@ if [[ -z "${GLOBAL_CORE_INCLUDED:-}" ]]; then
return 1
}
function io::readFile() { RETURNED_VALUE="$(<"${1}")"; }
function interactive::startProgress() { :; }
function interactive::stopProgress() { :; }
function progress::start() { :; }
function progress::stop() { :; }

VALET_CONFIG_FILE="${VALET_CONFIG_FILE:-"${VALET_CONFIG_DIRECTORY:-${XDG_CONFIG_HOME:-${HOME}/.config}/valet}/config"}"
else
Expand Down
14 changes: 7 additions & 7 deletions commands.d/self-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fi
source self-test-utils
# shellcheck source=../libraries.d/lib-string
source string
# shellcheck source=../libraries.d/lib-interactive
source interactive
# shellcheck source=../libraries.d/lib-progress
source progress
# shellcheck source=../libraries.d/lib-bash
source bash

Expand Down Expand Up @@ -244,8 +244,8 @@ function selfTest_runSingleTestSuites() {
# run a custom user script before the tests if it exists
selfTestUtils_runHookScript "${testsDotDirectory}/before-tests"

interactive::startProgress
interactive::updateProgress 0 "Running test suites."
progress::start
progress::update 0 "Running test suites."

# sequential run
if ((_TEST_NB_PARALLEL_TEST_SUITES == 0 || ${#testSuiteDirectories[@]} == 1)); then
Expand All @@ -254,7 +254,7 @@ function selfTest_runSingleTestSuites() {
testDirectoryName="${testDirectory##*/}"
log::debug "Starting test suite ⌜${testDirectoryName}⌝."

interactive::updateProgress $((testSuitesDoneCount * 100 / ${#testSuiteDirectories[@]})) "Running test suite ⌜${testDirectoryName}⌝."
progress::update $((testSuitesDoneCount * 100 / ${#testSuiteDirectories[@]})) "Running test suite ⌜${testDirectoryName}⌝."
(selfTest_runSingleTestSuite "${testDirectory}") || _TEST_FAILED_TEST_SUITES+=("${testDirectoryName}")
testSuitesDoneCount+=1
done
Expand All @@ -272,7 +272,7 @@ function selfTest_runSingleTestSuites() {
bash::runInParallel _TEST_SUITE_NAMES _TEST_SUITE_COMMANDS "${_TEST_NB_PARALLEL_TEST_SUITES}" selfTest_parallelCallback
fi

interactive::stopProgress
progress::stop

# run a custom user script after the tests if it exists
selfTestUtils_runHookScript "${testsDotDirectory}/after-tests"
Expand All @@ -294,7 +294,7 @@ function selfTest_parallelCallback() {
local -i exitCode="${3}"
local -i percent="${4}"

interactive::updateProgress "${percent}" "Done running test suite ⌜${jobName}⌝."
progress::update "${percent}" "Done running test suite ⌜${jobName}⌝."

# display the job output
io::readFile "${_TEST_SUITE_OUTPUT_FILES[${index}]}"
Expand Down
3 changes: 3 additions & 0 deletions docs/content/docs/800.roadmap/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ url: /docs/roadmap

This page lists the features that I would like to implement in Valet. They come in addition to new features described in the [issues][valet-issues].

- add info of the extension from which a command comes from
- we split the commands file into several one, per extension, so we don't have to load everything immediately
- in the menu we can filter by extension (and we see the extension of a command)
- propagate the set -x in self test subshells to have profiling enabled. We can create a new method in lib-profiler to profiler::reapply.
- test the "sudo" feature: it runs the command by forking. We could add an option to instead rerun valet with sudo.
- document the test; use the lib-test test as an example, and also link to the test:: lib.
Expand Down
6 changes: 3 additions & 3 deletions docs/static/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,22 +226,22 @@ VALET_CONFIG_FSFS_COUNT_RESET="${VALET_CONFIG_FSFS_COUNT_RESET:-}"
## -----------

# Change the default progress bar template.
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_PROGRESS_BAR_TEMPLATE="${VALET_CONFIG_PROGRESS_BAR_TEMPLATE:-}"

# Change the default progress bar size
VALET_CONFIG_PROGRESS_BAR_SIZE="${VALET_CONFIG_PROGRESS_BAR_SIZE:-}"

# Change the default time between two frames for the animation of the spinner in the progress
# (in seconds, can be a float number).
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_PROGRESS_ANIMATION_DELAY="${VALET_CONFIG_PROGRESS_ANIMATION_DELAY:-}"

# The default number of animation frames to wait between two updates of the progress bar.
VALET_CONFIG_PROGRESS_BAR_UPDATE_INTERVAL="${VALET_CONFIG_PROGRESS_BAR_UPDATE_INTERVAL:-}"

# Change the default spinner characters (one character represents one frame of the animation).
# See interactive::startProgress.
# See progress::start.
VALET_CONFIG_SPINNER_CHARACTERS="${VALET_CONFIG_SPINNER_CHARACTERS:-}"

# The default character to use to represent the selected item in the interactive mode.
Expand Down
17 changes: 9 additions & 8 deletions examples.d/showcase/commands.d/showcase-interactive.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
# Importing libraries from the core (note that we could do that in the function that needs it as well)
# shellcheck disable=SC1091
source interactive

# shellcheck disable=SC1091
source progress
# shellcheck disable=SC1091
source fsfs

Expand All @@ -23,12 +24,12 @@ function showcaseInteractive() {
core::checkParseResults "${help:-}" "${parsingErrors:-}"

log::info "Getting the cursor size:"
interactive::getCursorPosition
tui::getCursorPosition
log::info "GLOBAL_CURSOR_LINE: ⌜${GLOBAL_CURSOR_LINE}"
log::info "GLOBAL_CURSOR_COLUMN: ⌜${GLOBAL_CURSOR_COLUMN}"

log::info "Getting the terminal size:"
interactive::getTerminalSize
tui::getTerminalSize
log::info "GLOBAL_LINES: ⌜${GLOBAL_LINES}"
log::info "GLOBAL_COLUMNS: ⌜${GLOBAL_COLUMNS}"

Expand Down Expand Up @@ -56,7 +57,7 @@ function showcaseInteractive() {
fi

log::info "Creating some space below this line."
interactive::createSpace 4
tui::createSpace 4

############################
# Fsfs
Expand All @@ -79,7 +80,7 @@ function showcaseInteractive() {

log::warning "Now displaying a spinner"

interactive::startProgress "#spinner" "" 0.05 "" "" "⢄⢂⢁⡁⡈⡐⡠"
progress::start "#spinner" "" 0.05 "" "" "⢄⢂⢁⡁⡈⡐⡠"

IDX=0
while [[ ${IDX} -lt 3 ]]; do
Expand All @@ -88,7 +89,7 @@ function showcaseInteractive() {
sleep 1
done

interactive::stopProgress
progress::stop


############################
Expand All @@ -99,13 +100,13 @@ function showcaseInteractive() {

IDX=0
while [[ ${IDX} -le 50 ]]; do
interactive::updateProgress $((IDX * 2)) "doing something ${IDX}..."
progress::update $((IDX * 2)) "doing something ${IDX}..."
IDX=$((IDX + 1))
# log::info "alright ${IDX}..."
sleep 0.1
done

interactive::stopProgress
progress::stop


log::info "End of demo!"
Expand Down
8 changes: 8 additions & 0 deletions extras/base.code-snippets
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@
"\\${GLOBAL_CURSOR_COLUMN}$0"
]
},
"$GLOBAL_TEST_TEMP_FILE": {
"prefix": "$GLOBAL_TEST_TEMP_FILE",
"description": "The path to a temporary file that can be used during tests.",
"scope": "",
"body": [
"\\${GLOBAL_TEST_TEMP_FILE}$0"
]
},
"$LAST_KEY_PRESSED": {
"prefix": "$LAST_KEY_PRESSED",
"description": "",
Expand Down
17 changes: 11 additions & 6 deletions libraries.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ function io::cleanupTempFiles() {
}

#===============================================================
# >>> Logging
# >>> Terminal UI
#===============================================================

# ## interactive::getTerminalSize
# ## tui::getTerminalSize
#
# This function exports the terminal size.
#
Expand All @@ -217,10 +217,10 @@ function io::cleanupTempFiles() {
# - `GLOBAL_LINES`: The number of lines in the terminal.
#
# ```bash
# interactive::getTerminalSize
# tui::getTerminalSize
# printf '%s\n' "The terminal has ⌜${GLOBAL_COLUMNS}⌝ columns and ⌜${GLOBAL_LINES}⌝ lines."
# ```
function interactive::getTerminalSize() {
function tui::getTerminalSize() {
shopt -s checkwinsize
# the following subshell is required to correctly compute the columns and lines
# the bash manual says these are computed after the execution of an external command
Expand All @@ -234,6 +234,11 @@ function interactive::getTerminalSize() {
shopt -u checkwinsize
}

#===============================================================
# >>> Logging
#===============================================================


# ## log::setLevel
#
# Set the log level.
Expand Down Expand Up @@ -1757,10 +1762,10 @@ esac

# Export terminal size on window resize if we are in interactive mode.
if [[ -t 2 ]]; then
interactive::getTerminalSize
tui::getTerminalSize
GLOBAL_SCREEN_REDRAW_REQUIRED=false
# shellcheck disable=SC2090
trap 'interactive::getTerminalSize; GLOBAL_SCREEN_REDRAW_REQUIRED=true; log::createPrintFunction; eval "${GLOBAL_LOG_PRINT_FUNCTION}"' SIGWINCH
trap 'tui::getTerminalSize; GLOBAL_SCREEN_REDRAW_REQUIRED=true; log::createPrintFunction; eval "${GLOBAL_LOG_PRINT_FUNCTION}"' SIGWINCH
else
GLOBAL_COLUMNS="${VALET_DEFAULT_COLUMNS:-120}"
GLOBAL_LINES="${VALET_DEFAULT_LINES:-30}"
Expand Down
Loading

0 comments on commit 3f80715

Please sign in to comment.