Skip to content

Commit ba6a3e1

Browse files
authored
8971 Skip Tests On Docs Only (#9109)
Fixes #8971. ### Description This is a possible way of getting CI tests to skip cleanly when only documentation is changed. This introduces a job to the CI action to assess whether only documentation is changed, that is only documentation files or docstrings/comments in Python files. Any other changes triggers tests. ### Types of changes <!--- Put an `x` in all the boxes that apply, and remove the not applicable items --> - [x] Non-breaking change (fix or new feature that would not break existing functionality). - [ ] Breaking change (fix or new feature that would cause existing functionality to change). - [x] New tests added to cover the changes. - [ ] Integration tests passed locally by running `./runtests.sh -f -u --net --coverage`. - [ ] Quick tests passed locally by running `./runtests.sh --quick --unittests --disttests`. - [ ] In-line docstrings updated. - [x] Documentation updated, tested `make html` command in the `docs/` folder. --------- Signed-off-by: Eric Kerfoot <17726042+ericspod@users.noreply.github.com>
1 parent f2ec87a commit ba6a3e1

5 files changed

Lines changed: 301 additions & 19 deletions

File tree

‎.github/workflows/cicd_tests.yml‎

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,11 @@ on:
77
- dev
88
- main
99
- releasing/*
10-
paths-ignore: # skip if only docs are modified
11-
- '**.md'
12-
- '**.rst'
13-
- 'docs/**'
1410
pull_request:
1511
branches:
1612
- dev
1713
- main
1814
- releasing/*
19-
paths-ignore: # skip if only docs are modified
20-
- '**.md'
21-
- '**.rst'
22-
- 'docs/**'
2315

2416
permissions:
2517
contents: read
@@ -55,6 +47,53 @@ env:
5547
# When support is dropped for a version it is important to update these as appropriate.
5648

5749
jobs:
50+
# This job will set its output value `skip` to 2 if only documentation was changed, either documentation files or
51+
# the docstrings of source files. If any source lines are changed, or non-documentation files are changed, the
52+
# result is 0 which then triggers tests to run. This job works by using the compare_sources.py script to compare
53+
# changed files to see if they differ in ways other than comments or docstrings.
54+
test_skip:
55+
runs-on: ubuntu-latest
56+
outputs:
57+
skip: ${{ steps.check-changes.outputs.skip }}
58+
steps:
59+
- uses: actions/checkout@v7
60+
with:
61+
fetch-depth: 2
62+
- name: Set up Python ${{ env.PYTHON_VER1 }}
63+
uses: actions/setup-python@v6
64+
with:
65+
python-version: ${{ env.PYTHON_VER1 }}
66+
cache: 'pip'
67+
- name: Check File Changes
68+
id: check-changes
69+
shell: bash
70+
run: |
71+
echo "skip=1" >> $GITHUB_OUTPUT
72+
73+
mkdir -p /tmp/check_changes
74+
if ! git diff --name-only HEAD^..HEAD ':!docs' > /tmp/check_changes/changed_files
75+
then
76+
echo "skip=0" >> "$GITHUB_OUTPUT"
77+
exit 0
78+
fi
79+
80+
while read i
81+
do
82+
echo "Checking for changes: $i"
83+
if ! git show HEAD^:$i > /tmp/check_changes/$(basename $i)
84+
then
85+
echo "New file found, or 'git show' failed, running tests."
86+
elif ! python monai/utils/compare_sources.py $i /tmp/check_changes/$(basename $i)
87+
then
88+
echo "Change found, running tests."
89+
else
90+
continue
91+
fi
92+
93+
echo "skip=0" >> $GITHUB_OUTPUT
94+
break
95+
done < /tmp/check_changes/changed_files
96+
5897
static-checks: # Perform static type and other checks using runtests.sh
5998
runs-on: ubuntu-latest
6099
strategy:
@@ -87,6 +126,7 @@ jobs:
87126
88127
min-dep: # Test with minumum dependencies installed for different OS, Python, and PyTorch combinations
89128
runs-on: ${{ matrix.os }}
129+
needs: test_skip
90130
strategy:
91131
fail-fast: false
92132
matrix:
@@ -123,25 +163,29 @@ jobs:
123163

124164
timeout-minutes: 40
125165
steps:
126-
- if: runner.os == 'Linux'
127-
name: Clean unused tools
166+
- name: Clean unused tools
167+
if: runner.os == 'Linux' && needs.test_skip.outputs.skip == 0
128168
run: |
129169
find /opt/hostedtoolcache/* -maxdepth 0 ! -name 'Python' -exec rm -rf {} \;
130170
sudo rm -rf /usr/share/dotnet
131171
sudo rm -rf /usr/local/lib/android
132172
sudo rm -rf /opt/ghc /usr/local/.ghcup
133173
sudo docker system prune -f
134174
- uses: actions/checkout@v7
175+
if: needs.test_skip.outputs.skip == 0
135176
- name: Set up Python ${{ matrix.python-version }}
177+
if: needs.test_skip.outputs.skip == 0
136178
uses: actions/setup-python@v6
137179
with:
138180
python-version: ${{ matrix.python-version }}
139181
cache: 'pip'
140182
- name: Prepare pip wheel
183+
if: needs.test_skip.outputs.skip == 0
141184
run: |
142185
which python
143186
python -m pip install --upgrade pip wheel tomli
144187
- name: Install the minimum dependencies
188+
if: needs.test_skip.outputs.skip == 0
145189
run: |
146190
# min. requirements
147191
python -m pip install torch==${{ matrix.pytorch-version }}
@@ -150,6 +194,7 @@ jobs:
150194
python -m pip list
151195
shell: bash
152196
- name: Run quick tests
197+
if: needs.test_skip.outputs.skip == 0
153198
run: |
154199
nvidia-smi || true
155200
python monai/config/check_env.py --env --monai
@@ -168,71 +213,77 @@ jobs:
168213
matrix:
169214
os: [windows-latest, ubuntu-latest, macOS-latest] # macOS-latest is very slow (#8864), testing install only
170215
timeout-minutes: 120
216+
needs: test_skip
171217
env:
172218
QUICKTEST: True
173219
steps:
174-
- if: runner.os == 'Linux'
175-
name: Clean unused tools
220+
- name: Clean unused tools
221+
if: runner.os == 'Linux' && needs.test_skip.outputs.skip == 0
176222
run: |
177223
find /opt/hostedtoolcache/* -maxdepth 0 ! -name 'Python' -exec rm -rf {} \;
178224
sudo rm -rf /usr/share/dotnet
179225
sudo rm -rf /usr/local/lib/android
180226
sudo rm -rf /opt/ghc /usr/local/.ghcup
181227
sudo docker system prune -f
182-
- if: runner.os == 'windows'
183-
name: Config pagefile (Windows only)
228+
- name: Config pagefile (Windows only)
229+
if: runner.os == 'windows' && needs.test_skip.outputs.skip == 0
184230
uses: al-cheb/configure-pagefile-action@v1.5
185231
with:
186232
minimum-size: 8GB
187233
maximum-size: 16GB
188234
disk-root: "D:"
189235
- uses: actions/checkout@v7
236+
if: needs.test_skip.outputs.skip == 0
190237
- name: Set up Python ${{ env.PYTHON_VER1 }}
238+
if: needs.test_skip.outputs.skip == 0
191239
uses: actions/setup-python@v6
192240
with:
193241
python-version: ${{ env.PYTHON_VER1 }}
194242
cache: 'pip'
195243
- name: Prepare pip wheel
244+
if: needs.test_skip.outputs.skip == 0
196245
run: |
197246
which python
198247
python -m pip install --upgrade pip wheel
199-
- if: runner.os == 'windows'
248+
- if: runner.os == 'windows' && needs.test_skip.outputs.skip == 0
200249
name: Install torch cpu from pytorch.org (Windows only)
201250
run: |
202251
python -m pip install torch==${WIN_FULLDEP_VER} torchvision==${WIN_FULLDEP_VISION_VER}+cpu --index-url https://download.pytorch.org/whl/cpu
203252
shell: bash
204-
- if: runner.os != 'windows'
253+
- if: runner.os != 'windows' && needs.test_skip.outputs.skip == 0
205254
name: Install torch gpu
206255
run: |
207256
# install the lowest version of PyTorch supported
208257
python -m pip install torch==${PYTORCH_VER1} torchvision==${TORCHVISION_VER1}
209258
shell: bash
210-
- if: runner.os == 'Linux'
259+
- if: runner.os == 'Linux' && needs.test_skip.outputs.skip == 0
211260
name: Install itk pre-release (Linux only)
212261
run: |
213262
python -m pip install --pre -U itk
214263
shell: bash
215264
- name: Install the complete dependencies
265+
if: needs.test_skip.outputs.skip == 0
216266
run: |
217267
python -m pip install --user --upgrade pip wheel tomli
218268
python monai/config/print_dependencies.py build-system | xargs pip install --no-build-isolation
219269
python -m pip install --no-build-isolation .[all,testing]
220270
python -m pip list
221271
shell: bash
222272
- name: Run compiled (${{ runner.os }})
273+
if: needs.test_skip.outputs.skip == 0
223274
run: |
224275
python -m pip uninstall -y monai
225276
BUILD_MONAI=1 python -m pip install --no-build-isolation -e . # compile the cpp extensions
226277
python -c 'import monai._C' > /dev/null
227278
nvidia-smi || true
228279
python monai/config/check_env.py --env --monai
229280
shell: bash
230-
- if: runner.os != 'macOS'
281+
- if: runner.os != 'macOS' && needs.test_skip.outputs.skip == 0
231282
name: Run full tests
232283
run: |
233284
python -m unittest -v
234285
shell: bash
235-
- if: runner.os == 'macOS'
286+
- if: runner.os == 'macOS' && needs.test_skip.outputs.skip == 0
236287
name: Run min tests
237288
run: |
238289
# TODO: enable large range of macOS tests which don't take a very long time
@@ -252,6 +303,8 @@ jobs:
252303
# ``.github/workflows/pythonapp-hyena-gpu.yml``.
253304
runs-on: ubuntu-latest
254305
timeout-minutes: 30
306+
needs: test_skip
307+
if: needs.test_skip.outputs.skip == 0
255308
steps:
256309
- name: Clean unused tools
257310
run: |

‎docs/source/utils.rst‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,8 @@ Safe Evaluation
8585
---------------
8686
.. automodule:: monai.utils.safeeval
8787
:members:
88+
89+
Compare Sources
90+
---------------
91+
.. automodule:: monai.utils.compare_sources
92+
:members:

‎monai/utils/__init__.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
from __future__ import annotations
1313

14+
from .compare_sources import SKIP_EXTS, files_considered_equal, sources_equal
1415
from .component_store import ComponentStore
1516
from .decorators import MethodReplacer, RestartGenerator
1617
from .deprecate_utils import DeprecatedError, deprecated, deprecated_arg, deprecated_arg_default

‎monai/utils/compare_sources.py‎

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# Copyright (c) MONAI Consortium
2+
# Licensed under the Apache License, Version 2.0 (the "License");
3+
# you may not use this file except in compliance with the License.
4+
# You may obtain a copy of the License at
5+
# http://www.apache.org/licenses/LICENSE-2.0
6+
# Unless required by applicable law or agreed to in writing, software
7+
# distributed under the License is distributed on an "AS IS" BASIS,
8+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
9+
# See the License for the specific language governing permissions and
10+
# limitations under the License.
11+
12+
"""
13+
This module is used in Github Actions to determine if a Python source file has been modified in a PR.
14+
"""
15+
16+
from __future__ import annotations
17+
18+
import ast
19+
import os
20+
import sys
21+
from os.path import splitext
22+
23+
PathLike = str | os.PathLike # needs to be duplicated here to avoid importing anything from MONAI inside actions
24+
25+
SKIP_EXTS = (".md", ".rst", ".cff")
26+
27+
28+
class RemoveDocstrings(ast.NodeTransformer):
29+
"""
30+
Strips docstrings from source files so that they aren't used for comparing ASTs.
31+
"""
32+
33+
def visit(self, node: ast.AST) -> ast.AST:
34+
try:
35+
# remove docstrings from the files
36+
if ast.get_docstring(node) is not None:
37+
del node.body[0]
38+
except TypeError:
39+
pass
40+
return super().visit(node)
41+
42+
43+
def sources_equal(src1: str, src2: str) -> bool:
44+
"""
45+
Compare Python source texts at the AST level without docstrings or comments. If two texts are equal except for
46+
changes to docstrings or comments, they will be considered equal by this function. Any other changes will appear
47+
as differences in the AST representations, which are compared inefficiently by comparing tree string dumps.
48+
49+
Args:
50+
src1: first Python source text.
51+
src2: second Python source text.
52+
53+
Returns:
54+
True if the texts `src1` and `src2` are equal with docstrings and comments removed, False otherwise.
55+
"""
56+
remdoc = RemoveDocstrings()
57+
58+
m1 = ast.parse(src1)
59+
m2 = ast.parse(src2)
60+
remdoc.visit(m1)
61+
remdoc.visit(m2)
62+
63+
list1 = list(ast.walk(m1))
64+
list2 = list(ast.walk(m2))
65+
66+
if len(list1) != len(list2) or any(type(n1) is not type(n2) for n1, n2 in zip(list1, list2)):
67+
return False
68+
69+
return ast.dump(m1) == ast.dump(m2)
70+
71+
72+
def files_considered_equal(file1: PathLike, file2: PathLike) -> bool:
73+
"""
74+
Returns True if the files are considered equal, that is they are doc files or differ only in docstrings or comments.
75+
76+
Args:
77+
file1: first file path to compare.
78+
file2: second file path to compare.
79+
80+
Returns:
81+
True if the files have the same extension, and either this extension is in `SKIP_EXTS`, or is ".py" with the
82+
files having the same content according to `sources_equal`. If the extensions differ, or are both ".py" but the
83+
file contents differ, returns False.
84+
"""
85+
_, ext1 = splitext(str(file1))
86+
_, ext2 = splitext(str(file2))
87+
88+
# if extensions aren't equal then definitely different (which shouldn't happen anyway)
89+
if ext1 != ext2:
90+
return False
91+
92+
# if extensions are an ignored type, ie. docs, don't compare at all
93+
if ext1 in SKIP_EXTS:
94+
return True
95+
96+
# if not Python source files, they aren't doc files at this point so assume different
97+
if ext1 != ".py":
98+
return False
99+
100+
# compare the actual parsed contents of the source files
101+
with open(file1) as o1, open(file2) as o2:
102+
return sources_equal(o1.read(), o2.read())
103+
104+
105+
if __name__ == "__main__":
106+
_, file1, file2 = sys.argv
107+
sys.exit(0 if files_considered_equal(file1, file2) else 1)

0 commit comments

Comments
 (0)