Skip to content

Commit

Permalink
♻️ refactored all lib tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed Jan 21, 2025
1 parent 3fa7376 commit 220d003
Show file tree
Hide file tree
Showing 37 changed files with 2,870 additions and 2,464 deletions.
3 changes: 2 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@
"cSpell.words": [
"nocasematch",
"Pids"
]
],
"editor.wordSeparators": "`~!@#$%^&*()-=+[{]}\\|;'\",.<>/?"
}
2 changes: 1 addition & 1 deletion commands.d/help.sh
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ examples:
function showCommandHelp() {
local -a commands
local parsingErrors help columns noColors help
main::parseFunctionArguments "${FUNCNAME[0]:-}" "$@"
command::parseFunctionArguments "${FUNCNAME[0]:-}" "$@"
eval "${RETURNED_VALUE}"

core::checkParseResults "${help:-}" "${parsingErrors:-}"
Expand Down
4 changes: 3 additions & 1 deletion commands.d/self-build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ function bumpValetBuildVersion() {
local currentVersion="${RETURNED_VALUE:-0.0.0}"
currentVersion="${currentVersion%%$'\n'*}"

string::bumpSemanticVersion "${currentVersion}" "patch" "false"
version::bump "${currentVersion}" "patch" "false"

printf '%s' "${RETURNED_VALUE}" >"${GLOBAL_VALET_HOME}/version"

Expand Down Expand Up @@ -716,6 +716,8 @@ source array
source io
# shellcheck source=../libraries.d/lib-string
source string
# shellcheck source=../libraries.d/lib-version
source version

# if this script is run directly, execute the function, otherwise valet will do it
if [[ ${_NOT_EXECUTED_FROM_VALET:-false} == "true" ]]; then
Expand Down
4 changes: 3 additions & 1 deletion commands.d/self-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ function selfUpdate() {
currentVersion="${currentVersion%%$'\n'*}"

# compare local and distant versions
string::compareSemanticVersion "${currentVersion}" "${version}"
version::compare "${currentVersion}" "${version}"
if [[ ${RETURNED_VALUE} == "0" || ${RETURNED_VALUE} == "1" ]]; then
log::info "The current local version ⌜${currentVersion}⌝ is higher or equal to the distant version ⌜${version}⌝."
log::success "You already have the latest version."
Expand Down Expand Up @@ -597,6 +597,8 @@ function selfUpdate_sourceDependencies() {
source io
# shellcheck source=../libraries.d/lib-string
source string
# shellcheck source=../libraries.d/lib-version
source version
}

# Create a shim (script that calls valet) in a bin directory.
Expand Down
4 changes: 3 additions & 1 deletion commands.d/self-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ source interactive
source system
# shellcheck source=../libraries.d/lib-array
source array
# shellcheck source=../libraries.d/lib-version
source version

#===============================================================
# >>> self release valet
Expand Down Expand Up @@ -312,7 +314,7 @@ function selfRelease::bumpVersion() {
log::info "The current version of valet is: ${version}."

# bump the version
string::bumpSemanticVersion "${version}" "${bumpLevel}" && newVersion="${RETURNED_VALUE}"
version::bump "${version}" "${bumpLevel}" && newVersion="${RETURNED_VALUE}"
if [[ "${dryRun:-}" != "true" ]]; then io::writeToFile "${GLOBAL_VALET_HOME}/version" "${newVersion}"; fi
log::info "The bumped version of valet is: ${newVersion}."

Expand Down
3 changes: 2 additions & 1 deletion commands.d/self-test-utils
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,9 @@ function selfTestUtils_setupValetForConsistency() {

# fix the time to a known value
export TZ=Etc/GMT+0
# unset EPOCHSECONDS EPOCHREALTIME
unset EPOCHSECONDS
export EPOCHSECONDS=548902800
# can't unset EPOCHREALTIME because we use it in PS4 during debug, #TODO: need to fix that
export EPOCHREALTIME=548902800.000000

log::createPrintFunction
Expand Down
1 change: 1 addition & 0 deletions docs/content/docs/800.roadmap/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,6 @@ This page lists the features that I would like to implement in Valet. They come
- handle shift + arrows to highlight text
- add a stack for kill/yank
- add a stack for undo/redo
- add a new command self diagnostic that will run a series of tests to check the environment and help figure out what's wrong.

[valet-issues]: https://github.com/jcaillon/valet/issues
9 changes: 6 additions & 3 deletions libraries.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ function core::setShellOptions() {
# default IFS
IFS=' '$'\t'$'\n'

# set a fixed locale (otherwise we would risk to have comma as decimal separator
# for EPOCHREALTIME for instance).
# Set a locale otherwise we will face inconsistencies in the output of commands:
# - file listing will not sort the same way
# - string comparison < and > will not have the same behavior
# - `EPOCHREALTIME` can output with a comma as decimal separator instead of a dot
# - ...
# If the user does not have the locale, they will get a warning here.
# In which case they must either generate the locale C.utf8 or set an existing locale
# listed in `locale -a` via the VALET_CONFIG_LOCALE environment variable.
Expand Down Expand Up @@ -1706,7 +1709,7 @@ function core::parseArguments() {
# get the function name of the calling function
functionName="${FUNCNAME[1]?"This function must be called from a command function."}"

main::parseFunctionArgumentsOrGoInteractive "${functionName}" "$@"
main::parseCmdFunctionArgumentsOrGoInteractive "${functionName}" "$@"
}

# ## core::checkParseResults
Expand Down
112 changes: 0 additions & 112 deletions libraries.d/lib-string
Original file line number Diff line number Diff line change
Expand Up @@ -47,118 +47,6 @@ function string::cutField() {
RETURNED_VALUE=""
}

# ## string::compareSemanticVersion
#
# This function allows to compare two semantic versions formatted like:
# major.minor.patch-prerelease+build
#
# - $1: **version1** _as string_:
# the first version to compare
# - $2: **version2** _as string_:
# the second version to compare
#
# Returns:
#
# - `RETURNED_VALUE`:
# - 0 if the versions are equal,
# - 1 if version1 is greater,
# - -1 if version2 is greater
#
# ```bash
# string::compareSemanticVersion "2.3.4-prerelease+build" "1.2.3-prerelease+build"
# local comparison="${RETURNED_VALUE}"
# ```
#
# > The prerelease and build are ignored in the comparison.
function string::compareSemanticVersion() {
local version1="${1#v}"
local version2="${2#v}"

local -i semVerIndex
local semVerNumber1 semVerNumber2
for semVerIndex in {0..2}; do
string::cutField "${version1}" "${semVerIndex}" "."
semVerNumber1="${RETURNED_VALUE%%-*}"
semVerNumber1="${semVerNumber1%%+*}"
string::cutField "${version2}" "${semVerIndex}" "."
semVerNumber2="${RETURNED_VALUE%%-*}"
semVerNumber2="${semVerNumber2%%+*}"
if [[ ! ${semVerNumber1} =~ ^[0-9]+$ || ! ${semVerNumber2} =~ ^[0-9]+$ ]]; then
core::fail "Failed to compare versions ⌜${version1}⌝ and ⌜${version2}⌝ because they are not valid semantic versions."
elif (( semVerNumber1 > semVerNumber2 )); then
RETURNED_VALUE=1
return 0
elif (( semVerNumber1 < semVerNumber2 )); then
RETURNED_VALUE=-1
return 0
fi
done
RETURNED_VALUE=0
}

# ## string::bumpSemanticVersion
#
# This function allows to bump a semantic version formatted like:
# major.minor.patch-prerelease+build
#
# - $1: **version** _as string_:
# the version to bump
# - $2: **level** _as string_:
# the level to bump (major, minor, patch)
# - $3: clear build and prerelease _as bool_:
# (optional) clear the prerelease and build
# (defaults to true)
#
# Returns:
#
# - `RETURNED_VALUE`: the new version string
#
# ```bash
# string::bumpSemanticVersion "1.2.3-prerelease+build" "major"
# local newVersion="${RETURNED_VALUE}"
# ```
function string::bumpSemanticVersion() {
local version level clearPreRelease
version="${1}"
bumpLevel="${2}"
clearPreRelease="${3:-true}"

local prerelease build modifiedVersion
modifiedVersion="${version}-+"
prerelease="${modifiedVersion#*-}"
prerelease="${prerelease%%+*}"
if [[ -n "${prerelease}" ]]; then prerelease="-${prerelease%-}"; fi
build="${modifiedVersion#*+}"
if [[ -n "${build}" ]]; then
build="+${build%-+}"
fi

# bump the version
local -i level semVerNumber semVerIndex
level=2
if [[ ${bumpLevel:-} == "major" ]]; then level=0; fi
if [[ ${bumpLevel:-} == "minor" ]]; then level=1; fi
local newVersion semVerString
for semVerIndex in {0..2}; do
string::cutField "${version}" "${semVerIndex}" "."
semVerString="${RETURNED_VALUE%-*}"
if [[ ! ${semVerString} =~ ^[0-9]+$ ]]; then
core::fail "Failed to bump the version ⌜${version}⌝ because it is not valid semantic version."
fi
semVerNumber="${semVerString%+}"
if [[ semVerIndex -eq level ]]; then semVerNumber=$((semVerNumber + 1)); fi
if [[ semVerIndex -gt level ]]; then semVerNumber=0; fi
newVersion+="${semVerNumber}."
done
newVersion="${newVersion%.}"

if [[ "${clearPreRelease}" != "true" ]]; then
newVersion="${newVersion%.}${prerelease}${build}"
fi

RETURNED_VALUE="${newVersion}"
}

# ## string::camelCaseToSnakeCase
#
# This function convert a camelCase string to a SNAKE_CASE string.
Expand Down
2 changes: 1 addition & 1 deletion libraries.d/lib-system
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ function system::env() {
#
# > This function avoid to call $(date) in a subshell (date is a an external executable).
function system::date() {
local format="${1:-'%(%F_%Hh%Mm%Ss)T'}"
local format="${1:-"%(%F_%Hh%Mm%Ss)T"}"
# shellcheck disable=SC2059
printf -v RETURNED_VALUE "${format}" "${EPOCHSECONDS}"
}
Expand Down
119 changes: 119 additions & 0 deletions libraries.d/lib-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
#!/usr/bin/env bash
# author: github.com/jcaillon
# description: This script can be sourced by commands to provide convenient functions.

# shellcheck source=lib-string
source string

# ## version::compare
#
# This function allows to compare two semantic versions formatted like:
# major.minor.patch-prerelease+build
#
# - $1: **version1** _as string_:
# the first version to compare
# - $2: **version2** _as string_:
# the second version to compare
#
# Returns:
#
# - `RETURNED_VALUE`:
# - 0 if the versions are equal,
# - 1 if version1 is greater,
# - -1 if version2 is greater
#
# ```bash
# version::compare "2.3.4-prerelease+build" "1.2.3-prerelease+build"
# local comparison="${RETURNED_VALUE}"
# ```
#
# > The prerelease and build are ignored in the comparison.
function version::compare() {
local version1="${1#v}"
local version2="${2#v}"

local -i semVerIndex
local semVerNumber1 semVerNumber2
for semVerIndex in {0..2}; do
string::cutField "${version1}" "${semVerIndex}" "."
semVerNumber1="${RETURNED_VALUE%%-*}"
semVerNumber1="${semVerNumber1%%+*}"
string::cutField "${version2}" "${semVerIndex}" "."
semVerNumber2="${RETURNED_VALUE%%-*}"
semVerNumber2="${semVerNumber2%%+*}"
if [[ ! ${semVerNumber1} =~ ^[0-9]+$ || ! ${semVerNumber2} =~ ^[0-9]+$ ]]; then
core::fail "Failed to compare versions ⌜${version1}⌝ and ⌜${version2}⌝ because they are not valid semantic versions."
elif (( semVerNumber1 > semVerNumber2 )); then
RETURNED_VALUE=1
return 0
elif (( semVerNumber1 < semVerNumber2 )); then
RETURNED_VALUE=-1
return 0
fi
done
RETURNED_VALUE=0
}

# ## version::bump
#
# This function allows to bump a semantic version formatted like:
# major.minor.patch-prerelease+build
#
# - $1: **version** _as string_:
# the version to bump
# - $2: **level** _as string_:
# the level to bump (major, minor, patch)
# - $3: clear build and prerelease _as bool_:
# (optional) clear the prerelease and build
# (defaults to true)
#
# Returns:
#
# - `RETURNED_VALUE`: the new version string
#
# ```bash
# version::bump "1.2.3-prerelease+build" "major"
# local newVersion="${RETURNED_VALUE}"
# ```
function version::bump() {
local version level clearPreRelease
version="${1}"
bumpLevel="${2}"
clearPreRelease="${3:-true}"

local prerelease build modifiedVersion
modifiedVersion="${version}-+"
prerelease="${modifiedVersion#*-}"
prerelease="${prerelease%%+*}"
if [[ -n "${prerelease}" ]]; then prerelease="-${prerelease%-}"; fi
build="${modifiedVersion#*+}"
if [[ -n "${build}" ]]; then
build="+${build%-+}"
fi

# bump the version
local -i level semVerNumber semVerIndex
level=2
if [[ ${bumpLevel:-} == "major" ]]; then level=0; fi
if [[ ${bumpLevel:-} == "minor" ]]; then level=1; fi
local newVersion semVerString
for semVerIndex in {0..2}; do
string::cutField "${version}" "${semVerIndex}" "."
semVerString="${RETURNED_VALUE%-*}"
if [[ ! ${semVerString} =~ ^[0-9]+$ ]]; then
core::fail "Failed to bump the version ⌜${version}⌝ because it is not valid semantic version."
fi
semVerNumber="${semVerString%+}"
if [[ semVerIndex -eq level ]]; then semVerNumber=$((semVerNumber + 1)); fi
if [[ semVerIndex -gt level ]]; then semVerNumber=0; fi
newVersion+="${semVerNumber}."
done
newVersion="${newVersion%.}"

if [[ "${clearPreRelease}" != "true" ]]; then
newVersion="${newVersion%.}${prerelease}${build}"
fi

RETURNED_VALUE="${newVersion}"
}

Loading

0 comments on commit 220d003

Please sign in to comment.