summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--cmd2/cmd2.py30
-rw-r--r--cmd2/transcript.py12
-rw-r--r--tests/test_transcript.py21
3 files changed, 49 insertions, 14 deletions
diff --git a/cmd2/cmd2.py b/cmd2/cmd2.py
index e2ff068e..3b47ee9e 100644
--- a/cmd2/cmd2.py
+++ b/cmd2/cmd2.py
@@ -3700,13 +3700,25 @@ class Cmd(cmd.Cmd):
relative_path = os.path.join(self._current_script_dir or '', file_path)
return self.do_load(relative_path)
- def run_transcript_tests(self, callargs: List[str]) -> None:
+ def _expand_transcripts(self, transcript_paths: List[str]) -> List[str]:
+ """Expand glob patterns to match transcript files.
+
+ :param transcript_paths: list of transcript file paths (expanded for user), possibly including glob patterns
+ :return: list of transcript file paths with glob patterns expanded
+ """
+ expanded_transcripts = []
+ for fileset in transcript_paths:
+ for fname in glob.glob(fileset):
+ expanded_transcripts.append(fname)
+ return expanded_transcripts
+
+ def run_transcript_tests(self, transcript_paths: List[str]) -> None:
"""Runs transcript tests for provided file(s).
This is called when either -t is provided on the command line or the transcript_files argument is provided
during construction of the cmd2.Cmd instance.
- :param callargs: list of transcript test file names
+ :param transcript_paths: list of transcript test file paths
"""
import unittest
from .transcript import Cmd2TestCase
@@ -3714,7 +3726,16 @@ class Cmd(cmd.Cmd):
class TestMyAppCase(Cmd2TestCase):
cmdapp = self
- self.__class__.testfiles = callargs
+ # Expand glob patterns
+ transcripts_expanded = self._expand_transcripts(transcript_paths)
+
+ # Validate that there is at least one transcript file
+ if not transcripts_expanded:
+ self.perror('No test files found - nothing to test', traceback_war=False)
+ self.exit_code = -1
+ return
+
+ self.__class__.testfiles = transcripts_expanded
sys.argv = [sys.argv[0]] # the --test argument upsets unittest.main()
testcase = TestMyAppCase()
stream = utils.StdSim(sys.stderr)
@@ -4013,9 +4034,8 @@ class Cmd(cmd.Cmd):
# If transcript testing was called for, use other arguments as transcript files
if callopts.test:
self._transcript_files = callargs
-
# If commands were supplied at invocation, then add them to the command queue
- if callargs:
+ elif callargs:
self.cmdqueue.extend(callargs)
# Grab terminal lock before the prompt has been drawn by readline
diff --git a/cmd2/transcript.py b/cmd2/transcript.py
index b5cb5397..b507cbf2 100644
--- a/cmd2/transcript.py
+++ b/cmd2/transcript.py
@@ -10,7 +10,6 @@ This file contains the classess necessary to make that work. These
classes are used in cmd2.py::run_transcript_tests()
"""
import re
-import glob
import unittest
from typing import Tuple
@@ -30,13 +29,10 @@ class Cmd2TestCase(unittest.TestCase):
def fetchTranscripts(self):
self.transcripts = {}
- for fileset in self.cmdapp.testfiles:
- for fname in glob.glob(fileset):
- tfile = open(fname)
- self.transcripts[fname] = iter(tfile.readlines())
- tfile.close()
- if not len(self.transcripts):
- raise Exception("No test files found - nothing to test.")
+ for fname in self.cmdapp.testfiles:
+ tfile = open(fname)
+ self.transcripts[fname] = iter(tfile.readlines())
+ tfile.close()
def setUp(self):
if self.cmdapp:
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