Skip to content

Commit 80d3973

Browse files
authored
Merge pull request #350 from naik-aakash/ci_updates
Misc ci updates
2 parents 4c6d25b + 4a4ad31 commit 80d3973

File tree

5 files changed

+146
-5
lines changed

5 files changed

+146
-5
lines changed
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Description: This script averages the test durations from all the artifacts
2+
import glob
3+
import json
4+
import os
5+
import subprocess
6+
from collections import defaultdict
7+
8+
9+
# Function to collect test names using pytest
10+
def collect_tests_with_pytest():
11+
# Run pytest with --collect-only to get the list of test cases
12+
result = subprocess.run(["pytest", "--collect-only"], capture_output=True, text=True, check=False)
13+
14+
collected_tests = defaultdict(list)
15+
16+
# Parse the output to organize tests under their respective modules
17+
for line in result.stdout.splitlines():
18+
if line.startswith("tests/"):
19+
collected_tests[line] = 0
20+
21+
return collected_tests
22+
23+
24+
# Consolidate durations from existing artifacts
25+
def consolidate_durations():
26+
durations = defaultdict(lambda: {"total_duration": 0, "count": 0})
27+
28+
# Iterate over all downloaded duration artifacts
29+
for folder in glob.glob("test-durations-*"):
30+
# The path to the duration file in each directory
31+
duration_file_path = os.path.join(folder, ".pytest-split-durations")
32+
33+
if os.path.isfile(duration_file_path):
34+
with open(duration_file_path) as f:
35+
data = json.load(f)
36+
for test, duration in data.items():
37+
durations[test]["total_duration"] += duration
38+
durations[test]["count"] += 1
39+
40+
# Calculate the average duration for each test
41+
return {test: info["total_duration"] / info["count"] for test, info in durations.items()}
42+
43+
44+
# Define the path to the consolidated durations file
45+
CONSOLIDATED_FILE = "tests/test_data/.pytest-split-durations"
46+
47+
48+
# Main script logic
49+
def main():
50+
# Collect tests grouped by modules using pytest
51+
collected_tests = collect_tests_with_pytest()
52+
53+
# Consolidate durations from artifacts
54+
consolidated_durations = consolidate_durations()
55+
56+
# Merge and update with consolidated durations
57+
updated_durations = {}
58+
for test, duration in collected_tests.items():
59+
# Update average test durations and exclude test which not exists (can happen on renaming or removing tests)
60+
if test in consolidated_durations:
61+
updated_durations[test] = consolidated_durations[test]
62+
63+
# Load the existing durations file if it exists
64+
existing_durations = {}
65+
if os.path.isfile(CONSOLIDATED_FILE):
66+
with open(CONSOLIDATED_FILE) as f:
67+
existing_durations = json.load(f)
68+
69+
# Sort the keys to compare the tests in both dictionaries
70+
updated_durations_key = sorted(updated_durations.keys())
71+
existing_durations_key = sorted(existing_durations.keys())
72+
73+
# Check if all keys in updated_durations are in existing_durations
74+
if updated_durations_key == existing_durations_key:
75+
print("No new tests detected; durations file remains unchanged.")
76+
else:
77+
# Write the updated durations to the consolidated file
78+
with open(CONSOLIDATED_FILE, "w") as f:
79+
json.dump(updated_durations, f, indent=4)
80+
print("New tests detected; updated the durations file.")
81+
82+
83+
if __name__ == "__main__":
84+
main()

.github/workflows/docs.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ jobs:
1515
docs:
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: actions/checkout@v3
19-
- uses: actions/setup-python@v3
18+
- uses: actions/checkout@v4
19+
- uses: actions/setup-python@v4
2020
with:
2121
python-version: "3.10"
2222
- name: Install dependencies

.github/workflows/python-package.yml

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,16 @@ on:
99
pull_request:
1010
branches: [ main ]
1111

12+
permissions:
13+
contents: write
14+
pull-requests: write
15+
16+
1217
jobs:
1318
lint:
1419
runs-on: ubuntu-latest
1520
steps:
16-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
1722

1823
- uses: actions/setup-python@v4
1924
with:
@@ -73,6 +78,14 @@ jobs:
7378
overwrite: false
7479
path: ./.coverage
7580

81+
- name: Upload test durations artifact
82+
if: matrix.python-version == '3.10'
83+
uses: actions/upload-artifact@v3
84+
with:
85+
name: test-durations-${{ matrix.python-version }}-${{ matrix.split }}
86+
include-hidden-files: true
87+
path: ./tests/test_data/.pytest-split-durations
88+
7689
coverage:
7790
needs: test
7891
runs-on: ubuntu-latest
@@ -105,6 +118,50 @@ jobs:
105118
token: ${{ secrets.CODECOV_TOKEN }}
106119
slug: JaGeo/LobsterPy
107120

121+
commit-durations:
122+
if: github.repository_owner == 'JaGeo' && github.ref == 'refs/heads/main'
123+
needs: test
124+
runs-on: ubuntu-latest
125+
defaults:
126+
run:
127+
shell: bash -l {0} # enables conda/mamba env activation by reading bash profile
128+
129+
steps:
130+
131+
- name: Check out repo
132+
uses: actions/checkout@v4
133+
- name: Set up micromamba
134+
uses: mamba-org/setup-micromamba@main
135+
- name: Create mamba environment
136+
run: |
137+
micromamba create -n lobpy_test python=3.10 --yes
138+
- name: Install uv
139+
run: micromamba run -n lobpy_test pip install uv
140+
- name: Install lobsterpy and dependencies
141+
run: |
142+
micromamba activate lobpy_test
143+
uv pip install --upgrade pip
144+
uv pip install -e .[tests,featurizer]
145+
146+
- name: Download test duration artifacts
147+
uses: actions/download-artifact@v4
148+
149+
- name: Compute average of test durations
150+
run: |
151+
micromamba activate lobpy_test
152+
python3 .github/scripts/average_test_durations.py
153+
rm -rf test-durations-*
154+
155+
- name: Create Pull Request to push consolidated test durations
156+
uses: peter-evans/create-pull-request@v7
157+
with:
158+
commit-message: update test durations
159+
title: Update test durations file
160+
body: Auto updated test durations file
161+
branch: update-test-durations
162+
delete-branch: true
163+
base: main
164+
108165
docs:
109166
runs-on: ubuntu-latest
110167

.github/workflows/python-publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
runs-on: ubuntu-latest
1919

2020
steps:
21-
- uses: actions/checkout@v3
21+
- uses: actions/checkout@v4
2222
- name: Set up Python
2323
uses: actions/setup-python@v4
2424
with:

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
exclude: ^(docs|examples|tests/test_data)
1+
exclude: ^(docs|examples|tests|.github)
22

33
ci:
44
autoupdate_schedule: monthly

0 commit comments

Comments
 (0)