Fix expectedFailure tests

main
Fabien Benureau 9 years ago
parent e979aae85b
commit 620eef4ace

@ -4,6 +4,9 @@ from __future__ import print_function, unicode_literals
import unittest import unittest
import re import re
import os import os
import sys
import six
import dotdot import dotdot
import fake_env import fake_env
@ -23,6 +26,15 @@ PRINT_OUTPUT=False
CAPTURE_OUTPUT=True CAPTURE_OUTPUT=True
class FakeSystemExit(Exception):
"""\
SystemExit exceptions are replaced by FakeSystemExit in the execute_cmds()
function, so they can be catched by ExpectedFailure tests in Python 2.x.
If a code is accepted to raise SystemExit, catch FakeSystemExit instead.
"""
pass
# code for fake fs # code for fake fs
class TestFakeInput(unittest.TestCase): class TestFakeInput(unittest.TestCase):
@ -72,42 +84,50 @@ class CommandTestCase(unittest.TestCase):
3. the expected output on stdout, verified with assertEqual. 3. the expected output on stdout, verified with assertEqual.
4. the expected output on stderr, verified with assertEqual. 4. the expected output on stderr, verified with assertEqual.
""" """
outs = [] try:
for cmd in cmds: outs = []
inputs = [] for cmd in cmds:
expected_out, expected_err = None, None inputs = []
actual_cmd = cmd expected_out, expected_err = None, None
if not isinstance(cmd, p3.ustr): actual_cmd = cmd
actual_cmd = cmd[0] if not isinstance(cmd, p3.ustr):
if len(cmd) == 2: # Inputs provided actual_cmd = cmd[0]
inputs = cmd[1] if len(cmd) == 2: # Inputs provided
if len(cmd) == 3: # Expected output provided inputs = cmd[1]
capture_output = True if len(cmd) == 3: # Expected output provided
expected_out = color.undye(cmd[2]) capture_output = True
if len(cmd) == 4: # Expected error output provided expected_out = color.undye(cmd[2])
expected_err = color.undye(cmd[3]) if len(cmd) == 4: # Expected error output provided
# Always set fake input: test should not ask unexpected user input expected_err = color.undye(cmd[3])
input = fake_env.FakeInput(inputs, [content, uis, p3]) # Always set fake input: test should not ask unexpected user input
input.as_global() input = fake_env.FakeInput(inputs, [content, uis, p3])
try: input.as_global()
if capture_output: try:
_, stdout, stderr = fake_env.redirect(pubs_cmd.execute)( if capture_output:
actual_cmd.split()) _, stdout, stderr = fake_env.redirect(pubs_cmd.execute)(
actual_out = color.undye(stdout) actual_cmd.split())
actual_err = color.undye(stderr) actual_out = color.undye(stdout)
if expected_out is not None: actual_err = color.undye(stderr)
self.assertEqual(actual_out, expected_out) if expected_out is not None:
if expected_err is not None: self.assertEqual(actual_out, expected_out)
self.assertEqual(actual_err, expected_err) if expected_err is not None:
outs.append(color.undye(actual_out)) self.assertEqual(actual_err, expected_err)
else: outs.append(color.undye(actual_out))
pubs_cmd.execute(actual_cmd.split()) else:
except fake_env.FakeInput.UnexpectedInput: pubs_cmd.execute(actual_cmd.split())
self.fail('Unexpected input asked by command: {}.'.format( except fake_env.FakeInput.UnexpectedInput:
actual_cmd)) self.fail('Unexpected input asked by command: {}.'.format(
if PRINT_OUTPUT: actual_cmd))
print(outs) if PRINT_OUTPUT:
return outs print(outs)
return outs
except SystemExit as exc:
exc_class, exc, tb = sys.exc_info()
if sys.version_info.major == 2:
# using six to avoid a SyntaxError in Python 3.x
six.reraise(FakeSystemExit, exc, tb)
else:
raise FakeSystemExit(exc).with_traceback(tb)
def tearDown(self): def tearDown(self):
fake_env.unset_fake_fs([content, filebroker, conf, init_cmd, import_cmd, configobj]) fake_env.unset_fake_fs([content, filebroker, conf, init_cmd, import_cmd, configobj])
@ -189,7 +209,7 @@ class TestAdd(DataCommandTestCase):
cmds = ['pubs init', cmds = ['pubs init',
('pubs add /bibexamples/utf8.bib', [], '', err), ('pubs add /bibexamples/utf8.bib', [], '', err),
] ]
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
self.execute_cmds(cmds) self.execute_cmds(cmds)
def test_add_doc_nocopy_does_not_copy(self): def test_add_doc_nocopy_does_not_copy(self):
@ -213,16 +233,17 @@ class TestAdd(DataCommandTestCase):
'pubs add /data/pagerank.bib', 'pubs add /data/pagerank.bib',
'pubs add -k Page99 /data/turing1950.bib', 'pubs add -k Page99 /data/turing1950.bib',
] ]
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
self.execute_cmds(cmds) self.execute_cmds(cmds)
# To be fixed
# def test_leading_citekey_space(self): @unittest.expectedFailure
# cmds = ['pubs init', def test_leading_citekey_space(self):
# 'pubs add /data/leadingspace.bib', cmds = ['pubs init',
# 'pubs rename LeadingSpace NoLeadingSpace', 'pubs add /bibexamples/leadingspace.bib',
# ] 'pubs rename LeadingSpace NoLeadingSpace',
# self.execute_cmds(cmds) ]
self.execute_cmds(cmds)
class TestList(DataCommandTestCase): class TestList(DataCommandTestCase):
@ -349,7 +370,7 @@ class TestTag(DataCommandTestCase):
def test_wrong_citekey(self): def test_wrong_citekey(self):
cmds = ['pubs tag Page999 a', cmds = ['pubs tag Page999 a',
] ]
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
self.execute_cmds(cmds) self.execute_cmds(cmds)
@ -428,7 +449,7 @@ class TestUsecase(DataCommandTestCase):
self.assertEqual(clean(correct[4]), clean(out[4])) self.assertEqual(clean(correct[4]), clean(out[4]))
def test_editor_abort(self): def test_editor_abort(self):
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
cmds = ['pubs init', cmds = ['pubs init',
('pubs add', ['abc', 'n']), ('pubs add', ['abc', 'n']),
('pubs add', ['abc', 'y', 'abc', 'n']), ('pubs add', ['abc', 'y', 'abc', 'n']),
@ -500,7 +521,7 @@ class TestUsecase(DataCommandTestCase):
'pubs update' 'pubs update'
] ]
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
self.execute_cmds(cmds) self.execute_cmds(cmds)
def test_add_with_tag(self): def test_add_with_tag(self):
@ -516,9 +537,9 @@ class TestUsecase(DataCommandTestCase):
'pubs add data/pagerank.bib', 'pubs add data/pagerank.bib',
'pubs doc open Page99' 'pubs doc open Page99'
] ]
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
self.execute_cmds(cmds) self.execute_cmds(cmds)
with self.assertRaises(SystemExit): with self.assertRaises(FakeSystemExit):
cmds[-1] == 'pubs doc open Page8' cmds[-1] == 'pubs doc open Page8'
self.execute_cmds(cmds) self.execute_cmds(cmds)

Loading…
Cancel
Save