Skip to content

Commit

Permalink
Update pagination against requested feedback.
Browse files Browse the repository at this point in the history
  • Loading branch information
aemous committed Sep 18, 2024
1 parent 93bfa49 commit f871ca6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 26 deletions.
6 changes: 3 additions & 3 deletions awscli/customizations/paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,16 @@ def add_to_parser(self, parser):
parser.add_argument(self.cli_name, dest=self.py_name,
type=self.type_map[self._parse_type])

def _check_and_warn_negative_max_items(self, value):
def _emit_non_positive_max_items_warning(self, value):
if int(value) <= 0:
uni_print(
"warning: Non-positive values for --max-items may result in undefined behavior.\n",
sys.stderr)

def add_to_params(self, parameters, value):
if self._serialized_name == 'MaxItems' and value is not None:
self._check_and_warn_negative_max_items(value)
if value is not None:
if self._serialized_name == 'MaxItems':
self._emit_non_positive_max_items_warning(value)
pagination_config = parameters.get('PaginationConfig', {})
pagination_config[self._serialized_name] = value
parameters['PaginationConfig'] = pagination_config
43 changes: 20 additions & 23 deletions tests/unit/customizations/test_paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
import contextlib
import io
import pytest

from awscli.customizations.paginate import PageArgument
from awscli.testutils import mock, unittest
Expand Down Expand Up @@ -355,24 +354,22 @@ def test_can_handle_missing_page_size(self):
self.assertIsNone(paginate.ensure_paging_params_not_set(
self.parsed_args, {}))

class TestNonPositiveIntWarnings(TestPaginateBase):

def setUp(self):
super(TestNonPositiveIntWarnings, self).setUp()
self._stderr = io.StringIO()
self._page_arg = PageArgument('max-items', 'documentation', int, 'MaxItems')

def test_positive_integer_does_not_raise_warning(self):
with contextlib.redirect_stderr(self._stderr):
self._page_arg.add_to_params({}, 1)
self.assertNotIn('Non-positive values for --max-items are unsupported', self._stderr.getvalue())

def test_zero_raises_warning(self):
with contextlib.redirect_stderr(self._stderr):
self._page_arg.add_to_params({}, 0)
self.assertIn('Non-positive values for --max-items may result in undefined behavior', self._stderr.getvalue())

def test_negative_integer_raises_warning(self):
with contextlib.redirect_stderr(self._stderr):
self._page_arg.add_to_params({}, -1)
self.assertIn('Non-positive values for --max-items may result in undefined behavior', self._stderr.getvalue())
class TestNonPositiveMaxItems:
@pytest.fixture
def max_items_page_arg(self):
return PageArgument('max-items', 'documentation', int, 'MaxItems')

def test_positive_integer_does_not_raise_warning(self, max_items_page_arg, capsys):
max_items_page_arg.add_to_params({}, 1)
captured = capsys.readouterr()
assert captured.err == ""

def test_zero_raises_warning(self, max_items_page_arg, capsys):
max_items_page_arg.add_to_params({}, 0)
captured = capsys.readouterr()
assert "Non-positive values for --max-items" in captured.err

def test_negative_integer_raises_warning(self, max_items_page_arg, capsys):
max_items_page_arg.add_to_params({}, -1)
captured = capsys.readouterr()
assert "Non-positive values for --max-items" in captured.err

0 comments on commit f871ca6

Please sign in to comment.