From da203d25dff1f05511af43b4f73fd537678d6e03 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Thu, 28 Jan 2021 16:13:28 -0500 Subject: Fixed width calculation when table column headers had tabs --- tests/test_table_creator.py | 42 ++++++++++++++++++++++++++++++------------ 1 file changed, 30 insertions(+), 12 deletions(-) (limited to 'tests') diff --git a/tests/test_table_creator.py b/tests/test_table_creator.py index 58dd6fdf..0a0e712d 100644 --- a/tests/test_table_creator.py +++ b/tests/test_table_creator.py @@ -20,18 +20,6 @@ from cmd2.table_creator import ( def test_column_creation(): - # No width specified, blank label - c = Column("") - assert c.width == 1 - - # No width specified, label isn't blank but has no width - c = Column(ansi.style('', fg=ansi.fg.green)) - assert c.width == 1 - - # No width specified, label has width - c = Column("short\nreally long") - assert c.width == ansi.style_aware_wcswidth("really long") - # Width less than 1 with pytest.raises(ValueError) as excinfo: Column("Column 1", width=0) @@ -46,6 +34,36 @@ def test_column_creation(): Column("Column 1", max_data_lines=0) assert "Max data lines cannot be less than 1" in str(excinfo.value) + # No width specified, blank label + c = Column("") + assert c.width is None + tc = TableCreator([c]) + assert tc.cols[0].width == 1 + + # No width specified, label isn't blank but has no width + c = Column(ansi.style('', fg=ansi.fg.green)) + assert c.width is None + tc = TableCreator([c]) + assert tc.cols[0].width == 1 + + # No width specified, label has width + c = Column("a line") + assert c.width is None + tc = TableCreator([c]) + assert tc.cols[0].width == ansi.style_aware_wcswidth("a line") + + # No width specified, label has width and multiple lines + c = Column("short\nreally long") + assert c.width is None + tc = TableCreator([c]) + assert tc.cols[0].width == ansi.style_aware_wcswidth("really long") + + # No width specified, label has tabs + c = Column("line\twith\ttabs") + assert c.width is None + tc = TableCreator([c]) + assert tc.cols[0].width == ansi.style_aware_wcswidth("line with tabs") + def test_column_alignment(): column_1 = Column("Col 1", width=10, -- cgit v1.2.1 From 7f76e23221677376bc3676094b6136bb5a259c52 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 29 Jan 2021 10:54:39 -0500 Subject: Added more validation to TableCreator --- tests/test_table_creator.py | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'tests') diff --git a/tests/test_table_creator.py b/tests/test_table_creator.py index 0a0e712d..649674a4 100644 --- a/tests/test_table_creator.py +++ b/tests/test_table_creator.py @@ -259,6 +259,10 @@ def test_tabs(): inter_cell='\t', post_line='\t') assert row == ' Col 1 Col 2 ' + with pytest.raises(ValueError) as excinfo: + TableCreator([column_1, column_2], tab_width=0) + assert "Tab width cannot be less than 1" in str(excinfo.value) + def test_simple_table_creation(): column_1 = Column("Col 1", width=16) -- cgit v1.2.1 From 0e70f0621273dd5f98aa5fffc42a5cb56f97c760 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Fri, 29 Jan 2021 11:21:49 -0500 Subject: Printing error when OSError other than FileNotFoundError occurs when deleting persistent history file with history --clear command. --- tests/test_history.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/test_history.py b/tests/test_history.py index ac8c6cb6..3021109c 100755 --- a/tests/test_history.py +++ b/tests/test_history.py @@ -520,7 +520,7 @@ def test_history_run_one_command(base_app): out2, err2 = run_cmd(base_app, 'history -r 1') assert out1 == out2 -def test_history_clear(hist_file): +def test_history_clear(mocker, hist_file): # Add commands to history app = cmd2.Cmd(persistent_history_file=hist_file) run_cmd(app, 'help') @@ -538,6 +538,17 @@ def test_history_clear(hist_file): assert out == [] assert not os.path.exists(hist_file) + # Clear the history again and make sure the FileNotFoundError from trying to delete missing history file is silent + run_cmd(app, 'history --clear') + + # Cause os.remove to fail and make sure error gets printed + mock_remove = mocker.patch('os.remove') + mock_remove.side_effect = OSError + + out, err = run_cmd(app, 'history --clear') + assert out == [] + assert 'Error removing history file' in err[0] + def test_history_verbose_with_other_options(base_app): # make sure -v shows a usage error if any other options are present options_to_test = ['-r', '-e', '-o file', '-t file', '-c', '-x'] -- cgit v1.2.1