From 6cdf70823b344b99d6623af19fb618a9c2dbdad4 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Thu, 6 Jun 2019 23:27:02 -0400 Subject: Refactored how and when transcript file glob patterns are expanded in order to present a better error message to user --- tests/test_transcript.py | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 70c9119c..d39fe286 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -268,9 +268,28 @@ def test_transcript_failure(request, capsys): sys_exit_code = app.cmdloop() assert sys_exit_code != 0 - # Check for the unittest "OK" condition for the 1 test which ran expected_start = "File " expected_end = "s\n\nFAILED (failures=1)\n\n" _, err = capsys.readouterr() assert err.startswith(expected_start) assert err.endswith(expected_end) + + +def test_transcript_no_file(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 + + # Need to patch sys.argv so cmd2 doesn't think it was called with + # arguments equal to the py.test args + testargs = ['prog', '-t'] + 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 = 'No test files found - nothing to test\n' + _, err = capsys.readouterr() + assert err == expected -- cgit v1.2.1 From 0b0ccc5b6e00258d9fe7e98f3a2dd1a1081ac457 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 7 Jun 2019 16:54:52 -0400 Subject: Removed copyright headers from source files and updated LICENSE with current year --- tests/test_transcript.py | 3 --- 1 file changed, 3 deletions(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index d39fe286..9d804a88 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -2,9 +2,6 @@ # flake8: noqa E302 """ Cmd2 functional testing based on transcript - -Copyright 2016 Federico Ceratto -Released under MIT license, see LICENSE file """ import argparse import os -- cgit v1.2.1 From 18f001c603135010582256984e92f6835898171e Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Mon, 10 Jun 2019 11:33:07 -0400 Subject: Added unit test for stopping during transcript generation --- tests/test_transcript.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 9d804a88..4af547b1 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -216,6 +216,29 @@ def test_load_record_transcript(base_app, request): assert xscript == expected +def test_generate_transcript_stop(capsys): + # Verify transcript generation stops when a command returns True for stop + app = CmdLineApp() + + # Make a tmp file to use as a transcript + fd, transcript_fname = tempfile.mkstemp(prefix='', suffix='.trn') + os.close(fd) + + # This should run all commands return False for stop + commands = ['help', 'alias'] + stop = app._generate_transcript(commands, transcript_fname) + _, err = capsys.readouterr() + assert not stop + assert err.startswith("2 commands") + + # Since quit returns True for stop, only the first 2 commands will run and stop should be True + commands = ['help', 'quit', 'alias'] + stop = app._generate_transcript(commands, transcript_fname) + _, err = capsys.readouterr() + assert stop + assert err.startswith("2 commands") + + @pytest.mark.parametrize('expected, transformed', [ # strings with zero or one slash or with escaped slashes means no regular # expression present, so the result should just be what re.escape returns. -- cgit v1.2.1 From 77633bc4498d6f11b07ea0a3bd13d4830f809ff8 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Tue, 11 Jun 2019 17:39:11 -0400 Subject: Removed support for cmd.cmdqueue allow_cli_args is now an argument to __init__ instead of a cmd2 class member --- tests/test_transcript.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 4af547b1..214ef87b 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -110,11 +110,6 @@ def test_commands_at_invocation(): ('word_boundaries.txt', False), ]) def test_transcript(request, capsys, filename, feedback_to_output): - # Create a cmd2.Cmd() instance and make sure basic settings are - # like we want for test - app = CmdLineApp() - app.feedback_to_output = feedback_to_output - # Get location of the transcript test_dir = os.path.dirname(request.module.__file__) transcript_file = os.path.join(test_dir, 'transcripts', filename) @@ -123,6 +118,11 @@ def test_transcript(request, capsys, filename, feedback_to_output): # arguments equal to the py.test args testargs = ['prog', '-t', transcript_file] with mock.patch.object(sys, 'argv', testargs): + # Create a cmd2.Cmd() instance and make sure basic settings are + # like we want for test + app = CmdLineApp() + app.feedback_to_output = feedback_to_output + # Run the command loop sys_exit_code = app.cmdloop() assert sys_exit_code == 0 @@ -192,7 +192,6 @@ def test_load_record_transcript(base_app, request): test_dir = os.path.dirname(request.module.__file__) filename = os.path.join(test_dir, 'scripts', 'help.txt') - assert base_app.cmdqueue == [] assert base_app._script_dir == [] assert base_app._current_script_dir is None @@ -203,7 +202,6 @@ def test_load_record_transcript(base_app, request): # Run the load command with the -r option to generate a transcript run_cmd(base_app, 'load {} -t {}'.format(filename, transcript_fname)) - assert base_app.cmdqueue == [] assert base_app._script_dir == [] assert base_app._current_script_dir is None @@ -271,11 +269,6 @@ def test_parse_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') @@ -284,6 +277,11 @@ def test_transcript_failure(request, capsys): # arguments equal to the py.test args testargs = ['prog', '-t', transcript_file] with mock.patch.object(sys, 'argv', testargs): + # Create a cmd2.Cmd() instance and make sure basic settings are + # like we want for test + app = CmdLineApp() + app.feedback_to_output = False + # Run the command loop sys_exit_code = app.cmdloop() assert sys_exit_code != 0 @@ -296,15 +294,15 @@ def test_transcript_failure(request, capsys): def test_transcript_no_file(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 - # Need to patch sys.argv so cmd2 doesn't think it was called with # arguments equal to the py.test args testargs = ['prog', '-t'] with mock.patch.object(sys, 'argv', testargs): + # Create a cmd2.Cmd() instance and make sure basic settings are + # like we want for test + app = CmdLineApp() + app.feedback_to_output = False + # Run the command loop sys_exit_code = app.cmdloop() assert sys_exit_code != 0 -- cgit v1.2.1 From bc24624c93d80e64fba9d59455b7bc5a24eb4a1a Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 12 Jun 2019 11:58:53 -0400 Subject: Fixed unit tests --- tests/test_transcript.py | 38 ++++++++++++++++++++------------------ 1 file changed, 20 insertions(+), 18 deletions(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 214ef87b..4142e386 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -87,10 +87,11 @@ def test_commands_at_invocation(): expected = "This is an intro banner ...\nhello\nGracie\n" with mock.patch.object(sys, 'argv', testargs): app = CmdLineApp() - app.stdout = StdSim(app.stdout) - app.cmdloop() - out = app.stdout.getvalue() - assert out == expected + + app.stdout = StdSim(app.stdout) + app.cmdloop() + out = app.stdout.getvalue() + assert out == expected @pytest.mark.parametrize('filename,feedback_to_output', [ ('bol_eol.txt', False), @@ -121,11 +122,12 @@ def test_transcript(request, capsys, filename, feedback_to_output): # Create a cmd2.Cmd() instance and make sure basic settings are # like we want for test app = CmdLineApp() - app.feedback_to_output = feedback_to_output - # Run the command loop - sys_exit_code = app.cmdloop() - assert sys_exit_code == 0 + app.feedback_to_output = feedback_to_output + + # 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 = ".\n----------------------------------------------------------------------\nRan 1 test in" @@ -280,11 +282,12 @@ 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 - # Run the command loop - sys_exit_code = app.cmdloop() - assert sys_exit_code != 0 + app.feedback_to_output = False + + # Run the command loop + sys_exit_code = app.cmdloop() + assert sys_exit_code != 0 expected_start = "File " expected_end = "s\n\nFAILED (failures=1)\n\n" @@ -298,14 +301,13 @@ def test_transcript_no_file(request, capsys): # arguments equal to the py.test args testargs = ['prog', '-t'] with mock.patch.object(sys, 'argv', testargs): - # Create a cmd2.Cmd() instance and make sure basic settings are - # like we want for test app = CmdLineApp() - app.feedback_to_output = False - # Run the command loop - sys_exit_code = app.cmdloop() - assert sys_exit_code != 0 + app.feedback_to_output = False + + # 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 = 'No test files found - nothing to test\n' -- cgit v1.2.1 From 6b38d416a38fdf0d7fec8425d2761800cb891f06 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 12 Jun 2019 15:19:48 -0400 Subject: Transcript generation no longer terminates _cmdloop() when a command returns True for stop --- tests/test_transcript.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index 4142e386..ed4c2755 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -231,12 +231,11 @@ def test_generate_transcript_stop(capsys): assert not stop assert err.startswith("2 commands") - # Since quit returns True for stop, only the first 2 commands will run and stop should be True + # Since quit returns True for stop, only the first 2 commands will run commands = ['help', 'quit', 'alias'] - stop = app._generate_transcript(commands, transcript_fname) + app._generate_transcript(commands, transcript_fname) _, err = capsys.readouterr() - assert stop - assert err.startswith("2 commands") + assert "triggered a stop" in err @pytest.mark.parametrize('expected, transformed', [ -- cgit v1.2.1 From 8e90658aac568ca5948d99a3103a5c8ae08f71a5 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 12 Jun 2019 17:21:29 -0400 Subject: Updated unit test --- tests/test_transcript.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) (limited to 'tests/test_transcript.py') diff --git a/tests/test_transcript.py b/tests/test_transcript.py index ed4c2755..5dd39e1b 100644 --- a/tests/test_transcript.py +++ b/tests/test_transcript.py @@ -224,18 +224,17 @@ def test_generate_transcript_stop(capsys): fd, transcript_fname = tempfile.mkstemp(prefix='', suffix='.trn') os.close(fd) - # This should run all commands return False for stop + # This should run all commands commands = ['help', 'alias'] - stop = app._generate_transcript(commands, transcript_fname) + app._generate_transcript(commands, transcript_fname) _, err = capsys.readouterr() - assert not stop assert err.startswith("2 commands") # Since quit returns True for stop, only the first 2 commands will run commands = ['help', 'quit', 'alias'] app._generate_transcript(commands, transcript_fname) _, err = capsys.readouterr() - assert "triggered a stop" in err + assert err.startswith("Command 2 triggered a stop") @pytest.mark.parametrize('expected, transformed', [ -- cgit v1.2.1