Skip to content

Commit ea1716a

Browse files
committed
Fix unit test failures I introduced in last commit
1 parent a1be014 commit ea1716a

File tree

4 files changed

+45
-43
lines changed

4 files changed

+45
-43
lines changed

cmd2/cmd2.py

Lines changed: 41 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3270,11 +3270,10 @@ def do_run_pyscript(self, args: argparse.Namespace) -> bool:
32703270
finally:
32713271
# Restore command line arguments to original state
32723272
sys.argv = orig_args
3273-
3274-
if args.__statement__.command == "pyscript":
3275-
self.perror("pyscript has been renamed and will be removed in the next release, "
3276-
"please use run_pyscript instead\n",
3277-
traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
3273+
if args.__statement__.command == "pyscript":
3274+
self.perror("pyscript has been renamed and will be removed in the next release, "
3275+
"please use run_pyscript instead\n",
3276+
traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
32783277

32793278
return py_return
32803279

@@ -3655,49 +3654,52 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
36553654
"""
36563655
expanded_path = os.path.abspath(os.path.expanduser(args.script_path))
36573656

3658-
# Make sure the path exists and we can access it
3659-
if not os.path.exists(expanded_path):
3660-
self.perror("'{}' does not exist or cannot be accessed".format(expanded_path), traceback_war=False)
3661-
return
3657+
# Wrap everything in a try/finally just to make sure the warning prints at end if `load` was called
3658+
try:
3659+
# Make sure the path exists and we can access it
3660+
if not os.path.exists(expanded_path):
3661+
self.perror("'{}' does not exist or cannot be accessed".format(expanded_path), traceback_war=False)
3662+
return
36623663

3663-
# Make sure expanded_path points to a file
3664-
if not os.path.isfile(expanded_path):
3665-
self.perror("'{}' is not a file".format(expanded_path), traceback_war=False)
3666-
return
3664+
# Make sure expanded_path points to a file
3665+
if not os.path.isfile(expanded_path):
3666+
self.perror("'{}' is not a file".format(expanded_path), traceback_war=False)
3667+
return
36673668

3668-
# Make sure the file is not empty
3669-
if os.path.getsize(expanded_path) == 0:
3670-
self.perror("'{}' is empty".format(expanded_path), traceback_war=False)
3671-
return
3669+
# Make sure the file is not empty
3670+
if os.path.getsize(expanded_path) == 0:
3671+
self.perror("'{}' is empty".format(expanded_path), traceback_war=False)
3672+
return
36723673

3673-
# Make sure the file is ASCII or UTF-8 encoded text
3674-
if not utils.is_text_file(expanded_path):
3675-
self.perror("'{}' is not an ASCII or UTF-8 encoded text file".format(expanded_path), traceback_war=False)
3676-
return
3674+
# Make sure the file is ASCII or UTF-8 encoded text
3675+
if not utils.is_text_file(expanded_path):
3676+
self.perror("'{}' is not an ASCII or UTF-8 encoded text file".format(expanded_path), traceback_war=False)
3677+
return
36773678

3678-
try:
3679-
# Read all lines of the script
3680-
with open(expanded_path, encoding='utf-8') as target:
3681-
script_commands = target.read().splitlines()
3682-
except OSError as ex: # pragma: no cover
3683-
self.perror("Problem accessing script from '{}': {}".format(expanded_path, ex))
3684-
return
3679+
try:
3680+
# Read all lines of the script
3681+
with open(expanded_path, encoding='utf-8') as target:
3682+
script_commands = target.read().splitlines()
3683+
except OSError as ex: # pragma: no cover
3684+
self.perror("Problem accessing script from '{}': {}".format(expanded_path, ex))
3685+
return
36853686

3686-
orig_script_dir_count = len(self._script_dir)
3687+
orig_script_dir_count = len(self._script_dir)
36873688

3688-
try:
3689-
self._script_dir.append(os.path.dirname(expanded_path))
3689+
try:
3690+
self._script_dir.append(os.path.dirname(expanded_path))
36903691

3691-
if args.transcript:
3692-
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
3693-
else:
3694-
return self.runcmds_plus_hooks(script_commands)
3692+
if args.transcript:
3693+
self._generate_transcript(script_commands, os.path.expanduser(args.transcript))
3694+
else:
3695+
return self.runcmds_plus_hooks(script_commands)
36953696

3697+
finally:
3698+
with self.sigint_protection:
3699+
# Check if a script dir was added before an exception occurred
3700+
if orig_script_dir_count != len(self._script_dir):
3701+
self._script_dir.pop()
36963702
finally:
3697-
with self.sigint_protection:
3698-
# Check if a script dir was added before an exception occurred
3699-
if orig_script_dir_count != len(self._script_dir):
3700-
self._script_dir.pop()
37013703
if args.__statement__.command == "load":
37023704
self.perror("load has been renamed and will be removed in the next release, "
37033705
"please use run_script instead\n",

examples/hooks.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ class CmdLineApp(cmd2.Cmd):
3838
# Setting this true makes it run a shell command if a cmd2/cmd command doesn't exist
3939
# default_to_shell = True
4040
def __init__(self, *args, **kwargs):
41-
# sneakily remove the cmd2.Cmd command called load
41+
# sneakily remove the cmd2.Cmd command called run_script
4242
# this lets a user enter a command like "l5" and allows it to
4343
# be unambiguous
44-
delattr(cmd2.Cmd, "do_load")
44+
delattr(cmd2.Cmd, "do_run_script")
4545

4646
super().__init__(*args, **kwargs)
4747

tests/test_cmd2.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ def test_run_script(base_app, request):
288288
def test_load_deprecated(base_app):
289289
"""Delete this when load alias is removed"""
290290
_, err = run_cmd(base_app, "load fake")
291-
assert "load has been renamed and will be removed" in err[0]
291+
assert "load has been renamed and will be removed" in err[-1]
292292

293293
def test_run_script_with_empty_args(base_app):
294294
out, err = run_cmd(base_app, 'run_script')

tests/test_run_pyscript.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,4 @@ def test_run_pyscript_stop(base_app, request):
8888
def test_pyscript_deprecated(base_app):
8989
"""Delete this when pyscript alias is removed"""
9090
_, err = run_cmd(base_app, "pyscript fake")
91-
assert "pyscript has been renamed and will be removed" in err[0]
91+
assert "pyscript has been renamed and will be removed" in err[-1]

0 commit comments

Comments
 (0)