My original question was "can I doctest bash"?
I've found a few similar questions, and the answers are pretty much:
- Use
subprocess - No
- Use other module (
docshtestwas the best I could find)
I want to go on another approach. Is it possible to patch doctest? I changed the code myself to run shell instead of python. Here is my diff:
1329,1330c1329
< exec(compile(example.source, filename, "single",
< compileflags, 1), test.globs)
---
> import subprocess; proc = subprocess.run(example.source, shell=True, stdout=subprocess.PIPE)
1339c1338
< got = self._fakeout.getvalue() # the actual output
---
> got = proc.stdout.decode() # the actual output
which obviously is not ideal, but it gets the job done:
Example run:
(venv) docs$ make doctest
Running Sphinx v4.4.0
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [doctest]: targets for 1 source files that are out of date
updating environment: 0 added, 1 changed, 0 removed
reading sources... [100%] index
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
running tests...
Document: index
---------------
**********************************************************************
File "index.rst", line 3, in default
Failed example:
echo bla
Expected:
blu
Got:
bla
**********************************************************************
1 items had failures:
1 of 2 in default
2 tests in 1 items.
1 passed and 1 failed.
***Test Failed*** 1 failures.
Doctest summary
===============
2 tests
1 failure in tests
0 failures in setup code
0 failures in cleanup code
build finished with problems.
Makefile:20: recipe for target 'doctest' failed
make: *** [doctest] Error 1
This was run on an index.rst containing shell examples instead of python examples:
.. doctest::
>>> echo bla
bla
>>> echo bla
blu
So, my question is this - how can I patch doctest to allow users to run bash examples as well as python examples?
Do I need write a wrapper module that mocks things in doctest, or can I fork something (this is an official python library, so I guess not).
What would be the best way to still use all the goodies doctest offers, but tweak its mechanics a bit, in a way that suits me (and I think others)?