From 6efd1e14822d2d5340abe81997a0227eb3104c85 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 12 Oct 2024 14:51:02 +0200 Subject: [PATCH 1/2] refactor: build.sh --- build.sh | 124 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 80 insertions(+), 44 deletions(-) diff --git a/build.sh b/build.sh index 9a8533a5..256143cb 100755 --- a/build.sh +++ b/build.sh @@ -2,53 +2,45 @@ source src/check_os.sh +BASHUNIT_ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +export BASHUNIT_ROOT_DIR + function build() { local out=$1 - generate_bin "$out" - generate_checksum "$out" + build::generate_bin "$out" + build::generate_checksum "$out" - echo "⚡️Build completed⚡️" + echo "⚡️ Build completed ⚡️" } -function verify_build() { +function build::verify() { local out=$1 echo "Verifying build ⏱️" "$out" tests \ --simple \ + --parallel \ --log-junit "bin/log-junit.xml" \ --report-html "bin/report.html" \ - --stop-on-failure & - - pid=$! - - function cleanup() { - kill "$pid" 2>/dev/null - tput cnorm # Show the cursor - exit 1 - } + --stop-on-failure - trap cleanup SIGINT - spinner $pid - wait $pid - echo "✅ Build verified ✅ " + if [[ $? -eq 0 ]]; then + echo "✅ Build verified ✅" + fi } -function generate_bin() { +function build::generate_bin() { local out=$1 local temp temp="$(dirname "$out")/temp.sh" echo '#!/bin/bash' > "$temp" echo "Generating bashunit in the '$(dirname "$out")' folder..." - for file in src/*.sh; do - { - echo "# $file" - tail -n +2 "$file" >> "$temp" - echo "" - } >> "$temp" + + for file in $(build::dependencies); do + build::process_file "$file" "$temp" done cat bashunit >> "$temp" @@ -57,16 +49,76 @@ function generate_bin() { chmod u+x "$out" } -function generate_checksum() { +# Recursive function to process each file and any files it sources +function build::process_file() { + local file=$1 + local temp=$2 + + { + echo "# $(basename "$file")" + tail -n +2 "$file" >> "$temp" + echo "" + } >> "$temp" + + # Search for any 'source' lines in the current file + grep '^source ' "$file" | while read -r line; do + # Extract the path from the 'source' command + local sourced_file + sourced_file=$(echo "$line" | awk '{print $2}' | sed 's/^"//;s/"$//') # Remove any quotes + + # Handle cases where the path uses $BASHUNIT_ROOT_DIR or other variables + sourced_file=$(eval echo "$sourced_file") + + # Handle relative paths if necessary + if [[ ! "$sourced_file" =~ ^/ ]]; then + sourced_file="$(dirname "$file")/$sourced_file" + fi + + # Recursively process the sourced file if it exists + if [[ -f "$sourced_file" ]]; then + build::process_file "$sourced_file" "$temp" + fi + done +} + +function build::dependencies() { + deps=( + "src/check_os.sh" + "src/str.sh" + "src/globals.sh" + "src/dependencies.sh" + "src/io.sh" + "src/math.sh" + "src/parallel.sh" + "src/env.sh" + "src/clock.sh" + "src/state.sh" + "src/colors.sh" + "src/console_header.sh" + "src/console_results.sh" + "src/helpers.sh" + "src/upgrade.sh" + "src/assertions.sh" + "src/reports.sh" + "src/runner.sh" + "src/bashunit.sh" + "src/main.sh" + ) + + echo "${deps[@]}" +} + +function build::generate_checksum() { local out=$1 if [[ "$_OS" == "Windows" ]]; then return fi - if [[ "$_OS" == "OSX" ]]; then + # Use a single command for both macOS and Linux + if command -v shasum &>/dev/null; then checksum=$(shasum -a 256 "$out") - elif [[ "$_OS" == "Linux" ]]; then + else checksum=$(sha256sum "$out") fi @@ -74,22 +126,6 @@ function generate_checksum() { echo "$checksum" } -function spinner() { - local pid=$1 - local delay=0.1 - local spinstr="|/-\\" - tput civis # Hide the cursor - printf "\r[%c] " " " - while kill -0 "$pid" 2>/dev/null; do - local temp=${spinstr#?} - printf "\r [%c] " "$spinstr" - local spinstr=$temp${spinstr%"$temp"} - sleep $delay - done - printf "\r \r" # Clear spinner - tput cnorm # Show the cursor -} - ######################## ######### MAIN ######### ######################## @@ -114,5 +150,5 @@ OUT="$DIR/bashunit" build "$OUT" if [[ $SHOULD_VERIFY_BUILD == true ]]; then - verify_build "$OUT" + build::verify "$OUT" fi From 7d7f3f3d1444ec18372bc32570afe08d6a4dcd49 Mon Sep 17 00:00:00 2001 From: Chemaclass Date: Sat, 12 Oct 2024 15:35:27 +0200 Subject: [PATCH 2/2] refactor: remove unnecesary debug_test --- build.sh | 1 + tests/unit/debug_test.sh | 7 ------- 2 files changed, 1 insertion(+), 7 deletions(-) delete mode 100644 tests/unit/debug_test.sh diff --git a/build.sh b/build.sh index 256143cb..54f93943 100755 --- a/build.sh +++ b/build.sh @@ -26,6 +26,7 @@ function build::verify() { --report-html "bin/report.html" \ --stop-on-failure + # shellcheck disable=SC2181 if [[ $? -eq 0 ]]; then echo "✅ Build verified ✅" fi diff --git a/tests/unit/debug_test.sh b/tests/unit/debug_test.sh deleted file mode 100644 index e9c388c2..00000000 --- a/tests/unit/debug_test.sh +++ /dev/null @@ -1,7 +0,0 @@ -#!/bin/bash - -function test_dump() { - assert_equals \ - "[DUMP] tests/unit/debug_test.sh:6: hi, debugging"\ - "$(dump "hi, debugging")" -}