From da3c9d4e633b9a58f1ef0397e4b99dafa2e243ab Mon Sep 17 00:00:00 2001 From: JP-Ellis Date: Fri, 8 Nov 2024 13:30:24 +1100 Subject: [PATCH] chore: warn if consumer/provider name has spaces Discoeverd by user-reported issue in #850. Signed-off-by: JP-Ellis --- src/pact/message_consumer.py | 6 ++++ src/pact/provider.py | 6 ++++ tests/test_issue.py | 67 ++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+) create mode 100644 tests/test_issue.py diff --git a/src/pact/message_consumer.py b/src/pact/message_consumer.py index ea122a553..fe020d984 100644 --- a/src/pact/message_consumer.py +++ b/src/pact/message_consumer.py @@ -63,6 +63,12 @@ def __init__( PendingDeprecationWarning, stacklevel=2, ) + if len(name.split()) > 1: + warnings.warn( + "Consumer name should not contain spaces.", + UserWarning, + stacklevel=2 + ) self.name = name self.service_cls = service_cls self.tags = tags diff --git a/src/pact/provider.py b/src/pact/provider.py index 4ee48cbee..f4e1c7db3 100644 --- a/src/pact/provider.py +++ b/src/pact/provider.py @@ -21,4 +21,10 @@ def __init__(self, name): PendingDeprecationWarning, stacklevel=2, ) + if len(name.split()) > 1: + warnings.warn( + "Provider name should not contain spaces.", + UserWarning, + stacklevel=2, + ) self.name = name diff --git a/tests/test_issue.py b/tests/test_issue.py new file mode 100644 index 000000000..46ba439c3 --- /dev/null +++ b/tests/test_issue.py @@ -0,0 +1,67 @@ +""" +Tests for user-reported issues. +""" + +import os +from pathlib import Path + +import pytest + +from pact import MessageConsumer, Provider, matchers + + +def test_github_850_issue() -> None: + """ + See https://github.com/pact-foundation/pact-python/issues/850. + + User reported an issue with Pact files not being written when using consumer + and provider names with spaces. A warning was added to the code to alert the + user that the names should not contain spaces. + """ + contract = MessageConsumer("My Consumer").has_pact_with( + Provider("My Provider"), + pact_dir="pacts", + ) + + # Define os dados do evento + event_data = { + "invoice_id": "12345", + "amount": 100.00, + "currency": "USD", + "created": matchers.Format().iso_8601_datetime(), + } + + # Cria uma mensagem que representa o evento + contract.given("Create contract").expects_to_receive( + "An event of contract" + ).with_content(event_data) + + with contract: + pass + + pact_file = Path("pacts/my_consumer-my_provider.json") + # The file should only be created on non-Windows systems + assert pact_file.exists() == (os.name != "nt") + + +def test_github_850_fix(recwarn: pytest.WarningsRecorder) -> None: + """ + See https://github.com/pact-foundation/pact-python/issues/850. + + User reported an issue with Pact files not being written when using consumer + and provider names with spaces. A warning was added to the code to alert the + user that the names should not contain spaces. + """ + MessageConsumer("My Consumer").has_pact_with( + Provider("My Provider"), + pact_dir="pacts", + ) + + # Check for warnings + warnings = [str(warning.message) for warning in recwarn] + assert any( + "Consumer name should not contain spaces." in warning for warning in warnings + ) + assert any( + "Provider name should not contain spaces." in warning for warning in warnings + )