From 0120ad9041304c03f1b1371318e1000be79dc5ba Mon Sep 17 00:00:00 2001 From: JP-Ellis Date: Fri, 8 Nov 2024 16:01:45 +1100 Subject: [PATCH] chore: warn if consumer/provider name has spaces Signed-off-by: JP-Ellis --- src/pact/message_consumer.py | 6 ++++++ src/pact/provider.py | 6 ++++++ tests/test_issue.py | 40 +++++++++++++++--------------------- 3 files changed, 29 insertions(+), 23 deletions(-) diff --git a/src/pact/message_consumer.py b/src/pact/message_consumer.py index ea122a553a..fe020d9844 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 4ee48cbeed..f4e1c7db3a 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 index 2c0d0063b1..86db61585a 100644 --- a/tests/test_issue.py +++ b/tests/test_issue.py @@ -2,35 +2,29 @@ Tests for user-reported issues. """ -import json -from pathlib import Path +import pytest -from pact import MessageConsumer, Provider, matchers +from pact import MessageConsumer, Provider -def test_github_850() -> None: +def test_github_850(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. """ - contract = MessageConsumer("my_contract_consumer").has_pact_with( - Provider("my_contract_provider"), + MessageConsumer("my_contract Consumer").has_pact_with( + Provider("my_contract Provider"), pact_dir="pacts", ) - event_data = { - "invoice_id": "12345", - "amount": 100.00, - "currency": "USD", - "created": matchers.Format().iso_8601_datetime(), - } - - contract.given("Create contract").expects_to_receive( - "An event of contract" - ).with_content(event_data) - - with contract: - pass - - with Path("pacts/my_contract_consumer-my_contract_provider.json").open() as f: - data = json.load(f) - assert data["messages"][0]["contents"]["created"] == "1991-02-20T06:35:26+00:00" + # 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 + )