github.com/crowdsecurity/crowdsec@v1.6.1/test/lib/color-formatter (about)

     1  #!/usr/bin/env bash
     2  
     3  #
     4  # Taken from pretty formatter, minus the cursor movements.
     5  # Used in gihtub workflows CI where color is allowed.
     6  #
     7  
     8  set -e
     9  
    10  # shellcheck source=lib/bats-core/formatter.bash
    11  source "$BATS_ROOT/lib/bats-core/formatter.bash"
    12  
    13  BASE_PATH=.
    14  BATS_ENABLE_TIMING=
    15  
    16  while [[ "$#" -ne 0 ]]; do
    17    case "$1" in
    18    -T)
    19      BATS_ENABLE_TIMING="-T"
    20      ;;
    21    --base-path)
    22      shift
    23      normalize_base_path BASE_PATH "$1"
    24      ;;
    25    esac
    26    shift
    27  done
    28  
    29  update_count_column_width() {
    30    count_column_width=$((${#count} * 2 + 2))
    31    if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    32      # additional space for ' in %s sec'
    33      count_column_width=$((count_column_width + ${#SECONDS} + 8))
    34    fi
    35    # also update dependent value
    36    update_count_column_left
    37  }
    38  
    39  update_screen_width() {
    40    screen_width="$(tput cols)"
    41    # also update dependent value
    42    update_count_column_left
    43  }
    44  
    45  update_count_column_left() {
    46    count_column_left=$((screen_width - count_column_width))
    47  }
    48  
    49  # avoid unset variables
    50  count=0
    51  screen_width=80
    52  update_count_column_width
    53  #update_screen_width
    54  test_result=
    55  
    56  #trap update_screen_width WINCH
    57  
    58  begin() {
    59    test_result= # reset to avoid carrying over result state from previous test
    60    line_backoff_count=0
    61    #go_to_column 0
    62    #update_count_column_width
    63    #buffer_with_truncation $((count_column_left - 1)) '   %s' "$name"
    64    #clear_to_end_of_line
    65    #go_to_column $count_column_left
    66    #if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    67    #  buffer "%${#count}s/${count} in %s sec" "$index" "$SECONDS"
    68    #else
    69    #  buffer "%${#count}s/${count}" "$index"
    70    #fi
    71    #go_to_column 1
    72    buffer "%${#count}s" "$index"
    73  }
    74  
    75  finish_test() {
    76    #move_up $line_backoff_count
    77    #go_to_column 0
    78    buffer "$@"
    79    if [[ -n "${TIMEOUT-}" ]]; then
    80      set_color 2
    81      if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    82        buffer ' [%s (timeout: %s)]' "$TIMING" "$TIMEOUT"
    83      else
    84        buffer ' [timeout: %s]' "$TIMEOUT"
    85      fi
    86    else
    87      if [[ -n "$BATS_ENABLE_TIMING" ]]; then
    88        set_color 2
    89        buffer ' [%s]' "$TIMING"
    90      fi
    91    fi
    92    advance
    93    move_down $((line_backoff_count - 1))
    94  }
    95  
    96  pass() {
    97    local TIMING="${1:-}"
    98    finish_test ' ✓ %s' "$name"
    99    test_result=pass
   100  }
   101  
   102  skip() {
   103    local reason="$1" TIMING="${2:-}"
   104    if [[ -n "$reason" ]]; then
   105      reason=": $reason"
   106    fi
   107    finish_test ' - %s (skipped%s)' "$name" "$reason"
   108    test_result=skip
   109  }
   110  
   111  fail() {
   112    local TIMING="${1:-}"
   113    set_color 1 bold
   114    finish_test ' ✗ %s' "$name"
   115    test_result=fail
   116  }
   117  
   118  timeout() {
   119    local TIMING="${1:-}"
   120    set_color 3 bold
   121    TIMEOUT="${2:-}" finish_test ' ✗ %s' "$name"
   122    test_result=timeout
   123  }
   124  
   125  log() {
   126    case ${test_result} in
   127    pass)
   128      clear_color
   129      ;;
   130    fail)
   131      set_color 1
   132      ;;
   133    timeout)
   134      set_color 3
   135      ;;
   136    esac
   137    buffer '   %s\n' "$1"
   138    clear_color
   139  }
   140  
   141  summary() {
   142    if [ "$failures" -eq 0 ]; then
   143      set_color 2 bold
   144    else
   145      set_color 1 bold
   146    fi
   147  
   148    buffer '\n%d test' "$count"
   149    if [[ "$count" -ne 1 ]]; then
   150      buffer 's'
   151    fi
   152  
   153    buffer ', %d failure' "$failures"
   154    if [[ "$failures" -ne 1 ]]; then
   155      buffer 's'
   156    fi
   157  
   158    if [[ "$skipped" -gt 0 ]]; then
   159      buffer ', %d skipped' "$skipped"
   160    fi
   161  
   162    if ((timed_out > 0)); then
   163      buffer ', %d timed out' "$timed_out"
   164    fi
   165  
   166    not_run=$((count - passed - failures - skipped - timed_out))
   167    if [[ "$not_run" -gt 0 ]]; then
   168      buffer ', %d not run' "$not_run"
   169    fi
   170  
   171    if [[ -n "$BATS_ENABLE_TIMING" ]]; then
   172      buffer " in $SECONDS seconds"
   173    fi
   174  
   175    buffer '\n'
   176    clear_color
   177  }
   178  
   179  buffer_with_truncation() {
   180    local width="$1"
   181    shift
   182    local string
   183  
   184    # shellcheck disable=SC2059
   185    printf -v 'string' -- "$@"
   186  
   187    if [[ "${#string}" -gt "$width" ]]; then
   188      buffer '%s...' "${string:0:$((width - 4))}"
   189    else
   190      buffer '%s' "$string"
   191    fi
   192  }
   193  
   194  move_up() {
   195    if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
   196      buffer '\x1B[%dA' "$1"
   197    fi
   198  }
   199  
   200  move_down() {
   201    if [[ $1 -gt 0 ]]; then # avoid moving if we got 0
   202      buffer '\x1B[%dB' "$1"
   203    fi
   204  }
   205  
   206  go_to_column() {
   207    local column="$1"
   208    buffer '\x1B[%dG' $((column + 1))
   209  }
   210  
   211  clear_to_end_of_line() {
   212    buffer '\x1B[K'
   213  }
   214  
   215  advance() {
   216    clear_to_end_of_line
   217    buffer '\n'
   218    clear_color
   219  }
   220  
   221  set_color() {
   222    local color="$1"
   223    local weight=22
   224  
   225    if [[ "${2:-}" == 'bold' ]]; then
   226      weight=1
   227    fi
   228    buffer '\x1B[%d;%dm' "$((30 + color))" "$weight"
   229  }
   230  
   231  clear_color() {
   232    buffer '\x1B[0m'
   233  }
   234  
   235  _buffer=
   236  
   237  buffer() {
   238    local content
   239    # shellcheck disable=SC2059
   240    printf -v content -- "$@"
   241    _buffer+="$content"
   242  }
   243  
   244  prefix_buffer_with() {
   245    local old_buffer="$_buffer"
   246    _buffer=''
   247    "$@"
   248    _buffer="$_buffer$old_buffer"
   249  }
   250  
   251  flush() {
   252    printf '%s' "$_buffer"
   253    _buffer=
   254  }
   255  
   256  finish() {
   257    flush
   258    printf '\n'
   259  }
   260  
   261  trap finish EXIT
   262  trap '' INT
   263  
   264  bats_tap_stream_plan() {
   265    count="$1"
   266    index=0
   267    passed=0
   268    failures=0
   269    skipped=0
   270    timed_out=0
   271    name=
   272    update_count_column_width
   273  }
   274  
   275  bats_tap_stream_begin() {
   276    index="$1"
   277    name="$2"
   278    begin
   279    flush
   280  }
   281  
   282  bats_tap_stream_ok() {
   283    index="$1"
   284    name="$2"
   285    ((++passed))
   286  
   287    pass "${BATS_FORMATTER_TEST_DURATION:-}"
   288  }
   289  
   290  bats_tap_stream_skipped() {
   291    index="$1"
   292    name="$2"
   293    ((++skipped))
   294    skip "$3" "${BATS_FORMATTER_TEST_DURATION:-}"
   295  }
   296  
   297  bats_tap_stream_not_ok() {
   298    index="$1"
   299    name="$2"
   300  
   301    if [[ ${BATS_FORMATTER_TEST_TIMEOUT-x} != x ]]; then
   302      timeout "${BATS_FORMATTER_TEST_DURATION:-}" "${BATS_FORMATTER_TEST_TIMEOUT}s"
   303      ((++timed_out))
   304    else
   305      fail "${BATS_FORMATTER_TEST_DURATION:-}"
   306      ((++failures))
   307    fi
   308  
   309  }
   310  
   311  bats_tap_stream_comment() { # <comment> <scope>
   312    local scope=$2
   313    # count the lines we printed after the begin text,
   314    if [[ $line_backoff_count -eq 0 && $scope == begin ]]; then
   315      # if this is the first line after begin, go down one line
   316      buffer "\n"
   317      ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
   318    fi
   319  
   320    ((++line_backoff_count))
   321    ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
   322    log "$1"
   323  }
   324  
   325  bats_tap_stream_suite() {
   326    #test_file="$1"
   327    line_backoff_count=0
   328    index=
   329    # indicate filename for failures
   330    local file_name="${1#"$BASE_PATH"}"
   331    name="File $file_name"
   332    set_color 4 bold
   333    buffer "%s\n" "$file_name"
   334    clear_color
   335  }
   336  
   337  line_backoff_count=0
   338  bats_tap_stream_unknown() { # <full line> <scope>
   339    local scope=$2
   340    # count the lines we printed after the begin text, (or after suite, in case of syntax errors)
   341    if [[ $line_backoff_count -eq 0 && ($scope == begin || $scope == suite) ]]; then
   342      # if this is the first line after begin, go down one line
   343      buffer "\n"
   344      ((++line_backoff_count)) # prefix-increment to avoid "error" due to returning 0
   345    fi
   346  
   347    ((++line_backoff_count))
   348    ((line_backoff_count += ${#1} / screen_width)) # account for linebreaks due to length
   349    buffer "%s\n" "$1"
   350    flush
   351  }
   352  
   353  bats_parse_internal_extended_tap
   354  
   355  summary