Skip to content

Commit 4294917

Browse files
committed
fix finding notebook paths, add directions in CONTRIBUTING
1 parent 74353ab commit 4294917

File tree

2 files changed

+57
-32
lines changed

2 files changed

+57
-32
lines changed

CONTRIBUTING.md

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,31 @@ because pytest does not like spaces in command line arguments.
99

1010
The docker image must be able to run all notebooks. Whenever a change is made to
1111
the docker image, it must be validated. This can be accomplished by automatically
12-
running all of the notebooks using the supplied test script. From the root
13-
directory within the docker container, run:
14-
```
15-
$> pytest tests/test_notebooks.py
12+
running all of the notebooks using the supplied test script. This is accomplished
13+
by running the notebook in interactive mode, achieved by adding `/bin/bash` to
14+
the container run command, e.g.
15+
```bash
16+
docker run -it --rm -p 8888:8888 -v $PWD:/home/jovyan/work -e PL_API_KEY='[YOUR-API-KEY]' planet-notebooks /bin/bash
1617
```
1718

18-
or, optionally, run only notebooks in a subdirectory using
19-
```
20-
$> pytest tests/test_notebooks.py --path <subdirectory>
21-
```
19+
From the root directory within the docker container, run one of the following:
20+
21+
1. To run all notebooks
22+
```bash
23+
$> pytest tests/test_notebooks.py
24+
```
25+
1. run only notebooks in a subdirectory using
26+
```bash
27+
$> pytest tests/test_notebooks.py --path <subdirectory>
28+
```
29+
1. run one or more notebooks (separated by spaces)
30+
```bash
31+
$> pytest tests/test_notebooks.py --notebooks <notebook1> <notebook2> <...>
32+
```
33+
1. run only notebooks that have a given dependency, <package>
34+
```bash
35+
$> pytest tests/test_notebooks.py --notebooks "$(grep -rl import <package> jupyter-notebooks/)"
36+
```
2237

2338
## Automated Running and Skipping
2439

@@ -28,3 +43,6 @@ the notebooks can be excluded from automated running by adding its path to
2843
`tests/skip_notebooks`. Excluding a notebook from automated running
2944
means that it is excluded from Docker Image validation. **If a notebook is
3045
skipped, it will not be guaranteed to be supported by the Docker image.**
46+
47+
Skipping of notebooks within `skip_notebooks` can be achieved with by adding the
48+
`--no-skip` option to the pytest command.

tests/conftest.py

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import os
2-
from pathlib import Path
2+
from pathlib import Path, PurePath
33

44
import pytest
55

66
NOTEBOOK_EXT = '.ipynb'
77
ROOT_DIR = 'jupyter-notebooks'
88

9-
# do not look for notebooks in .ipynb_checkpoints dir
10-
SKIP_DIRECTORIES = ['.ipynb_checkpoints']
9+
# do not run notebooks in these directories or sub-directories
10+
SKIP_DIRECTORIES = set(['.ipynb_checkpoints'])
1111

1212
def pytest_addoption(parser):
1313
parser.addoption("--path", action="store",
@@ -30,13 +30,15 @@ def pytest_generate_tests(metafunc):
3030

3131
if 'notebook_path' in metafunc.fixturenames:
3232
if notebooks:
33-
notebook_paths = notebooks
33+
notebook_paths = [Path(n) for n in notebooks]
3434
else:
3535
root_path = normalize_path(option_path)
36-
notebook_paths = find_notebooks(root_path)
36+
notebook_paths = get_all_paths(root_path)
3737

38+
notebook_paths = valid_notebooks(notebook_paths)
3839
metafunc.parametrize(['notebook_path', 'do_skip'],
39-
zip(notebook_paths, [do_skip] * len(notebook_paths)))
40+
zip([str(p) for p in notebook_paths],
41+
[do_skip] * len(notebook_paths)))
4042

4143

4244
def normalize_path(path):
@@ -48,24 +50,29 @@ def normalize_path(path):
4850
return os.path.join(*path_parts)
4951

5052

51-
def find_notebooks(root_dir):
52-
'''Find all of the notebooks within the root directory.
53+
def get_all_paths(root_dir):
54+
paths = []
55+
for path, _, files in os.walk(root_dir):
56+
for name in files:
57+
paths.append(PurePath(path, name))
58+
return paths
5359

54-
Skip notebooks and subdirectories of any directory with 'norun' file
55-
'''
56-
print('finding notebooks in {}...'.format(root_dir))
57-
notebooks = []
5860

59-
for (dirpath, dirnames, filenames) in os.walk(root_dir):
60-
# modify in place to filter subdirectories in walk()
61-
# https://stackoverflow.com/questions/19859840/excluding-directories-in-os-walk
62-
dirnames[:] = [n for n in dirnames if n not in SKIP_DIRECTORIES]
61+
def valid_notebooks(paths):
62+
'''Filters a list of paths to only include valid notebooks.
63+
64+
Removes paths that include skip directories and paths that do not end
65+
in notebook extension, .ipynb'''
66+
valid_paths = [p for p in paths if not _in_skip_dir(p) and _is_notebook(p)]
67+
print(' {} out of {} paths are valid'.format(len(valid_paths), len(paths)))
68+
return valid_paths
6369

64-
for name in filenames:
65-
ext = os.path.splitext(name)[1]
66-
if ext == NOTEBOOK_EXT:
67-
# convert to string so test displays the path
68-
notebook_name = str(os.path.join(dirpath, name))
69-
notebooks.append(notebook_name)
70-
print('found {} notebooks'.format(len(notebooks)))
71-
return notebooks
70+
71+
def _in_skip_dir(path):
72+
'''path is a Path object'''
73+
return len(SKIP_DIRECTORIES.intersection(path.parts)) > 0
74+
75+
76+
def _is_notebook(path):
77+
'''path is a Path object'''
78+
return path.suffix == NOTEBOOK_EXT

0 commit comments

Comments
 (0)