Skip to content

Commit

Permalink
chore: warn if consumer/provider name has spaces
Browse files Browse the repository at this point in the history
Discoeverd by user-reported issue in #850.

Signed-off-by: JP-Ellis <[email protected]>
  • Loading branch information
JP-Ellis committed Nov 10, 2024
1 parent 87ad916 commit da3c9d4
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 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
67 changes: 67 additions & 0 deletions tests/test_issue.py
Original file line number Diff line number Diff line change
@@ -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
)

0 comments on commit da3c9d4

Please sign in to comment.