<commit_before>#! /bin/bash
#
# Helper functions for `lib/log` tests.

run_log_script() {
  create_test_go_script \
    ". \"\$_GO_USE_MODULES\" 'log'" \
    'if [[ -n "$TEST_LOG_FILE" ]]; then' \
    '  @go.log_add_output_file "$TEST_LOG_FILE"' \
    'fi' \
    "$@"
  run "$TEST_GO_SCRIPT"
}

# For tests that run command scripts via @go, set _GO_CMD to make sure that's
# the variable included in the log.
test-go() {
  env _GO_CMD="$FUNCNAME" "$TEST_GO_SCRIPT" "$@"
}

# Note that this must be called before any other log assertion, because it needs
# to set _GO_LOG_FORMATTING before importing the `log` module.
format_label() {
  local label="$1"

  if [[ -n "$__GO_LOG_INIT" ]]; then
    echo "$FUNCNAME must be called before any other function or assertion" \
      "that calls \`. \$_GO_USE_MODULES 'log'\` because it needs to set" \
      "\`_GO_LOG_FORMATTING\`." >&2
      return 1
  fi

  _GO_LOG_FORMATTING='true'
  . "$_GO_USE_MODULES" 'log'
  _@go.log_init

  local __go_log_level_index=0
  if ! _@go.log_level_index "$label"; then
    echo "Unknown log level label: $label" >&2
    return 1
  fi
  echo "${__GO_LOG_LEVELS_FORMATTED[$__go_log_level_index]}"
}

__expected_log_line() {
  local level="$1"
  local message="$2"

  if [[ "${level:0:3}" == '\e[' ]]; then
    stripped_level="${level//\\e\[[0-9]m}"
    stripped_level="${stripped_level//\\e\[[0-9][0-9]m}"
    stripped_level="${stripped_level//\\e\[[0-9][0-9][0-9]m}"
    level="${level}${padding:0:$((${#padding} - ${#stripped_level}))}"
    printf '%b\n' "$level $message\e[0m"
  else
    level="${level}${padding:0:$((${#padding} - ${#level}))}"
    printf '%b\n' "$level $message"
  fi
}

assert_log_equals() {
  set +o functrace
  local level
  local padding=''
  local expected=()
  local __go_log_level_index
  local i
  local result=0

  . "$_GO_USE_MODULES" 'log'

  for level in "${_GO_LOG_LEVELS[@]}"; do
    while [[ "${#padding}" -lt "${#level}" ]]; do
      padding+=' '
    done
  done

  for ((i=0; $# != 0; ++i)); do
    if _@go.log_level_index "$1" || [[ "$1" =~ ^\\e\[ ]]; then
      expected+=("$(__expected_log_line "$1" "$2")")
      if ! shift 2; then
        echo "ERROR: Wrong number of arguments for log line $i." >&2
        return_from_bats_assertion "$BASH_SOURCE" 1<commit_msg>tests/log/helpers: More robust log level parsing<commit_after>#! /bin/bash
#
# Helper functions for `lib/log` tests.

run_log_script() {
  create_test_go_script \
    ". \"\$_GO_USE_MODULES\" 'log'" \
    'if [[ -n "$TEST_LOG_FILE" ]]; then' \
    '  @go.log_add_output_file "$TEST_LOG_FILE"' \
    'fi' \
    "$@"
  run "$TEST_GO_SCRIPT"
}

# For tests that run command scripts via @go, set _GO_CMD to make sure that's
# the variable included in the log.
test-go() {
  env _GO_CMD="$FUNCNAME" "$TEST_GO_SCRIPT" "$@"
}

# Note that this must be called before any other log assertion, because it needs
# to set _GO_LOG_FORMATTING before importing the `log` module.
format_label() {
  local label="$1"

  if [[ -n "$__GO_LOG_INIT" ]]; then
    echo "$FUNCNAME must be called before any other function or assertion" \
      "that calls \`. \$_GO_USE_MODULES 'log'\` because it needs to set" \
      "\`_GO_LOG_FORMATTING\`." >&2
      return 1
  fi

  _GO_LOG_FORMATTING='true'
  . "$_GO_USE_MODULES" 'log'
  _@go.log_init

  local __go_log_level_index=0
  if ! <EMAIL> "$label"; then
    echo "Unknown log level label: $label" >&2
    return 1
  fi
  echo "${__GO_LOG_LEVELS_FORMATTED[$__go_log_level_index]}"
}

__parse_log_level_label() {
  local level="$1"
  local try_level="$level"
  if [[ "${level:0:3}" == '\e[' ]]; then
    try_level="${try_level//\\e\[[0-9]m}"
    try_level="${try_level//\\e\[[0-9][0-9]m}"
    try_level="${try_level//\\e\[[0-9][0-9][0-9]m}"
    try_level="${try_level%% *}"
  fi

  if ! _@<EMAIL>level_index "$try_level"; then
    return 1
  fi
  __log_level_label="$level"

  # If it's a label formatted with `format_label`, it's already padded.
  if [[ "${level:0:3}" != '\e[' ]]; then
    __log_level_label+="${__padding:0:$((${#__padding} - ${#try_level}))}"
  fi
}

__expected_log_line() {
  local level="$1"
  local message="$2"

  if [[ "${level:0:3}" == '\e[' ]]; then
    printf '%b\n' "$level $message\e[0m"
  else
    printf '%b\n' "$level $message"
  fi
}

assert_log_equals() {
  set +o functrace
  local level
  local __padding=''
  local __log_level_label
  local expected=()
  local i
  local result=0

  . "$_GO_USE_MODULES" 'log'

  for level in "${_GO_LOG_LEVELS[@]}"; do
    while [[ "${#__padding}" -lt "${#level}" ]]; do
      __padding+=' '
    done
  done

  for ((i=0; $# != 0; ++i)); do
    if __parse_log_level_label "$1"; then
      expected+=("$(__expected_log_line "$__log_level_label" "$2")")
      if ! shift 2; then
        echo "ERROR: Wrong number of arguments for log line $i." >&2
        return_from_bats_assertion "$BASH_SOURCE" 1
{'fraction_non_alphanumeric': 0.18159458905513426, 'fraction_numerical': 0.014962082393933183, 'mean_word_length': 3.428312159709619, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '734400', 'n_tokens_mistral': 1908, 'n_tokens_neox': 1868, 'n_words': 452}