Last active
October 24, 2024 05:27
-
-
Save mdalvi/2659e69ca2b876ebc67b33244652353e to your computer and use it in GitHub Desktop.
Setup Codecov or Your Project
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# ========================================== | |
# Code generated using Claude Sonnet 3.5 | |
# Command to use the file: curl -s https://gist.githubusercontent.com/mdalvi/2659e69ca2b876ebc67b33244652353e/raw/c9fe7e2afa9ff4041c52ccce82023f3d8cc5a0ee/setup-codecov.sh | bash | |
# ========================================== | |
# setup-codecov.sh | |
set -e # Exit on error | |
echo "π Setting up Codecov integration..." | |
# 1. Create necessary directories | |
echo "π Creating directories..." | |
mkdir -p .github/workflows | |
# 2. Add dev dependencies | |
echo "π¦ Adding test dependencies to Poetry..." | |
poetry add --group dev pytest pytest-cov | |
# 3. Create GitHub Actions workflow | |
echo "βοΈ Creating GitHub Actions workflow..." | |
cat > .github/workflows/codecov.yml << 'EOL' | |
name: CodeCov Coverage | |
on: | |
push: | |
branches: [ main, master ] | |
pull_request: | |
branches: [ main, master ] | |
jobs: | |
coverage: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up Python | |
uses: actions/setup-python@v4 | |
with: | |
python-version: '3.x' | |
- name: Install Poetry | |
run: | | |
curl -sSL https://install.python-poetry.org | python3 - | |
- name: Configure Poetry | |
run: | | |
poetry config virtualenvs.create true | |
poetry config virtualenvs.in-project true | |
- name: Install dependencies | |
run: | | |
poetry install --with dev | |
- name: Run tests with coverage | |
run: | | |
poetry run pytest --cov=./ --cov-report=xml | |
- name: Upload coverage to Codecov | |
uses: codecov/codecov-action@v3 | |
with: | |
token: ${{ secrets.CODECOV_TOKEN }} | |
files: ./coverage.xml | |
fail_ci_if_error: true | |
EOL | |
# 4. Create basic pytest configuration in pyproject.toml if it doesn't exist | |
echo "π§ Adding pytest configuration..." | |
if ! grep -q "\[tool.pytest.ini_options\]" pyproject.toml; then | |
echo " | |
[tool.pytest.ini_options] | |
addopts = \"--cov=./ --cov-report=xml --cov-report=term-missing\" | |
testpaths = [\"tests\"]" >> pyproject.toml | |
fi | |
# 5. Create basic .gitignore if it doesn't exist or append to it | |
echo "π Updating .gitignore..." | |
for item in "coverage.xml" ".coverage" "htmlcov/"; do | |
if ! grep -q "^${item}$" .gitignore 2>/dev/null; then | |
echo "$item" >> .gitignore | |
fi | |
done | |
# 6. Create basic tests directory and sample test | |
echo "π§ͺ Creating sample test directory..." | |
mkdir -p tests | |
if [ ! -f tests/__init__.py ]; then | |
touch tests/__init__.py | |
fi | |
if [ ! -f tests/test_sample.py ]; then | |
cat > tests/test_sample.py << 'EOL' | |
def test_sample(): | |
assert True | |
EOL | |
fi | |
echo "β Setup complete! Next steps:" | |
echo "1. Create a Codecov account at https://codecov.io" | |
echo "2. Get your CODECOV_TOKEN" | |
echo "3. Add CODECOV_TOKEN to your GitHub repository secrets" | |
echo "4. Push your changes to GitHub" | |
echo "5. Check the Actions tab to see your workflow run" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment