Skip to content

Commit

Permalink
chore: warn if consumer/provider name has spaces
Browse files Browse the repository at this point in the history
Signed-off-by: JP-Ellis <josh@jpellis.me>
  • Loading branch information
JP-Ellis committed Nov 8, 2024
1 parent b4d6c39 commit 0120ad9
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 23 deletions.
6 changes: 6 additions & 0 deletions src/pact/message_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ def __init__(
PendingDeprecationWarning,
stacklevel=2,
)
if len(name.split()) > 1:
warnings.warn(

Check warning on line 67 in src/pact/message_consumer.py

View check run for this annotation

Codecov / codecov/patch

src/pact/message_consumer.py#L67

Added line #L67 was not covered by tests
"Consumer name should not contain spaces.",
UserWarning,
stacklevel=2
)
self.name = name
self.service_cls = service_cls
self.tags = tags
Expand Down
6 changes: 6 additions & 0 deletions src/pact/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,10 @@ def __init__(self, name):
PendingDeprecationWarning,
stacklevel=2,
)
if len(name.split()) > 1:
warnings.warn(

Check warning on line 25 in src/pact/provider.py

View check run for this annotation

Codecov / codecov/patch

src/pact/provider.py#L25

Added line #L25 was not covered by tests
"Provider name should not contain spaces.",
UserWarning,
stacklevel=2,
)
self.name = name
40 changes: 17 additions & 23 deletions tests/test_issue.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)

0 comments on commit 0120ad9

Please sign in to comment.