Testing Workflow
Syntax validation
Check for syntax errors without executing the script:This performs a dry-run that only parses the script without executing it.
Static analysis
Use ShellCheck to find common issues and anti-patterns:ShellCheck catches issues like:
- Unquoted variables
- Useless cats and pipes
- Missing error handling
- Deprecated syntax
Debug mode testing
Run the script in debug mode to see what it would do:Debug mode should:
- Show what would be executed
- Print debug messages
- Not make actual changes
Syntax Validation
Thebash -n command checks syntax without execution:
What it catches:
- Missing quotes or brackets
- Unclosed strings or heredocs
- Invalid command syntax
- Incorrect function definitions
Example:
Run
bash -n after any changes to catch syntax errors immediately.Static Analysis with ShellCheck
ShellCheck is a static analysis tool that finds bugs and style issues:Installation
Basic usage:
Example output:
Common issues ShellCheck finds:
Unquoted variables:Ignoring specific warnings:
If you need to ignore a warning:Only disable warnings when you understand why they’re triggered and know it’s safe to ignore them.
Debug Mode Testing
All scripts should support debug mode via the--debug flag:
Implementation:
Testing with debug mode:
Debug function:
The template includes a debug function that only prints in debug mode:Manual Testing
Test Cases
Create a checklist of scenarios to test:- Happy path - Script works with valid inputs
- Missing arguments - Script handles missing required arguments
- Invalid arguments - Script rejects invalid options
- File not found - Script handles missing files gracefully
- Permission denied - Script handles permission errors
- Edge cases - Empty inputs, large files, special characters
Example test script:
Interactive Testing
For interactive scripts (like gdrive_ingest.sh), test:UI elements:
- Arrow key navigation works
- Enter/selection works
- Quit/cancel works
- Cursor visibility (hidden during UI, shown after)
Progress indicators:
- Progress bars update correctly
- Percentages are accurate
- Cleanup happens on interruption
Error recovery:
- Ctrl+C handling
- Cleanup on exit
- Cursor restoration
Testing Traps and Cleanup
Test that cleanup happens correctly:- Normal exit - cleanup runs
- Ctrl+C (SIGINT) - cleanup runs
- Script error - cleanup runs
- Kill signal (SIGTERM) - cleanup runs
Testing Error Handling
Verify error handling works correctly:Test with invalid inputs:
Test error propagation:
Testing Output
Verify color codes:
Test stderr vs stdout:
Test with no color support:
Continuous Testing
Integrate testing into your workflow:Pre-commit hook:
Create.git/hooks/pre-commit:
Testing Checklist
Before considering a script complete:- Syntax validated with
bash -n - Static analysis with
shellcheck - Help flag displays correctly
- Debug mode works without making changes
- Error messages are clear and actionable
- Missing arguments handled gracefully
- Invalid arguments rejected with error
- File operations handle missing files
- Cleanup runs on exit/interruption
- Colors display correctly
- No hardcoded paths (use variables)
- Dependencies validated on startup
Common Issues and Solutions
Issue: Script works in terminal but fails in cron
Solution: Source the full environment or use absolute pathsIssue: Variables with spaces break commands
Solution: Always quote variablesIssue: Pipeline failures not caught
Solution: Useset -o pipefail
Tools and Resources
Essential tools:
- bash -n - Syntax validation (built-in)
- ShellCheck - Static analysis (shellcheck.net)
- bashdb - Bash debugger for complex scripts
- shellspec - Testing framework for bash
Online resources:
- ShellCheck online - Test snippets in browser
- Bash Guide - Comprehensive bash reference
- Google Shell Style Guide - Best practices
Next Steps
- Review Script Standards for coding conventions
- See Creating Scripts to build new scripts
- Study existing scripts in the repository for real-world examples