Skip to content

Commit

Permalink
- Exclude e-TeenAstronomyCafe NBs from default testing.
Browse files Browse the repository at this point in the history
- Add ability to inject a code cell to every tested notebook (read code from prerun.py file)
- Update emails and timestamps.
  • Loading branch information
Robert Nikutta committed May 28, 2021
1 parent 019a4d8 commit 3963373
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions tests/prerun.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# All Python code in this file, if not commented out, will be prepended to every notebook before it is executed by testnotebooks.py

#print('foobar')
12 changes: 7 additions & 5 deletions tests/testnotebooks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,18 @@
"metadata": {},
"outputs": [],
"source": [
"__version__ = '20200922'\n",
"__author__ = ['Robert Nikutta <nikutta@noao.edu>', 'Data Lab Team <datalab@noao.edu']"
"__version__ = '20210527'\n",
"__author__ = ['Robert Nikutta <robert.nikutta@noirlab.edu>', 'Data Lab Team <datalab@noirlab.edu']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Automatic notebook testing\n",
"### Run the cells in this notebook in order."
"### Run the cells in this notebook in order.\n",
"\n",
"Note: if you wish to run any Python code as the first cell of any tested notebooks, please edit the file `prerun.py` in this directory. Any not commented-out code there will be injected as the first cell to any notebooks tested by `testnotebooks`."
]
},
{
Expand Down Expand Up @@ -81,7 +83,7 @@
"# * means \"any number if intermediate characters in this directory\"\n",
"# A few default notebooks are explicitly excluded here b/c they absolutely require interactive execution.\n",
"# A few others are temporarily excluded until they will be fixed.\n",
"exclude = ('**/*AuthClient.ipynb','**/Rowstore*.ipynb')"
"exclude = ('**/*AuthClient.ipynb','**/Rowstore*.ipynb','**/e-Teen*/**/*ipynb')"
]
},
{
Expand Down Expand Up @@ -135,7 +137,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.0"
"version": "3.7.6"
}
},
"nbformat": 4,
Expand Down
27 changes: 21 additions & 6 deletions tests/testnotebooks.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
__version__ = '20200712'
__author__ = 'Robert Nikutta <nikutta@noao.edu>'
__version__ = '20210527'
__author__ = 'Robert Nikutta <robert.nikutta@noirlab.edu>'

# imports

Expand Down Expand Up @@ -120,10 +120,25 @@ def run_notebook(nbpath,kernel='python3'):

nbname, _ = os.path.splitext(os.path.basename(nbpath))
dirname = os.path.dirname(nbpath)


# check if any code needs to be injected as the first cell in a notebook
if os.path.isfile('prerun.py'):
with open('prerun.py','r') as prerun:
lines = prerun.readlines() # read on all lines
lines = [line for line in lines if not line[0] in ('#','\n')] # remove comments and empty lines
if len(lines) > 0:
lines.insert(0,"# Code cell inserted from prerun.py file. Comment out in prerun.py if you don't wish to run it.\n")
lines[-1] = lines[-1].strip() # strip line break from very last line
else:
lines = []

with open(nbpath) as f:
nb = nbformat.read(f, as_version=4)


# inject a code cell as first cell, if there is code in prerun.py
if len(lines) > 0:
nb.cells.insert(0,nbformat.v4.new_code_cell(''.join(lines)))

proc = ExecutePreprocessor(timeout=6000, kernel_name=kernel)
proc.allow_errors = True

Expand Down Expand Up @@ -361,8 +376,8 @@ def run(paths,include=('/**/*.ipynb',),exclude=('/**/*_tested.ipynb',),plain=Fal
# * means "any number if intermediate characters in this directory"
# A few default notebooks are explicitly excluded here b/c they absolutely require interactive execution.
# A few others are temporarily excluded until they will be fixed.
exclude = ('**/*AuthClient.ipynb','**/Rowstore*.ipynb')

exclude = ('**/*AuthClient.ipynb','**/Rowstore*.ipynb','**/e-Teen*/**/*ipynb')
# log in to Data Lab once
cprint('Login to Data Lab',color='yellow',bar=0,pad='')
token = login(input('Username: '),getpass('Password: '))
Expand Down

0 comments on commit 3963373

Please sign in to comment.