Skip to content

Commit

Permalink
✨ simplified temp files mngmt for tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jcaillon committed May 1, 2024
1 parent 4f19d6f commit 5b7b3bc
Show file tree
Hide file tree
Showing 19 changed files with 35 additions and 40 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ Please check the [CONTRIBUTING.md][contributing] documentation if you intend to
- Betters checks in self build!
- Support alternative single comments `# ## VALET COMMAND` instead of multiline comments for command declaration (see we can have help in autocompletion).
- A command can declare dependencies to auto check if some tools are installed before running the command.
- Generate vscode snippets from each function in core and lib-* to provide autocompletion for users.

[releases]: https://github.com/jcaillon/valet/releases
[latest-release]: https://github.com/jcaillon/valet/releases/latest
Expand Down
2 changes: 2 additions & 0 deletions docs/content/_index.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ keywords:
{{< /hextra/hero-headline >}}
</div>

Valet is a zero dependency framework that helps you build fast, robust, testable and interactive bash scripts.

<div class="hx-mb-12">
{{< hextra/hero-subtitle >}}
Build awesome bash scripts!
Expand Down
1 change: 0 additions & 1 deletion tests.d/1001-main-functions/01.sort-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

function testSortCommandsAndLastChoice() {
# overriding core::getLocalStateDirectory to return a temporary directory
setTempFilesNumber 700
io::createTempDirectory && local localStateDirectory="${RETURNED_VALUE}"
VALET_CONFIG_LOCAL_STATE_DIRECTORY="${localStateDirectory}"
VALET_CONFIG_REMEMBER_LAST_CHOICES=5
Expand Down
1 change: 0 additions & 1 deletion tests.d/1005-lib-io/01.invoke.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env bash

function testIo::invoke5() {
setTempFilesNumber 300
io::createTempFile && local tmpFile="${RETURNED_VALUE}"
local -i exitCode

Expand Down
1 change: 0 additions & 1 deletion tests.d/1100-self-config/01.self-config.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function testSelfConfig() {
local -i exitCode
local originalConfigFile="${VALET_CONFIG_FILE:-}"
local configFile
setTempFilesNumber 400
io::createTempFile && configFile="${RETURNED_VALUE}"
VALET_CONFIG_FILE="${configFile}"

Expand Down
18 changes: 9 additions & 9 deletions tests.d/1100-self-config/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ Exit code: `0`

```plaintext
→ selfConfig
▶ called myEditor: /tmp/valet.d/f401-0
▶ called myEditor: /tmp/valet.d/f1-0
cat ${configFile}
#!/usr/bin/env bash
Expand Down Expand Up @@ -200,8 +200,8 @@ VALET_CONFIG_BUMP_VERSION_ON_BUILD="${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-}"
**Error** output:

```log
INFO Creating the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Opening the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Creating the valet config file ⌜/tmp/valet.d/f1-0⌝.
INFO Opening the valet config file ⌜/tmp/valet.d/f1-0⌝.
```

### Testing selfConfig (should only open, file exists)
Expand All @@ -212,13 +212,13 @@ Exit code: `0`

```plaintext
→ selfConfig
▶ called myEditor: /tmp/valet.d/f401-0
▶ called myEditor: /tmp/valet.d/f1-0
```

**Error** output:

```log
INFO Opening the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Opening the valet config file ⌜/tmp/valet.d/f1-0⌝.
```

### Testing selfConfig override no edit
Expand All @@ -234,7 +234,7 @@ Exit code: `0`
**Error** output:

```log
INFO Creating the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Creating the valet config file ⌜/tmp/valet.d/f1-0⌝.
```

### Testing selfConfig override export
Expand All @@ -245,7 +245,7 @@ Exit code: `0`

```plaintext
→ (selfConfig --override --export-current-values)
▶ called myEditor: /tmp/valet.d/f401-0
▶ called myEditor: /tmp/valet.d/f1-0
cat ${configFile}
#!/usr/bin/env bash
Expand Down Expand Up @@ -435,7 +435,7 @@ VALET_CONFIG_BUMP_VERSION_ON_BUILD="${VALET_CONFIG_BUMP_VERSION_ON_BUILD:-}"
**Error** output:

```log
INFO Creating the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Opening the valet config file ⌜/tmp/valet.d/f401-0⌝.
INFO Creating the valet config file ⌜/tmp/valet.d/f1-0⌝.
INFO Opening the valet config file ⌜/tmp/valet.d/f1-0⌝.
```

1 change: 0 additions & 1 deletion tests.d/1102-self-build/01.self-build.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#!/usr/bin/env bash

function testSelfBuild() {
setTempFilesNumber 500
io::createTempFile && local tempFile="${RETURNED_VALUE}"

(
Expand Down
2 changes: 1 addition & 1 deletion tests.d/1102-self-build/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ self mock3 A command that only for testing valet core functions.
self release Release a new version of valet.
self setup The command run after the installation of Valet to setup the tool.
INFO The command definition variables have been written to ⌜/tmp/valet.d/f501-0⌝.
INFO The command definition variables have been written to ⌜/tmp/valet.d/f1-0⌝.
SUCCESS The valet user commands have been successfully built
```

1 change: 0 additions & 1 deletion tests.d/1103-self-release/01.self-release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ function kurl::toFile() {
}

function main() {
setTempFilesNumber 100
io::createTempFile && local tmpFile="${RETURNED_VALUE}"
cp -f "${GLOBAL_VALET_HOME}/valet.d/version" "${tmpFile}"
echo -n "1.2.3" > "${GLOBAL_VALET_HOME}/valet.d/version"
Expand Down
1 change: 0 additions & 1 deletion tests.d/1104-self-setup/00.self-setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ function testselfSetup() {
local -i exitCode
local originalConfigFile="${VALET_CONFIG_FILE:-}"
local configFile
setTempFilesNumber 800
io::createTempFile && configFile="${RETURNED_VALUE}"
VALET_CONFIG_FILE="${configFile}"

Expand Down
4 changes: 2 additions & 2 deletions tests.d/1104-self-setup/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ INFO If you see the replacement character ? in my terminal, it means you don
You can download any font here: https://www.nerdfonts.com/font-downloads and install it.
After that, you need to setup your terminal to use this newly installed font.
You can also choose to enable the icons in Valet if you plan to install a nerd font.
INFO Creating the valet config file ⌜/tmp/valet.d/f801-0⌝.
INFO Creating the valet config file ⌜/tmp/valet.d/f1-0⌝.
SUCCESS You are all set!
INFO As a reminder, you can modify the configuration done during this set up by either:
- replaying the command ⌜valet self setup⌝,
Expand Down Expand Up @@ -107,7 +107,7 @@ An information icon: II

```log
INFO Now setting up Valet.
CININFO II CDE Creating the valet config file CHI⌜/tmp/valet.d/f801-0⌝CDE.
CININFO II CDE Creating the valet config file CHI⌜/tmp/valet.d/f1-0⌝CDE.
CWAWARNING IW CDE The tool CHI⌜awk⌝CDE is missing. It is needed for cleaning the profiler logs.
CWAWARNING IW CDE The tool CHI⌜diff⌝CDE is missing. It is needed for the self test command.
CWAWARNING IW CDE The tool CHI⌜curl⌝CDE is missing. It is needed for the self update command.
Expand Down
1 change: 0 additions & 1 deletion tests.d/1300-valet-cli/.before-test
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ fi
_TEST_0000_COMMON_UTILS=1

# override the local state and config directories to return temp directories
setTempFilesNumber 800
io::createTempDirectory
export TEST_VALET_CONFIG_LOCAL_STATE_DIRECTORY="${RETURNED_VALUE}"

Expand Down
1 change: 0 additions & 1 deletion tests.d/1300-valet-cli/06.misc.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function testCleaning() {
function testUserDirectory() {
# testing with a non exising user directory
local previousUserDirectory="${VALET_USER_DIRECTORY:-}"
setTempFilesNumber 600
io::createTempDirectory && export VALET_USER_DIRECTORY="${RETURNED_VALUE}/non-existing"

echo "→ VALET_USER_DIRECTORY=non-existing self mock1 logging-level"
Expand Down
4 changes: 2 additions & 2 deletions tests.d/1300-valet-cli/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -744,9 +744,9 @@ Exit code: `1`
**Error** output:

```log
INFO The valet user directory ⌜/tmp/valet.d/d601-0/non-existing⌝ does not contain a built ⌜commands⌝ file.
INFO The valet user directory ⌜/tmp/valet.d/d2-0/non-existing⌝ does not contain a built ⌜commands⌝ file.
Now building it using ⌜valet self build⌝ command.
WARNING Skipping user directory ⌜/tmp/valet.d/d601-0/non-existing⌝ because it does not exist.
WARNING Skipping user directory ⌜/tmp/valet.d/d2-0/non-existing⌝ because it does not exist.
TRACE This is an error trace message which is always displayed.
INFO This is an info message with a super long sentence. The value of life is not in its duration, but in its donation. You are not important because of how long you live, you are important because of how effective you live. Give a man a fish and you feed him for a day; teach a man to fish and you feed him for a lifetime. Surround yourself with the best people you can find, delegate authority, and don't interfere as long as the policy you've decided upon is being carried out.
SUCCESS This is a success message.
Expand Down
1 change: 0 additions & 1 deletion tests.d/1301-profiler/00.profiler.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ commentTest "The profiler is an excellent tool to debug your command. The follow

function testProfiler() {
# testing command profiling + startup
setTempFilesNumber 200
io::createTempFile && export VALET_CONFIG_COMMAND_PROFILING_FILE="${RETURNED_VALUE}"
io::createTempFile && export VALET_CONFIG_STARTUP_PROFILING_FILE="${RETURNED_VALUE}"
export VALET_CONFIG_COMMAND_PROFILING_FILE
Expand Down
4 changes: 2 additions & 2 deletions tests.d/1301-profiler/results.approved.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ D I S timer delta source:line function
**Error** output:

```log
INFO Starting profiler, writing in ⌜/tmp/valet.d/f201-0⌝. main
INFO Starting profiler, writing in ⌜/tmp/valet.d/f1-0⌝. main
INFO First argument: arg1.
INFO Option 1: .
INFO Option 2: .
Expand All @@ -72,7 +72,7 @@ A command profiling file has been created to log everything happening in the cho
**Error** output:

```log
INFO Starting profiler, writing in ⌜/tmp/valet.d/f202-0⌝.
INFO Starting profiler, writing in ⌜/tmp/valet.d/f2-0⌝.
TRACE This is an error trace message which is always displayed.
```

23 changes: 11 additions & 12 deletions valet.d/commands.d/self-test-utils
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ function endTest() {
# Usage:
# setTempFilesNumber 3
function setTempFilesNumber() {
TEMPORARY_FILE_NUMBER=${1}
TEMPORARY_DIRECTORY_NUMBER=${1}
return 0
}

function echoFileWithLineNumberSubstitution() {
Expand Down Expand Up @@ -121,7 +120,7 @@ function runCoreTests() {

# we should always run the test suite from the valet home directory to have consistent paths
# in the report files
pushd "${GLOBAL_VALET_HOME}" 1> /dev/null
pushd "${GLOBAL_VALET_HOME}" 1>/dev/null

log::info "Running all test suites in directory ⌜${GLOBAL_VALET_HOME}/tests.d⌝."
runTestSuites "${GLOBAL_VALET_HOME}/tests.d"
Expand All @@ -137,7 +136,7 @@ function runCoreTests() {
runTestSuites "${GLOBAL_VALET_HOME}/examples.d/showcase/tests.d"
fi

popd 1> /dev/null
popd 1>/dev/null

# reload the orignal commands
core::reloadUserCommands
Expand All @@ -148,7 +147,7 @@ function rebuildCommands() {
log::getLevel && originalLogLevel="${RETURNED_VALUE}"
log::setLevel warning true
core::sourceFunction selfBuild
if (( $# == 1)); then
if (($# == 1)); then
selfBuild --user-directory "${1:-}"
else
selfBuild --user-directory "${1:-}" --output "${2}"
Expand All @@ -171,13 +170,10 @@ function rebuildCommands() {
function runTestSuites() {
local testSuiteDirectory="${1}"

# reset the temp files
io::cleanupTempFiles

io::createTempFile && _TEST_STANDARD_OUTPUT_FILE="${RETURNED_VALUE}"
io::createTempFile && _TEST_STANDARD_ERROR_FILE="${RETURNED_VALUE}"
io::createTempFile && _TEST_REPORT_FILE="${RETURNED_VALUE}"
io::createTempFile && _TEST_TEMP_FILE="${RETURNED_VALUE}"
_TEST_STANDARD_OUTPUT_FILE="${GLOBAL_TEMPORARY_IN_MEM_PREFIX}${BASHPID}.valet-test-stdout"
_TEST_STANDARD_ERROR_FILE="${GLOBAL_TEMPORARY_IN_MEM_PREFIX}${BASHPID}.valet-test-stderr"
_TEST_REPORT_FILE="${GLOBAL_TEMPORARY_IN_MEM_PREFIX}${BASHPID}.valet-test-report"
_TEST_TEMP_FILE="${GLOBAL_TEMPORARY_IN_MEM_PREFIX}${BASHPID}.valet-test-tempfile"

# save the original important variables so we can restore them after the test suite
log::getLevel
Expand Down Expand Up @@ -213,6 +209,9 @@ function runTestSuites() {
continue
fi

# reset the temp files
io::cleanupTempFiles

log::info "Running test suite ⌜${testDirectory##*/}⌝."

# run a custom user script before the test suite if it exists
Expand Down
6 changes: 4 additions & 2 deletions valet.d/core
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ function log::printFileString() { return 0; }

# Display a string in the log.
# The string will be aligned with the current log output and hard wrapped if necessary.
# Does not check the log level.
#
# $1: the content to log (can contain new lines)
# $2: the string with which to prepend each wrapped line (empty by default)
Expand All @@ -234,6 +235,7 @@ function log::printFileString() { return 0; }
function log::printString() { return 0; }

# Display something in the log stream.
# Does not check the log level.
#
# $1: the content to print (can contain new lines)
#
Expand Down Expand Up @@ -333,13 +335,13 @@ function log::createPrintFunction() {
printStatement='
printf "'"${printfFormat}"'" '"${printfArguments}"' 1>&'"${VALET_CONFIG_LOG_FD:-2}"
standardPrintStatement='
printf "%s" "${toPrint}" 1>&'"${VALET_CONFIG_LOG_FD:-2}"
printf "%s" "${toPrint:-}" 1>&'"${VALET_CONFIG_LOG_FD:-2}"
else
# it is a file
printStatement='
printf "'"${printfFormat}"'" '"${printfArguments}"' 1>>'"${VALET_CONFIG_LOG_FD}"
standardPrintStatement='
printf "%s" "${toPrint}" 1>>'"${VALET_CONFIG_LOG_FD}"
printf "%s" "${toPrint:-}" 1>>'"${VALET_CONFIG_LOG_FD}"
fi

# string with the number of spaces necessary to align with other log messages
Expand Down
2 changes: 1 addition & 1 deletion valet.d/version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.11.167
0.11.198

0 comments on commit 5b7b3bc

Please sign in to comment.