Skip to content

Commit e4983ab

Browse files
committed
Fixing unit tests
1 parent 710a9e2 commit e4983ab

File tree

5 files changed

+40
-57
lines changed

5 files changed

+40
-57
lines changed

cmd2/cmd2.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3727,6 +3727,11 @@ def do__relative_run_script(self, args: argparse.Namespace) -> Optional[bool]:
37273727
Run commands in script file that is encoded as either ASCII or UTF-8 text
37283728
:return: True if running of commands should stop
37293729
"""
3730+
if args.__statement__.command == "_relative_load":
3731+
self.perror("_relative_load has been renamed and will be removed in the next release, "
3732+
"please use _relative_run_script instead\n",
3733+
traceback_war=False, err_color=Fore.LIGHTYELLOW_EX)
3734+
37303735
file_path = args.file_path
37313736
# NOTE: Relative path is an absolute path, it is just relative to the current script directory
37323737
relative_path = os.path.join(self._current_script_dir or '', file_path)

tests/relative_multiple.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
_relative_load scripts/one_down.txt
1+
_relative_run_script scripts/one_down.txt

tests/scripts/nested.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
_relative_load precmds.txt
1+
_relative_run_script precmds.txt
22
help
33
shortcuts
4-
_relative_load postcmds.txt
4+
_relative_run_script postcmds.txt

tests/scripts/one_down.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
_relative_load ../script.txt
1+
_relative_run_script ../script.txt

tests/test_cmd2.py

Lines changed: 31 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -291,15 +291,15 @@ def test_base_error(base_app):
291291
assert "is not a recognized command" in err[0]
292292

293293

294-
def test_base_load(base_app, request):
294+
def test_base_run_script(base_app, request):
295295
test_dir = os.path.dirname(request.module.__file__)
296296
filename = os.path.join(test_dir, 'script.txt')
297297

298298
assert base_app._script_dir == []
299299
assert base_app._current_script_dir is None
300300

301301
# Get output out the script
302-
script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
302+
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
303303

304304
assert base_app._script_dir == []
305305
assert base_app._current_script_dir is None
@@ -318,60 +318,40 @@ def test_base_load(base_app, request):
318318
assert script_out == manual_out
319319
assert script_err == manual_err
320320

321-
def test_load_with_empty_args(base_app):
322-
# The way the load command works, we can't directly capture its stdout or stderr
323-
out, err = run_cmd(base_app, 'load')
324-
325-
# The load command requires a file path argument, so we should get an error message
321+
def test_run_script_with_empty_args(base_app):
322+
out, err = run_cmd(base_app, 'run_script')
326323
assert "the following arguments are required" in err[1]
327324

328-
329-
def test_load_with_nonexistent_file(base_app, capsys):
330-
# The way the load command works, we can't directly capture its stdout or stderr
331-
out, err = run_cmd(base_app, 'load does_not_exist.txt')
332-
333-
# The load command requires a path to an existing file
325+
def test_run_script_with_nonexistent_file(base_app, capsys):
326+
out, err = run_cmd(base_app, 'run_script does_not_exist.txt')
334327
assert "does not exist" in err[0]
335328

336-
def test_load_with_directory(base_app, request):
329+
def test_run_script_with_directory(base_app, request):
337330
test_dir = os.path.dirname(request.module.__file__)
338-
339-
# The way the load command works, we can't directly capture its stdout or stderr
340-
out, err = run_cmd(base_app, 'load {}'.format(test_dir))
341-
331+
out, err = run_cmd(base_app, 'run_script {}'.format(test_dir))
342332
assert "is not a file" in err[0]
343333

344-
def test_load_with_empty_file(base_app, request):
334+
def test_run_script_with_empty_file(base_app, request):
345335
test_dir = os.path.dirname(request.module.__file__)
346336
filename = os.path.join(test_dir, 'scripts', 'empty.txt')
347-
348-
# The way the load command works, we can't directly capture its stdout or stderr
349-
out, err = run_cmd(base_app, 'load {}'.format(filename))
350-
351-
# The load command requires non-empty script files
337+
out, err = run_cmd(base_app, 'run_script {}'.format(filename))
352338
assert "is empty" in err[0]
353339

354-
355-
def test_load_with_binary_file(base_app, request):
340+
def test_run_script_with_binary_file(base_app, request):
356341
test_dir = os.path.dirname(request.module.__file__)
357342
filename = os.path.join(test_dir, 'scripts', 'binary.bin')
358-
359-
# The way the load command works, we can't directly capture its stdout or stderr
360-
out, err = run_cmd(base_app, 'load {}'.format(filename))
361-
362-
# The load command requires non-empty scripts files
343+
out, err = run_cmd(base_app, 'run_script {}'.format(filename))
363344
assert "is not an ASCII or UTF-8 encoded text file" in err[0]
364345

365-
366-
def test_load_with_utf8_file(base_app, request):
346+
def test_run_script_with_utf8_file(base_app, request):
367347
test_dir = os.path.dirname(request.module.__file__)
368348
filename = os.path.join(test_dir, 'scripts', 'utf8.txt')
369349

370350
assert base_app._script_dir == []
371351
assert base_app._current_script_dir is None
372352

373353
# Get output out the script
374-
script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
354+
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
375355

376356
assert base_app._script_dir == []
377357
assert base_app._current_script_dir is None
@@ -390,59 +370,57 @@ def test_load_with_utf8_file(base_app, request):
390370
assert script_out == manual_out
391371
assert script_err == manual_err
392372

393-
394-
def test_load_nested_loads(base_app, request):
395-
# Verify that loading a script with nested load commands works correctly,
396-
# and loads the nested script commands in the correct order.
373+
def test_run_script_nested_run_scripts(base_app, request):
374+
# Verify that running a script with nested run_script commands works correctly,
375+
# and runs the nested script commands in the correct order.
397376
test_dir = os.path.dirname(request.module.__file__)
398377
filename = os.path.join(test_dir, 'scripts', 'nested.txt')
399378

400-
# Load the top level script
401-
initial_load = 'load ' + filename
402-
run_cmd(base_app, initial_load)
379+
# Run the top level script
380+
initial_run = 'run_script ' + filename
381+
run_cmd(base_app, initial_run)
403382

404383
# Check that the right commands were executed.
405384
expected = """
406385
%s
407-
_relative_load precmds.txt
386+
_relative_run_script precmds.txt
408387
set colors Always
409388
help
410389
shortcuts
411-
_relative_load postcmds.txt
412-
set colors Never""" % initial_load
390+
_relative_run_script postcmds.txt
391+
set colors Never""" % initial_run
413392
out, err = run_cmd(base_app, 'history -s')
414393
assert out == normalize(expected)
415394

416-
417395
def test_base_runcmds_plus_hooks(base_app, request):
418396
test_dir = os.path.dirname(request.module.__file__)
419397
prefilepath = os.path.join(test_dir, 'scripts', 'precmds.txt')
420398
postfilepath = os.path.join(test_dir, 'scripts', 'postcmds.txt')
421399

422-
base_app.runcmds_plus_hooks(['load ' + prefilepath,
400+
base_app.runcmds_plus_hooks(['run_script ' + prefilepath,
423401
'help',
424402
'shortcuts',
425-
'load ' + postfilepath])
403+
'run_script ' + postfilepath])
426404
expected = """
427-
load %s
405+
run_script %s
428406
set colors Always
429407
help
430408
shortcuts
431-
load %s
409+
run_script %s
432410
set colors Never""" % (prefilepath, postfilepath)
433411

434412
out, err = run_cmd(base_app, 'history -s')
435413
assert out == normalize(expected)
436414

437-
def test_base_relative_load(base_app, request):
415+
def test_base_relative_run_script(base_app, request):
438416
test_dir = os.path.dirname(request.module.__file__)
439417
filename = os.path.join(test_dir, 'script.txt')
440418

441419
assert base_app._script_dir == []
442420
assert base_app._current_script_dir is None
443421

444422
# Get output out the script
445-
script_out, script_err = run_cmd(base_app, 'load {}'.format(filename))
423+
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
446424

447425
assert base_app._script_dir == []
448426
assert base_app._current_script_dir is None
@@ -461,8 +439,8 @@ def test_base_relative_load(base_app, request):
461439
assert script_out == manual_out
462440
assert script_err == manual_err
463441

464-
def test_relative_load_requires_an_argument(base_app):
465-
out, err = run_cmd(base_app, '_relative_load')
442+
def test_relative_run_script_requires_an_argument(base_app):
443+
out, err = run_cmd(base_app, '_relative_run_script')
466444
assert 'Error: the following arguments' in err[1]
467445

468446

0 commit comments

Comments
 (0)