Skip to content

Commit

Permalink
Remove unicode_literals and enable warnings.
Browse files Browse the repository at this point in the history
Fixes #2
  • Loading branch information
jamadden committed Mar 14, 2017
1 parent b42d9ef commit 9f241f5
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
3 changes: 1 addition & 2 deletions src/sphinxcontrib/programoutput/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@
.. moduleauthor:: Sebastian Wiesner <lunaryorn@gmail.com>
"""

from __future__ import (print_function, division, unicode_literals,
absolute_import)
from __future__ import print_function, division, absolute_import

import sys
import os
Expand Down
31 changes: 20 additions & 11 deletions src/sphinxcontrib/programoutput/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import shutil
import tempfile

from docutils.parsers.rst import directives
from sphinx.application import Sphinx

from functools import update_wrapper
Expand Down Expand Up @@ -49,13 +50,16 @@ def __get__(self, inst, class_):

class AppMixin(object):

document_content = 'dummy content'
document_content = '=============\ndummy content\n=============\n'

def setUp(self):
pass
# Avoid "WARNING: while setting up extension
# sphinxcontrib.programoutput: directive u'program-output' is
# already registered, it will be overridden".
self.directives = directives._directives.copy()

def tearDown(self):
pass
directives._directives = self.directives

@Lazy
def tmpdir(self):
Expand Down Expand Up @@ -84,7 +88,11 @@ def srcdir(self):
os.mkdir(content_directory)
content_document = os.path.join(content_directory, 'doc.rst')
with open(content_document, 'w') as f:
f.write("=====\n")
f.write("Title\n")
f.write("=====\n\n")
f.write(self.document_content)

return srcdir

@Lazy
Expand All @@ -99,7 +107,6 @@ def doctreedir(self):
def confoverrides(self):
return {}

build_app = False

@Lazy
def app(self):
Expand All @@ -110,13 +117,8 @@ def app(self):
outdir = self.outdir
doctreedir = self.doctreedir
confoverrides = self.confoverrides
# XXX: We are always ignoring warnings now, because newer versions
# of sphinx produce "WARNING: while setting up extension
# sphinxcontrib.programoutput: directive u'program-output' is
# already registered, it will be overridden". We really need to use
# a new app each time
#warningiserror = 'ignore_warnings' not in request.keywords
warningiserror = False
warningiserror = not self.ignore_warnings

app = Sphinx(str(srcdir), str(srcdir), str(outdir), str(doctreedir), 'html',
status=None, warning=None, freshenv=None,
warningiserror=warningiserror, confoverrides=confoverrides)
Expand All @@ -128,6 +130,10 @@ def app(self):
def build_app(self):
return False

@Lazy
def ignore_warnings(self):
return False

@Lazy
def doctree(self):
getattr(self, 'build_app')
Expand All @@ -140,6 +146,9 @@ def factory(f):
def w(self):
self.document_content = content
if kwargs:
if 'ignore_warnings' in kwargs:
getattr(self, 'ignore_warnings')
self.ignore_warnings = kwargs.pop("ignore_warnings")
getattr(self, 'confoverrides')
self.confoverrides = kwargs
f(self)
Expand Down
29 changes: 14 additions & 15 deletions src/sphinxcontrib/programoutput/tests/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

from __future__ import (print_function, division, unicode_literals,
absolute_import)
from __future__ import print_function, division, absolute_import

import os
import pickle
Expand All @@ -35,23 +34,23 @@
from . import AppMixin


def assert_cache(cache, cmd, output, returncode=0):
result = (returncode, output)
assert not cache
assert cache[cmd] == result
assert cache == {cmd: result}

class TestCache(AppMixin,
unittest.TestCase):

def assert_cache(self, cache, cmd, output, returncode=0):
result = (returncode, output)
self.assertFalse(cache)
self.assertEqual(cache[cmd], result)
self.assertEqual(cache, {cmd: result})

def test_simple(self):
cache = ProgramOutputCache()
assert_cache(cache, Command(['echo', 'blök']), 'blök')
self.assert_cache(cache, Command([u'echo', u'blök']), u'blök')


def test_shell(self):
cache = ProgramOutputCache()
assert_cache(cache, Command('echo blök', shell=True), 'blök')
self.assert_cache(cache, Command(u'echo blök', shell=True), u'blök')


def test_working_directory(self):
Expand All @@ -60,7 +59,7 @@ def test_working_directory(self):
os.mkdir(cwd)
cwd = os.path.realpath(os.path.normpath(str(cwd)))
cmd = ['python', '-c', 'import sys, os; sys.stdout.write(os.getcwd())']
assert_cache(cache, Command(cmd, working_directory=cwd), cwd)
self.assert_cache(cache, Command(cmd, working_directory=cwd), cwd)


def test_working_directory_shell(self):
Expand All @@ -69,25 +68,25 @@ def test_working_directory_shell(self):
os.mkdir(cwd)
cwd = os.path.realpath(os.path.normpath(str(cwd)))
cmd = Command('echo $PWD', working_directory=cwd, shell=True)
assert_cache(cache, cmd, cwd)
self.assert_cache(cache, cmd, cwd)


def test_hidden_standard_error(self):
cache = ProgramOutputCache()
cmd = ['python', '-c', 'import sys; sys.stderr.write("spam")']
assert_cache(cache, Command(cmd, hide_standard_error=True), '')
self.assert_cache(cache, Command(cmd, hide_standard_error=True), '')


def test_nonzero_return_code(self):
cache = ProgramOutputCache()
cmd = ['python', '-c', 'import sys; sys.exit(1)']
assert_cache(cache, Command(cmd), '', returncode=1)
self.assert_cache(cache, Command(cmd), '', returncode=1)


def test_nonzero_return_code_shell(self):
cache = ProgramOutputCache()
cmd = "python -c 'import sys; sys.exit(1)'"
assert_cache(cache, Command(cmd, shell=True), '', returncode=1)
self.assert_cache(cache, Command(cmd, shell=True), '', returncode=1)

def test_cache_pickled(self):
doctreedir = self.doctreedir
Expand Down
31 changes: 14 additions & 17 deletions src/sphinxcontrib/programoutput/tests/test_directive.py
Original file line number Diff line number Diff line change
Expand Up @@ -245,25 +245,26 @@ def test_ellipsis_start_and_negative_stop(self):

@with_content("""\
.. program-output:: python -c 'import sys; sys.exit(1)'""")
@unittest.skip("Fails")
def test_unexpected_return_code(self):
with self.assertRaises(SphinxWarning) as excinfo:
self.app.build()
exc_message = 'WARNING: Unexpected return code 1 from command {0!r}\n'.format(
"python -c 'import sys; sys.exit(1)'")
assert str(excinfo.value) == exc_message
self.assertIn('WARNING: Unexpected return code 1 from command',
excinfo.exception.args[0])
self.assertIn("python -c 'import sys; sys.exit(1)'",
excinfo.exception.args[0])


@with_content("""\
.. program-output:: python -c 'import sys; sys.exit(1)'
:shell:""")
@unittest.skip("Fails")
def test_shell_with_unexpected_return_code(self):
with self.assertRaises(SphinxWarning) as excinfo:
self.app.build()
exc_message = 'WARNING: Unexpected return code 1 from command {0!r}\n'.format(
"python -c 'import sys; sys.exit(1)'")
assert str(excinfo.value) == exc_message
self.assertIn('WARNING: Unexpected return code 1 from command',
excinfo.exception.args[0])
self.assertIn("python -c 'import sys; sys.exit(1)'",
excinfo.exception.args[0])



@with_content("""\
Expand All @@ -290,24 +291,24 @@ def test_prompt_with_return_code(self):
returncode=1)


@with_content(u".. program-output:: 'spam with eggs'")
@with_content(u".. program-output:: 'spam with eggs'", ignore_warnings=True)
def test_non_existing_executable(self):
# check that a proper error message appears in the document
message = self.doctree.next_node(system_message)
assert message
srcfile = os.path.join(self.srcdir, 'content', 'doc.rst')
self.assertEqual(message['source'], srcfile)
self.assertEqual(message['line'], 1)
self.assertEqual(message['line'], 5)

message_text = message.astext()
self.assertIn(srcfile, message_text)
self.assertIn('spam with eggs', message_text)
self.assertIn("No such file or directory", message_text)
self.assertIn("Errno", message_text)


@with_content("""\
.. program-output:: echo spam
:cwd: ./subdir""")
:cwd: ./subdir""", ignore_warnings=True)
def test_non_existing_working_directory(self):
# check that a proper error message appears in the document
doctree = self.doctree
Expand All @@ -316,12 +317,8 @@ def test_non_existing_working_directory(self):
assert message
srcfile = os.path.join(srcdir, 'content', 'doc.rst')
self.assertEqual(message['source'], srcfile)
self.assertEqual(message['line'], 1)
self.assertEqual(message['line'], 5)

msgtemplate = ("{0}:4: (ERROR/3) Command {1!r} failed: "
"[Errno 2] No such file or directory: {2!r}")
filename = os.path.join(os.path.realpath(os.path.join(srcdir, 'content')),
'subdir')
message_text = message.astext()
self.assertIn(srcfile, message_text)
self.assertIn('subdir', message_text)
Expand Down

0 comments on commit 9f241f5

Please sign in to comment.