algo/scripts/test-templates.sh
Dan Guido cddb5df395
Add comprehensive pre-commit hooks for code quality (#14831)
* Add comprehensive pre-commit hooks for code quality

- Set up pre-commit framework with hooks for Python, YAML, Ansible, and shell
- Configure ruff for Python linting and formatting
- Add yamllint for YAML validation
- Include ansible-lint and syntax checks
- Add shellcheck for shell scripts
- Create development documentation
- Auto-fix trailing whitespace and file endings

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* Remove redundant DEVELOPMENT.md and update CONTRIBUTING.md

- Removed docs/DEVELOPMENT.md as it was redundant with existing documentation
- Added pre-commit hooks setup instruction to CONTRIBUTING.md for contributors
- Consolidated development guidance into a single location

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-09-15 10:03:46 -04:00

84 lines
2.6 KiB
Bash
Executable file
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/bin/bash
# Test all Jinja2 templates in the Algo codebase
# This script is called by CI and can be run locally
set -e
echo "======================================"
echo "Running Jinja2 Template Tests"
echo "======================================"
echo ""
# Color codes for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color
FAILED=0
# 1. Run the template syntax validator
echo "1. Validating Jinja2 template syntax..."
echo "----------------------------------------"
if python tests/validate_jinja2_templates.py; then
echo -e "${GREEN}✓ Template syntax validation passed${NC}"
else
echo -e "${RED}✗ Template syntax validation failed${NC}"
FAILED=$((FAILED + 1))
fi
echo ""
# 2. Run the template rendering tests
echo "2. Testing template rendering..."
echo "--------------------------------"
if python tests/unit/test_template_rendering.py; then
echo -e "${GREEN}✓ Template rendering tests passed${NC}"
else
echo -e "${RED}✗ Template rendering tests failed${NC}"
FAILED=$((FAILED + 1))
fi
echo ""
# 3. Run the StrongSwan template tests
echo "3. Testing StrongSwan templates..."
echo "----------------------------------"
if python tests/unit/test_strongswan_templates.py; then
echo -e "${GREEN}✓ StrongSwan template tests passed${NC}"
else
echo -e "${RED}✗ StrongSwan template tests failed${NC}"
FAILED=$((FAILED + 1))
fi
echo ""
# 4. Run ansible-lint with Jinja2 checks enabled
echo "4. Running ansible-lint Jinja2 checks..."
echo "----------------------------------------"
# Check only for jinja[invalid] errors, not spacing warnings
if ansible-lint --nocolor 2>&1 | grep -E "jinja\[invalid\]"; then
echo -e "${RED}✗ ansible-lint found Jinja2 syntax errors${NC}"
ansible-lint --nocolor 2>&1 | grep -E "jinja\[invalid\]" | head -10
FAILED=$((FAILED + 1))
else
echo -e "${GREEN}✓ No Jinja2 syntax errors found${NC}"
# Show spacing warnings as info only
if ansible-lint --nocolor 2>&1 | grep -E "jinja\[spacing\]" | head -1 > /dev/null; then
echo -e "${YELLOW} Note: Some spacing style issues exist (not failures)${NC}"
fi
fi
echo ""
# Summary
echo "======================================"
if [ $FAILED -eq 0 ]; then
echo -e "${GREEN}All template tests passed!${NC}"
exit 0
else
echo -e "${RED}$FAILED test suite(s) failed${NC}"
echo ""
echo "To debug failures, run individually:"
echo " python tests/validate_jinja2_templates.py"
echo " python tests/unit/test_template_rendering.py"
echo " python tests/unit/test_strongswan_templates.py"
echo " ansible-lint"
exit 1
fi