From 3662badfe00a4f315fb4c8daec3ffb7d8ffd7299 Mon Sep 17 00:00:00 2001
From: Volker Schukai <volker.schukai@schukai.com>
Date: Tue, 2 Apr 2024 13:32:33 +0200
Subject: [PATCH] chore: test runners are now empowered to run selected files
 #166

---
 devenv.nix | 146 ++++++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 145 insertions(+), 1 deletion(-)

diff --git a/devenv.nix b/devenv.nix
index 38c93d890..7fd292a5e 100644
--- a/devenv.nix
+++ b/devenv.nix
@@ -11,6 +11,104 @@
     if pkgs.stdenv.system != null
     then pkgs.stdenv.system
     else builtins.currentSystem;
+    
+
+  commonFunctionsScript = pkgs.writeScript "common-functions" ''
+
+    if [ -t 1 ]; then
+        RED='\033[0;31m'
+        GREEN='\033[0;32m'
+        BLUE='\033[0;34m'
+        RESET='\033[0m'
+        BOLD='\033[1m'
+    else
+        RED=""
+        GREEN=""
+        BLUE=""
+        RESET=""
+    fi
+
+    is_true() {
+        val="$1"
+        if [ "$val" = 'true' ] || [ "$val" = '1' ]; then
+            return 0
+        else
+            return 1
+        fi
+    }
+
+    CURRENT_LEVEL=0
+
+    get_padding() {
+        local padding=""
+        for ((i=0; i < CURRENT_LEVEL; i++)); do
+            padding+="  "
+        done
+        echo "$padding"
+    }
+
+    increase_level() {
+        CURRENT_LEVEL=$((CURRENT_LEVEL + 1))
+    }
+
+    decrease_level() {
+        CURRENT_LEVEL=$((CURRENT_LEVEL - 1))
+    }
+
+    echo_ok() {
+        padding=$(get_padding)
+        echo -e "$padding$GREEN✔$RESET $1"
+    }
+
+    echo_delimiter() {
+        echo " "
+    }
+
+    echo_section() {
+        decrease_level
+        padding=$(get_padding)
+        echo -e "\n\n$padding$BOLD$1$RESET"
+        increase_level
+    }
+
+    echo_text() {
+        padding=$(get_padding)
+        echo -e "$padding$1"
+    }
+
+    echo_fail() {
+        padding=$(get_padding)
+        echo -e "$padding$RED✖ $1$RESET"
+    }
+
+    echo_hint() {
+        padding=$(get_padding)
+        echo -e "$padding$BLUE→ $1$RESET"
+    }
+
+    echo_step() {
+        padding=$(get_padding)
+        echo -e "$padding$BLUE•$RESET $1"
+    }
+
+    convert_multiline_for_nix() {
+       printf "%s" "$1" | ${pkgs.gnused}/bin/sed 's/\\/\\\\/g; s/"/\\"/g; s/$/\\n/g' | tr -d '\n'
+    }
+
+    if [ -z "$DEBUG_MODE" ]; then
+        DEBUG_MODE='false'
+    fi
+
+    if [ -n "$CI_JOB_TOKEN" ]; then
+        CI_MODE='true'
+    fi
+
+    if is_true "$DEBUG_MODE" || is_true "$CI_MODE"; then
+        set -x
+    fi
+
+  '';
+    
 in {
   env.APP_NAME = "monster";
   env.dddd = "@schukai/monster";
@@ -91,6 +189,7 @@ in {
   scripts.build-changelog.exec = ''
     ${pkgs.git-chglog}/bin/git-chglog --config ${config.devenv.root}/.chglog/config.yml -o ${config.devenv.root}/CHANGELOG.md
   '';
+  
 
   scripts.build-and-publish.exec = ''
     
@@ -256,13 +355,57 @@ in {
 
 
   '';
+  
   scripts.run-tests.exec = ''
+    #!${pkgs.bash}/bin/bash
+    
+    TEST_PATH="${config.devenv.root}/test"
+    TEST_CASES_PATH="''${TEST_PATH}/cases/"
+    
+    source ${commonFunctionsScript}
     update-versions
-    if ! ${pkgs.nodePackages.pnpm}/bin/pnpm mocha --colors --jobs 1 --bail --recursive test/cases/
+
+    echo_section "run tests"
+
+    selection=$(gum choose "run specific test" "run all tests" "Cancel")
+    
+    if [[ "$selection" == "Cancel" ]]; then
+      echo_text "Exiting."
+      exit 0
+    fi
+    
+    if [[ "$selection" == "run all tests" ]]; then
+       echo_step "run specific tests"    
+       if ! ${pkgs.nodePackages.pnpm}/bin/pnpm mocha --colors --jobs 1 --bail --recursive $TEST_CASES_PATH
+       then
+          echo "ERROR: Tests failed, check your JS!"
+          exit 1
+       fi
+       exit 0
+    fi
+    
+    escaped_test_cases_path=$(printf '%s\n' "$TEST_CASES_PATH" | sed 's:[\\/&]:\\&:g;$!s/$/\\/')
+
+    echo_step "run all tests"    
+    files=$(find $TEST_CASES_PATH -type f -name "*.mjs")
+    relative_files=$(echo "$files" | sed "s|''${escaped_test_cases_path}||g")
+    
+    selected=$(echo "$relative_files" | gum filter --no-limit --header="Select the test case to run" --placeholder="Select test case to run, use tab to select multiple files")
+    
+    if [[ -z "$selected" ]]; then
+      echo_text "No files selected. Exiting."
+      exit 0
+    fi
+
+    full_paths=$(echo "$selected" | sed "s|^|''${escaped_test_cases_path}|")
+    if ! ${pkgs.nodePackages.pnpm}/bin/pnpm mocha --colors --jobs 1 --bail --recursive $full_paths
     then
       echo "ERROR: Tests failed, check your JS!"
       exit 1
     fi
+    
+      
+    
   '';
 
   scripts.build-monster-mjs.exec = ''
@@ -312,6 +455,7 @@ in {
     ${pkgs.xdg-utils}/bin/xdg-open ''${TEST_PATH}web/test.html
 
   '';
+  
 
   scripts.publish-doc.exec = ''
     #!${pkgs.bash}/bin/bash
-- 
GitLab