Skip to content
This repository was archived by the owner on Aug 28, 2025. It is now read-only.

Commit 400dd2a

Browse files
Bordaakihironitta
andauthored
CI: add internal testing (#140)
* list commands * update requirements * adding tests * install default * fixing * Apply suggestions from code review Co-authored-by: Aki Nitta <nitta@akihironitta.com>
1 parent 34aa043 commit 400dd2a

File tree

7 files changed

+120
-24
lines changed

7 files changed

+120
-24
lines changed

.actions/assistant.py

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ def bash_render(folder: str) -> str:
328328
cmd.append(f"python .actions/assistant.py update-env-details {folder}")
329329
# copy and add to version the enriched meta config
330330
cmd += [f"cp {meta_file} {pub_meta}", f"git add {pub_meta}"]
331-
# if thumb image is linked to to the notebook, copy and version it too
331+
# if thumb image is linked to the notebook, copy and version it too
332332
if thumb_file:
333333
cmd += [f"cp {thumb_file} {pub_thumb}", f"git add {pub_thumb}"]
334334
# add the generated notebook to version
@@ -361,7 +361,7 @@ def bash_test(folder: str) -> str:
361361

362362
cmd.append(f"# available: {AssistantCLI.DEVICE_ACCELERATOR}")
363363
if AssistantCLI._valid_accelerator(folder):
364-
cmd.append(f"python -m pytest {ipynb_file} -v --nbval")
364+
cmd.append(f"python -m pytest {ipynb_file} -v --nbval --nbval-cell-timeout=300")
365365
else:
366366
warn("Invalid notebook's accelerator for this device. So no tests will be run!!!", RuntimeWarning)
367367
# deactivate and clean local environment
@@ -443,7 +443,7 @@ def group_folders(
443443
fpath_gitdiff: str,
444444
fpath_change_folders: str = "changed-folders.txt",
445445
fpath_drop_folders: str = "dropped-folders.txt",
446-
fpaths_actual_dirs: Sequence[str] = tuple(),
446+
fpath_actual_dirs: Sequence[str] = tuple(),
447447
strict: bool = True,
448448
root_path: str = "",
449449
) -> None:
@@ -458,23 +458,23 @@ def group_folders(
458458
459459
fpath_change_folders: output file with changed folders
460460
fpath_drop_folders: output file with deleted folders
461-
fpaths_actual_dirs: files with listed all folder in particular stat
461+
fpath_actual_dirs: files with listed all folder in particular stat
462462
strict: raise error if some folder outside skipped does not have valid meta file
463463
root_path: path to the root tobe added for all local folder paths in files
464464
465465
Example:
466-
>> python assistant.py group-folders ../target-diff.txt --fpaths_actual_dirs "['../dirs-main.txt', '../dirs-publication.txt']"
466+
>> python assistant.py group-folders ../target-diff.txt --fpath_actual_dirs "['../dirs-main.txt', '../dirs-publication.txt']"
467467
"""
468468
with open(fpath_gitdiff) as fp:
469469
changed = [ln.strip() for ln in fp.readlines()]
470470
dirs = [os.path.dirname(ln) for ln in changed]
471471
# not empty paths
472472
dirs = [ln for ln in dirs if ln]
473473

474-
if fpaths_actual_dirs:
475-
assert isinstance(fpaths_actual_dirs, list)
476-
assert all(os.path.isfile(p) for p in fpaths_actual_dirs)
477-
dir_sets = [{ln.strip() for ln in open(fp).readlines()} for fp in fpaths_actual_dirs]
474+
if fpath_actual_dirs:
475+
assert isinstance(fpath_actual_dirs, list)
476+
assert all(os.path.isfile(p) for p in fpath_actual_dirs)
477+
dir_sets = [{ln.strip() for ln in open(fp).readlines()} for fp in fpath_actual_dirs]
478478
# get only different
479479
dirs += list(set.union(*dir_sets) - set.intersection(*dir_sets))
480480

@@ -564,7 +564,7 @@ def copy_notebooks(
564564
path_docs_ipynb: str = "notebooks",
565565
path_docs_images: str = "_static/images",
566566
patterns: Sequence[str] = (".", "**"),
567-
):
567+
) -> None:
568568
"""Copy all notebooks from a folder to doc folder.
569569
570570
Args:
@@ -609,13 +609,13 @@ def copy_notebooks(
609609
ipynb_content.append(os.path.join("notebooks", sub_ipynb))
610610

611611
@staticmethod
612-
def update_env_details(dir_path: str):
612+
def update_env_details(folder: str) -> None:
613613
"""Export the actual packages used in runtime.
614614
615615
Args:
616-
dir_path: path to the folder
616+
folder: path to the folder
617617
"""
618-
meta = AssistantCLI._load_meta(dir_path)
618+
meta = AssistantCLI._load_meta(folder)
619619
# default is COU runtime
620620
with open(PATH_REQ_DEFAULT) as fp:
621621
req = fp.readlines()
@@ -636,9 +636,16 @@ def _parse_package_name(pkg: str, keys: str = " <=>[]@", egg_name: str = "#egg="
636636
meta["environment"] = [env[r] for r in require]
637637
meta["published"] = datetime.now().isoformat()
638638

639-
fmeta = os.path.join(DIR_NOTEBOOKS, dir_path) + ".yaml"
639+
fmeta = os.path.join(DIR_NOTEBOOKS, folder) + ".yaml"
640640
yaml.safe_dump(meta, stream=open(fmeta, "w"), sort_keys=False)
641641

642+
@staticmethod
643+
def list_dirs(folder: str = ".notebooks") -> str:
644+
"""List all sub-folders in a given tree including any ipynb."""
645+
ipynbs = glob.glob(os.path.join(folder, "*.ipynb")) + glob.glob(os.path.join(folder, "**", "*.ipynb"))
646+
ipynbs = sorted(os.path.splitext(os.path.sep.join(p.split(os.path.sep)[1:]))[0] for p in ipynbs)
647+
return os.linesep.join(ipynbs)
648+
642649

643650
if __name__ == "__main__":
644651
fire.Fire(AssistantCLI)

.actions/git-diff-sync.sh

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,13 @@ git checkout $2
1818
b2="${2//'/'/'_'}"
1919
printf "Branch alias: $b2\n"
2020
# list all dirs in target branch
21-
python -c "
22-
import os
23-
from glob import glob
24-
from os.path import sep, splitext
25-
ipynbs = glob('.notebooks/*.ipynb') + glob('.notebooks/**/*.ipynb')
26-
ipynbs = sorted([splitext(sep.join(p.split(sep)[1:]))[0] for p in ipynbs])
27-
print(os.linesep.join(ipynbs))" > "dirs-$b2.txt"
21+
python .actions/assistant.py list_dirs > "dirs-$b2.txt"
2822
cat "dirs-$b2.txt"
2923

3024
printf "\n\n"
3125
git merge --ff -s resolve origin/$1
3226

33-
python .actions/assistant.py group-folders target-diff.txt --fpaths_actual_dirs "['dirs-$b1.txt', 'dirs-$b2.txt']"
27+
python .actions/assistant.py group-folders target-diff.txt --fpath_actual_dirs "['dirs-$b1.txt', 'dirs-$b2.txt']"
3428
printf "\n\nChanged folders:\n"
3529
cat changed-folders.txt
3630
printf "\n\nDropped folders:\n"

.actions/requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@ Fire
22
tqdm
33
PyYAML
44
wcmatch
5+
requests
56
pip

.actions/test_cli.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import os
2+
3+
import pytest
4+
from assistant import AssistantCLI
5+
6+
_PATH_ROOT = os.path.dirname(os.path.dirname(__file__))
7+
_PATH_TEMPLATES = os.path.join(_PATH_ROOT, "templates")
8+
_PATH_DIR_SIMPLE = os.path.join(_PATH_TEMPLATES, "simple")
9+
_PATH_DIR_TITANIC = os.path.join(_PATH_TEMPLATES, "titanic")
10+
11+
12+
@pytest.mark.parametrize(
13+
"cmd,args",
14+
[
15+
("list_dirs", []),
16+
("bash_render", [_PATH_DIR_SIMPLE]),
17+
("bash_test", [_PATH_DIR_SIMPLE]),
18+
# ("group_folders", [_PATH_SIMPLE]),
19+
("augment_script", [_PATH_DIR_SIMPLE]),
20+
("copy_notebooks", [_PATH_ROOT]),
21+
("update_env_details", [_PATH_DIR_SIMPLE]),
22+
],
23+
)
24+
def test_assistant_commands(cmd: str, args: list):
25+
AssistantCLI().__getattribute__(cmd)(*args)

.github/workflows/ci_test-acts.yml

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
name: CI internal
2+
3+
# see: https://help.github.com/en/actions/reference/events-that-trigger-workflows
4+
on: # Trigger the workflow on push or pull request, but only for the main branch
5+
push: {}
6+
pull_request:
7+
branches: [main]
8+
9+
jobs:
10+
pytest-internal:
11+
12+
runs-on: ${{ matrix.os }}
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
os: [ubuntu-20.04 ]
17+
python-version: ["3.9"]
18+
# Timeout: https://stackoverflow.com/a/59076067/4521646
19+
timeout-minutes: 10
20+
21+
steps:
22+
- uses: actions/checkout@v2
23+
- name: Set up Python ${{ matrix.python-version }}
24+
uses: actions/setup-python@v2
25+
with:
26+
python-version: ${{ matrix.python-version }}
27+
28+
- name: Install requirements
29+
run: |
30+
pip --version
31+
pip install -q -r .actions/requirements.txt
32+
pip install -q "pytest==6.*" coverage jupytext
33+
# this is needed to be able to run package version parsing test
34+
pip install -q matplotlib -r requirements/default.txt --find-links https://download.pytorch.org/whl/cpu/torch_stable.html
35+
36+
- name: Convert templates
37+
run: |
38+
jupytext --set-formats ipynb,py:percent templates/simple/template.py
39+
jupytext --set-formats ipynb,py:percent templates/titanic/tutorial.py
40+
# mv templates/simple/template.ipynb templates/simple.ipynb
41+
42+
- name: Testing
43+
run: |
44+
coverage run -m pytest .actions -v --junitxml="junit/test-results-${{ runner.os }}-${{ matrix.python-version }}.xml"
45+
46+
- name: Upload pytest test results
47+
uses: actions/upload-artifact@v2
48+
with:
49+
name: pytest-results-${{ runner.os }}-${{ matrix.python-version }}
50+
path: junit/test-results-${{ runner.os }}-${{ matrix.python-version }}.xml
51+
if: failure()
52+
53+
- name: Statistics
54+
if: success()
55+
run: |
56+
coverage report
57+
coverage xml
58+
59+
- name: Upload coverage to Codecov
60+
uses: codecov/codecov-action@v1
61+
if: always()
62+
# see: https://github.com/actions/toolkit/issues/399
63+
continue-on-error: true
64+
with:
65+
token: ${{ secrets.CODECOV_TOKEN }}
66+
file: coverage.xml
67+
flags: pytest,${{ runner.os }}
68+
name: CLI-coverage
69+
fail_ci_if_error: false

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# PytorchLightning Tutorials
22

3+
[![CI internal](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/ci_test-acts.yml/badge.svg?event=push)](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/ci_test-acts.yml)
34
[![Build Status](https://dev.azure.com/PytorchLightning/Tutorials/_apis/build/status/PyTorchLightning.Tutorials-publishing?branchName=main)](https://dev.azure.com/PytorchLightning/Tutorials/_build/latest?definitionId=11&branchName=main)
4-
[![Code formatting](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/ci_code-format.yml/badge.svg?event=push)](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/ci_code-format.yml)
5+
[![codecov](https://codecov.io/gh/PyTorchLightning/lightning-tutorials/branch/main/graph/badge.svg?token=C6T3XOOR56)](https://codecov.io/gh/PyTorchLightning/lightning-tutorials)
56
[![Deploy Docs](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/docs-deploy.yml/badge.svg)](https://github.com/PyTorchLightning/lightning-tutorials/actions/workflows/docs-deploy.yml)
67
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/PyTorchLightning/lightning-tutorials/main.svg)](https://results.pre-commit.ci/latest/github/PyTorchLightning/lightning-tutorials/main)
78

setup.cfg

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ addopts =
88
--strict
99
--doctest-modules
1010
--color=yes
11-
--nbval-cell-timeout=300
1211

1312
[coverage:report]
1413
exclude_lines =

0 commit comments

Comments
 (0)