This document describes how to use the comprehensive test suite for caffeinate-linux.
# Run all tests
./test_caffeinate.sh
# Run tests with verbose output
bash -x ./test_caffeinate.shThe test suite covers the following categories:
- Verifies the caffeinate script exists and is executable
- Help flag (
-h) - Default behavior (no arguments)
-i(idle prevention)-d(display sleep prevention)-s(system sleep prevention - AC only)-u(user activity simulation)-v(verbose output)-t(timeout)
-d -s(display + system sleep prevention)-d -i(display + idle prevention)-s -i(system + idle prevention)-d -s -i(all sleep prevention)-d -s -u(display + system + user activity)- All flags together
- Timeout combinations (
-d -t,-s -t,-d -s -t)
- Execute commands with caffeinate
- Execute with various flags
- Wait for process completion
- Invalid timeout values
- Invalid PID values
- Invalid flags
- Verify verbose logging format
- Test behavior in headless/SSH sessions
- Verify proper resource cleanup on exit
- Verify AC power detection works correctly
The test script provides color-coded output:
- ✅ PASS (green): Test passed successfully
- ❌ FAIL (red): Test failed
⚠️ SKIP (yellow): Test was skipped- ℹ️ INFO (yellow): Informational messages
You can run specific test categories by modifying the script or commenting out sections you don't want to run.
For CI/CD pipelines, use:
# Exit with non-zero status if any test fails
./test_caffeinate.sh
# Continue even if tests fail (for reporting)
bash test_caffeinate.sh || true- Bash 4+ (for the test script)
- Standard Linux utilities (timeout, grep, etc.)
- The same dependencies as caffeinate itself
To add new tests:
- Add test functions following the existing pattern
- Use
run_testfor simple exit code tests - Use
log_pass,log_fail,log_skipfor custom tests - Update the summary section
If tests fail:
- Check the detailed output for specific failures
- Run with
bash -xfor verbose debugging:bash -x ./test_caffeinate.sh
- Check temporary test files in
/tmp/caffeinate_test_*
Update tests when:
- New features are added to caffeinate
- Bug fixes are implemented
- New compatibility requirements are identified
For testing across different environments:
# Test in different desktop environments
XDG_SESSION_TYPE=wayland ./test_caffeinate.sh
XDG_SESSION_TYPE=x11 ./test_caffeinate.sh
# Test in non-GUI environment
XDG_SESSION_TYPE=tty DISPLAY= WAYLAND_DISPLAY= ./test_caffeinate.shAll tests should pass on a properly configured system with:
- systemd
- D-Bus
- X11 or Wayland session
- AC power (for
-sflag tests)
- Total tests: 30
- Expected pass: 29
- Expected skip: 1 (PID waiting test due to race conditions)
- Expected fail: 0
-
PID waiting test: This test is skipped due to race conditions in the test environment. The
-wflag functionality works correctly when tested manually. -
Filesystem restrictions: If running from a
noexecmounted filesystem, scripts must be executed withbash script.shinstead of./script.sh. -
Container environments: Some tests may behave differently in containers without proper D-Bus or systemd support.
For comprehensive testing, also manually verify:
# Test PID waiting functionality (known to work but hard to test automatically)
sleep 10 & bash caffeinate -w $! -v
# Test in different desktop environments
# Test on battery power (if available)
# Test in VMs and containersExample CI configuration:
# GitHub Actions example
- name: Run caffeinate tests
run: bash test_caffeinate_fixed.sh
# GitLab CI example
test:
script:
- bash test_caffeinate_fixed.sh