Home > Software engineering >  How to test formatting in Bash 5 with Bats Core?
How to test formatting in Bash 5 with Bats Core?

Time:05-24

Trying to get into Shell testing with Bash (5.1.16), using 2

I don't understand why this does not work, while the following in other tests work.

  [[ "${output}" == "[30mHello World.(B[m How are you?" ]] # also with ESC in IntelliJ

Edit: I assume the underlying code to work, because the output is correct. However, here the underlying code for message() from sourced file 1:


# arguments and stuff here
if is_false "${no_icons:-}" && is_present "${icon:-}"; then
  output_string="      [${icon}] "
  unset spaces
fi

output_string="$output_string${spaces:-}${title:-}\n"

text_red "${output_string:-}"

if is_blank "${message:-}"; then
  return
fi

echo "$message" | while read -r line; do

  # Indirect color function call     # Trim whitespaces from line beginning
  text_red "      $(echo -e "${line}" | sed -e 's/^[[:space:]]*//')\n"

done

Here the coloring functions from sourced file 2:

  function _print_foreground_color() {
    printf "%b" "$(tput setaf "${2:-}" 2>/dev/null)" "${1:-}" "$(tput sgr0 2>/dev/null)"
  }

  function text_red() {
    _print_foreground_color "${1:-}" 1
  }

My questions:

  1. What am I doing wrong?
  2. What is the best way to, with Bats Core, test formatting?

Any help would be much appreciated!

CodePudding user response:

You can use printf "%q" to get the value you want to test :

#!/usr/bin/env bash
   
function _print_foreground_color() {
    printf "%b" "$(tput setaf "${2:-}" 2>/dev/null)" "${1:-}" "$(tput sgr0 2>/dev/null)"
} 

function text_red() {
    _print_foreground_color "${1:-}" 1
} 

text_red alert; echo
# Output :
# alert in red

printf "%q\n" "$(text_red alert)"
# Output :
# $'\E[31malert\E(B\E[m'

if test "$(text_red alert)" = $'\E[31malert\E(B\E[m'; then
    echo "Compared successfully"
fi
# Output :
# Compared successfully
  • Related