summaryrefslogtreecommitdiff
path: root/tests/test_transcript.py
diff options
context:
space:
mode:
authorTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-20 23:47:50 -0400
committerTodd Leonhardt <todd.leonhardt@gmail.com>2019-05-20 23:47:50 -0400
commit2f0dbf5bad19d96880e2ef795660db1b8f04cdc7 (patch)
tree2d3ef06b705d8347fac5c9a7fe73f472973c4a41 /tests/test_transcript.py
parent1fd474fc11d22e0c1201784cf4602139e3f7c637 (diff)
downloadcmd2-git-2f0dbf5bad19d96880e2ef795660db1b8f04cdc7.tar.gz
Refactor exit_code implementation
cmd2.Cmd.cmdloop() now returns self.exit_code which should be an integer Also: - Refactored examples to call sys.exit(app.cmdloop()) in their __main__ - Running transcript tests now sets the exit_code accordingly based on success/failure - Updated CHANGELOG - Updated README - Updated Sphinx docs - Added unit test for case when transcript test fails
Diffstat (limited to 'tests/test_transcript.py')
-rw-r--r--tests/test_transcript.py29
1 files changed, 28 insertions, 1 deletions
diff --git a/tests/test_transcript.py b/tests/test_transcript.py
index acdbe703..7a2bc38a 100644
--- a/tests/test_transcript.py
+++ b/tests/test_transcript.py
@@ -127,7 +127,8 @@ def test_transcript(request, capsys, filename, feedback_to_output):
testargs = ['prog', '-t', transcript_file]
with mock.patch.object(sys, 'argv', testargs):
# Run the command loop
- app.cmdloop()
+ sys_exit_code = app.cmdloop()
+ assert sys_exit_code == 0
# Check for the unittest "OK" condition for the 1 test which ran
expected_start = ".\n----------------------------------------------------------------------\nRan 1 test in"
@@ -247,3 +248,29 @@ def test_parse_transcript_expected(expected, transformed):
testcase = TestMyAppCase()
assert testcase._transform_transcript_expected(expected) == transformed
+
+
+def test_transcript_failure(request, capsys):
+ # Create a cmd2.Cmd() instance and make sure basic settings are
+ # like we want for test
+ app = CmdLineApp()
+ app.feedback_to_output = False
+
+ # Get location of the transcript
+ test_dir = os.path.dirname(request.module.__file__)
+ transcript_file = os.path.join(test_dir, 'transcripts', 'failure.txt')
+
+ # Need to patch sys.argv so cmd2 doesn't think it was called with
+ # arguments equal to the py.test args
+ testargs = ['prog', '-t', transcript_file]
+ with mock.patch.object(sys, 'argv', testargs):
+ # Run the command loop
+ sys_exit_code = app.cmdloop()
+ assert sys_exit_code != 0
+
+ # Check for the unittest "OK" condition for the 1 test which ran
+ expected_start = "F\n======================================================================\nFAIL: runTest"
+ expected_end = "s\n\nFAILED (failures=1)\nTests failed\n"
+ _, err = capsys.readouterr()
+ assert err.startswith(expected_start)
+ assert err.endswith(expected_end)