Skip to content

Commit 7b32a19

Browse files
committed
Added self.last_result unit tests for run_script, _relative_run_script, and set commands.
1 parent 8f2a9b0 commit 7b32a19

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

cmd2/cmd2.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4872,6 +4872,7 @@ def do_run_script(self, args: argparse.Namespace) -> Optional[bool]:
48724872
try:
48734873
# An empty file is not an error, so just return
48744874
if os.path.getsize(expanded_path) == 0:
4875+
self.last_result = True
48754876
return None
48764877

48774878
# Make sure the file is ASCII or UTF-8 encoded text

tests/test_cmd2.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ def test_base_set(base_app):
133133
expected = normalize(SET_TXT)
134134
assert out == expected
135135

136+
assert len(base_app.last_result) == len(base_app.settables)
137+
for param in base_app.last_result:
138+
assert base_app.last_result[param] == base_app.settables[param].get_value()
139+
136140

137141
def test_set(base_app):
138142
out, err = run_cmd(base_app, 'set quiet True')
@@ -143,6 +147,7 @@ def test_set(base_app):
143147
"""
144148
)
145149
assert out == expected
150+
assert base_app.last_result is True
146151

147152
out, err = run_cmd(base_app, 'set quiet')
148153
expected = normalize(
@@ -153,18 +158,22 @@ def test_set(base_app):
153158
"""
154159
)
155160
assert out == expected
161+
assert len(base_app.last_result) == 1
162+
assert base_app.last_result['quiet'] is True
156163

157164

158165
def test_set_val_empty(base_app):
159166
base_app.editor = "fake"
160167
out, err = run_cmd(base_app, 'set editor ""')
161168
assert base_app.editor == ''
169+
assert base_app.last_result is True
162170

163171

164172
def test_set_val_is_flag(base_app):
165173
base_app.editor = "fake"
166174
out, err = run_cmd(base_app, 'set editor "-h"')
167175
assert base_app.editor == '-h'
176+
assert base_app.last_result is True
168177

169178

170179
def test_set_not_supported(base_app):
@@ -175,13 +184,15 @@ def test_set_not_supported(base_app):
175184
"""
176185
)
177186
assert err == expected
187+
assert base_app.last_result is False
178188

179189

180190
def test_set_no_settables(base_app):
181191
base_app._settables.clear()
182192
out, err = run_cmd(base_app, 'set quiet True')
183193
expected = normalize("There are no settable parameters")
184194
assert err == expected
195+
assert base_app.last_result is False
185196

186197

187198
@pytest.mark.parametrize(
@@ -202,6 +213,7 @@ def test_set_allow_style(base_app, new_val, is_valid, expected):
202213

203214
# Use the set command to alter it
204215
out, err = run_cmd(base_app, 'set allow_style {}'.format(new_val))
216+
assert base_app.last_result is is_valid
205217

206218
# Verify the results
207219
assert ansi.allow_style == expected
@@ -239,6 +251,7 @@ def test_set_onchange_hook(onchange_app):
239251
"""
240252
)
241253
assert out == expected
254+
assert onchange_app.last_result is True
242255

243256

244257
def test_base_shell(base_app, monkeypatch):
@@ -281,6 +294,7 @@ def test_run_script(base_app, request):
281294

282295
# Get output out the script
283296
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
297+
assert base_app.last_result is True
284298

285299
assert base_app._script_dir == []
286300
assert base_app._current_script_dir is None
@@ -303,31 +317,36 @@ def test_run_script(base_app, request):
303317
def test_run_script_with_empty_args(base_app):
304318
out, err = run_cmd(base_app, 'run_script')
305319
assert "the following arguments are required" in err[1]
320+
assert base_app.last_result is None
306321

307322

308323
def test_run_script_with_invalid_file(base_app, request):
309324
# Path does not exist
310325
out, err = run_cmd(base_app, 'run_script does_not_exist.txt')
311326
assert "Problem accessing script from " in err[0]
327+
assert base_app.last_result is False
312328

313329
# Path is a directory
314330
test_dir = os.path.dirname(request.module.__file__)
315331
out, err = run_cmd(base_app, 'run_script {}'.format(test_dir))
316332
assert "Problem accessing script from " in err[0]
333+
assert base_app.last_result is False
317334

318335

319336
def test_run_script_with_empty_file(base_app, request):
320337
test_dir = os.path.dirname(request.module.__file__)
321338
filename = os.path.join(test_dir, 'scripts', 'empty.txt')
322339
out, err = run_cmd(base_app, 'run_script {}'.format(filename))
323340
assert not out and not err
341+
assert base_app.last_result is True
324342

325343

326344
def test_run_script_with_binary_file(base_app, request):
327345
test_dir = os.path.dirname(request.module.__file__)
328346
filename = os.path.join(test_dir, 'scripts', 'binary.bin')
329347
out, err = run_cmd(base_app, 'run_script {}'.format(filename))
330348
assert "is not an ASCII or UTF-8 encoded text file" in err[0]
349+
assert base_app.last_result is False
331350

332351

333352
def test_run_script_with_python_file(base_app, request):
@@ -338,6 +357,7 @@ def test_run_script_with_python_file(base_app, request):
338357
filename = os.path.join(test_dir, 'pyscript', 'stop.py')
339358
out, err = run_cmd(base_app, 'run_script {}'.format(filename))
340359
assert "appears to be a Python file" in err[0]
360+
assert base_app.last_result is False
341361

342362

343363
def test_run_script_with_utf8_file(base_app, request):
@@ -349,6 +369,7 @@ def test_run_script_with_utf8_file(base_app, request):
349369

350370
# Get output out the script
351371
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
372+
assert base_app.last_result is True
352373

353374
assert base_app._script_dir == []
354375
assert base_app._current_script_dir is None
@@ -377,6 +398,7 @@ def test_run_script_nested_run_scripts(base_app, request):
377398
# Run the top level script
378399
initial_run = 'run_script ' + filename
379400
run_cmd(base_app, initial_run)
401+
assert base_app.last_result is True
380402

381403
# Check that the right commands were executed.
382404
expected = (
@@ -447,7 +469,8 @@ def test_relative_run_script(base_app, request):
447469
assert base_app._current_script_dir is None
448470

449471
# Get output out the script
450-
script_out, script_err = run_cmd(base_app, 'run_script {}'.format(filename))
472+
script_out, script_err = run_cmd(base_app, '_relative_run_script {}'.format(filename))
473+
assert base_app.last_result is True
451474

452475
assert base_app._script_dir == []
453476
assert base_app._current_script_dir is None
@@ -481,6 +504,7 @@ def test_relative_run_script_with_odd_file_names(base_app, file_name, monkeypatc
481504
def test_relative_run_script_requires_an_argument(base_app):
482505
out, err = run_cmd(base_app, '_relative_run_script')
483506
assert 'Error: the following arguments' in err[1]
507+
assert base_app.last_result is None
484508

485509

486510
def test_in_script(request):
@@ -1822,10 +1846,12 @@ def test_alias_create(base_app):
18221846
out, err = run_cmd(base_app, 'alias list')
18231847
assert out == normalize('alias create fake run_pyscript')
18241848
assert len(base_app.last_result) == len(base_app.aliases)
1849+
assert base_app.last_result['fake'] == "run_pyscript"
18251850

18261851
# Look up the new alias
18271852
out, err = run_cmd(base_app, 'alias list fake')
18281853
assert out == normalize('alias create fake run_pyscript')
1854+
assert len(base_app.last_result) == 1
18291855
assert base_app.last_result['fake'] == "run_pyscript"
18301856

18311857
# Overwrite alias
@@ -1836,6 +1862,7 @@ def test_alias_create(base_app):
18361862
# Look up the updated alias
18371863
out, err = run_cmd(base_app, 'alias list fake')
18381864
assert out == normalize('alias create fake help')
1865+
assert len(base_app.last_result) == 1
18391866
assert base_app.last_result['fake'] == "help"
18401867

18411868

@@ -1852,6 +1879,7 @@ def test_alias_create_with_quoted_tokens(base_app):
18521879
# Look up the new alias and verify all quotes are preserved
18531880
out, err = run_cmd(base_app, 'alias list fake')
18541881
assert out == normalize(create_command)
1882+
assert len(base_app.last_result) == 1
18551883
assert base_app.last_result[alias_name] == alias_command
18561884

18571885

@@ -1954,10 +1982,12 @@ def test_macro_create(base_app):
19541982
out, err = run_cmd(base_app, 'macro list')
19551983
assert out == normalize('macro create fake run_pyscript')
19561984
assert len(base_app.last_result) == len(base_app.macros)
1985+
assert base_app.last_result['fake'] == "run_pyscript"
19571986

19581987
# Look up the new macro
19591988
out, err = run_cmd(base_app, 'macro list fake')
19601989
assert out == normalize('macro create fake run_pyscript')
1990+
assert len(base_app.last_result) == 1
19611991
assert base_app.last_result['fake'] == "run_pyscript"
19621992

19631993
# Overwrite macro
@@ -1968,6 +1998,7 @@ def test_macro_create(base_app):
19681998
# Look up the updated macro
19691999
out, err = run_cmd(base_app, 'macro list fake')
19702000
assert out == normalize('macro create fake help')
2001+
assert len(base_app.last_result) == 1
19712002
assert base_app.last_result['fake'] == "help"
19722003

19732004

@@ -1984,6 +2015,7 @@ def test_macro_create_with_quoted_tokens(base_app):
19842015
# Look up the new macro and verify all quotes are preserved
19852016
out, err = run_cmd(base_app, 'macro list fake')
19862017
assert out == normalize(create_command)
2018+
assert len(base_app.last_result) == 1
19872019
assert base_app.last_result[macro_name] == macro_command
19882020

19892021

0 commit comments

Comments
 (0)