Skip to content

Commit

Permalink
Merge pull request #161 from ceball/some_option_renaming
Browse files Browse the repository at this point in the history
Add "nbval" into --current-env and --sanitize-with option names
  • Loading branch information
vidartf committed Jan 13, 2021
2 parents deb72e6 + ce960f2 commit a13a03b
Show file tree
Hide file tree
Showing 9 changed files with 34 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ either `--nbval-kernel-name mykernel` to run all the notebooks using
`mykernel`, or `--current-env` to use a kernel in the same environment
in which pytest itself was launched.

If the output lines are going to be sanitized, an extra flag, `--sanitize-with`
If the output lines are going to be sanitized, an extra flag, `--nbval-sanitize-with`
together with the path to a confguration file with regex expressions, must be passed,
i.e.

py.test --nbval my_notebook.ipynb --sanitize-with path/to/my_sanitize_file
py.test --nbval my_notebook.ipynb --nbval-sanitize-with path/to/my_sanitize_file

where `my_sanitize_file` has the following structure.

Expand Down
2 changes: 1 addition & 1 deletion dodo.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def _clean_dist_cmd():
def task_test():
return {
'actions': [
_make_cmd(["py.test", "-v", "tests/", "--nbval", "--current-env", "--sanitize-with", "tests/sanitize_defaults.cfg", "--ignore", "tests/ipynb-test-samples"]),
_make_cmd(["py.test", "-v", "tests/", "--nbval", "--nbval-current-env", "--nbval-sanitize-with", "tests/sanitize_defaults.cfg", "--ignore", "tests/ipynb-test-samples"]),
],
}

Expand Down
28 changes: 22 additions & 6 deletions nbval/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,12 @@ def pytest_addoption(parser):
help="Run Jupyter notebooks, only validating output on "
"cells marked with # NBVAL_CHECK_OUTPUT")

group.addoption('--sanitize-with',
group.addoption('--nbval-sanitize-with',
help='File with regex expressions to sanitize '
'the outputs. This option only works when '
'the --nbval flag is passed to py.test')

group.addoption('--current-env', action='store_true',
group.addoption('--nbval-current-env', action='store_true',
help='Force test execution to use a python kernel in '
'the same environment that py.test was '
'launched from. Without this flag, the kernel stored '
Expand All @@ -100,6 +100,12 @@ def pytest_addoption(parser):
type=float,
help='Timeout for kernel startup, in seconds.')

group.addoption('--sanitize-with',
help='(deprecated) Alias of --nbval-sanitize-with')

group.addoption('--current-env', action='store_true',
help='(deprecated) Alias of --nbval-current-env')

term_group = parser.getgroup("terminal reporting")
term_group._addoption(
'--nbdime', action='store_true',
Expand All @@ -111,6 +117,16 @@ def pytest_configure(config):
from .nbdime_reporter import NbdimeReporter
reporter = NbdimeReporter(config, sys.stdout)
config.pluginmanager.register(reporter, 'nbdimereporter')
if config.option.sanitize_with:
warnings.warn("--sanitize-with has been renamed to --nbval-sanitize-with", DeprecationWarning)
if config.option.nbval_sanitize_with:
raise ValueError("--sanitize-with and --nbval-sanitize-with were both supplied.")
config.option.nbval_sanitize_with = config.option.sanitize_with
if config.option.current_env:
warnings.warn("--current-env has been renamed to --nbval-current-env", DeprecationWarning)
if config.option.nbval_current_env:
raise ValueError("--current-env and --nbval-current-env were both supplied.")
config.option.nbval_current_env = config.option.current_env
if config.option.nbval or config.option.nbval_lax:
if config.option.nbval_kernel_name and config.option.current_env:
raise ValueError("--current-env and --nbval-kernel-name are mutually exclusive.")
Expand Down Expand Up @@ -239,9 +255,9 @@ def setup(self):
Called by pytest to setup the collector cells in .
Here we start a kernel and setup the sanitize patterns.
"""
# we've already checked that --current-env and
# we've already checked that --nbval-current-env and
# --nbval-kernel-name were not both supplied
if self.parent.config.option.current_env:
if self.parent.config.option.nbval_current_env:
kernel_name = CURRENT_ENV_KERNEL_NAME
elif self.parent.config.option.nbval_kernel_name:
kernel_name = self.parent.config.option.nbval_kernel_name
Expand Down Expand Up @@ -276,8 +292,8 @@ def get_sanitize_files(self):
this is likely to change in the future
"""
if self.parent.config.option.sanitize_with is not None:
return [self.parent.config.option.sanitize_with]
if self.parent.config.option.nbval_sanitize_with is not None:
return [self.parent.config.option.nbval_sanitize_with]
else:
return []

Expand Down
6 changes: 3 additions & 3 deletions tests/Makefile
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
PYTEST ?= py.test
TEST_OPTIONS ?= -v --current-env
TEST_OPTIONS ?= -v --nbval-current-env

all: test

test:
$(PYTEST) $(TEST_OPTIONS) .
$(PYTEST) --nbval --current-env --sanitize-with sanitize_defaults.cfg sample_notebook.ipynb
$(PYTEST) --nbval --current-env latex-example.ipynb
$(PYTEST) --nbval --nbval-current-env --nbval-sanitize-with sanitize_defaults.cfg sample_notebook.ipynb
$(PYTEST) --nbval --nbval-current-env latex-example.ipynb

.PHONY: all test
4 changes: 2 additions & 2 deletions tests/test_collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def test_collection_nbval(testdir):
_write_nb(sources, os.path.join(str(testdir.tmpdir), 'test_collection.ipynb'))

# Run tests
items, recorder = testdir.inline_genitems('--nbval', '--current-env')
items, recorder = testdir.inline_genitems('--nbval', '--nbval-current-env')

# Debug output:
for item in items:
Expand All @@ -60,7 +60,7 @@ def test_collection_nbval_lax(testdir):
_write_nb(sources, os.path.join(str(testdir.tmpdir), 'test_collection.ipynb'))

# Run tests
items, recorder = testdir.inline_genitems('--nbval-lax', '--current-env')
items, recorder = testdir.inline_genitems('--nbval-lax', '--nbval-current-env')

# Debug output:
for item in items:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_sum():
str(testdir.tmpdir), 'test_coverage.ipynb'))

# Run tests
result = testdir.runpytest_inprocess('--nbval', '--current-env', '--cov', '.')
result = testdir.runpytest_inprocess('--nbval', '--nbval-current-env', '--cov', '.')

# Check tests went off as they should:
assert result.ret == 0
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ignore.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def test_conf_ignore_stderr(testdir):
str(testdir.tmpdir), 'test_ignore.ipynb'))

# Run tests
result = testdir.runpytest_subprocess('--nbval', '--current-env', '.')
result = testdir.runpytest_subprocess('--nbval', '--nbval-current-env', '.')

# Check tests went off as they should:
assert result.ret == 0
2 changes: 1 addition & 1 deletion tests/test_timeouts.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_timeouts(testdir):
str(testdir.tmpdir), 'test_timeouts.ipynb'))

# Run tests
result = testdir.inline_run('--nbval', '--current-env', '--nbval-cell-timeout', '5', '-s')
result = testdir.inline_run('--nbval', '--nbval-current-env', '--nbval-cell-timeout', '5', '-s')
reports = result.getreports('pytest_runtest_logreport')

# Setup and teardown of cells should have no issues:
Expand Down
2 changes: 1 addition & 1 deletion tests/test_unit_tests_in_notebooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ def create_test_cases_from_filenames():
@pytest.mark.parametrize("filename, correctoutcome", testdata, ids=testnames)
def test_print(filename, correctoutcome):

command = ["py.test", "--nbval", "-v", "--current-env", filename]
command = ["py.test", "--nbval", "-v", "--nbval-current-env", filename]
print("Starting parametrized test with filename={}, correctoutcome={}"
.format(filename, correctoutcome))
print("Command about to execute is '{}'".format(command))
Expand Down

0 comments on commit a13a03b

Please sign in to comment.