Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Keep ratchets off release branches (#81033)

Chris Truter39ad1d516756
diff --git a/.github/actions/create-backport/action.yml b/.github/actions/create-backport/action.yml
index a44176f3b62..eeb83f3386b 100644
--- a/.github/actions/create-backport/action.yml
+++ b/.github/actions/create-backport/action.yml
@@ -25,6 +25,8 @@ runs:
COMMIT: ${{ inputs.backport-commit }}
shell: bash
run: | # bash
+ source "${{ github.action_path }}/kondo-ratchets.sh"
+
git config --global user.email "[email protected]"
git config --global user.name "Metabase bot"
@@ -34,7 +36,7 @@ runs:
git checkout ${TARGET_BRANCH}
git fetch --all
git checkout -b ${BACKPORT_BRANCH}
- git cherry-pick ${COMMIT} || true
+ cherry_pick_backport "${COMMIT}"
CONFLICT_LIST=$(git ls-files -u)
@@ -46,10 +48,7 @@ runs:
echo "has-conflicts=true" >> "$GITHUB_OUTPUT"
# Add a shell script for resolving conflicts
- echo "git reset HEAD~1" > ./backport.sh
- echo "rm ./backport.sh" >> ./backport.sh
- echo "git cherry-pick ${COMMIT}" >> ./backport.sh
- echo "echo 'Resolve conflicts and force push this branch.\n\nTo backport translations run: bin/i18n/merge-translations <release-branch>'" >> ./backport.sh
+ write_backport_script "${COMMIT}"
chmod +x ./backport.sh
git add ./backport.sh
git commit -m "Add backport resolution script"
diff --git a/.github/actions/create-backport/create-backport-test.sh b/.github/actions/create-backport/create-backport-test.sh
new file mode 100755
index 00000000000..0facfb65e02
--- /dev/null
+++ b/.github/actions/create-backport/create-backport-test.sh
@@ -0,0 +1,375 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+action_dir=$(cd "$(dirname "$0")" && pwd)
+cut_release_workflow="$action_dir/../../workflows/cut-release-branch.yml"
+
+# shellcheck source=.github/actions/create-backport/kondo-ratchets.sh
+source "$action_dir/kondo-ratchets.sh"
+
+# Any editor launch fails the run, so a continuation that waits on an editor cannot pass.
+export GIT_EDITOR=false
+
+test_root=
+trap '[ -z "$test_root" ] || rm -rf -- "$test_root"' EXIT
+trap 'exit 129' HUP
+trap 'exit 130' INT
+trap 'exit 143' TERM
+test_root=$(mktemp -d)
+
+active_ratchets='{:ignore-counts {:example 1}}'
+disabled_ratchets=';; Kondo ignore ratchets apply only to master; this release branch opts out.
+;; The test and fixer recognize this explicit opt-out.
+{:disabled true}'
+
+# Coverage:
+#
+# Target state Incoming change Other conflict Expected result
+# ----------------- -------------------- --------------- ------------------------------------------
+# active ratchets unrelated file none leave ratchets unchanged
+# active ratchets ratchets none install the release opt-out
+# disabled ratchets ratchets none keep the opt-out; finish with an empty commit
+# divergent ratchets ratchets none resolve ratchets and finish the cherry-pick
+# divergent ratchets ratchets (on a pty) none finish without opening an editor
+# active ratchets unrelated file app.txt leave the conflict for manual resolution
+# divergent ratchets ratchets + app.txt app.txt resolve only ratchets
+# release cut ratchets none cut and backport commit the same opt-out
+# n/a cut-release workflow n/a the workflow calls the shared writer
+# any invalid commit n/a propagate the cherry-pick failure
+# divergent ratchets generated backport.sh app.txt resolve only ratchets through the manual script
+#
+# Known gaps: an older target with no ratchets file, and an incoming commit that
+# deletes the ratchets file.
+
+test_unrelated_backport_preserves_target_ratchets() (
+ local repo="$test_root/unrelated"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_app)
+ git -C "$repo" checkout -q release
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_eq feature "$(cat app.txt)" "the unrelated change was not cherry-picked"
+ assert_ratchets "$active_ratchets"
+ assert_cherry_pick_completed
+ assert_worktree_clean
+)
+
+test_clean_ratchet_backport_installs_release_opt_out() (
+ local repo="$test_root/clean-ratchet"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_ratchets)
+ git -C "$repo" checkout -q release
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_ratchets_disabled
+ assert_cherry_pick_completed
+ assert_worktree_clean
+)
+
+test_disabled_release_absorbs_ratchet_only_backport() (
+ local repo="$test_root/disabled-release"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_ratchets)
+ git -C "$repo" checkout -q release
+ commit_release_change "$repo" disable_release_ratchets "Disable release ratchets"
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_ratchets_disabled
+ assert_cherry_pick_completed
+ assert_empty_commit
+ assert_worktree_clean
+)
+
+test_ratchet_only_conflict_is_resolved() (
+ local repo="$test_root/ratchet-conflict"
+ init_repo "$repo"
+ local commit
+ commit=$(conflicting_ratchet_commit "$repo")
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_ratchets_disabled
+ assert_cherry_pick_completed
+ assert_worktree_clean
+)
+
+test_continuation_never_opens_an_editor() (
+ local repo="$test_root/editor"
+ init_repo "$repo"
+ local commit
+ commit=$(conflicting_ratchet_commit "$repo")
+
+ cd "$repo"
+ # Git only opens an editor for the continuation when stdin is a terminal, so run it on a pty. The
+ # exported GIT_EDITOR=false turns any editor launch into a failure. The single quotes are deliberate:
+ # $1 and $2 are for the inner bash, not this shell.
+ # shellcheck disable=SC2016
+ python3 -c 'import os, pty, sys; sys.exit(os.waitstatus_to_exitcode(pty.spawn(sys.argv[1:])))' \
+ bash -c 'source "$1/kondo-ratchets.sh" && cherry_pick_backport "$2"' bash "$action_dir" "$commit" >/dev/null ||
+ fail "the cherry-pick continuation opened an editor"
+
+ assert_ratchets_disabled
+ assert_cherry_pick_completed
+)
+
+test_non_ratchet_conflict_remains_manual() (
+ local repo="$test_root/non-ratchet-conflict"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_app)
+ git -C "$repo" checkout -q release
+ commit_release_change "$repo" change_release_app "Change release app"
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_ratchets "$active_ratchets"
+ assert_only_conflicts app.txt
+ assert_cherry_pick_pending
+)
+
+test_mixed_conflict_resolves_only_ratchets() (
+ local repo="$test_root/mixed-conflict"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_app_and_ratchets)
+ git -C "$repo" checkout -q release
+ commit_release_change "$repo" change_release_app_and_ratchets "Change release app and ratchets"
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+
+ assert_ratchets_disabled
+ assert_only_conflicts app.txt
+ assert_cherry_pick_pending
+)
+
+test_release_cut_and_backport_commit_the_same_opt_out() (
+ local cut_repo="$test_root/release-cut"
+ init_repo "$cut_repo"
+ cd "$cut_repo"
+ git checkout -qb release-x.99.x
+ write_disabled_ratchets
+ git add --sparse .clj-kondo/ratchets.edn
+ git commit -q --allow-empty --no-verify -m "Cut release-x.99.x branch"
+ git show HEAD:.clj-kondo/ratchets.edn > "$test_root/release-cut.edn"
+
+ local backport_repo="$test_root/release-cut-backport"
+ init_repo "$backport_repo"
+ local commit
+ commit=$(feature_commit "$backport_repo" change_ratchets)
+ git -C "$backport_repo" checkout -q release
+ cd "$backport_repo"
+ cherry_pick_backport "$commit"
+ git show HEAD:.clj-kondo/ratchets.edn > "$test_root/backport.edn"
+
+ cmp -s "$test_root/release-cut.edn" "$test_root/backport.edn" ||
+ fail "the release cut and the backport committed different opt-out files"
+ assert_eq "$disabled_ratchets" "$(cat "$test_root/release-cut.edn")" \
+ "the release cut did not install the expected opt-out"
+)
+
+test_cut_release_workflow_uses_the_shared_writer() (
+ grep -Fq 'source .github/actions/create-backport/kondo-ratchets.sh' "$cut_release_workflow" ||
+ fail "the cut-release workflow does not source kondo-ratchets.sh"
+ grep -Fq 'write_disabled_ratchets' "$cut_release_workflow" ||
+ fail "the cut-release workflow does not call write_disabled_ratchets"
+ ! grep -Fq ':disabled' "$cut_release_workflow" ||
+ fail "the cut-release workflow carries its own copy of the opt-out"
+)
+
+test_invalid_commit_failure_is_propagated() (
+ local repo="$test_root/invalid"
+ init_repo "$repo"
+ cd "$repo"
+
+ if cherry_pick_backport deadbeef; then
+ fail "an invalid commit succeeded"
+ fi
+)
+
+test_generated_script_resolves_ratchets_end_to_end() (
+ local repo="$test_root/generated-script"
+ init_repo "$repo"
+ local commit
+ commit=$(feature_commit "$repo" change_app_and_ratchets)
+ git -C "$repo" checkout -q release
+ commit_release_change "$repo" change_release_app_and_ratchets "Change release app and ratchets"
+
+ cd "$repo"
+ cherry_pick_backport "$commit"
+ git cherry-pick --abort
+
+ local release_commit
+ release_commit=$(git rev-parse HEAD)
+ write_backport_script "$commit"
+ chmod +x backport.sh
+ git add backport.sh
+ git commit -qm "Add manual backport script"
+
+ bash -n backport.sh
+ grep -Fq 'write_disabled_ratchets ()' backport.sh || fail "the generated script omitted write_disabled_ratchets"
+ ./backport.sh
+
+ assert_ratchets_disabled
+ assert_only_conflicts app.txt
+ assert_cherry_pick_pending
+ assert_eq "$release_commit" "$(git rev-parse HEAD)" "the generated script did not return HEAD to the release commit"
+ [ ! -e backport.sh ] || fail "the generated script did not remove itself"
+ ! git ls-files --error-unmatch backport.sh >/dev/null 2>&1 || fail "the generated script is still tracked"
+ ! git status --porcelain | grep -q backport.sh || fail "the generated script left a status entry"
+)
+
+# Test harness
+
+fail() {
+ echo "FAIL: $*" >&2
+ exit 1
+}
+
+assert_eq() {
+ local expected=$1
+ local actual=$2
+ local message=$3
+ [ "$expected" = "$actual" ] || fail "$message (expected '$expected', got '$actual')"
+}
+
+assert_ratchets() {
+ assert_eq "$1" "$(cat .clj-kondo/ratchets.edn)" "unexpected ratchet state"
+}
+
+assert_ratchets_disabled() {
+ assert_ratchets "$disabled_ratchets"
+}
+
+assert_cherry_pick_completed() {
+ ! git rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null || fail "the cherry-pick is still pending"
+}
+
+assert_cherry_pick_pending() {
+ git rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null || fail "the cherry-pick state was lost"
+}
+
+assert_only_conflicts() {
+ local expected
+ local actual
+ expected=$(printf '%s\n' "$@" | sort)
+ actual=$(git diff --name-only --diff-filter=U | sort)
+ assert_eq "$expected" "$actual" "unexpected unresolved files"
+}
+
+assert_empty_commit() {
+ git diff --quiet HEAD^ HEAD || fail "the backport commit was not empty"
+}
+
+assert_worktree_clean() {
+ [ -z "$(git status --porcelain)" ] || fail "the backport left worktree changes"
+}
+
+init_repo() {
+ local repo=$1
+ git init -q -b release "$repo"
+ git -C "$repo" config user.email [email protected]
+ git -C "$repo" config user.name "Backport test"
+ printf 'base\n' > "$repo/app.txt"
+ mkdir -p "$repo/.clj-kondo"
+ printf '%s\n' "$active_ratchets" > "$repo/.clj-kondo/ratchets.edn"
+ git -C "$repo" add .
+ git -C "$repo" commit -qm "Base"
+}
+
+feature_commit() {
+ local repo=$1
+ shift
+ git -C "$repo" checkout -qb feature
+ "$@" "$repo"
+ git -C "$repo" add .
+ git -C "$repo" commit -qm "Feature"
+ git -C "$repo" rev-parse HEAD
+}
+
+# Sets up a release branch whose ratchets conflict with the feature commit, and prints that commit.
+conflicting_ratchet_commit() {
+ local repo=$1
+ local commit
+ commit=$(feature_commit "$repo" change_ratchets)
+ git -C "$repo" checkout -q release
+ commit_release_change "$repo" change_release_ratchets "Change release ratchets"
+ echo "$commit"
+}
+
+change_app() {
+ printf 'feature\n' > "$1/app.txt"
+}
+
+change_ratchets() {
+ printf '{:ignore-counts {:example 2}}\n' > "$1/.clj-kondo/ratchets.edn"
+}
+
+change_app_and_ratchets() {
+ change_app "$1"
+ change_ratchets "$1"
+}
+
+change_release_app() {
+ printf 'release\n' > "$1/app.txt"
+}
+
+change_release_ratchets() {
+ printf '{:ignore-counts {:release 1}}\n' > "$1/.clj-kondo/ratchets.edn"
+}
+
+change_release_app_and_ratchets() {
+ change_release_app "$1"
+ change_release_ratchets "$1"
+}
+
+# Setup writes the opt-out through the shared writer; the only copy is the expected text at the top.
+disable_release_ratchets() {
+ (cd "$1" && write_disabled_ratchets)
+}
+
+commit_release_change() {
+ local repo=$1
+ local change=$2
+ local message=$3
+ "$change" "$repo"
+ git -C "$repo" add .
+ git -C "$repo" commit -qm "$message"
+}
+
+run_test() {
+ local description=$1
+ local test=${description// /_}
+ test=${test//-/_}
+ test="test_$test"
+
+ declare -F "$test" >/dev/null || fail "missing test function: $test"
+
+ printf 'TEST: %s\n' "$description"
+ "$test"
+ printf 'PASS: %s\n' "$description"
+}
+
+run_test "unrelated backport preserves target ratchets"
+run_test "clean ratchet backport installs release opt-out"
+run_test "disabled release absorbs ratchet-only backport"
+run_test "ratchet-only conflict is resolved"
+run_test "continuation never opens an editor"
+run_test "non-ratchet conflict remains manual"
+run_test "mixed conflict resolves only ratchets"
+run_test "release cut and backport commit the same opt-out"
+run_test "cut-release workflow uses the shared writer"
+run_test "invalid commit failure is propagated"
+run_test "generated script resolves ratchets end-to-end"
diff --git a/.github/actions/create-backport/kondo-ratchets.sh b/.github/actions/create-backport/kondo-ratchets.sh
new file mode 100644
index 00000000000..2211034b705
--- /dev/null
+++ b/.github/actions/create-backport/kondo-ratchets.sh
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+
+set -euo pipefail
+
+# The only place that knows what a release branch's opt-out looks like. The cut-release workflow, the
+# backport helper, and the tests all go through this function.
+write_disabled_ratchets() {
+ mkdir -p .clj-kondo
+ printf '%s\n' \
+ ';; Kondo ignore ratchets apply only to master; this release branch opts out.' \
+ ';; The test and fixer recognize this explicit opt-out.' \
+ '{:disabled true}' \
+ > .clj-kondo/ratchets.edn
+}
+
+disable_ratchets() {
+ local commit=$1
+ local file=.clj-kondo/ratchets.edn
+ [ -n "$(git diff-tree --root --no-commit-id --name-only -r "$commit" -- "$file")" ] || return 0
+
+ echo "$file: disabling kondo ratchets on the release branch"
+ write_disabled_ratchets
+ git add -- "$file"
+
+ if ! git rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null; then
+ if ! git diff --cached --quiet HEAD; then
+ git commit --amend --no-edit
+ fi
+ return 0
+ fi
+
+ if [ -z "$(git ls-files -u)" ]; then
+ echo "$file was the only conflict, finishing the cherry-pick"
+ if git diff --cached --quiet HEAD; then
+ echo "nothing left to apply, committing the backport empty"
+ git commit --allow-empty --no-edit
+ else
+ # GIT_EDITOR outranks core.editor, VISUAL, and EDITOR, so the continuation can never wait on
+ # an editor; the repository's own editor configuration is left alone.
+ GIT_EDITOR=true git cherry-pick --continue
+ fi
+ fi
+}
+
+cherry_pick_backport() {
+ local commit=$1
+ local status=0
+ git cherry-pick "$commit" || status=$?
+ if [ "$status" -ne 0 ] && ! git rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null; then
+ echo "cherry-pick failed without entering a resolvable conflict state" >&2
+ return "$status"
+ fi
+
+ disable_ratchets "$commit"
+ if [ "$status" -ne 0 ] &&
+ git rev-parse -q --verify CHERRY_PICK_HEAD >/dev/null &&
+ [ -z "$(git ls-files -u)" ]; then
+ echo "cherry-pick stopped without a file conflict to resolve" >&2
+ return "$status"
+ fi
+}
+
+write_backport_script() {
+ local commit=$1
+ {
+ echo "#!/usr/bin/env bash"
+ echo "set -euo pipefail"
+ echo "git reset HEAD~1"
+ echo "rm ./backport.sh"
+ declare -f write_disabled_ratchets disable_ratchets cherry_pick_backport
+ printf 'cherry_pick_backport %q\n' "$commit"
+ printf '%s\n' "printf 'Resolve conflicts and force push this branch.\\n\\nTo backport translations run: bin/i18n/merge-translations <release-branch>\\n'"
+ } > ./backport.sh
+}
diff --git a/.github/workflows/cut-release-branch.yml b/.github/workflows/cut-release-branch.yml
index f03f3d0c895..dcb47dfcb95 100644
--- a/.github/workflows/cut-release-branch.yml
+++ b/.github/workflows/cut-release-branch.yml
@@ -59,6 +59,12 @@ jobs:
# Pushing the new branch triggers set-milestone.yml, which does the
# full milestone backfill and then dispatches release-notes-preview
# to upload the markdown to #writing.
+ #
+ # Kondo ignore ratchets apply only to master. The cut commit replaces the
+ # budgets with an explicit opt-out so future budget changes are not applied
+ # to the release branch. Keeping the opt-out in the canonical file avoids
+ # introducing a separate marker that could drift out of sync, and the
+ # backport helper writes the same opt-out so both paths stay identical.
- name: Cut the branch
id: cut
run: |
@@ -66,6 +72,9 @@ jobs:
git config user.name github-actions
git config user.email [email protected]
git checkout -b "$BRANCH"
+ source .github/actions/create-backport/kondo-ratchets.sh
+ write_disabled_ratchets
+ git add --sparse .clj-kondo/ratchets.edn
git commit --allow-empty --no-verify -m "Cut $BRANCH branch"
git push -u origin "$BRANCH"
echo "sha=$(git rev-parse HEAD)" >> "$GITHUB_OUTPUT"
diff --git a/.github/workflows/mage.yml b/.github/workflows/mage.yml
index 9ac44fb1479..74de0c922fc 100644
--- a/.github/workflows/mage.yml
+++ b/.github/workflows/mage.yml
@@ -16,3 +16,7 @@ jobs:
run: ./bin/mage -h
- name: Run mage tests
run: ./bin/mage -test
+ - name: Lint create-backport scripts
+ run: shellcheck .github/actions/create-backport/{kondo-ratchets,create-backport-test}.sh
+ - name: Run create-backport tests
+ run: ./.github/actions/create-backport/create-backport-test.sh
diff --git a/CLAUDE.md b/CLAUDE.md
index 25309165b16..561425517d8 100644
--- a/CLAUDE.md
+++ b/CLAUDE.md
@@ -84,6 +84,10 @@ tree may contain, and how many config-level suppressions (`:off` switches and `:
`.clj-kondo/config.edn`) exist. `metabase.core.kondo-ratchet-test` fails when either budget drifts from the
actual counts, in either direction. Prefer fixing the underlying warning over adding an ignore.
+The ratchets apply only to `master`. When a release branch is cut, `.clj-kondo/ratchets.edn` is replaced
+with `{:disabled true}`. The test and fixer recognize this explicit opt-out, while a missing file still
+causes an error on `master`.
+
Budget too high (you removed ignores): a local run of the test tightens the file for you — commit the
change. PRs labelled `kondo-ratchets-self-healing` get the lowered budgets committed to the branch by CI.
To tighten by hand (babashka, no JVM; a no-op prints `unchanged`):
diff --git a/dev/src/dev/kondo_ratchet.clj b/dev/src/dev/kondo_ratchet.clj
index f111eba9217..807ba2f1db3 100644
--- a/dev/src/dev/kondo_ratchet.clj
+++ b/dev/src/dev/kondo_ratchet.clj
@@ -14,10 +14,28 @@
(set! *warn-on-reflection* true)
-(def ratchets-file
+(def ^:dynamic *ratchets-file*
"The budgets file, relative to the repo root."
".clj-kondo/ratchets.edn")
+(defn read-ratchets
+ "Parsed contents of [[*ratchets-file*]], with empty defaults for omitted keys.
+ Throws when the file is missing; only an explicit `{:disabled true}` opts out of enforcement."
+ []
+ (let [file (io/file *ratchets-file*)]
+ (when-not (.exists file)
+ (throw (ex-info (str *ratchets-file* " is missing -- only {:disabled true} opts out of enforcement")
+ {:file *ratchets-file*})))
+ (merge {:ignore-counts {}, :config-counts {}, :comment-exempt #{}}
+ (edn/read-string (slurp file)))))
+
+(defn disabled?
+ "Whether `ratchets` (default: [[read-ratchets]]) explicitly disables the ratchets."
+ ([]
+ (disabled? (read-ratchets)))
+ ([ratchets]
+ (true? (:disabled ratchets))))
+
(def ^:private source-roots
["src" "test" "enterprise" "modules/drivers" "dev" "bin" "mage"])
@@ -320,13 +338,6 @@
:when (not= budget n)]
[linter {:recorded budget, :actual n}])))
-(defn read-ratchets
- "Parsed contents of [[ratchets-file]], with empty defaults when the file doesn't exist."
- []
- (merge {:ignore-counts {}, :config-counts {}, :comment-exempt #{}}
- (when (.exists (io/file ratchets-file))
- (edn/read-string (slurp ratchets-file)))))
-
(def ^:private header
(str ";; Budgets for kondo suppressions: inline `" ignore-marker "` forms per linter (:ignore-counts),\n"
";; and config-level waivers in .clj-kondo/config.edn (:config-counts -- :off switches and :exclude\n"
@@ -417,24 +428,26 @@
(format "unexempted %s (all its ignores are justified now)" linter)))))
(defn fix!
- "Rewrite [[ratchets-file]]: lower budgets, drop stale comment exemptions, normalize formatting.
+ "Rewrite [[*ratchets-file*]]: lower budgets, drop stale comment exemptions, normalize formatting.
`--seed LINTER` (`{:seed \"...\"}` here) sets that budget to the actual count, adding or raising it.
- Prints the [[change-report]], or `unchanged` on a no-op."
+ Prints the [[change-report]], or `unchanged` on a no-op.
+ Does nothing, including seeding, when the file sets `:disabled` to `true`."
([]
(fix! nil))
([{:keys [seed]}]
- (let [{:keys [ignore-counts config-counts comment-exempt] :as ratchets} (read-ratchets)
- occurrences (scan)
- seeded (if seed [(keyword (str/replace-first seed #"^:" ""))] [])
- actual (actual-counts occurrences)
- config-actual (config-suppressions)
- text (render {:ignore-counts (lowered-counts ignore-counts actual seeded)
- :config-counts (lowered-counts config-counts config-actual [])
- :comment-exempt (reduce disj comment-exempt (stale-exemptions comment-exempt occurrences))})
- file (io/file ratchets-file)
- old (when (.exists file) (slurp file))]
- (run! println (change-report ratchets occurrences config-actual seeded))
- (if (= old text)
- (println "unchanged")
- (do (spit file text)
- (println (str "wrote " ratchets-file)))))))
+ (let [{:keys [ignore-counts config-counts comment-exempt] :as ratchets} (read-ratchets)]
+ (if (disabled? ratchets)
+ (println (str *ratchets-file* " is disabled -- nothing to do"))
+ (let [occurrences (scan)
+ seeded (if seed [(keyword (str/replace-first seed #"^:" ""))] [])
+ actual (actual-counts occurrences)
+ config-actual (config-suppressions)
+ text (render {:ignore-counts (lowered-counts ignore-counts actual seeded)
+ :config-counts (lowered-counts config-counts config-actual [])
+ :comment-exempt (reduce disj comment-exempt (stale-exemptions comment-exempt occurrences))})
+ old (slurp *ratchets-file*)]
+ (run! println (change-report ratchets occurrences config-actual seeded))
+ (if (= old text)
+ (println "unchanged")
+ (do (spit *ratchets-file* text)
+ (println (str "wrote " *ratchets-file*)))))))))
diff --git a/mage/src/mage/kondo_ratchet.clj b/mage/src/mage/kondo_ratchet.clj
index abfc3500f44..0ec1a0eedf0 100644
--- a/mage/src/mage/kondo_ratchet.clj
+++ b/mage/src/mage/kondo_ratchet.clj
@@ -496,4 +496,4 @@
(when (seq (kondo-ratchet/unjustified #{} (filter #(some #{linter} (:linters %))
(kondo-ratchet/scan roots))))
(println (format "Also add %s to :comment-exempt in %s -- the inserted ignores have no comments."
- linter kondo-ratchet/ratchets-file))))))))
+ linter kondo-ratchet/*ratchets-file*))))))))
diff --git a/test/metabase/core/kondo_ratchet_test.clj b/test/metabase/core/kondo_ratchet_test.clj
index c7f4f016ab2..9c05d904520 100644
--- a/test/metabase/core/kondo_ratchet_test.clj
+++ b/test/metabase/core/kondo_ratchet_test.clj
@@ -22,6 +22,9 @@
@scan
(kondo-ratchet/scan)))
+(defn- ratchets-enabled? []
+ (not (kondo-ratchet/disabled?)))
+
;; Outside CI, tighten the ratchets before asserting — the fix rides along in your next commit.
;; The self-heal workflow does the same for labelled PRs.
(use-fixtures :once (fn [thunk]
@@ -38,48 +41,54 @@
;;;; ---------------------------------------------------------------------------
(deftest ^:parallel budgets-match-actual-counts-test
- (testing (str "\nBudgets in " kondo-ratchet/ratchets-file " must match the actual inline ignore counts.\n"
- "Budget too low: remove an ignore, or seed the budget with\n"
- "`./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR.\n"
- "Budget too high: run `./bin/mage fix-kondo-ratchets`, or label the PR\n"
- "kondo-ratchets-self-healing and CI commits the fix to your branch. Too high in a local\n"
- "run means `fix!` itself is broken, since the test fixture just ran it.")
- (is (= {}
- (kondo-ratchet/drift (:ignore-counts (kondo-ratchet/read-ratchets))
- (tree-scan))))))
+ (when (ratchets-enabled?)
+ (testing (str "\nBudgets in " kondo-ratchet/*ratchets-file* " must match the actual inline ignore counts.\n"
+ "Budget too low: remove an ignore, or seed the budget with\n"
+ "`./bin/mage fix-kondo-ratchets --seed <linter>` and defend it in the PR.\n"
+ "Budget too high: run `./bin/mage fix-kondo-ratchets`, or label the PR\n"
+ "kondo-ratchets-self-healing and CI commits the fix to your branch. Too high in a local\n"
+ "run means `fix!` itself is broken, since the test fixture just ran it.")
+ (is (= {}
+ (kondo-ratchet/drift (:ignore-counts (kondo-ratchet/read-ratchets))
+ (tree-scan)))))))
(deftest ^:parallel ignores-are-justified-test
- (testing (str "\nInline ignores of these linters need an explanatory `;;` comment on the line above\n"
- "(or trailing on the same line) saying why the suppression is warranted.\n"
- "Linters in :comment-exempt in " kondo-ratchet/ratchets-file " are grandfathered;\n"
- "widening that set is a hand edit to defend in the PR.")
- (let [exempt (:comment-exempt (kondo-ratchet/read-ratchets))]
- (is (= []
- (map #(dissoc % :justified?)
- (kondo-ratchet/unjustified exempt (tree-scan))))))))
+ (when (ratchets-enabled?)
+ (testing (str "\nInline ignores of these linters need an explanatory `;;` comment on the line above\n"
+ "(or trailing on the same line) saying why the suppression is warranted.\n"
+ "Linters in :comment-exempt in " kondo-ratchet/*ratchets-file* " are grandfathered;\n"
+ "widening that set is a hand edit to defend in the PR.")
+ (let [exempt (:comment-exempt (kondo-ratchet/read-ratchets))]
+ (is (= []
+ (map #(dissoc % :justified?)
+ (kondo-ratchet/unjustified exempt (tree-scan)))))))))
(deftest ^:parallel no-stale-exemptions-test
- (testing (str "\nEvery linter in :comment-exempt still has at least one unjustified ignore; once the last\n"
- "one gains a comment, the exemption goes. Run `./bin/mage fix-kondo-ratchets`, or label\n"
- "the PR kondo-ratchets-self-healing.")
- (let [{:keys [comment-exempt]} (kondo-ratchet/read-ratchets)]
- (is (= #{}
- (kondo-ratchet/stale-exemptions comment-exempt (tree-scan)))))))
+ (when (ratchets-enabled?)
+ (testing (str "\nEvery linter in :comment-exempt still has at least one unjustified ignore; once the last\n"
+ "one gains a comment, the exemption goes. Run `./bin/mage fix-kondo-ratchets`, or label\n"
+ "the PR kondo-ratchets-self-healing.")
+ (let [{:keys [comment-exempt]} (kondo-ratchet/read-ratchets)]
+ (is (= #{}
+ (kondo-ratchet/stale-exemptions comment-exempt (tree-scan))))))))
(deftest ^:parallel config-budgets-match-actual-test
- (testing (str "\nConfig-level suppression budgets in " kondo-ratchet/ratchets-file " must match\n"
- ".clj-kondo/config.edn (:off switches and :exclude entries, per linter).\n"
- "Budget too low: remove the new config suppression, or raise the budget by hand and\n"
- "defend it in the PR. Budget too high: run `./bin/mage fix-kondo-ratchets`.")
- (is (= {}
- (kondo-ratchet/config-drift (:config-counts (kondo-ratchet/read-ratchets))
- (kondo-ratchet/config-suppressions))))))
+ (when (ratchets-enabled?)
+ (testing (str "\nConfig-level suppression budgets in " kondo-ratchet/*ratchets-file* " must match\n"
+ ".clj-kondo/config.edn (:off switches and :exclude entries, per linter).\n"
+ "Budget too low: remove the new config suppression, or raise the budget by hand and\n"
+ "defend it in the PR. Budget too high: run `./bin/mage fix-kondo-ratchets`.")
+ (is (= {}
+ (kondo-ratchet/config-drift (:config-counts (kondo-ratchet/read-ratchets))
+ (kondo-ratchet/config-suppressions)))))))
(deftest ^:parallel ratchets-file-normalized-test
- (testing (str "\n" kondo-ratchet/ratchets-file " should be sorted and aligned exactly as the generator"
- " writes it.\nAfter a hand edit, run `./bin/mage fix-kondo-ratchets` to normalize the formatting.")
- (is (= (kondo-ratchet/render (kondo-ratchet/read-ratchets))
- (slurp kondo-ratchet/ratchets-file)))))
+ (when (ratchets-enabled?)
+ (testing (str "\n" kondo-ratchet/*ratchets-file* " should be sorted and aligned exactly as the generator"
+ " writes it.\nAfter a hand edit, run `./bin/mage fix-kondo-ratchets` to normalize the"
+ " formatting.")
+ (is (= (kondo-ratchet/render (kondo-ratchet/read-ratchets))
+ (slurp kondo-ratchet/*ratchets-file*))))))
;;;; ---------------------------------------------------------------------------
;;;; Scanner unit tests
@@ -272,6 +281,31 @@
{:file "f.clj", :line line, :linters [:a]})]
(is (= 5 (count (:examples (:a (kondo-ratchet/drift {} occurrences)))))))))
+(deftest ^:parallel fix-when-disabled-test
+ (testing "fix! explains that the ratchets are disabled and leaves the file unchanged"
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ budgets (doto (io/file dir "ratchets.edn") (spit "{:disabled true}\n"))]
+ (binding [kondo-ratchet/*ratchets-file* (.getPath budgets)]
+ (is (kondo-ratchet/disabled?))
+ (is (= (str (.getPath budgets) " is disabled -- nothing to do\n")
+ (with-out-str (kondo-ratchet/fix! {:seed "whatever"}))))
+ (is (= "{:disabled true}\n" (slurp budgets)))))))
+
+(deftest ^:parallel missing-ratchets-file-fails-test
+ (let [dir (.toFile (java.nio.file.Files/createTempDirectory
+ "kondo-ratchet-test"
+ (make-array java.nio.file.attribute.FileAttribute 0)))
+ missing (.getPath (io/file dir "missing-ratchets.edn"))]
+ (binding [kondo-ratchet/*ratchets-file* missing]
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"ratchets.edn is missing"
+ (kondo-ratchet/read-ratchets)))
+ (is (thrown-with-msg? clojure.lang.ExceptionInfo
+ #"ratchets.edn is missing"
+ (kondo-ratchet/disabled?))))))
+
;;;; ---------------------------------------------------------------------------
;;;; Justification bookkeeping unit tests
;;;; ---------------------------------------------------------------------------