forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlint_warn_assert.sh
More file actions
67 lines (61 loc) · 2.43 KB
/
lint_warn_assert.sh
File metadata and controls
67 lines (61 loc) · 2.43 KB
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
#!/usr/bin/env bash
set -euo pipefail
#
# lint_warn_assert.sh - Shared helper for testify/assert advisory warnings
#
# Usage:
# source /path/to/lint_warn_assert.sh
# test_warn_testify_assert
# Advisory check for testify/assert usage - warns but doesn't fail.
# Only runs on CI PRs, checking new code only.
# assert continues execution after failure, require fails fast.
# Developers should consciously choose when assert is appropriate.
#
# To run locally: WARN_TESTIFY_ASSERT=1 TESTS='warn_testify_assert' ./scripts/lint.sh
function test_warn_testify_assert {
local root_dir
root_dir="$(cd "$(dirname "${BASH_SOURCE[0]}")"; cd .. && pwd)"
local config_path="${root_dir}/.golangci-warn-assert.yml"
local args=(
--config "$config_path"
--issues-exit-code=0
)
if [[ -n "${GITHUB_BASE_REF:-}" ]]; then
# In a PR: fetch base branch and only check new code
git fetch origin "${GITHUB_BASE_REF}" --depth=1 2>/dev/null || true
args+=(--new-from-rev="origin/${GITHUB_BASE_REF}")
elif [[ -z "${WARN_TESTIFY_ASSERT:-}" ]]; then
echo "Skipping (only runs on CI PRs or with WARN_TESTIFY_ASSERT=1)"
return 0
fi
# Run golangci-lint and transform output to GitHub warning annotations
local output
output=$("${root_dir}/scripts/run_tool.sh" golangci-lint run "${args[@]}" 2>&1) || true
if [[ -z "$output" ]]; then
return 0
fi
# In GitHub Actions, emit ::warning annotations (not raw output which shows confusing "Error:" lines)
# For local runs, show the raw output
#
# Note: GitHub annotations don't support multiline messages, so we use a short message to avoid a horizontal scroll.
# See: https://github.com/actions/toolkit/issues/193
# https://github.com/orgs/community/discussions/122594
if [[ -n "${GITHUB_ACTIONS:-}" ]]; then
echo "$output" | grep -E '^[^:]+:[0-9]+:[0-9]+:' | while IFS= read -r line; do
# Parse "file:line:col: message"
local file line_num col msg
file=$(echo "$line" | cut -d: -f1)
line_num=$(echo "$line" | cut -d: -f2)
col=$(echo "$line" | cut -d: -f3)
# Clean up forbidigo's boilerplate phrasing
msg=$(echo "$line" | cut -d: -f4- \
| sed 's/^ *//' \
| sed 's/^use of //' \
| sed 's/ forbidden because "/" /' \
| sed 's/" (forbidigo)$//')
echo "::warning file=${file},line=${line_num},col=${col}::${msg}"
done || true # grep returns 1 when no matches, which is fine
else
echo "$output"
fi
}