This directory contains unit and integration tests for the shell scripts in the embabel-learning workspace.
📖 Important: Read ARCHITECTURE.md before writing tests! It explains how to properly use the configuration system and avoid hardcoding values.
📖 Important: Read ARCHITECTURE.md before writing tests! It explains how to properly use the configuration system and avoid hardcoding values.
test/
├── README.md # This file
├── run-tests.sh # Main test runner
├── helpers/
│ └── test-framework.sh # Simple test framework (self-contained)
├── unit/
│ ├── test-config-loader.sh # Tests for config-loader.sh
│ ├── test-safety-checks.sh # Tests for safety-checks.sh
│ └── test-sync-discord.sh # Tests for discord-sync/sync-discord.sh
└── integration/
└── (future integration tests)
We use a self-contained, lightweight test framework inspired by shunit2. No external dependencies required!
assertTrue "message" command- Asserts command succeedsassertFalse "message" command- Asserts command failsassertEquals expected actual "message"- Asserts equalityassertNotEquals expected actual "message"- Asserts inequalityassertContains haystack needle "message"- Asserts substring existsassertNotContains haystack needle "message"- Asserts substring doesn't existassertFileExists file "message"- Asserts file existsassertFileNotExists file "message"- Asserts file doesn't existassertDirectoryExists dir "message"- Asserts directory exists
cd test
./run-tests.shcd test/unit
bash test-config-loader.shThe test framework shows detailed output by default. To see more details, check the test file directly.
- How to use the configuration system (never hardcode values!)
- How
TEST_UPSTREAM_ORGworks - Best practices for test structure
#!/bin/bash
# Unit tests for my-script.sh
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
TEST_FRAMEWORK="$SCRIPT_DIR/../helpers/test-framework.sh"
source "$TEST_FRAMEWORK"
# Setup function (runs before each test)
setUp() {
# Create test environment
TEST_DIR="/tmp/test-$$"
mkdir -p "$TEST_DIR"
}
# Teardown function (runs after each test)
tearDown() {
# Cleanup
rm -rf "$TEST_DIR"
}
# Test functions (must start with "test")
testMyFunctionWorks() {
# Your test code here
assertTrue "Function should work" my_function
}
testMyFunctionFails() {
assertFalse "Function should fail with bad input" my_function "bad_input"
}
# Run tests if executed directly
if [ "${0##*/}" = "test-my-script.sh" ]; then
resetCounters
runTests "$0"
fi- Load test framework - Source
test-framework.sh - Set up test environment - Use
setUp()function - Write test functions - Functions starting with
test - Clean up - Use
tearDown()function - Run tests - Call
runTestsif executed directly
- ✅ config-loader.sh - Configuration loading, defaults, warnings
- ✅ safety-checks.sh - Commit/push blocking, repo detection
- ✅ sync-discord.sh - Discord sync script argument parsing, validation, path resolution
-
monitor-embabel.sh- Monitoring functionality -
sync-upstream.sh- Sync operations -
view-pr.sh- PR viewing -
list-embabel-repos.sh- Repository listing - Integration tests for common workflows
Tests use temporary directories (/tmp/embabel-learning-test-*) to avoid polluting the actual workspace. All test directories are cleaned up automatically.
- Each test file runs in its own subshell
- Test directories are created with unique IDs (
$$) - Cleanup happens automatically in
tearDown()
To run tests in CI/CD:
# Install dependencies (none required!)
# Run tests
cd test && ./run-tests.shThe test framework is self-contained and doesn't require any external tools.
When adding new scripts:
- Write tests first (TDD approach) or
- Add tests alongside the script
- Update this README with test coverage
- Ensure all tests pass before committing
Solution: Make test files executable:
chmod +x test/**/*.shSolution: Make sure you're running from the test directory:
cd test
./run-tests.shSolution: Ensure test-framework.sh is executable and in helpers/:
ls -l test/helpers/test-framework.sh
chmod +x test/helpers/test-framework.shThe test framework (test-framework.sh) provides:
- Assertion functions - Various assert functions for testing
- Test statistics - Automatic counting of passed/failed tests
- Color output - Green for pass, red for fail
- Summary reporting - Automatic summary at end
- Error handling - Proper exit codes for CI/CD
No external dependencies - just pure bash!