Skip to content

Commit d935a23

Browse files
committed
chore: warn if consumer/provider name has spaces
Signed-off-by: JP-Ellis <[email protected]>
1 parent b10dcd7 commit d935a23

File tree

3 files changed

+29
-23
lines changed

3 files changed

+29
-23
lines changed

src/pact/message_consumer.py

+6
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ def __init__(
6363
PendingDeprecationWarning,
6464
stacklevel=2,
6565
)
66+
if len(name.split()) > 1:
67+
warnings.warn(
68+
"Consumer name should not contain spaces.",
69+
UserWarning,
70+
stacklevel=2
71+
)
6672
self.name = name
6773
self.service_cls = service_cls
6874
self.tags = tags

src/pact/provider.py

+6
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,10 @@ def __init__(self, name):
2121
PendingDeprecationWarning,
2222
stacklevel=2,
2323
)
24+
if len(name.split()) > 1:
25+
warnings.warn(
26+
"Provider name should not contain spaces.",
27+
UserWarning,
28+
stacklevel=2,
29+
)
2430
self.name = name

tests/test_issue.py

+17-23
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,29 @@
22
Tests for user-reported issues.
33
"""
44

5-
import json
6-
from pathlib import Path
5+
import pytest
76

8-
from pact import MessageConsumer, Provider, matchers
7+
from pact import MessageConsumer, Provider
98

109

11-
def test_github_850() -> None:
10+
def test_github_850(recwarn: pytest.WarningsRecorder) -> None:
1211
"""
1312
See https://github.com/pact-foundation/pact-python/issues/850.
13+
14+
User reported an issue with Pact files not being written when using consumer
15+
and provider names with spaces. A warning was added to the code to alert the
16+
user that the names should not contain spaces.
1417
"""
15-
contract = MessageConsumer("my_contract_consumer").has_pact_with(
16-
Provider("my_contract_provider"),
18+
MessageConsumer("my_contract Consumer").has_pact_with(
19+
Provider("my_contract Provider"),
1720
pact_dir="pacts",
1821
)
1922

20-
event_data = {
21-
"invoice_id": "12345",
22-
"amount": 100.00,
23-
"currency": "USD",
24-
"created": matchers.Format().iso_8601_datetime(),
25-
}
26-
27-
contract.given("Create contract").expects_to_receive(
28-
"An event of contract"
29-
).with_content(event_data)
30-
31-
with contract:
32-
pass
33-
34-
with Path("pacts/my_contract_consumer-my_contract_provider.json").open() as f:
35-
data = json.load(f)
36-
assert data["messages"][0]["contents"]["created"] == "1991-02-20T06:35:26+00:00"
23+
# Check for warnings
24+
warnings = [str(warning.message) for warning in recwarn]
25+
assert any(
26+
"Consumer name should not contain spaces." in warning for warning in warnings
27+
)
28+
assert any(
29+
"Provider name should not contain spaces." in warning for warning in warnings
30+
)

0 commit comments

Comments
 (0)