Skip to content

Commit da3c9d4

Browse files
committed
chore: warn if consumer/provider name has spaces
Discoeverd by user-reported issue in #850. Signed-off-by: JP-Ellis <[email protected]>
1 parent 87ad916 commit da3c9d4

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

src/pact/message_consumer.py

Lines changed: 6 additions & 0 deletions
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

Lines changed: 6 additions & 0 deletions
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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
"""
2+
Tests for user-reported issues.
3+
"""
4+
5+
import os
6+
from pathlib import Path
7+
8+
import pytest
9+
10+
from pact import MessageConsumer, Provider, matchers
11+
12+
13+
def test_github_850_issue() -> None:
14+
"""
15+
See https://github.com/pact-foundation/pact-python/issues/850.
16+
17+
User reported an issue with Pact files not being written when using consumer
18+
and provider names with spaces. A warning was added to the code to alert the
19+
user that the names should not contain spaces.
20+
"""
21+
contract = MessageConsumer("My Consumer").has_pact_with(
22+
Provider("My Provider"),
23+
pact_dir="pacts",
24+
)
25+
26+
# Define os dados do evento
27+
event_data = {
28+
"invoice_id": "12345",
29+
"amount": 100.00,
30+
"currency": "USD",
31+
"created": matchers.Format().iso_8601_datetime(),
32+
}
33+
34+
# Cria uma mensagem que representa o evento
35+
contract.given("Create contract").expects_to_receive(
36+
"An event of contract"
37+
).with_content(event_data)
38+
39+
with contract:
40+
pass
41+
42+
pact_file = Path("pacts/my_consumer-my_provider.json")
43+
# The file should only be created on non-Windows systems
44+
assert pact_file.exists() == (os.name != "nt")
45+
46+
47+
def test_github_850_fix(recwarn: pytest.WarningsRecorder) -> None:
48+
"""
49+
See https://github.com/pact-foundation/pact-python/issues/850.
50+
51+
User reported an issue with Pact files not being written when using consumer
52+
and provider names with spaces. A warning was added to the code to alert the
53+
user that the names should not contain spaces.
54+
"""
55+
MessageConsumer("My Consumer").has_pact_with(
56+
Provider("My Provider"),
57+
pact_dir="pacts",
58+
)
59+
60+
# Check for warnings
61+
warnings = [str(warning.message) for warning in recwarn]
62+
assert any(
63+
"Consumer name should not contain spaces." in warning for warning in warnings
64+
)
65+
assert any(
66+
"Provider name should not contain spaces." in warning for warning in warnings
67+
)

0 commit comments

Comments
 (0)