Skip to content

Commit

Permalink
Merge pull request #19 from executablebooks/tests
Browse files Browse the repository at this point in the history
✨ NEW: adding new test regime using pytest
  • Loading branch information
TomasBeuzen authored Oct 2, 2020
2 parents 8c6f36f + a73f4ea commit d01e4b8
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 35 deletions.
39 changes: 20 additions & 19 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,7 @@ on:
branches:
- master
schedule:
# cookiecutter/jupyter-book is updated regularly, let's run these tests every month in case something fails
# <minute [0,59]> <hour [0,23]> <day of the month [1,31]> <month of the year [1,12]> <day of the week [0,6]>
# https://pubs.opengroup.org/onlinepubs/9699919799/utilities/crontab.html#tag_20_25_07
# https://crontab.guru/every-month
# Run cron job every month
# Run cron job every month, https://crontab.guru/every-month
- cron: '0 0 1 * *'

jobs:
Expand All @@ -26,27 +22,32 @@ jobs:
python-version: [3.7, 3.8]
steps:
- uses: actions/checkout@v2
# Install CC
# Setup
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v1
with:
python-version: ${{ matrix.python-version }}
- name: Install CC
- name: Install dependencies
run: |
pip install cookiecutter
pip install -r requirements.txt
# Use the default CC
- name: Cookiecutter no GHA
- name: Check black style
run: |
cookiecutter . --no-input include_ci=no
# Use the CC with no GitHub action file included
- name: Cookiecutter default
black ./ --check
# Run tests
- name: Pytest tests
run: |
cookiecutter . --no-input --overwrite-if-exists
# Install requirements.txt
- name: Install requirements
run: |
pip install -r my_book/requirements.txt
pytest
# Build the example book
- name: Build book
- name: Build default book
run: |
jupyter-book build my_book/my_book/
cookiecutter . --no-input --overwrite-if-exists
pip install -r my_book/requirements.txt
jupyter-book build my_book/my_book/
# Push example book to gh-pages-test
- name: GitHub Pages action
uses: peaceiris/actions-gh-pages@v3.6.1
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
publish_dir: my_book/my_book/_build/html
publish_branch: gh-pages-test
34 changes: 18 additions & 16 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,27 @@ Look through the (https://github.com/executablebooks/cookiecutter-jupyter-book/i
Ready to contribute? Here's how to set up `cookiecutter-jupyter-book` for local development.

1. Fork the `cookiecutter-jupyter-book` repo on GitHub.
2. Clone your fork locally:
2. Clone your fork locally and install requirements:

```
git clone git@github.com:your_name_here/cookiecutter-jupyter-book.git
```
```sh
git clone git@github.com:your_name_here/cookiecutter-jupyter-book.git
pip install -r requirements.txt
```

4. Create a branch for local development:
3. Create a branch for local development:

```
git checkout -b name-of-your-bugfix-or-feature
```
Now you can make your changes locally.
```sh
git checkout -b name-of-your-bugfix-or-feature
```

5. Commit your changes and push your branch to GitHub:
4. Make your desired changes, run tests, and push your branch to GitHub when you're ready:

```
git add .
git commit -m "Your detailed description of your changes."
git push origin name-of-your-bugfix-or-feature
```
```sh
pytest
black ./ --check
git add .
git commit -m "Your detailed description of your changes."
git push origin name-of-your-bugfix-or-feature
```

6. Open a pull request through the GitHub website. Naming convention for pull requests is [detailed here](https://github.com/executablebooks/.github/blob/master/CONTRIBUTING.md#commit-messages). For example, a pull request that adds a new feature might be titled: `✨ NEW: validate entered github username`.
5. Open a pull request through the GitHub website. Naming convention for pull requests is [detailed here](https://github.com/executablebooks/.github/blob/master/CONTRIBUTING.md#commit-messages). For example, a pull request that adds a new feature might be titled: `✨ NEW: validate entered github username`.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cookiecutter
pytest
black
96 changes: 96 additions & 0 deletions tests/test_cookiecutter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import json
import subprocess
from pathlib import Path
from itertools import product
from pytest import fixture, mark


@fixture()
def base_command(tmpdir):
return (f"cookiecutter . --no-input --output-dir {tmpdir}", tmpdir)


def test_cookiecutter_default_options(base_command):
result = subprocess.run(base_command[0], shell=True)
assert result.returncode == 0


with open("cookiecutter.json") as f:
options = json.load(f)
combinations = list(product(options["open_source_license"], options["include_ci"]))


@mark.parametrize("open_source_license,include_ci", combinations)
def test_cookiecutter_all_options(base_command, open_source_license, include_ci):
params = f" open_source_license='{open_source_license}' include_ci={include_ci}"
path = Path(base_command[1])
result = subprocess.run(base_command[0] + params, shell=True)
assert result.returncode == 0
try:
# annoying work-around for MAC OS .DS_Store files
path.joinpath("my_book", ".DS_Store").unlink()
path.joinpath("my_book", "my_book", ".DS_Store").unlink()
except:
pass
assert len(list(path.joinpath("my_book", "my_book").iterdir())) == 8
print(open_source_license)
print(include_ci)
if open_source_license == "None":
if include_ci == "github":
assert (
len(list(path.joinpath("my_book", ".github", "workflows").iterdir()))
== 1
)
assert len(list(path.joinpath("my_book").iterdir())) == 6
elif include_ci == "gitlab":
assert len(list(path.joinpath("my_book").iterdir())) == 6
else:
assert len(list(path.joinpath("my_book").iterdir())) == 5
else:
if include_ci == "github":
assert (
len(list(path.joinpath("my_book", ".github", "workflows").iterdir()))
== 1
)
assert len(list(path.joinpath("my_book").iterdir())) == 7
elif include_ci == "gitlab":
assert len(list(path.joinpath("my_book").iterdir())) == 7
else:
assert len(list(path.joinpath("my_book").iterdir())) == 6


def test_jupyter_book_cookiecutter(base_command):
# same tests being run in the Jupyter Book test regime
# https://github.com/executablebooks/jupyter-book/blob/master/tests/test_build.py#L26-L35
# note that default cookiecutter book name is "my_book" which is why it's used below
path = Path(base_command[1])
result = subprocess.run(base_command[0], shell=True)
try:
# annoying work-around for MAC OS .DS_Store files
path.joinpath("my_book", ".DS_Store").unlink()
path.joinpath("my_book", "my_book", ".DS_Store").unlink()
except:
pass
assert result.returncode == 0
assert path.joinpath("my_book", "my_book", "_config.yml").exists()
print(list(path.joinpath("my_book").iterdir()))
assert len(list(path.joinpath("my_book").iterdir())) == 7
assert len(list(path.joinpath("my_book", ".github", "workflows").iterdir())) == 1
assert len(list(path.joinpath("my_book", "my_book").iterdir())) == 8


@mark.parametrize(
"username,service,msg",
[
("fake_captainjupyter_fake", "github", "WARNING"),
("fake_captainjupyter_fake", "gitlab", "WARNING"),
],
)
def test_warning_message(base_command, username, service, msg):
path = Path(base_command[1])
result = subprocess.run(
base_command[0] + f" include_ci={service}", shell=True, capture_output=True
)
print(result.stdout.decode("ascii"))
assert result.returncode == 0
assert msg in result.stdout.decode("ascii")

0 comments on commit d01e4b8

Please sign in to comment.