Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/workflows/shellcheck.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
name: Lint and Test

on:
push:
branches: [ main, 'feat/**' ]
pull_request:
branches: [ main ]

jobs:
shellcheck:
name: ShellCheck and Validation
runs-on: ubuntu-latest
steps:
- name: Check out repository
uses: actions/checkout@v4

- name: Install dependencies
run: |
sudo apt-get update
sudo apt-get install -y shellcheck

- name: Run test suite
run: |
bash tests/test_all.sh
1 change: 1 addition & 0 deletions tests/fixtures/.gitkeep
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Test fixtures directory for automated tests
78 changes: 78 additions & 0 deletions tests/test_all.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/usr/bin/env bash

# Test suite for LinuxScripts repository.
# Discovers all *.sh scripts under the repository and performs basic validation:
# 1. Syntax check (bash -n)
# 2. ShellCheck (if available)
# 3. Ensure executable permission
# 4. Run help output (-h) and expect exit status 0 (if script implements it)

set -euo pipefail

show_help() {
cat <<'EOF'
Usage: test_all.sh [options]

Options:
-h, --help Show this help message and exit

Description:
Discovers all utility scripts in the repository, validates syntax (bash -n),
runs ShellCheck if installed, verifies executable permissions, and tests --help/-h.
EOF
exit 0
}

# Handle options
while (( "$#" )); do
case "$1" in
-h|--help) show_help ;;
*) echo "Unknown option: $1" >&2; exit 1 ;;
esac
shift
done

ROOT_DIR="$(git rev-parse --show-toplevel)"
FAILED=0
SELF="$(realpath "$0")"

while IFS= read -r script; do
# Skip self to prevent recursive execution
if [[ "$(realpath "$script")" == "$SELF" ]]; then
continue
fi
echo "Testing $script"
# 1. Syntax
if ! bash -n "$script"; then
echo " ❌ Syntax error in $script"
FAILED=$((FAILED+1))
continue
fi
# 2. ShellCheck (optional)
if command -v shellcheck >/dev/null 2>&1; then
if ! shellcheck "$script"; then
echo " ❌ ShellCheck warnings/errors in $script"
FAILED=$((FAILED+1))
fi
fi
# 3. Executable permission
if [[ ! -x "$script" ]]; then
echo " ⚠️ Adding executable permission to $script"
chmod +x "$script"
fi
# 4. Help flag test (-h or --help)
if "$script" -h >/dev/null 2>&1 || "$script" --help >/dev/null 2>&1; then
echo " ✅ Help flag works"
else
echo " ❌ Help flag (-h / --help) failed"
FAILED=$((FAILED+1))
fi
done < <(find "$ROOT_DIR/scripts" -type f -name "*.sh" | sort)

if [[ $FAILED -eq 0 ]]; then
echo "All tests passed."
exit 0
else
echo "$FAILED test(s) failed."
exit 1
fi
Loading