1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
# -*- coding: utf-8 -*-
import os
import sys
import sphinx
from six import StringIO
from sphinx_testing.path import path
from sphinx_testing.tmpdir import mkdtemp
from sphinx_testing.util import TestApp, with_app
if sys.version_info < (2, 7):
import unittest2 as unittest
else:
import unittest
if sys.version_info < (3,):
unittest.TestCase.assertCountEqual = unittest.TestCase.assertItemsEqual
if sys.version_info < (3, 3):
from mock import patch
else:
from unittest.mock import patch
class TestSphinxTesting(unittest.TestCase):
def test_TestApp(self):
try:
srcdir = path(__file__).dirname() / 'examples'
app = TestApp(srcdir=srcdir)
self.assertIsInstance(app._status, StringIO)
self.assertIsInstance(app._warning, StringIO)
if sphinx.__version__ < '1.0.0':
app.build(True, None)
else:
app.build()
self.assertIn('index.html', os.listdir(app.outdir))
finally:
app.cleanup()
def test_TestApp_when_srcdir_specified(self):
try:
srcdir = path(__file__).dirname() / 'examples'
app = TestApp(srcdir=srcdir)
self.assertEqual(srcdir, app.srcdir)
self.assertNotEqual(app.srcdir, app.builddir.dirname())
self.assertTrue(app.builddir.isdir())
self.assertCountEqual(['conf.py', 'index.rst'],
os.listdir(app.srcdir))
self.assertEqual((srcdir / 'conf.py').read_text(),
(app.srcdir / 'conf.py').read_text())
self.assertEqual((srcdir / 'index.rst').read_text(),
(app.srcdir / 'index.rst').read_text())
finally:
app.cleanup()
self.assertFalse(app.builddir.exists())
def test_TestApp_when_srcdir_is_None(self):
with self.assertRaises(AssertionError):
TestApp(srcdir=None)
def test_TestApp_when_create_new_srcdir(self):
try:
app = TestApp(create_new_srcdir=True)
self.assertIsNotNone(app.srcdir)
self.assertEqual(['conf.py'], os.listdir(app.srcdir))
self.assertEqual('', (app.srcdir / 'conf.py').read_text())
finally:
app.cleanup()
def test_TestApp_when_srcdir_and_create_new_srcdir_conflict(self):
with self.assertRaises(AssertionError):
TestApp(srcdir='examples', create_new_srcdir=True)
def test_TestApp_when_copy_srcdir_to_tmpdir(self):
try:
srcdir = path(__file__).dirname() / 'examples'
app = TestApp(srcdir=srcdir, copy_srcdir_to_tmpdir=True)
self.assertNotEqual(srcdir, app.srcdir)
self.assertEqual(app.srcdir, app.builddir.dirname())
self.assertTrue(app.builddir.isdir())
self.assertCountEqual(['_build', 'conf.py', 'index.rst'],
os.listdir(app.srcdir))
self.assertEqual((srcdir / 'conf.py').read_text(),
(app.srcdir / 'conf.py').read_text())
self.assertEqual((srcdir / 'index.rst').read_text(),
(app.srcdir / 'index.rst').read_text())
finally:
app.cleanup()
self.assertFalse(app.srcdir.exists())
self.assertFalse(app.builddir.exists())
def test_TestApp_cleanup(self):
app = TestApp(create_new_srcdir=True)
self.assertTrue(app.builddir.exists())
if sphinx.__version__ < '2.0':
with patch("sphinx.ext.autodoc.AutoDirective") as AutoDirective:
app.cleanup()
self.assertEqual(1, AutoDirective._registry.clear.call_count)
self.assertFalse(app.builddir.exists())
else:
app.cleanup()
self.assertFalse(app.builddir.exists())
def test_TestApp_cleanup_when_cleanup_on_errors(self):
app = TestApp(create_new_srcdir=True, cleanup_on_errors=False)
self.assertTrue(app.builddir.exists())
if sphinx.__version__ < '2.0':
with patch("sphinx.ext.autodoc.AutoDirective") as AutoDirective:
app.cleanup(error=True)
self.assertEqual(0, AutoDirective._registry.clear.call_count)
self.assertTrue(app.builddir.exists())
else:
app.cleanup(error=True)
self.assertTrue(app.builddir.exists())
if sphinx.__version__ < '2.0':
with patch("sphinx.ext.autodoc.AutoDirective") as AutoDirective:
app.cleanup(error=None)
self.assertEqual(1, AutoDirective._registry.clear.call_count)
self.assertFalse(app.builddir.exists())
else:
app.cleanup(error=None)
self.assertFalse(app.builddir.exists())
def test_with_app(self):
srcdir = path(__file__).dirname() / 'examples'
builddir = []
@with_app(srcdir=srcdir, copy_srcdir_to_tmpdir=True)
def execute(app, status, warning):
(app.srcdir / 'unknown.rst').write_text('')
builddir.append(app.builddir) # store to check outside of func
if sphinx.__version__ < '1.0.0':
app.build(True, None)
else:
app.build()
self.assertIsInstance(status, StringIO)
self.assertIsInstance(warning, StringIO)
self.assertIn('index.html', os.listdir(app.outdir))
self.assertIn('Running Sphinx', status.getvalue())
self.assertIn("WARNING: document isn't included in any toctree",
warning.getvalue())
execute()
self.assertFalse(builddir[0].exists())
@patch("sphinx_testing.util.mkdtemp")
def test_with_app_bad_args(self, _mkdtemp):
tmpdir = _mkdtemp.return_value = mkdtemp()
srcdir = path(__file__).dirname() / 'examples'
@with_app(srcdir=srcdir, copy_srcdir_to_tmpdir=True)
def execute(oops):
pass
with self.assertRaises(TypeError):
# TypeError: execute() takes 1 positional argument but 3 were given
execute()
self.assertFalse(tmpdir.exists())
def test_with_app_write_docstring(self):
@with_app(create_new_srcdir=True, write_docstring=True)
def execute(app, status, warning):
""" Hello world """
master_doc = (app.srcdir / (app.config.master_doc + '.rst'))
self.assertEqual('Hello world ', master_doc.read_text())
execute()
def test_with_app_write_docstring_with_master_doc(self):
@with_app(create_new_srcdir=True, write_docstring=True,
confoverrides={'master_doc': 'index'})
def execute(app, status, warning):
""" Hello world """
content = (app.srcdir / 'index.rst').read_text()
self.assertEqual('Hello world ', content)
execute()
def test_with_app_write_docstring_with_source_suffix(self):
@with_app(create_new_srcdir=True, write_docstring=True,
confoverrides={'source_suffix': '.txt'})
def execute(app, status, warning):
""" Hello world """
master_doc = (app.srcdir / (app.config.master_doc + '.txt'))
self.assertEqual('Hello world ', master_doc.read_text())
execute()
def test_with_app_write_docstring_by_name(self):
@with_app(create_new_srcdir=True, write_docstring='hello.rst')
def execute(app, status, warning):
""" Hello world """
content = (app.srcdir / 'hello.rst').read_text()
self.assertEqual('Hello world ', content)
execute()
def test_with_app_return_value(self):
@with_app(create_new_srcdir=True)
def execute(ret, app, status, warning):
return ret
s = 'What goes in, must come out'
self.assertEqual(execute(s), s)
|