Skip to content

Commit

Permalink
[v2] Add warning for non-positive max-items value (#8855)
Browse files Browse the repository at this point in the history
Emit a warning when a non-positive integer is entered for the max-items pagination parameter.
  • Loading branch information
aemous authored Sep 20, 2024
1 parent 683244b commit 5114834
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changes/next-release/enhancement-paginator-41862.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"type": "enhancement",
"category": "paginator",
"description": "Add warning when a non-positive value is provided for the max-items pagination parameter."
}
10 changes: 9 additions & 1 deletion awscli/customizations/paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"""
import logging
import sys
from functools import partial

from botocore import xform_name
Expand All @@ -32,7 +33,7 @@

from awscli.arguments import BaseCLIArgument
from awscli.customizations.exceptions import ParamValidationError

from awscli.customizations.utils import uni_print

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -313,6 +314,11 @@ def __init__(self, name, documentation, parse_type, serialized_name):
self._parse_type = parse_type
self._required = False

def _emit_non_positive_max_items_warning(self):
uni_print(
"warning: Non-positive values for --max-items may result in undefined behavior.\n",
sys.stderr)

@property
def cli_name(self):
return '--' + self._name
Expand All @@ -339,6 +345,8 @@ def add_to_parser(self, parser):

def add_to_params(self, parameters, value):
if value is not None:
if self._serialized_name == 'MaxItems' and int(value) <= 0:
self._emit_non_positive_max_items_warning()
pagination_config = parameters.get('PaginationConfig', {})
pagination_config[self._serialized_name] = value
parameters['PaginationConfig'] = pagination_config
24 changes: 24 additions & 0 deletions tests/unit/customizations/test_paginate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# 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 pytest

from awscli.customizations.paginate import PageArgument
from awscli.testutils import mock, unittest

from botocore.exceptions import DataNotFoundError
Expand All @@ -20,6 +23,11 @@
from awscli.customizations.exceptions import ParamValidationError


@pytest.fixture
def max_items_page_arg():
return PageArgument('max-items', 'documentation', int, 'MaxItems')


class TestPaginateBase(unittest.TestCase):

def setUp(self):
Expand Down Expand Up @@ -350,3 +358,19 @@ def test_can_handle_missing_page_size(self):
del self.parsed_args.page_size
self.assertIsNone(paginate.ensure_paging_params_not_set(
self.parsed_args, {}))

class TestNonPositiveMaxItems:
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 5114834

Please sign in to comment.