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 3fa7376
Show file tree
Hide file tree
Showing 50 changed files with 2,539 additions and 2,530 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/self-test-installation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ on:
push:
branches: [ "main" ]
paths:
- 'tests.d/1106-self-install/**'
- 'tests.d/self-install/**'
- 'commands.d/self-install.sh'
- 'commands.d/self-extend.sh'
pull_request:
paths:
- 'tests.d/1106-self-install/**'
- 'tests.d/self-install/**'
- 'commands.d/self-install.sh'
- 'commands.d/self-extend.sh'

Expand Down Expand Up @@ -62,6 +62,6 @@ jobs:
RUNNING_IN_DOCKER: true
BRANCH_NAME: ${{ github.head_ref || github.ref_name }}
run: |
chmod +x ./tests.d/1106-self-install/test
./tests.d/1106-self-install/test
chmod +x ./tests.d/self-install/test
./tests.d/self-install/test
11 changes: 8 additions & 3 deletions commands.d/self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ VALET_CONFIG_LOCAL_STATE_DIRECTORY=\"\${VALET_CONFIG_LOCAL_STATE_DIRECTORY:-${EX
# Defaults to the temporary directory (TMPDIR or /tmp).
VALET_CONFIG_WORK_FILES_DIRECTORY=\"\${VALET_CONFIG_WORK_FILES_DIRECTORY:-${EXPORTED_VALET_CONFIG_WORK_FILES_DIRECTORY:-}}\"
# The directory in which to write work files (small files to capture output of programs).
# You can set it to a tmpfs directory (such as /dev/shm) to speed up the execution of valet.
# Defaults to the temporary directory (TMPDIR or /tmp).
VALET_CONFIG_LOCALE=\"\${VALET_CONFIG_LOCALE:-${EXPORTED_VALET_CONFIG_LOCALE:-}}\"
# Number of last choices to remember when selecting an item from a command menu.
# Set to 0 to disable this feature and always display items in the alphabetical order.
VALET_CONFIG_REMEMBER_LAST_CHOICES=\"\${VALET_CONFIG_REMEMBER_LAST_CHOICES:-${EXPORTED_VALET_CONFIG_REMEMBER_LAST_CHOICES:-}}\"
Expand Down Expand Up @@ -335,22 +340,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
20 changes: 11 additions & 9 deletions commands.d/self-extend.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ source string
source system
# shellcheck source=../libraries.d/lib-interactive
source interactive
# shellcheck source=../libraries.d/lib-progress
source progress
# shellcheck source=../libraries.d/lib-curl
source curl

Expand Down Expand Up @@ -282,9 +284,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 +328,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 +337,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 +376,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 +556,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
23 changes: 12 additions & 11 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 Expand Up @@ -352,9 +352,10 @@ function selfTest_runSingleTestSuite() {
mkdir -p "${GLOBAL_TEST_OUTPUT_TEMPORARY_DIRECTORY}"
# shellcheck disable=SC2034
GLOBAL_TEST_CURRENT_DIRECTORY="${testSuiteDirectory}"
GLOBAL_TESTS_D_DIRECTORY="${testsDotDirectory}"

# run a custom user script before the test suite if it exists
selfTestUtils_runHookScript "${testsDotDirectory}/before-each-test-suite"
selfTestUtils_runHookScript "${GLOBAL_TESTS_D_DIRECTORY}/before-each-test-suite"

# write the test suite title
printf "%s\n\n" "# Test suite ${GLOBAL_TEST_SUITE_NAME}" >"${GLOBAL_TEST_REPORT_FILE}"
Expand Down Expand Up @@ -459,7 +460,7 @@ function selfTest_runSingleTestSuite() {
popd 1>/dev/null

# run a custom user script after the test suite if it exists
selfTestUtils_runHookScript "${testsDotDirectory}/after-each-test-suite"
selfTestUtils_runHookScript "${GLOBAL_TESTS_D_DIRECTORY}/after-each-test-suite"

# exit with an error if there were errors in the test scripts
if ((nbOfScriptsWithErrors > 0)); then
Expand Down Expand Up @@ -547,14 +548,14 @@ function selfTest_runSingleTest() {
exec 4>&2 2>"${GLOBAL_TEST_STANDARD_ERROR_FILE}"

# run a custom user script before the test if it exists
selfTestUtils_runHookScript "${testDirectory}/before-each-test"
selfTestUtils_runHookScript "${GLOBAL_TESTS_D_DIRECTORY}/before-each-test"

# run the test
# shellcheck disable=SC1090
builtin source "${testScript}"

# run a custom user script after the test if it exists
selfTestUtils_runHookScript "${testDirectory}/after-each-test"
selfTestUtils_runHookScript "${GLOBAL_TESTS_D_DIRECTORY}/after-each-test"

exec 3>&-
exec 4>&-
Expand Down
6 changes: 3 additions & 3 deletions docs/content/docs/030.test-commands/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ Here is an example of directory structure for your user directory:
{{< /filetree/folder >}}
{{< filetree/folder name="shared-commands" >}}
{{< filetree/folder name="tests.d" >}}
{{< filetree/file name="before-each-test" >}}
{{< filetree/folder name="test-suite3" >}}
{{< filetree/file name="before-each-test" >}}
{{< filetree/file name="test.sh" >}}
{{< /filetree/folder >}}
{{< /filetree/folder >}}
Expand Down Expand Up @@ -166,8 +166,8 @@ In addition to the test scripts, you can create other specific scripts which wil
| `tests.d/after-tests` | Source'd after all the test suites inside the tests.d folder are executed. |
| `tests.d/before-each-test-suite` | Source'd before the execution of each test suite. |
| `tests.d/after-each-test-suite` | Source'd after the execution of each test suite. |
| `tests.d/{test-suite}/before-each-test` | Source'd before the execution of each test script. |
| `tests.d/{test-suite}/after-each-test` | Source'd after the execution of each test script. |
| `tests.d/before-each-test` | Source'd before the execution of each test script. |
| `tests.d/after-each-test` | Source'd after the execution of each test script. |

{{< cards >}}
{{< card icon="arrow-circle-left" link="../command-properties" title="Command properties" >}}
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
Loading

0 comments on commit 3fa7376

Please sign in to comment.