-
Notifications
You must be signed in to change notification settings - Fork 127
Unit test coverage for transcripts from history #387
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
c539ef4
Fix commented out transcript tests
kotfu 6d2f520
Moved some argparse tests from test_transcript.py to test_argparse.py
kotfu dedd045
Unit test for history transcript generation
kotfu bf72952
Fix #384, multiline commands now appear properly in transcripts
kotfu 23824e8
Fix transcript generate to close #385
kotfu 6c33b58
Refactor transcript from history implementation and test
kotfu f826598
Merge branch 'master' into transcript_tests
tleonhardt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,12 +10,13 @@ | |
| import sys | ||
| import re | ||
| import random | ||
| import tempfile | ||
|
|
||
| from unittest import mock | ||
| import pytest | ||
|
|
||
| from cmd2 import cmd2 | ||
| from .conftest import run_cmd, StdOut, normalize | ||
| from .conftest import run_cmd, StdOut | ||
|
|
||
| class CmdLineApp(cmd2.Cmd): | ||
|
|
||
|
|
@@ -26,7 +27,6 @@ class CmdLineApp(cmd2.Cmd): | |
| def __init__(self, *args, **kwargs): | ||
| self.multiline_commands = ['orate'] | ||
| self.maxrepeats = 3 | ||
| self.redirector = '->' | ||
|
|
||
| # Add stuff to settable and/or shortcuts before calling base class initializer | ||
| self.settable['maxrepeats'] = 'Max number of `--repeat`s allowed' | ||
|
|
@@ -63,7 +63,7 @@ def do_speak(self, opts, arg): | |
| def do_mumble(self, opts, arg): | ||
| """Mumbles what you tell me to.""" | ||
| repetitions = opts.repeat or 1 | ||
| arg = arg.split() | ||
| #arg = arg.split() | ||
| for i in range(min(repetitions, self.maxrepeats)): | ||
| output = [] | ||
| if random.random() < .33: | ||
|
|
@@ -77,136 +77,6 @@ def do_mumble(self, opts, arg): | |
| self.poutput(' '.join(output)) | ||
|
|
||
|
|
||
| class DemoApp(cmd2.Cmd): | ||
| hello_parser = argparse.ArgumentParser() | ||
| hello_parser.add_argument('-n', '--name', help="your name") | ||
| @cmd2.with_argparser_and_unknown_args(hello_parser) | ||
| def do_hello(self, opts, arg): | ||
| """Says hello.""" | ||
| if opts.name: | ||
| self.stdout.write('Hello {}\n'.format(opts.name)) | ||
| else: | ||
| self.stdout.write('Hello Nobody\n') | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def _cmdline_app(): | ||
| c = CmdLineApp() | ||
| c.stdout = StdOut() | ||
| return c | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def _demo_app(): | ||
| c = DemoApp() | ||
| c.stdout = StdOut() | ||
| return c | ||
|
|
||
|
|
||
| def _get_transcript_blocks(transcript): | ||
| cmd = None | ||
| expected = '' | ||
| for line in transcript.splitlines(): | ||
| if line.startswith('(Cmd) '): | ||
| if cmd is not None: | ||
| yield cmd, normalize(expected) | ||
|
|
||
| cmd = line[6:] | ||
| expected = '' | ||
| else: | ||
| expected += line + '\n' | ||
| yield cmd, normalize(expected) | ||
|
|
||
|
|
||
| def test_base_with_transcript(_cmdline_app): | ||
| app = _cmdline_app | ||
| transcript = """ | ||
| (Cmd) help | ||
|
|
||
| Documented commands (type help <topic>): | ||
| ======================================== | ||
| alias help load orate pyscript say shell speak | ||
| edit history mumble py quit set shortcuts unalias | ||
|
|
||
| (Cmd) help say | ||
| usage: speak [-h] [-p] [-s] [-r REPEAT] | ||
|
|
||
| Repeats what you tell me to. | ||
|
|
||
| optional arguments: | ||
| -h, --help show this help message and exit | ||
| -p, --piglatin atinLay | ||
| -s, --shout N00B EMULATION MODE | ||
| -r REPEAT, --repeat REPEAT | ||
| output [n] times | ||
|
|
||
| (Cmd) say goodnight, Gracie | ||
| goodnight, Gracie | ||
| (Cmd) say -ps --repeat=5 goodnight, Gracie | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| (Cmd) set maxrepeats 5 | ||
| maxrepeats - was: 3 | ||
| now: 5 | ||
| (Cmd) say -ps --repeat=5 goodnight, Gracie | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| (Cmd) history | ||
| -------------------------[1] | ||
| help | ||
| -------------------------[2] | ||
| help say | ||
| -------------------------[3] | ||
| say goodnight, Gracie | ||
| -------------------------[4] | ||
| say -ps --repeat=5 goodnight, Gracie | ||
| -------------------------[5] | ||
| set maxrepeats 5 | ||
| -------------------------[6] | ||
| say -ps --repeat=5 goodnight, Gracie | ||
| (Cmd) history -r 4 | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| OODNIGHT, GRACIEGAY | ||
| (Cmd) set prompt "---> " | ||
| prompt - was: (Cmd) | ||
| now: ---> | ||
| """ | ||
|
|
||
| for cmd, expected in _get_transcript_blocks(transcript): | ||
| out = run_cmd(app, cmd) | ||
| assert out == expected | ||
|
|
||
|
|
||
| class TestMyAppCase(cmd2.Cmd2TestCase): | ||
| CmdApp = CmdLineApp | ||
| CmdApp.testfiles = ['tests/transcript.txt'] | ||
|
|
||
|
|
||
| def test_comment_stripping(_cmdline_app): | ||
| out = run_cmd(_cmdline_app, 'speak it was /* not */ delicious! # Yuck!') | ||
| expected = normalize("""it was delicious!""") | ||
| assert out == expected | ||
|
|
||
|
|
||
| def test_argparser_correct_args_with_quotes_and_midline_options(_cmdline_app): | ||
| out = run_cmd(_cmdline_app, "speak 'This is a' -s test of the emergency broadcast system!") | ||
| expected = normalize("""THIS IS A TEST OF THE EMERGENCY BROADCAST SYSTEM!""") | ||
| assert out == expected | ||
|
|
||
|
|
||
| def test_argparser_options_with_spaces_in_quotes(_demo_app): | ||
| out = run_cmd(_demo_app, "hello foo -n 'Bugs Bunny' bar baz") | ||
| expected = normalize("""Hello Bugs Bunny""") | ||
| assert out == expected | ||
|
|
||
|
|
||
| def test_commands_at_invocation(): | ||
| testargs = ["prog", "say hello", "say Gracie", "quit"] | ||
| expected = "This is an intro banner ...\nhello\nGracie\n" | ||
|
|
@@ -217,27 +87,20 @@ def test_commands_at_invocation(): | |
| out = app.stdout.buffer | ||
| assert out == expected | ||
|
|
||
| def test_invalid_syntax(_cmdline_app, capsys): | ||
| run_cmd(_cmdline_app, 'speak "') | ||
| out, err = capsys.readouterr() | ||
| expected = normalize("""ERROR: Invalid syntax: No closing quotation""") | ||
| assert normalize(str(err)) == expected | ||
|
|
||
|
|
||
| @pytest.mark.parametrize('filename, feedback_to_output', [ | ||
| @pytest.mark.parametrize('filename,feedback_to_output', [ | ||
| ('bol_eol.txt', False), | ||
| ('characterclass.txt', False), | ||
| ('dotstar.txt', False), | ||
| ('extension_notation.txt', False), | ||
| # ('from_cmdloop.txt', True), | ||
| ('from_cmdloop.txt', True), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for fixing these |
||
| ('multiline_no_regex.txt', False), | ||
| ('multiline_regex.txt', False), | ||
| ('regex_set.txt', False), | ||
| ('singleslash.txt', False), | ||
| ('slashes_escaped.txt', False), | ||
| ('slashslash.txt', False), | ||
| ('spaces.txt', False), | ||
| # ('word_boundaries.txt', False), | ||
| ('word_boundaries.txt', False), | ||
| ]) | ||
| def test_transcript(request, capsys, filename, feedback_to_output): | ||
| # Create a cmd2.Cmd() instance and make sure basic settings are | ||
|
|
@@ -263,6 +126,32 @@ def test_transcript(request, capsys, filename, feedback_to_output): | |
| assert err.startswith(expected_start) | ||
| assert err.endswith(expected_end) | ||
|
|
||
| def test_history_transcript(request, capsys): | ||
| app = CmdLineApp() | ||
| app.stdout = StdOut() | ||
| run_cmd(app, 'orate this is\na /multiline/\ncommand;\n') | ||
| run_cmd(app, 'speak /tmp/file.txt is not a regex') | ||
|
|
||
| expected = r"""(Cmd) orate this is | ||
| > a /multiline/ | ||
| > command; | ||
| this is a \/multiline\/ command | ||
| (Cmd) speak /tmp/file.txt is not a regex | ||
| \/tmp\/file.txt is not a regex | ||
| """ | ||
|
|
||
| # make a tmp file | ||
| fd, history_fname = tempfile.mkstemp(prefix='', suffix='.txt') | ||
| os.close(fd) | ||
|
|
||
| # tell the history command to create a transcript | ||
| run_cmd(app, 'history -t "{}"'.format(history_fname)) | ||
|
|
||
| # read in the transcript created by the history command | ||
| with open(history_fname) as f: | ||
| transcript = f.read() | ||
|
|
||
| assert transcript == expected | ||
|
|
||
| @pytest.mark.parametrize('expected, transformed', [ | ||
| # strings with zero or one slash or with escaped slashes means no regular | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for refactoring and improving this.