From 30e07d3e68534babc312451dd74681091dc6fbcf Mon Sep 17 00:00:00 2001 From: Simon Willison Date: Fri, 12 Aug 2022 10:22:08 -0700 Subject: [PATCH] Fixed error when returning empty bucket, closes #76 --- s3_credentials/cli.py | 2 +- tests/test_s3_credentials.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/s3_credentials/cli.py b/s3_credentials/cli.py index a46bb76..79d8ee7 100644 --- a/s3_credentials/cli.py +++ b/s3_credentials/cli.py @@ -1169,7 +1169,7 @@ def stream_indented_json(iterator, indent=2): def paginate(service, method, list_key, **kwargs): paginator = service.get_paginator(method) for response in paginator.paginate(**kwargs): - yield from response[list_key] + yield from response.get(list_key) or [] def fix_json(row): diff --git a/tests/test_s3_credentials.py b/tests/test_s3_credentials.py index 8fb0a4c..b45d346 100644 --- a/tests/test_s3_credentials.py +++ b/tests/test_s3_credentials.py @@ -997,6 +997,15 @@ def test_list_bucket(stub_s3, options, expected): assert result.output == expected +def test_list_bucket_empty(stub_s3): + stub_s3.add_response("list_objects_v2", {}) + runner = CliRunner() + with runner.isolated_filesystem(): + result = runner.invoke(cli, ["list-bucket", "test-bucket"]) + assert result.exit_code == 0 + assert result.output == "[]\n" + + @pytest.fixture def stub_iam_for_list_roles(stub_iam): stub_iam.add_response(