Extended test covering README file to also cover .rst doc files.
NB: .rst docs are currently slightly broken and need updating.
import os
import sys
+from glob import glob
from os.path import join, dirname
from subprocess import Popen, PIPE
+from tempfile import NamedTemporaryFile
from unittest.case import TestCase
import eventsourcing
@@ -10,49 +12,71 @@ import eventsourcing
class TestReadmeFile(TestCase):
def test_code_snippets_in_readme_file(self):
+ path = join(dirname(dirname(eventsourcing.__file__)), 'README.md')
+ self.check_code_snippets_in_file(path)
+ for path in glob(join(dirname(dirname(eventsourcing.__file__)), 'docs', '*', '*.rst')):
+ print("Testing code snippets in {}".format(path))
+ self.check_code_snippets_in_file(path)
+
+ def check_code_snippets_in_file(self, doc_path):
# Extract lines of Python code from the README.md file.
- readme_filename = 'README.md'
- readme_path = join(dirname(dirname(eventsourcing.__file__)), readme_filename)
- if not os.path.exists(readme_path):
+ if not os.path.exists(doc_path):
self.skipTest("Skipped test because usage instructions in README file are "
"not available for testing once this package is installed")
- temp_filename = '.README.py'
- temp_path = os.path.join(os.path.dirname(readme_path), temp_filename)
lines = []
count_code_lines = 0
is_code = False
- with open(readme_path) as file:
- for line in file:
- if is_code and line.startswith('```'):
+ is_md = False
+ is_rst = False
+ with open(doc_path) as doc_file:
+ for line in doc_file:
+ line = line.strip('\n')
+ if is_code and is_md and line.startswith('```'):
+ is_code = False
+ line = ''
+ elif is_code and is_rst and line and not line.startswith(' '):
is_code = False
line = ''
elif is_code:
- line = line.strip('\n')
+ if is_rst:
+ line = line[4:]
count_code_lines += 1
elif line.startswith('```python'):
is_code = True
+ is_md = True
+ line = ''
+ elif line.startswith('.. code:: python'):
+ is_code = True
+ is_rst = True
line = ''
else:
line = ''
lines.append(line)
- self.assertTrue(count_code_lines)
+ print("There are {} lines of code in {}".format(count_code_lines, doc_path))
+ # print(lines)
+ # print('\n'.join(lines) + '\n')
# Write the code into a temp file.
- with open(temp_path, 'w+') as readme_py:
- readme_py.writelines("\n".join(lines) + '\n')
+ tempfile = NamedTemporaryFile('w+')
+ temp_path = tempfile.name
+
+ # for line in lines:
+ # tempfile.write(line + '\n')
+ tempfile.writelines("\n".join(lines) + '\n')
+ tempfile.flush()
# Run the code and catch errors.
p = Popen([sys.executable, temp_path], stdout=PIPE, stderr=PIPE)
out, err = p.communicate()
- out = out.decode('utf8').replace(temp_filename, readme_filename)
- err = err.decode('utf8').replace(temp_filename, readme_filename)
+ out = out.decode('utf8').replace(temp_path, doc_path)
+ err = err.decode('utf8').replace(temp_path, doc_path)
exit_status = p.wait()
# Check for errors running the code.
self.assertEqual(exit_status, 0, "Usage exit status {}:\n{}\n{}".format(exit_status, out, err))
- # Delete the temp file.
- os.unlink(temp_path)
+ # Close (deletes) the tempfile.
+ tempfile.close()
