<commit_before>#! /usr/bin/env bats
#
# Tests for scripts/test.

load environment
load assertions

ALL_TESTS=($("$BASH" './go' 'glob' '--compact' '--ignore' 'bats/*' \
  'tests' '.bats'))

_fill_expected_completions() {
  local pattern="$1"
  local entry

  for entry in $pattern; do
    if [[ -d "$entry" ]]; then
      if [[ -f "$entry.bats" ]]; then
        expected+=("$entry")
      fi

      local contents=($entry/*.bats)
      if [[ "${contents[0]}" != "$entry/*.bats" ]]; then
        expected+=("$entry/")
      fi
    elif [[ -f "$entry" && "${entry##*.}" = "bats" \
      && ! -d "${entry%.bats}" ]]; then
      expected+=("${entry%.bats}")
    fi
  done
  expected=("${expected[@]#tests/}")
}

@test "test: tab complete flags" {
  run "$BASH" ./go test --complete 0 '-'
  assert_success "--list"
}

@test "test: tab completion lists first-level tests and directories" {
  local expected=('--list')
  _fill_expected_completions 'tests/*'

  run "$BASH" ./go test --complete 0 ''
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: tab completion matches test file and matching directory" {
  expected=('core' 'core/')
  run "$BASH" ./go test --complete 0 'core'
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: tab completion lists second-level tests and directories" {
  local expected=()
  _fill_expected_completions 'tests/core/*'

  run "$BASH" ./go test --complete 0 'core/'
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: no arguments after --list lists all tests" {
  run "$BASH" ./go test --list
  local IFS=$'\n'
  assert_success "${ALL_TESTS[*]}"
}

@test "test: list specific files and directories" {
  run "$BASH" ./go test --list test aliases 'builtins*'

  local expected=(test aliases builtins)
  _fill_expected_completions 'tests/builtins/*'

  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: produce an error if any test pattern fails to match" {
  run "$BASH" ./go test --list test 'foo*'
  assert_failure '"foo*" does not match any .bats files in tests.'
}
<commit_msg>tests/test: Refactor to remove complex function<commit_after>#! /usr/bin/env bats
#
# Tests for scripts/test.

load environment
load assertions

@test "test: tab complete flags" {
  run "$BASH" ./go test --complete 0 '-'
  assert_success "--list"
}

@test "test: tab completion lists first-level tests and directories" {
  local expected=(--list)
  expected+=($("$BASH" './go' 'glob' '--complete' '5' \
    '--compact' '--ignore' 'bats/*' 'tests' '.bats'))
  [[ "${#expected[@]}" -ne 1 ]]

  run "$BASH" ./go test --complete 0 ''
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: tab completion matches test file and matching directory" {
  expected=('core' 'core/')
  run "$BASH" ./go test --complete 0 'core'
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

_trim_expected() {
  expected=("${expected[@]#tests/}")
  expected=("${expected[@]%.bats}")
}

@test "test: tab completion lists second-level tests and directories" {
  local expected=(tests/core/*.bats)
  _trim_expected

  run "$BASH" ./go test --complete 0 'core/'
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: no arguments after --list lists all tests" {
  local expected=(
    $("$BASH" './go' 'glob' '--compact' '--ignore' 'bats/*' 'tests' '.bats'))
  [[ "${#expected[@]}" -ne 0 ]]

  run "$BASH" ./go test --list
  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: list specific files and directories" {
  run "$BASH" ./go test --list test aliases 'builtins*'

  local expected=(test aliases builtins tests/builtins/*)
  _trim_expected

  local IFS=$'\n'
  assert_success "${expected[*]}"
}

@test "test: produce an error if any test pattern fails to match" {
  run "$BASH" ./go test --list test 'foo*'
  assert_failure '"foo*" does not match any .bats files in tests.'
}

{'fraction_non_alphanumeric': 0.1946811257423186, 'fraction_numerical': 0.003356571133488252, 'mean_word_length': 3.4837962962962963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3898460', 'n_tokens_mistral': 1367, 'n_tokens_neox': 1322, 'n_words': 409}