Skip to content

Commit 5e2090c

Browse files
author
Juliya Smith
authored
Integ 1061 test format str to cols (#115)
1 parent b86f3dc commit 5e2090c

3 files changed

Lines changed: 73 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ how a consumer would use the library (e.g. adding unit tests, updating documenta
1010

1111
## Unreleased
1212

13+
### Fixed
14+
15+
- Bug where `code42 legal-hold show` would error when terminal was too small.
16+
1317
### Changed
1418

1519
- `-i` (`--incremental`) has been removed, use `-c` (`--use-checkpoint`) with a string name for the checkpoint instead.

src/code42cli/util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def format_string_list_to_columns(string_list, max_width=None):
7878
if not max_width:
7979
max_width, _ = shutil.get_terminal_size()
8080
column_width = len(max(string_list, key=len)) + _PADDING_SIZE
81-
num_columns = int(max_width / column_width)
81+
num_columns = int(max_width / column_width) or 1
8282
format_string = "{{:<{0}}}".format(column_width) * num_columns
8383
batches = [string_list[i : i + num_columns] for i in range(0, len(string_list), num_columns)]
8484
padding = ["" for _ in range(num_columns)]

tests/test_util.py

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
import pytest
22

33
from code42cli import PRODUCT_NAME
4-
from code42cli.util import does_user_agree, find_format_width
4+
from code42cli.util import (
5+
does_user_agree,
6+
find_format_width,
7+
format_string_list_to_columns,
8+
_PADDING_SIZE,
9+
)
510

611
TEST_HEADER = {u"key1": u"Column 1", u"key2": u"Column 10", u"key3": u"Column 100"}
712

@@ -22,9 +27,20 @@ def context_without_assume_yes(mocker, cli_state):
2227
return mocker.patch("code42cli.util.get_current_context", return_value=ctx)
2328

2429

30+
@pytest.fixture
31+
def echo_output(mocker):
32+
return mocker.patch("code42cli.util.echo")
33+
34+
2535
_NAMESPACE = "{}.util".format(PRODUCT_NAME)
2636

2737

38+
def get_expected_row_width(max_col_len, max_width):
39+
col_size = max_col_len + _PADDING_SIZE
40+
num_cols = int(max_width / col_size) or 1
41+
return col_size * num_cols
42+
43+
2844
def test_does_user_agree_when_user_says_y_returns_true(mocker, context_without_assume_yes):
2945
mocker.patch("builtins.input", return_value="y")
3046
assert does_user_agree("Test Prompt")
@@ -76,3 +92,54 @@ def test_find_format_width_filters_keys_not_present_in_header():
7692
result, _ = find_format_width(report, header_with_subset_keys)
7793
for item in result:
7894
assert u"key2" not in item.keys()
95+
96+
97+
def test_format_string_list_to_columns_when_given_no_string_list_does_not_echo(echo_output):
98+
format_string_list_to_columns([], None)
99+
format_string_list_to_columns(None, None)
100+
assert not echo_output.call_count
101+
102+
103+
def test_format_string_list_to_columns_when_not_given_max_uses_shell_size(mocker, echo_output):
104+
terminal_size = mocker.patch("code42cli.util.shutil.get_terminal_size")
105+
max_width = 30
106+
terminal_size.return_value = (max_width, None) # Cols, Rows
107+
108+
columns = ["col1", "col2"]
109+
format_string_list_to_columns(columns)
110+
111+
printed_row = echo_output.call_args_list[0][0][0]
112+
assert len(printed_row) == get_expected_row_width(4, max_width)
113+
assert printed_row == "col1 col2 "
114+
115+
116+
def test_format_string_list_to_columns_when_given_small_max_width_prints_one_column_per_row(echo_output):
117+
max_width = 5
118+
119+
columns = ["col1", "col2"]
120+
format_string_list_to_columns(columns, max_width)
121+
122+
expected_row_width = get_expected_row_width(4, max_width)
123+
printed_row = echo_output.call_args_list[0][0][0]
124+
assert len(printed_row) == expected_row_width
125+
assert printed_row == "col1 "
126+
127+
printed_row = echo_output.call_args_list[1][0][0]
128+
assert len(printed_row) == expected_row_width
129+
assert printed_row == "col2 "
130+
131+
132+
def test_format_string_list_to_columns_uses_width_of_longest_string(echo_output):
133+
max_width = 5
134+
135+
columns = ["col1", "col2_that_is_really_long"]
136+
format_string_list_to_columns(columns, max_width)
137+
138+
expected_row_width = get_expected_row_width(len("col2_that_is_really_long"), max_width)
139+
printed_row = echo_output.call_args_list[0][0][0]
140+
assert len(printed_row) == expected_row_width
141+
assert printed_row == "col1 "
142+
143+
printed_row = echo_output.call_args_list[1][0][0]
144+
assert len(printed_row) == expected_row_width
145+
assert printed_row == "col2_that_is_really_long "

0 commit comments

Comments
 (0)