Build a minimal Sphinx project, returning the count of ipython directives that actually executed code.
(rst_content, builder="html", tags=None)
| 12 | |
| 13 | |
| 14 | def _build_sphinx(rst_content, builder="html", tags=None): |
| 15 | """Build a minimal Sphinx project, returning the count of ipython |
| 16 | directives that actually executed code.""" |
| 17 | original_run = IPythonDirective.run |
| 18 | executed = [] |
| 19 | |
| 20 | def tracking_run(self): |
| 21 | if not self._is_inside_excluded_only(): |
| 22 | executed.append(True) |
| 23 | return original_run(self) |
| 24 | |
| 25 | IPythonDirective.run = tracking_run |
| 26 | try: |
| 27 | with tempfile.TemporaryDirectory() as tmpdir: |
| 28 | srcdir = os.path.join(tmpdir, "source") |
| 29 | outdir = os.path.join(tmpdir, "build") |
| 30 | os.makedirs(srcdir) |
| 31 | |
| 32 | with open(os.path.join(srcdir, "conf.py"), "w") as f: |
| 33 | f.write("extensions = ['IPython.sphinxext.ipython_directive']\n") |
| 34 | |
| 35 | with open(os.path.join(srcdir, "index.rst"), "w") as f: |
| 36 | f.write(rst_content) |
| 37 | |
| 38 | app = Sphinx( |
| 39 | srcdir, |
| 40 | srcdir, |
| 41 | outdir, |
| 42 | os.path.join(tmpdir, "doctrees"), |
| 43 | builder, |
| 44 | status=io.StringIO(), |
| 45 | warning=None, |
| 46 | ) |
| 47 | |
| 48 | if tags: |
| 49 | for tag in tags: |
| 50 | app.tags.add(tag) |
| 51 | |
| 52 | app.build() |
| 53 | finally: |
| 54 | IPythonDirective.run = original_run |
| 55 | |
| 56 | return len(executed) |
| 57 | |
| 58 | |
| 59 | def test_only_excluded_skips_execution(): |
no test coverage detected
searching dependent graphs…