|
1 | 1 | from __future__ import print_function
|
2 | 2 |
|
3 | 3 | import os
|
4 |
| -from importlib import import_module, reload |
| 4 | +from importlib import import_module, invalidate_caches, reload |
5 | 5 | from pathlib import Path
|
| 6 | +from unittest.mock import patch |
6 | 7 |
|
7 | 8 | from nbformat import read
|
8 | 9 | from pytest import ExitCode, Pytester
|
@@ -207,3 +208,47 @@ def test_when_not_json_then_correct_err_msg(pytester: Pytester, testdir2: Never)
|
207 | 208 | assert longrepr is not None
|
208 | 209 | assert "NBMAKE INTERNAL ERROR" not in longrepr.term
|
209 | 210 | assert "json" in longrepr.term
|
| 211 | + |
| 212 | + |
| 213 | +def test_when_testing_nbs_then_submit_metrics(pytester: Pytester): |
| 214 | + write_nb(failing_nb, Path(pytester.path) / "a.ipynb") |
| 215 | + |
| 216 | + with patch("nbmake.metrics.submit_event", return_value="1") as mock_method: |
| 217 | + hook_recorder = pytester.inline_run("--nbmake", "-v") |
| 218 | + assert hook_recorder.ret == ExitCode.TESTS_FAILED |
| 219 | + mock_method.assert_called_once() |
| 220 | + |
| 221 | + |
| 222 | +def test_when_metrics_fail_then_ignore(pytester: Pytester): |
| 223 | + write_nb(failing_nb, Path(pytester.path) / "a.ipynb") |
| 224 | + |
| 225 | + with patch("nbmake.metrics.submit_event", return_value="1") as mock_method: |
| 226 | + mock_method.side_effect = Exception("test") |
| 227 | + write_nb(passing_nb, Path(pytester.path) / "a.ipynb") |
| 228 | + |
| 229 | + items, hook_recorder = pytester.inline_genitems("--nbmake") |
| 230 | + |
| 231 | + assert hook_recorder.ret == ExitCode.OK |
| 232 | + assert len(items) == 1 |
| 233 | + mock_method.assert_called_once() |
| 234 | + |
| 235 | + |
| 236 | +def test_when_metrics_disabled_dont_log_metrics(pytester: Pytester): |
| 237 | + write_nb(failing_nb, Path(pytester.path) / "a.ipynb") |
| 238 | + # not thread-safe |
| 239 | + os.environ["NBMAKE_METRICS"] = "0" |
| 240 | + |
| 241 | + with patch("nbmake.metrics.submit_event", return_value="1") as mock_method: |
| 242 | + hook_recorder = pytester.inline_run("--nbmake", "-v") |
| 243 | + assert hook_recorder.ret == ExitCode.TESTS_FAILED |
| 244 | + mock_method.assert_not_called() |
| 245 | + |
| 246 | + del os.environ["NBMAKE_METRICS"] |
| 247 | + |
| 248 | + |
| 249 | +def test_when_no_nbmake_flag_then_no_metrics(pytester: Pytester): |
| 250 | + write_nb(failing_nb, Path(pytester.path) / "a.ipynb") |
| 251 | + with patch("nbmake.metrics.submit_event", return_value="1") as mock_method: |
| 252 | + hook_recorder = pytester.inline_run("-v") |
| 253 | + assert hook_recorder.ret == ExitCode.NO_TESTS_COLLECTED |
| 254 | + mock_method.assert_not_called() |
0 commit comments