Skip to content

Commit cd37707

Browse files
authored
Merge pull request #1050 from python-cmd2/misc
Miscellaneous changes for the next release (1.4.1)
2 parents 2055dfc + 246ce9d commit cd37707

File tree

6 files changed

+33
-1
lines changed

6 files changed

+33
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
* Enhancements
88
* Added `silent_startup_script` option to `cmd2.Cmd.__init__()`. If `True`, then the startup script's
99
output will be suppressed. Anything written to stderr will still display.
10+
* cmd2 now uses pyreadline3 when running Python 3.8 or greater on Windows
1011

1112
## 1.4.0 (November 11, 2020)
1213
* Bug Fixes

cmd2/cmd2.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3993,6 +3993,9 @@ def do_history(self, args: argparse.Namespace) -> Optional[bool]:
39933993
os.remove(self.persistent_history_file)
39943994
except FileNotFoundError:
39953995
pass
3996+
except OSError as ex:
3997+
self.pexcept("Error removing history file '{}': {}".format(self.persistent_history_file, ex))
3998+
return
39963999

39974000
if rl_type != RlType.NONE:
39984001
readline.clear_history()

cmd2/table_creator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ def __init__(self, cols: Sequence[Column], *, tab_width: int = 4) -> None:
132132
:param cols: column definitions for this table
133133
:param tab_width: all tabs will be replaced with this many spaces. If a row's fill_char is a tab,
134134
then it will be converted to one space.
135+
:raises: ValueError if tab_width is less than 1
135136
"""
137+
if tab_width < 1:
138+
raise ValueError("Tab width cannot be less than 1")
139+
136140
self.cols = copy.copy(cols)
137141
self.tab_width = tab_width
138142

docs/features/startup_commands.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,12 @@ like so::
6464
This text file should contain a :ref:`Command Script
6565
<features/scripting:Command Scripts>`. See the AliasStartup_ example for a
6666
demonstration.
67+
68+
You can silence a startup script's output by setting ``silent_startup_script``
69+
to True::
70+
71+
cmd2.Cmd.__init__(self, startup_script='.cmd2rc', silent_startup_script=True)
72+
73+
Anything written to stderr will still print. Additionally, a startup script
74+
cannot be silenced if ``allow_redirection`` is False since silencing works
75+
by redirecting a script's output to ``os.devnull``.

tests/test_history.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ def test_history_run_one_command(base_app):
520520
out2, err2 = run_cmd(base_app, 'history -r 1')
521521
assert out1 == out2
522522

523-
def test_history_clear(hist_file):
523+
def test_history_clear(mocker, hist_file):
524524
# Add commands to history
525525
app = cmd2.Cmd(persistent_history_file=hist_file)
526526
run_cmd(app, 'help')
@@ -538,6 +538,17 @@ def test_history_clear(hist_file):
538538
assert out == []
539539
assert not os.path.exists(hist_file)
540540

541+
# Clear the history again and make sure the FileNotFoundError from trying to delete missing history file is silent
542+
run_cmd(app, 'history --clear')
543+
544+
# Cause os.remove to fail and make sure error gets printed
545+
mock_remove = mocker.patch('os.remove')
546+
mock_remove.side_effect = OSError
547+
548+
out, err = run_cmd(app, 'history --clear')
549+
assert out == []
550+
assert 'Error removing history file' in err[0]
551+
541552
def test_history_verbose_with_other_options(base_app):
542553
# make sure -v shows a usage error if any other options are present
543554
options_to_test = ['-r', '-e', '-o file', '-t file', '-c', '-x']

tests/test_table_creator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ def test_tabs():
259259
inter_cell='\t', post_line='\t')
260260
assert row == ' Col 1 Col 2 '
261261

262+
with pytest.raises(ValueError) as excinfo:
263+
TableCreator([column_1, column_2], tab_width=0)
264+
assert "Tab width cannot be less than 1" in str(excinfo.value)
265+
262266

263267
def test_simple_table_creation():
264268
column_1 = Column("Col 1", width=16)

0 commit comments

Comments
 (0)