Skip to content

Commit d395883

Browse files
Add pytests, and remove explicit workflow testing (#430)
* Add pytests, and remove explicit workflow testing * Don't test something we can't test in CI. * Add dependency. * Update tests/test_package_creation.py Remove extra spaces. --------- Co-authored-by: Drew Oldag <[email protected]>
1 parent b22e735 commit d395883

File tree

4 files changed

+131
-107
lines changed

4 files changed

+131
-107
lines changed

.github/workflows/ci.yml

Lines changed: 10 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -5,121 +5,26 @@ on:
55
branches: [ main ]
66
pull_request:
77
branches: [ main ]
8-
schedule:
9-
- cron: 45 6 * * *
10-
# Allows you to run this workflow manually from the Actions tab
11-
workflow_dispatch:
128

139
jobs:
14-
tests:
15-
name: ${{ matrix.copier_config.name }} - Python ${{ matrix.python-version }}
10+
build:
11+
1612
runs-on: ubuntu-latest
1713
strategy:
18-
fail-fast: true
1914
matrix:
20-
python-version: ['3.9', '3.10', '3.11']
21-
copier_config:
22-
- name: Base example
23-
package_name: example_package # The default package_name
24-
extra_flags: ''
25-
foldername: base_example
26-
- name: No example module
27-
package_name: 'drewtonian' # Same module name provided in `extra_flags` on the next line.
28-
extra_flags: >-
29-
--data project_name=new_science
30-
--data package_name=drewtonian
31-
--data author_name=Drew
32-
33-
--data project_license=BSD
34-
--data mypy_type_checking=basic
35-
--data create_example_module=no
36-
--data include_notebooks=no
37-
foldername: 'black_w_o_example_module'
38-
- name: Example module
39-
package_name: 'drewtonian' # Same module name provided in `extra_flags` on the next line.
40-
extra_flags: >-
41-
--data project_name=new_science
42-
--data package_name=drewtonian
43-
--data author_name=Drew
44-
45-
--data project_license=BSD
46-
--data mypy_type_checking=basic
47-
--data create_example_module=yes
48-
--data include_notebooks=no
49-
foldername: 'black_w_example_module'
50-
- name: No sphinx docs
51-
package_name: 'drewtonian'
52-
extra_flags: >-
53-
--data project_name=new_science
54-
--data package_name=drewtonian
55-
--data include_docs=no
56-
--data include_notebooks=no
57-
foldername: 'no_sphinx_docs'
15+
python-version: ['3.9', '3.10', '3.11', '3.12']
5816

5917
steps:
60-
61-
- name: Checkout code
62-
uses: actions/checkout@v4
63-
64-
- name: Set up Python
65-
uses: actions/setup-python@v5
18+
- uses: actions/checkout@main
19+
- name: Set up Python ${{ matrix.python-version }}
20+
uses: actions/setup-python@main
6621
with:
6722
python-version: ${{ matrix.python-version }}
68-
69-
- name: Install Python dependencies
23+
- name: Install dependencies
7024
run: |
7125
sudo apt-get update
7226
python -m pip install --upgrade pip
73-
python -m pip install copier mypy
74-
75-
- name: Generate package
76-
run: |
77-
copier copy --vcs-ref HEAD --defaults ${{ matrix.copier_config.extra_flags }} ./ ../test/${{ matrix.copier_config.foldername }}
78-
cd ../test/${{ matrix.copier_config.foldername }}
79-
cat .copier-answers.yml
80-
81-
- name: Build package
82-
run: |
83-
cd ../test/${{ matrix.copier_config.foldername }}
84-
git config --global user.email "[email protected]"
85-
git config --global user.name "Test Name"
86-
git init --initial-branch=main
87-
git add src
88-
git commit -m initial
89-
pip install .[dev]
90-
91-
- name: black checks
92-
uses: psf/black@stable
93-
with:
94-
src: "../test/${{ matrix.copier_config.foldername }}/src"
95-
96-
- name: mypy checks basic
97-
if: ${{ contains(matrix.copier_config.extra_flags, 'mypy_type_checking=basic') && !contains(matrix.copier_config.extra_flags, 'create_example_module=no') }}
98-
run: |
99-
cd "../test/${{ matrix.copier_config.foldername }}"
100-
mypy src tests
101-
102-
- name: mypy checks strict
103-
if: ${{ contains(matrix.copier_config.extra_flags, 'mypy_type_checking=strict') && !contains(matrix.copier_config.extra_flags, 'create_example_module=no') }}
104-
run: |
105-
cd "../test/${{ matrix.copier_config.foldername }}"
106-
mypy --strict src tests
107-
108-
- name: Install notebook requirements
109-
if: ${{ !contains(matrix.copier_config.extra_flags, 'include_notebooks=no') }}
110-
run: |
111-
sudo apt-get install pandoc
112-
pip install -r ../test/${{ matrix.copier_config.foldername }}/docs/requirements.txt
113-
cat ../test/${{ matrix.copier_config.foldername }}/docs/requirements.txt
114-
115-
- name: Build docs
116-
if: ${{ !contains(matrix.copier_config.extra_flags, 'include_docs=no') }}
117-
run: |
118-
cd ../test/${{ matrix.copier_config.foldername }}
119-
sphinx-build -T -E -b html -d docs/build/doctrees ./docs docs/build/html
120-
121-
- name: Tests
122-
if: ${{ !contains(matrix.copier_config.extra_flags, 'create_example_module=no') }}
27+
pip install -e .[dev]
28+
- name: Run unit tests with pytest / pytest-copie
12329
run: |
124-
cd ../test/${{ matrix.copier_config.foldername }}
125-
python -m pytest --cov=${{ matrix.copier_config.package_name }} --cov-report=xml
30+
python -m pytest

.github/workflows/smoke-test.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# This workflow will run daily at 06:45.
2+
# It will install Python dependencies and run tests with a variety of Python versions.
3+
# See documentation for help debugging smoke test issues:
4+
# https://lincc-ppt.readthedocs.io/en/latest/practices/ci_testing.html#version-culprit
5+
6+
name: Unit test smoke test
7+
8+
on:
9+
10+
# Runs this workflow automatically
11+
schedule:
12+
- cron: 45 6 * * *
13+
14+
# Allows you to run this workflow manually from the Actions tab
15+
workflow_dispatch:
16+
17+
jobs:
18+
build:
19+
20+
runs-on: ubuntu-latest
21+
strategy:
22+
matrix:
23+
python-version: ['3.9', '3.10', '3.11', '3.12']
24+
25+
steps:
26+
- uses: actions/checkout@main
27+
- name: Set up Python ${{ matrix.python-version }}
28+
uses: actions/setup-python@main
29+
with:
30+
python-version: ${{ matrix.python-version }}
31+
- name: Install dependencies
32+
run: |
33+
sudo apt-get update
34+
python -m pip install --upgrade pip
35+
pip install -e .[dev]
36+
- name: List dependencies
37+
run: |
38+
pip list
39+
- name: Run unit tests with pytest / pytest-copie
40+
run: |
41+
python -m pytest

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ dev = [
2626
"black", # Used to format code
2727
"pre-commit", # Used to run checks prior to committing code
2828
"pytest", # Used to run tests
29+
"pylint", # test pylint in unit tests
2930
"pytest-copie", # Used to create hydrated copier projects for testing
3031
"tox", # Used to run tests in multiple environments
3132
]

tests/test_package_creation.py

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import pytest
22
import subprocess
33

4+
import pytest
5+
46

57
def successfully_created_project(result):
68
"""Basic assertions that indicate the copier was able to create a project"""
@@ -55,6 +57,22 @@ def unit_tests_in_project_run_successfully(result, package_name="example_package
5557
return pytest_results.returncode == 0
5658

5759

60+
def docs_build_successfully(result):
61+
"""Test that we can build the doc tree.
62+
63+
!!! NOTE - This doesn't currently work because we need to `pip install` the hydrated
64+
project before running the tests. And we don't have a way to create a temporary
65+
virtual environment for the project.
66+
"""
67+
68+
sphinx_results = subprocess.run(
69+
["make", "html"],
70+
cwd=(result.project_dir / "docs"),
71+
)
72+
73+
return sphinx_results.returncode == 0
74+
75+
5876
def github_workflows_are_valid(result):
5977
"""Test to ensure that the GitHub workflows are valid"""
6078
workflows_results = subprocess.run(
@@ -160,13 +178,22 @@ def test_code_style_combinations(copie, enforce_style):
160178
assert black_runs_successfully(result)
161179

162180

163-
def test_smoke_test_notification(copie):
181+
@pytest.mark.parametrize(
182+
"notification",
183+
[
184+
[],
185+
["slack"],
186+
["email"],
187+
["email", "slack"],
188+
],
189+
)
190+
def test_smoke_test_notification(copie, notification):
164191
"""Confirm we can generate a "smoke_test.yaml" file, with all
165192
notification mechanisms selected."""
166193

167194
# provide a dictionary of the non-default answers to use
168195
extra_answers = {
169-
"failure_notification": ["email", "slack"],
196+
"failure_notification": notification,
170197
}
171198

172199
# run copier to hydrate a temporary project
@@ -177,6 +204,56 @@ def test_smoke_test_notification(copie):
177204
assert black_runs_successfully(result)
178205

179206

207+
@pytest.mark.parametrize(
208+
"doc_answers",
209+
[
210+
{
211+
"include_docs": True,
212+
"include_notebooks": True,
213+
},
214+
{
215+
"include_docs": True,
216+
"include_notebooks": False,
217+
},
218+
],
219+
)
220+
def test_doc_combinations(copie, doc_answers):
221+
"""Confirm the docs directory is well-formed, when including docs."""
222+
223+
# run copier to hydrate a temporary project
224+
result = copie.copy(extra_answers=doc_answers)
225+
226+
assert successfully_created_project(result)
227+
assert directory_structure_is_correct(result)
228+
assert black_runs_successfully(result)
229+
assert (result.project_dir / "docs").is_dir()
230+
231+
232+
@pytest.mark.parametrize(
233+
"doc_answers",
234+
[
235+
{
236+
"include_docs": False,
237+
"include_notebooks": False,
238+
},
239+
{
240+
"include_docs": False,
241+
"include_notebooks": True,
242+
},
243+
],
244+
)
245+
def test_doc_combinations_no_docs(copie, doc_answers):
246+
"""Confirm there is no 'docs' directory, if not including docs."""
247+
248+
# run copier to hydrate a temporary project
249+
result = copie.copy(extra_answers=doc_answers)
250+
251+
assert successfully_created_project(result)
252+
assert directory_structure_is_correct(result)
253+
assert black_runs_successfully(result)
254+
assert not (result.project_dir / "docs").is_dir()
255+
256+
180257
def test_github_workflows_schema(copie):
181258
"""Confirm the current GitHub workflows have valid schemas."""
182259
extra_answers = {

0 commit comments

Comments
 (0)