public inbox for ~johnnyrichard/olang-devel@lists.sr.ht
 help / color / mirror / code / Atom feed
From: Carlos Maniero <carlos@maniero.me>
To: ~johnnyrichard/olang-devel@lists.sr.ht
Cc: Carlos Maniero <carlos@maniero.me>
Subject: [PATCH olang v1] tests: show test name into test execution output
Date: Fri, 11 Oct 2024 12:26:03 +0000 (UTC)	[thread overview]
Message-ID: <20241011122554.549125-1-carlos@maniero.me> (raw)

The test output was printing a bunch of dots *.* during the test
execution. Now the tests are printing in the following format:

./0002_binary_operator_addition.ol                           [ OK ] [  222ms ]
./0005_binary_operator_reminder.ol                           [ OK ] [  264ms ]
./0004_binary_operator_division.ol                           [ OK ] [  259ms ]
./0003_binary_operator_multiplication.ol                     [ OK ] [  252ms ]
./0006_binary_operator_subtraction.ol                        [ OK ] [  260ms ]
./0008_binary_operator_lt.ol                                 [ OK ] [  231ms ]

The script changed from multiple printf calls into a single call to make
sure the scripts can be executed in parallel without breaking the
output.

Signed-off-by: Carlos Maniero <carlos@maniero.me>
---
 tests/olc/run.sh | 59 +++++++++++++++++++++++++-----------------------
 1 file changed, 31 insertions(+), 28 deletions(-)

diff --git a/tests/olc/run.sh b/tests/olc/run.sh
index 503f960..033ab5b 100755
--- a/tests/olc/run.sh
+++ b/tests/olc/run.sh
@@ -28,7 +28,6 @@ TEST_INVOCATION=""
 TEST_LINE_NUMBER=""
 TEST_CONTENTS_PATH=""
 TEST_ARGS=""
-TEST_SOME_PASSED=""
 
 # UI
 COLOR_RED=1
@@ -37,17 +36,21 @@ COLOR_YELLOW=3
 COLOR_CYAN=6
 COLOR_GRAY=7
 
+if [ -t 1 ]; then
+  if tput setaf 1 > /dev/null 2>&1; then
+    HAS_COLORS=1
+  fi
+fi
+
 colored() {
   text="$1"
 
-  if [ -t 1 ]; then
-    if tput setaf 1 > /dev/null 2>&1; then
-      color=$(tput setaf "$2")
-      reset=$(tput sgr0)
+  if [ -n "$HAS_COLORS" ]; then
+    color=$(tput setaf "$2")
+    reset=$(tput sgr0)
 
-      printf "%s%s%s" "$color" "$text" "$reset"
-      return
-    fi
+    printf "%s%s%s" "$color" "$text" "$reset"
+    return
   fi
 
   printf "%s" "$text"
@@ -58,22 +61,12 @@ colored() {
 print_failed() {
   reason="$1"
 
-  if [ -n "$TEST_SOME_PASSED" ]; then
-    echo
-  fi
-
-  colored "$TEST_FILE:$TEST_LINE_NUMBER:1: " $COLOR_GRAY
-  colored "[$TEST_INVOCATION] " $COLOR_CYAN
-  colored "failed" $COLOR_RED
-  if [ -n "$reason" ]; then
-    printf ": %s" "$reason"
-  fi
-  echo
-}
-
-print_passed() {
-  colored "." $COLOR_GREEN
-  TEST_SOME_PASSED="true"
+  printf "%s:%s:1: %s %s: %s\n" \
+    "$TEST_FILE" \
+    "$TEST_LINE_NUMBER" \
+    "$(colored "failed" "$COLOR_RED")" \
+    "$(colored "$TEST_INVOCATION" "$COLOR_CYAN")" \
+    "$reason"
 }
 # end test output
 
@@ -88,9 +81,9 @@ expect_output_contains() {
 
     if [ "$(grep "$(printf "%s" "$expected_line" | sed 's/\\/\\\\/g')" "$actual_file" | wc -c)" = "0" ]; then
       print_failed
-      colored "(not found) $expected_line" $COLOR_YELLOW
+      colored "(not found) $expected_line" "$COLOR_YELLOW"
       echo
-      colored "$(awk '{print "(actual) " $0}' "$actual_file")" $COLOR_GRAY
+      colored "$(awk '{print "(actual) " $0}' "$actual_file")" "$COLOR_GRAY"
       echo
       exit 1
     fi
@@ -119,6 +112,8 @@ cleanup() {
 main() {
   all_test_end="$(grep -n "# END" "$TEST_FILE" | awk -F: '{print $1}')"
 
+  ts=$(date +%s%N)
+
   grep -n "# TEST " "$TEST_FILE" | while read -r line ; do
     TEST_LINE_NUMBER="$(echo "$line" | awk -F: '{ print $1 }')"
     TEST_INVOCATION="$(echo "$line" | awk -F: '{ print $2 }' | sed 's/^\# TEST //' | sed 's/ WITH$//')"
@@ -147,14 +142,22 @@ main() {
 
     if type "$TEST_NAME" | grep -q 'function'; then
         "$TEST_NAME"
-        print_passed
     else
         print_failed "test not implemented"
     fi
   done || exit 1;
 
+  elapsed=$((($(date +%s%N) - ts)/1000000))
+
+  padsize=$((59 - ${#TEST_FILE}))
+
+  printf "%s %*s [ %s ] [ % 4dms ]\n" \
+    "$TEST_FILE" \
+    $(( padsize > 0 ? padsize : 0 )) "" \
+    "$(colored "OK" "$COLOR_GREEN")"\
+    $elapsed
+
   cleanup
-  echo
 }
 
 get_test_args() {

base-commit: 3a988ec943cd6b97d9fd6ff255dcdc387f80516c
-- 
2.46.1


             reply	other threads:[~2024-10-11 12:26 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-10-11 12:26 Carlos Maniero [this message]
2024-10-11 12:26 ` [olang/patches/.build.yml] build success builds.sr.ht
2024-10-11 17:06 ` [PATCH olang v1] tests: show test name into test execution output Johnny Richard

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20241011122554.549125-1-carlos@maniero.me \
    --to=carlos@maniero.me \
    --cc=~johnnyrichard/olang-devel@lists.sr.ht \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.johnnyrichard.com/olang.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox