Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Misc ci updates #350

Merged
merged 4 commits into from
Nov 24, 2024
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
84 changes: 84 additions & 0 deletions .github/scripts/average_test_durations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# Description: This script averages the test durations from all the artifacts
import glob
import json
import os
import subprocess
from collections import defaultdict


# Function to collect test names using pytest
def collect_tests_with_pytest():
# Run pytest with --collect-only to get the list of test cases
result = subprocess.run(["pytest", "--collect-only"], capture_output=True, text=True, check=False)

collected_tests = defaultdict(list)

# Parse the output to organize tests under their respective modules
for line in result.stdout.splitlines():
if line.startswith("tests/"):
collected_tests[line] = 0

return collected_tests


# Consolidate durations from existing artifacts
def consolidate_durations():
durations = defaultdict(lambda: {"total_duration": 0, "count": 0})

# Iterate over all downloaded duration artifacts
for folder in glob.glob("test-durations-*"):
# The path to the duration file in each directory
duration_file_path = os.path.join(folder, ".pytest-split-durations")

if os.path.isfile(duration_file_path):
with open(duration_file_path) as f:
data = json.load(f)
for test, duration in data.items():
durations[test]["total_duration"] += duration
durations[test]["count"] += 1

# Calculate the average duration for each test
return {test: info["total_duration"] / info["count"] for test, info in durations.items()}


# Define the path to the consolidated durations file
CONSOLIDATED_FILE = "tests/test_data/.pytest-split-durations"


# Main script logic
def main():
# Collect tests grouped by modules using pytest
collected_tests = collect_tests_with_pytest()

# Consolidate durations from artifacts
consolidated_durations = consolidate_durations()

# Merge and update with consolidated durations
updated_durations = {}
for test, duration in collected_tests.items():
# Update average test durations and exclude test which not exists (can happen on renaming or removing tests)
if test in consolidated_durations:
updated_durations[test] = consolidated_durations[test]

# Load the existing durations file if it exists
existing_durations = {}
if os.path.isfile(CONSOLIDATED_FILE):
with open(CONSOLIDATED_FILE) as f:
existing_durations = json.load(f)

# Sort the keys to compare the tests in both dictionaries
updated_durations_key = sorted(updated_durations.keys())
existing_durations_key = sorted(existing_durations.keys())

# Check if all keys in updated_durations are in existing_durations
if updated_durations_key == existing_durations_key:
print("No new tests detected; durations file remains unchanged.")
else:
# Write the updated durations to the consolidated file
with open(CONSOLIDATED_FILE, "w") as f:
json.dump(updated_durations, f, indent=4)
print("New tests detected; updated the durations file.")


if __name__ == "__main__":
main()
4 changes: 2 additions & 2 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ jobs:
docs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/setup-python@v3
- uses: actions/checkout@v4
- uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Install dependencies
Expand Down
59 changes: 58 additions & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,16 @@ on:
pull_request:
branches: [ main ]

permissions:
contents: write
pull-requests: write


jobs:
lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: actions/setup-python@v4
with:
Expand Down Expand Up @@ -73,6 +78,14 @@ jobs:
overwrite: false
path: ./.coverage

- name: Upload test durations artifact
if: matrix.python-version == '3.10'
uses: actions/upload-artifact@v3
with:
name: test-durations-${{ matrix.python-version }}-${{ matrix.split }}
include-hidden-files: true
path: ./tests/test_data/.pytest-split-durations

coverage:
needs: test
runs-on: ubuntu-latest
Expand Down Expand Up @@ -105,6 +118,50 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
slug: JaGeo/LobsterPy

commit-durations:
if: github.repository_owner == 'JaGeo' && github.ref == 'refs/heads/main'
needs: test
runs-on: ubuntu-latest
defaults:
run:
shell: bash -l {0} # enables conda/mamba env activation by reading bash profile

steps:

- name: Check out repo
uses: actions/checkout@v4
- name: Set up micromamba
uses: mamba-org/setup-micromamba@main
- name: Create mamba environment
run: |
micromamba create -n lobpy_test python=3.10 --yes
- name: Install uv
run: micromamba run -n lobpy_test pip install uv
- name: Install lobsterpy and dependencies
run: |
micromamba activate lobpy_test
uv pip install --upgrade pip
uv pip install -e .[tests,featurizer]

- name: Download test duration artifacts
uses: actions/download-artifact@v4

- name: Compute average of test durations
run: |
micromamba activate lobpy_test
python3 .github/scripts/average_test_durations.py
rm -rf test-durations-*

- name: Create Pull Request to push consolidated test durations
uses: peter-evans/create-pull-request@v7
with:
commit-message: update test durations
title: Update test durations file
body: Auto updated test durations file
branch: update-test-durations
delete-branch: true
base: main

docs:
runs-on: ubuntu-latest

Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/python-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- name: Set up Python
uses: actions/setup-python@v4
with:
Expand Down
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
exclude: ^(docs|examples|tests/test_data)
exclude: ^(docs|examples|tests|.github)

ci:
autoupdate_schedule: monthly
Expand Down
Loading