Skip to content

Commit 992422b

Browse files
ZviBaratzclaude
andcommitted
feat: auto-source assertions and auto-append in new-rule.sh
Replace 22 manual if/source blocks in run-tests.sh with glob-based auto-sourcing from tests/assertions/. New assertion files are automatically discovered — no need to edit run-tests.sh. Add interactive assertion auto-append to new-rule.sh: shows numbered list of existing assertion files, prompts for selection or new file creation, and appends directly. The full contributor workflow is now scripts/new-rule.sh → tests/run-tests.sh with no manual copy-paste. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 4330c31 commit 992422b

File tree

2 files changed

+54
-97
lines changed

2 files changed

+54
-97
lines changed

scripts/new-rule.sh

Lines changed: 49 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ EOF
133133

134134
echo "Created fixture at $fixture_dir/"
135135

136-
# --- 3. Print assertion lines ---
136+
# --- 3. Append assertions to test file ---
137137

138138
if [[ "$severity" == "blocking" ]]; then
139139
status_tag="FAIL"
@@ -145,14 +145,55 @@ else
145145
exit_val=0
146146
fi
147147

148+
ASSERTIONS_DIR="$SCRIPT_DIR/tests/assertions"
149+
150+
# Build assertion block
151+
assertion_block="# --- $fixture_name ---
152+
echo \"=== $fixture_name ===\"
153+
run_lint \"$fixture_name\"
154+
assert_exit_code \"$exit_label\" $exit_val
155+
assert_output_contains \"detects $rule_id\" \"\\[${status_tag}\\].*${rule_id}\"
156+
echo \"\""
157+
158+
echo ""
159+
echo "Assertion lines to append:"
148160
echo ""
149-
echo "--- Add the following to your assertion file (tests/assertions/*.sh) ---"
161+
echo "$assertion_block"
150162
echo ""
151-
echo "# --- $fixture_name ---"
152-
echo "echo \"=== $fixture_name ===\""
153-
echo "run_lint \"$fixture_name\""
154-
echo "assert_exit_code \"$exit_label\" $exit_val"
155-
echo "assert_output_contains \"detects $rule_id\" \"\\[${status_tag}\\].*${rule_id}\""
156-
echo "echo \"\""
163+
164+
# List existing assertion files
165+
mapfile -t files < <(ls "$ASSERTIONS_DIR"/*.sh 2>/dev/null | grep -v local-regression.sh | sort)
166+
if [[ ${#files[@]} -eq 0 ]]; then
167+
echo "No existing assertion files found."
168+
else
169+
echo "Existing assertion files:"
170+
for i in "${!files[@]}"; do
171+
printf " %2d) %s\n" "$((i + 1))" "$(basename "${files[$i]}")"
172+
done
173+
fi
174+
echo ""
175+
176+
read -rp "Append to which file? (number, or 'new' to create one, or 'skip' to print only): " choice
177+
178+
if [[ "$choice" == "skip" ]]; then
179+
echo "Skipped — copy the assertion lines above into your test file manually."
180+
elif [[ "$choice" == "new" ]]; then
181+
suggested_name="$(echo "$category" | tr '[:upper:]' '[:lower:]' | tr ' ' '-').sh"
182+
read -rp "Filename [$suggested_name]: " new_name
183+
new_name="${new_name:-$suggested_name}"
184+
[[ "$new_name" == *.sh ]] || new_name="${new_name}.sh"
185+
target="$ASSERTIONS_DIR/$new_name"
186+
echo "" >> "$target"
187+
echo "$assertion_block" >> "$target"
188+
echo "Assertions appended to tests/assertions/$new_name"
189+
elif [[ "$choice" =~ ^[0-9]+$ ]] && (( choice >= 1 && choice <= ${#files[@]} )); then
190+
target="${files[$((choice - 1))]}"
191+
echo "" >> "$target"
192+
echo "$assertion_block" >> "$target"
193+
echo "Assertions appended to tests/assertions/$(basename "$target")"
194+
else
195+
echo "Invalid choice — printing assertion lines for manual copy-paste."
196+
fi
197+
157198
echo ""
158199
echo "--- Done! Run 'bash tests/run-tests.sh' to verify. ---"

tests/run-tests.sh

Lines changed: 5 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -567,96 +567,12 @@ assert_output_not_contains "R-WEB-01 suppressed by next-line" "\[FAIL\].*R-WEB-0
567567
assert_output_not_contains "R-WEB-10 suppressed by inline" "\[FAIL\].*R-WEB-10"
568568
echo ""
569569

570-
# Extended assertion files (sourced by category)
570+
# Extended assertion files (auto-sourced from assertions/ directory)
571571
ASSERTIONS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/assertions"
572-
if [[ -f "$ASSERTIONS_DIR/init-lifecycle-prefs.sh" ]]; then
573-
source "$ASSERTIONS_DIR/init-lifecycle-prefs.sh"
574-
fi
575-
576-
if [[ -f "$ASSERTIONS_DIR/ai-slop-heuristics.sh" ]]; then
577-
source "$ASSERTIONS_DIR/ai-slop-heuristics.sh"
578-
fi
579-
580-
if [[ -f "$ASSERTIONS_DIR/cross-file-resources.sh" ]]; then
581-
source "$ASSERTIONS_DIR/cross-file-resources.sh"
582-
fi
583-
584-
if [[ -f "$ASSERTIONS_DIR/lifecycle-advanced.sh" ]]; then
585-
source "$ASSERTIONS_DIR/lifecycle-advanced.sh"
586-
fi
587-
588-
if [[ -f "$ASSERTIONS_DIR/quality-patterns.sh" ]]; then
589-
source "$ASSERTIONS_DIR/quality-patterns.sh"
590-
fi
591-
592-
if [[ -f "$ASSERTIONS_DIR/version-compat.sh" ]]; then
593-
source "$ASSERTIONS_DIR/version-compat.sh"
594-
fi
595-
596-
if [[ -f "$ASSERTIONS_DIR/slop-patterns-extended.sh" ]]; then
597-
source "$ASSERTIONS_DIR/slop-patterns-extended.sh"
598-
fi
599-
600-
if [[ -f "$ASSERTIONS_DIR/package-security.sh" ]]; then
601-
source "$ASSERTIONS_DIR/package-security.sh"
602-
fi
603-
604-
if [[ -f "$ASSERTIONS_DIR/metadata-init-fixes.sh" ]]; then
605-
source "$ASSERTIONS_DIR/metadata-init-fixes.sh"
606-
fi
607-
608-
if [[ -f "$ASSERTIONS_DIR/trademark-imports.sh" ]]; then
609-
source "$ASSERTIONS_DIR/trademark-imports.sh"
610-
fi
611-
612-
if [[ -f "$ASSERTIONS_DIR/lifecycle-dbus-subprocess.sh" ]]; then
613-
source "$ASSERTIONS_DIR/lifecycle-dbus-subprocess.sh"
614-
fi
615-
616-
if [[ -f "$ASSERTIONS_DIR/i18n-quality-ver50.sh" ]]; then
617-
source "$ASSERTIONS_DIR/i18n-quality-ver50.sh"
618-
fi
619-
620-
if [[ -f "$ASSERTIONS_DIR/settings-async-clipboard.sh" ]]; then
621-
source "$ASSERTIONS_DIR/settings-async-clipboard.sh"
622-
fi
623-
624-
if [[ -f "$ASSERTIONS_DIR/import-segregation.sh" ]]; then
625-
source "$ASSERTIONS_DIR/import-segregation.sh"
626-
fi
627-
628-
if [[ -f "$ASSERTIONS_DIR/donations-overflow.sh" ]]; then
629-
source "$ASSERTIONS_DIR/donations-overflow.sh"
630-
fi
631-
632-
if [[ -f "$ASSERTIONS_DIR/ai-defense-provenance.sh" ]]; then
633-
source "$ASSERTIONS_DIR/ai-defense-provenance.sh"
634-
fi
635-
636-
if [[ -f "$ASSERTIONS_DIR/severity-corrections.sh" ]]; then
637-
source "$ASSERTIONS_DIR/severity-corrections.sh"
638-
fi
639-
640-
if [[ -f "$ASSERTIONS_DIR/version-compat-batch3.sh" ]]; then
641-
source "$ASSERTIONS_DIR/version-compat-batch3.sh"
642-
fi
643-
644-
if [[ -f "$ASSERTIONS_DIR/obfuscation-formatting.sh" ]]; then
645-
source "$ASSERTIONS_DIR/obfuscation-formatting.sh"
646-
fi
647-
648-
if [[ -f "$ASSERTIONS_DIR/widget-lifecycle.sh" ]]; then
649-
source "$ASSERTIONS_DIR/widget-lifecycle.sh"
650-
fi
651-
652-
if [[ -f "$ASSERTIONS_DIR/quick-wins.sh" ]]; then
653-
source "$ASSERTIONS_DIR/quick-wins.sh"
654-
fi
655-
656-
# Local-only assertion files (not committed — for personal regression testing)
657-
if [[ -f "$ASSERTIONS_DIR/local-regression.sh" ]]; then
658-
source "$ASSERTIONS_DIR/local-regression.sh"
659-
fi
572+
for assertion_file in "$ASSERTIONS_DIR"/*.sh; do
573+
[[ -f "$assertion_file" ]] || continue
574+
source "$assertion_file"
575+
done
660576

661577
# --- Summary ---
662578
echo "============================================"

0 commit comments

Comments
 (0)