aboutsummaryrefslogtreecommitdiffstats
path: root/scripts/lib/test.sh
blob: 939f82208464adce14a01271ffeae4e312618462 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# shellcheck shell=bash
[[ -n "${BOLT_TEST_LOADED:-}" ]] && return
readonly BOLT_TEST_LOADED=1

readonly BOLT_TEST_SUITES=("mssql" "sqlite3" "postgresql" "mysql")

source "$(dirname "${BASH_SOURCE[0]}")/log.sh"
source "$(dirname "${BASH_SOURCE[0]}")/docker.sh"
source "$(dirname "${BASH_SOURCE[0]}")/path.sh"

# Run a full test suite with database setup and teardown
#
# DESCRIPTION:
#   Starts database, waits for health checks, runs tests with
#   environment variables, and cleans up afterward. Automatically handles
#   database lifecycle and generates connection strings from Docker configuration.
#   Supports versioned suite names (e.g., "postgresql-15") for testing against
#   specific database versions.
#
# ARGUMENTS:
#   $1: Name of the database test suite to run (e.g., "mssql", "postgresql", "postgresql-15", "sqlite3", "mysql", "mysql-8.0")
#   $2: Keep database running (true/false, default: false)
#
# RETURN:
#   0 - Tests passed successfully
#   1 - Tests failed or setup/teardown failed.
#
# EXAMPLES:
#   test_run_suite "mssql"
#   test_run_suite "postgresql" true
#   test_run_suite "postgresql-15" false
#   test_run_suite "mysql-8.0"
#
test_run_suite() {
  local db_test_suite_name="$1"
  local keepdb="${2:-false}"
  local container_name="bolt_${db_test_suite_name}_db"
  local coverage_file="${PATH_PROJECT_DIR}/coverage-${db_test_suite_name}.out"
  local test_result=0

  local db_type version
  read -r db_type version <<<"$(docker_parse_suite_name "$db_test_suite_name")"

  log_header "$db_test_suite_name Tests"

  if [[ $db_type != "sqlite3" ]]; then
    if ! docker_ensure_container_running "$container_name" "$db_test_suite_name" "$keepdb"; then
      return 1
    fi
  fi

  log_step "Running $db_test_suite_name tests..."

  if ! test_run_go_tests "$db_test_suite_name" "$coverage_file"; then
    local test_result=1
  fi

  if [[ $db_type != "sqlite3" && $keepdb == "false" ]]; then
    log_step "Cleaning up..."
    docker_stop_container "$container_name" || log_warn "Unable to clean up container"
  fi

  return $test_result
}

# Check if a test suite name is valid
#
# DESCRIPTION:
#   Validates test suite names. Accepts both simple names (postgresql) and
#   versioned names (postgresql-15). Validates that the base database type
#   is in the list of supported test suites.
#
# ARGUMENTS:
#   $1: Test suite name to validate
#
# RETURN:
#   0 - Valid test suite name
#   1 - Invalid test suite name
#
# EXAMPLES:
#   test_is_valid_suite "postgresql"      # valid
#   test_is_valid_suite "postgresql-15"   # valid
#   test_is_valid_suite "invalid"         # invalid
#
test_is_valid_suite() {
  local suite="$1"
  local db_type version
  read -r db_type version <<<"$(docker_parse_suite_name "$suite")"

  for valid in "${BOLT_TEST_SUITES[@]}"; do
    [[ "$db_type" == "$valid" ]] && return 0
  done
  return 1
}

# Run Go tests with optional database type and coverage
#
# DESCRIPTION:
#   Executes Go tests with optional build tags and coverage reporting.
#   Tests are run with parallelism disabled (-p 1). For database tests,
#   automatically generates connection strings from Docker configuration.
#   Supports versioned suite names (e.g., "postgresql-15") which use the
#   base database type for build tags.
#
# ARGUMENTS:
#   $1 - suite_name: Suite name (e.g., "mssql", "postgresql", "postgresql-15"). Defaults to empty string.
#   $2 - coverage_file: Output file for coverage profile. Defaults to empty string.
#
# RETURN:
#   0 - Tests passed successfully
#   1 - Tests failed
#
# EXAMPLES:
#   test_run_go_tests
#   test_run_go_tests "mssql" "coverage-mssql.out"
#   test_run_go_tests "postgresql-15" "coverage-postgresql-15.out"
#
test_run_go_tests() {
  local suite_name="${1:-}"
  local coverage_file="${2:-}"

  # NOTE: -p 1 is used because we want to run tests sequentially.
  # This is because our tests depend on shared resources e.g. a database.
  local cmd="go test -p 1"

  local db_type=""
  if [[ -n "$suite_name" ]]; then
    local version
    read -r db_type version <<<"$(docker_parse_suite_name "$suite_name")"
    cmd="$cmd -tags $db_type"
    log_step "Running tests with tags: $db_type"
  else
    log_step "Running tests"
  fi

  if [[ -n "$coverage_file" ]]; then
    cmd="$cmd -covermode=count -coverprofile=$coverage_file"
  fi

  cmd="$cmd ./..."

  if [[ -n "$suite_name" ]]; then
    local dsn
    dsn=$(docker_generate_dsn "$suite_name")
    cmd="BOLT_DB_DSN='$dsn' BOLT_DB_DRIVER='$db_type' BOLT_DB_MIGRATIONS_TABLE='bolt_migrations' $cmd"
  fi

  log_debug "Running command: $cmd"
  if eval "$cmd"; then
    log_success "Tests completed successfully!"
    if [[ -n "$coverage_file" && -f "$coverage_file" ]]; then
      test_filter_coverage "$coverage_file"
    fi
  else
    log_error "Tests failed!"
    return 1
  fi
}

# Combine coverage reports from multiple test suites
#
# DESCRIPTION:
#   Merges coverage files matching pattern coverage-*.out into a single report
#
# ARGUMENTS:
#   $1 - output_file: Output file for combined coverage
#
# RETURN:
#   0 - Coverage combined successfully
#   1 - No coverage files found or merge failed
#
test_combine_coverage() {
  local output_file="$1"

  log_step "Checking for coverage files..."
  if ! ls coverage-*.out >/dev/null 2>&1; then
    log_error "No coverage files found (coverage-*.out)"
    log_error "Run tests first to generate coverage reports"
    return 1
  fi

  log_step "Combining reports into $output_file..."
  if ! go run github.com/wadey/gocovmerge@b5bfa59ec0adc420475f97f89b58045c721d761c coverage-*.out >"$output_file"; then
    log_error "Failed to combine coverage reports"
    return 1
  fi

  log_success "Combined coverage report created: $output_file"
}

# Filter coverage file based on .covignore patterns
#
# DESCRIPTION:
#   Reads .covignore file and removes matching lines from coverage file.
#   Patterns are matched using grep against file paths in coverage output.
#
# ARGUMENTS:
#   $1 - coverage_file: Coverage file to filter
#
# RETURN:
#   0 - Filtering successful or no .covignore file
#   1 - Filtering failed
#
test_filter_coverage() {
  local coverage_file="$1"
  local covignore_file="${PATH_PROJECT_DIR}/.covignore"

  # If no .covignore file exists, nothing to filter
  [[ ! -f "$covignore_file" ]] && return 0
  # If coverage file exists but is empty, nothing to filter
  [[ ! -s "$coverage_file" ]] && return 0

  local temp_file="${coverage_file}.tmp"
  local patterns=()
  while IFS= read -r line; do
    # Skip comments and empty lines
    [[ "$line" =~ ^[[:space:]]*# ]] && continue
    [[ -z "${line// /}" ]] && continue
    patterns+=("$line")
  done <"$covignore_file"
  [[ ${#patterns[@]} -eq 0 ]] && return 0

  local grep_cmd="grep --invert-match"
  for pattern in "${patterns[@]}"; do
    grep_cmd="$grep_cmd -e '$pattern'"
  done

  if eval "$grep_cmd" "$coverage_file" >"$temp_file"; then
    mv "$temp_file" "$coverage_file"
  else
    # If grep returns non-zero, it might be because all lines were filtered
    # Check if temp file exists and has content
    if [[ -f "$temp_file" ]]; then
      mv "$temp_file" "$coverage_file"
    fi
  fi

  return 0
}

# Display coverage summary and HTML generation instructions
#
# ARGUMENTS:
#   $1 - coverage_file: Coverage file to summarize
#
test_show_coverage_summary() {
  local coverage_file="$1"
  log_step "Generating coverage summary..."
  go tool cover -func="$coverage_file"
}