Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions bin/awslocal
Original file line number Diff line number Diff line change
Expand Up @@ -168,8 +168,9 @@ def prepare_cmd_args():
print('ERROR: %s' % msg)
return sys.exit(1)

# prepare cmd args
cmd_args = sys.argv
# prepare cmd args (copy: the inserts below must not mutate sys.argv,
# otherwise repeated calls keep stacking --endpoint-url flags)
cmd_args = list(sys.argv)
if endpoint:
cmd_args.insert(1, '--endpoint-url=%s' % endpoint)
if 'https' in endpoint:
Expand Down
54 changes: 54 additions & 0 deletions tests/test_awslocal_prepare_cmd_args.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""Tests for prepare_cmd_args in bin/awslocal.

bin/awslocal has no .py extension, so it is loaded with SourceFileLoader.
localstack_client is stubbed: endpoint resolution is the only behavior
needed here, and the real package is an unrelated optional dependency.
"""

import sys
import types
from importlib.machinery import SourceFileLoader
from pathlib import Path

import pytest

BIN = Path(__file__).resolve().parent.parent / "bin" / "awslocal"


def _load_awslocal(monkeypatch):
fake = types.ModuleType("localstack_client")

class FakeConfig:
@staticmethod
def get_service_endpoints(localstack_host=None):
return {"sts": "http://localhost:4566", "s3": "http://localhost:4566"}

fake.config = FakeConfig()
monkeypatch.setitem(sys.modules, "localstack_client", fake)
return SourceFileLoader("awslocal_under_test", str(BIN)).load_module()


def test_prepare_cmd_args_inserts_endpoint_once(monkeypatch):
awslocal = _load_awslocal(monkeypatch)
monkeypatch.setattr(sys, "argv", ["awslocal", "s3", "ls"])
cmd_args = awslocal.prepare_cmd_args()
assert cmd_args.count("--endpoint-url=http://localhost:4566") == 1


def test_prepare_cmd_args_is_repeatable(monkeypatch):
"""Calling prepare_cmd_args twice must not duplicate flags.

Regression test: cmd_args aliased sys.argv, so every call inserted
another --endpoint-url into the live argv list.
"""
awslocal = _load_awslocal(monkeypatch)
monkeypatch.setattr(sys, "argv", ["awslocal", "s3", "ls"])
first = awslocal.prepare_cmd_args()
second = awslocal.prepare_cmd_args()
assert first.count("--endpoint-url=http://localhost:4566") == 1
assert second.count("--endpoint-url=http://localhost:4566") == 1
assert sys.argv.count("--endpoint-url=http://localhost:4566") <= 1


if __name__ == "__main__":
pytest.main([__file__])