Skip to content

GitHub Action to Publish Docs #102

GitHub Action to Publish Docs

GitHub Action to Publish Docs #102

Workflow file for this run

name: Run Modified Operator Tests
on:
pull_request:
branches:
- main
- development
- hotfix
paths:
- 'operators/**'
- '.github/workflows/operator-tests.yml'
jobs:
test-operators:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Install uv
uses: astral-sh/setup-uv@v5
- name: Print uv version and cache dir
run: |
uv --version
uv cache dir
which python
- name: Detect changes in operators
id: check-changes
run: |
# Get the base branch (target branch of the PR)
BASE_BRANCH=${{ github.base_ref }}
echo "Base branch: $BASE_BRANCH"
# Find changed files in operators directory
CHANGED_FILES=$(git diff --name-only origin/$BASE_BRANCH...HEAD operators/)
echo "Changed files:"
echo "$CHANGED_FILES" | sed 's/^/ /' # This indents each file for better readability
# If no files changed, set output to false, otherwise true
if [ -z "$CHANGED_FILES" ]; then
echo "No files were changed in the operators directory"
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "changed_files=" >> $GITHUB_OUTPUT
else
echo "Changes detected in operators directory"
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "changed_files<<EOF" >> $GITHUB_OUTPUT
echo "$CHANGED_FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
fi
- name: Create and activate virtual environment and install Feluda
if: steps.check-changes.outputs.has_changes == 'true'
run: |
uv venv
source .venv/bin/activate
uv pip install ".[all]"
uv pip install ".[dev]"
echo "Installing Operator system level packages"
echo "Installing ffmpeg and tesseract-ocr..."
sudo apt-get update > /dev/null
sudo apt-get install -y ffmpeg > /dev/null
sudo apt-get install -y tesseract-ocr tesseract-ocr-hin tesseract-ocr-tam tesseract-ocr-tel > /dev/null
echo "ffmpeg version:"
ffmpeg -version
echo "tesseract version:"
tesseract --version
- name: Run tests for changed operators
if: steps.check-changes.outputs.has_changes == 'true'
run: |
# Get the changed files from previous step
CHANGED_FILES="${{ steps.check-changes.outputs.changed_files }}"
# Print current working directory for context
echo "Current working directory: $(pwd)"
# Initialize a variable to track if we found any operators to test
FOUND_TESTS=false
# Loop through each operator directory and check if it contains changes
for OPERATOR_DIR in operators/*/; do
echo "Checking operator directory: $OPERATOR_DIR"
OPERATOR_NAME=$(basename "$OPERATOR_DIR")
if echo "$CHANGED_FILES" | grep -q "$OPERATOR_NAME"; then
echo "✓ Changes detected in operator: $OPERATOR_NAME"
TEST_FILE="${OPERATOR_DIR}test.py"
if [ -f "$TEST_FILE" ]; then
echo "✓ Found test.py in: ${OPERATOR_DIR}"
echo "→ Running tests for: $(basename $OPERATOR_DIR)"
echo "→ Current directory before running tests: $(pwd)"
echo "✓ Trying unittest"
set +e
uv pip install "$OPERATOR_DIR"
UNITTEST_OUTPUT=$(uv run -m unittest "$TEST_FILE" 2>&1)
UNITTEST_EXIT_CODE=$?
echo "$UNITTEST_OUTPUT"
set -e
if echo "$UNITTEST_OUTPUT" | grep -qE "Ran 0 tests|no tests found|No module named|AttributeError"; then
echo "⚠️ Unittest ran but found no tests — falling back to pytest"
uv pip install pytest
uv run pytest "$TEST_FILE"
else
echo "✓ Tests succeeded for $OPERATOR_NAME"
fi
FOUND_TESTS=true
else
echo "✗ No test.py found in: $OPERATOR_DIR"
fi
else
echo "✗ No changes detected in: $OPERATOR_NAME"
fi
done
# Final status report
echo "----------------------------------------"
echo "Final Status Report:"
if [ "$FOUND_TESTS" = false ]; then
echo "✗ No test.py files were found in changed operator directories"
else
echo "✓ Tests were executed for changed operators"
fi
echo "----------------------------------------"
# This ensures cleanup runs even if previous steps fail
- name: Cleanup
if: always()
run: |
echo "----------------------------------------"
echo "Starting cleanup process..."
echo "----------------------------------------"
# Check and print uv cache
echo "Cleaning UV cache..."
uv cache dir
uv cache prune
# uv cache clean
# Check and remove virtual environment
echo "----------------------------------------"
echo "Virtual Environment:"
if [ -d ".venv" ]; then
echo "Virtual environment exists!"
echo "Removing virtual environment..."
rm -rf .venv/
else
echo "No virtual environment found at .venv/"
fi
# Check and remove PyTorch cache
echo "----------------------------------------"
echo "PyTorch Cache:"
if [ -d ~/.cache/torch/hub ]; then
echo "PyTorch cache exists, contents:"
ls -la ~/.cache/torch/hub
echo "Removing PyTorch cache..."
rm -rf ~/.cache/torch/hub
else
echo "No PyTorch cache found at ~/.cache/torch/hub"
fi
# Check and remove Hugging Face cache
echo "----------------------------------------"
echo "Hugging Face Cache:"
if [ -d ~/.cache/huggingface/hub ]; then
echo "Hugging Face cache exists, contents:"
ls -la ~/.cache/huggingface/hub
echo "Removing Hugging Face cache..."
rm -rf ~/.cache/huggingface/hub
else
echo "No Hugging Face cache found at ~/.cache/huggingface/hub"
fi
# Remove ffmpeg
echo "----------------------------------------"
echo "Removing ffmpeg..."
sudo apt-get remove -y ffmpeg > /dev/null
sudo apt-get autoremove -y > /dev/null
echo "FFmpeg removed"
# Remove tesseract-ocr
echo "----------------------------------------"
echo "Removing tesseract-ocr..."
sudo apt-get remove -y tesseract-ocr tesseract-ocr-hin tesseract-ocr-tam tesseract-ocr-tel > /dev/null
sudo apt-get autoremove -y > /dev/null
echo "Tesseract removed"
echo "----------------------------------------"
echo "Cleanup completed"
echo "----------------------------------------"