diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-06 23:27:02 -0400 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2019-06-06 23:27:02 -0400 |
commit | 6cdf70823b344b99d6623af19fb618a9c2dbdad4 (patch) | |
tree | bede2cb855afa16facd5c92832ce06795d88ad2f /cmd2/cmd2.py | |
parent | abeb8e7cabec6b18c269debb74659f91df4f6058 (diff) | |
download | cmd2-git-6cdf70823b344b99d6623af19fb618a9c2dbdad4.tar.gz |
Refactored how and when transcript file glob patterns are expanded in order to present a better error message to user
Diffstat (limited to 'cmd2/cmd2.py')
-rw-r--r-- | cmd2/cmd2.py | 30 |
1 files changed, 25 insertions, 5 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 |