refactor(content-guards): replace fragile bash expansion with explicit branching#40
refactor(content-guards): replace fragile bash expansion with explicit branching#40JacobPEvans merged 3 commits intomainfrom
Conversation
…t branching The previous fix (PR #39, commit 2202144) used ${config_flag[@]+"${config_flag[@]}"} to work around bash 3.2's unbound variable error with empty arrays. While functional, this expansion is: - Hard to read and understand - Easy to get wrong (even the commit message described it backwards) - Fragile across bash versions This change replaces the arcane parameter expansion with explicit conditional branching: - Check array length with ${#config_flag[@]} (works correctly under set -u) - Separate code paths for empty/non-empty arrays - Clear, maintainable, and works on all bash versions including macOS bash 3.2 Verified with /bin/bash (bash 3.2) under set -euo pipefail with both code paths. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
There was a problem hiding this comment.
Important
Looks good to me! 👍
Reviewed everything up to 9a61fbb in 5 seconds. Click for details.
- Reviewed
18lines of code in1files - Skipped
0files when reviewing. - Skipped posting
0draft comments. View those below. - Modify your settings and rules to customize what types of comments Ellipsis leaves. And don't forget to react with 👍 or 👎 to teach Ellipsis.
Workflow ID: wflow_pMswbHJ2tSuW8c3c
You can customize by changing your verbosity settings, reacting with 👍 or 👎, replying to comments, or adding code review rules.
Summary of ChangesHello @JacobPEvans, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request refactors the validate-markdown.sh script to enhance its robustness and readability. It replaces an obscure bash parameter expansion used for conditionally passing arguments to markdownlint-cli2 with clear, explicit conditional logic. This change not only improves the script's maintainability but also provides a more reliable solution for an "unbound variable" error encountered in bash 3.2, ensuring consistent behavior across different bash versions. Highlights
Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request is a solid improvement, replacing a hard-to-read bash parameter expansion with a clear conditional statement. This significantly enhances the script's readability and maintainability, and also fixes a bug on older bash versions. I've added one suggestion to further refine the implementation by using an arguments array, which makes the code a bit more concise and avoids duplicating the command being called.
There was a problem hiding this comment.
Pull request overview
This PR refactors the markdown validation script to replace the arcane bash array expansion syntax ${config_flag[@]+"${config_flag[@]}"} (from PR #39) with explicit conditional branching. The goal is to improve code readability and maintainability while maintaining compatibility with bash 3.2's set -u strict mode.
Changes:
- Replaces single-line conditional command execution with explicit if-else branching for array-dependent arguments
- Separates command execution from exit code checking into two distinct steps
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Add comprehensive automated tests using bats-core to prevent regressions in the markdown validator hook, specifically targeting the unbound variable issue fixed in PR #39. Test coverage: - File type filtering (non-markdown, missing, dotfiles) - Config resolution (project vs fallback) - Cross-repo editing scenarios (CWD != file's directory) - Unbound variable regression prevention - Empty JSON input handling This is the first automated test suite in the Claude Code plugin ecosystem. Structure: - tests/content-guards/markdown-validator/validate-markdown.bats (8 tests) - tests/content-guards/markdown-validator/fixtures/ (test data) - content-guards/README.md updated with testing documentation All 8 tests pass successfully. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
…tion
Addresses review feedback from copilot-pull-request-reviewer and gemini-code-assist.
The previous implementation incorrectly checked $? after the if-else block,
which captured the exit code of the conditional test [[ ${#config_flag[@]} -gt 0 ]]
instead of the markdownlint-cli2 command. This meant:
- If array check was true (exit 0), errors wouldn't be captured even if markdownlint failed
- If array check was false (exit 1), errors would be incorrectly captured even if markdownlint succeeded
Fixed by using the `if ! command_output=$(command ...)` pattern consistently,
matching the cspell implementation on line 120. While this introduces some
duplication, it's the most reliable approach for bash 3.2 with set -u.
Verified with bats test suite - all 8 tests pass.
Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
|
@gemini-code-assist @copilot-pull-request-reviewer Thank you both for the excellent review feedback! I've implemented the fix suggested by copilot-pull-request-reviewer that addresses both concerns: Critical bug fixed: The Why not the args array approach: While @gemini-code-assist's suggestion was elegant, it unfortunately triggers the same Solution: Kept the explicit if-else branching as suggested by @copilot-pull-request-reviewer. While this has some duplication, it's the most reliable approach for bash 3.2 compatibility and matches the proven pattern used for cspell validation. All 8 automated tests pass ✅ Latest commit: 0469b22 |
|
Thank you, @JacobPEvans, for the detailed explanation and for thoroughly testing the different approaches! I appreciate you clarifying the |
Summary
${config_flag[@]+"${config_flag[@]}"}expansion with clear conditional branchingBackground
PR #39 (commit
2202144) fixed theconfig_flag[@]: unbound variableerror using the bash workaround${arr[@]+"${arr[@]}"}. While this works, it has significant drawbacks:Changes
Before (fragile):
After (robust):
Why This is Better
${#arr[@]}works correctly with empty arrays underset -uon all bash versionsTest Suite (NEW)
Added the first automated test suite in the Claude Code plugin ecosystem using bats-core:
Test coverage:
Run with:
bats tests/content-guards/markdown-validator/validate-markdown.batsAll 8 tests pass ✅
Test Plan
Verified on macOS with
/bin/bash(bash 3.2) underset -euo pipefail:Related Issues
Related to #39 - same bug, better fix
🤖 Generated with Claude Code