Skip to content

Commit 821671e

Browse files
Merge pull request #53 from pact-foundation/verify-directories
Resolve #22: --pact-url accepts directories
2 parents 1bcb7d5 + 8291bb7 commit 821671e

File tree

2 files changed

+78
-2
lines changed

2 files changed

+78
-2
lines changed

pact/test/test_verify.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,56 @@ def test_deprecated_pact_urls(self):
156156
timeout=30)
157157

158158

159+
class expand_directoriesTestCase(TestCase):
160+
def setUp(self):
161+
super(expand_directoriesTestCase, self).setUp()
162+
163+
def posix_join(*args):
164+
return '/'.join(args)
165+
166+
self.addCleanup(patch.stopall)
167+
self.mock_isdir = patch.object(verify, 'isdir', autospec=True).start()
168+
self.mock_join = patch.object(
169+
verify, 'join', new=posix_join).start()
170+
self.mock_listdir = patch.object(
171+
verify, 'listdir', autospec=True).start()
172+
173+
def test_directory(self):
174+
self.mock_isdir.return_value = True
175+
self.mock_listdir.return_value = [
176+
'consumer-provider.json',
177+
'consumer2-provider.json',
178+
'unrelated-file.txt']
179+
180+
result = verify.expand_directories(['/tmp'])
181+
self.assertEqual(result, [
182+
'/tmp/consumer-provider.json',
183+
'/tmp/consumer2-provider.json',
184+
])
185+
186+
def test_file(self):
187+
self.mock_isdir.return_value = False
188+
result = verify.expand_directories(['/tmp/consumer-provider.json'])
189+
self.assertEqual(result, ['/tmp/consumer-provider.json'])
190+
191+
def test_uri(self):
192+
result = verify.expand_directories(['http://broker'])
193+
self.assertEqual(result, ['http://broker'])
194+
195+
def test_windows_directories(self):
196+
self.mock_isdir.return_value = True
197+
self.mock_listdir.return_value = [
198+
'consumer-provider.json',
199+
'consumer2-provider.json',
200+
'unrelated-file.txt']
201+
202+
result = verify.expand_directories(['C:\\tmp'])
203+
self.assertEqual(result, [
204+
'C:/tmp/consumer-provider.json',
205+
'C:/tmp/consumer2-provider.json',
206+
])
207+
208+
159209
class path_existsTestCase(TestCase):
160210
def setUp(self):
161211
super(path_existsTestCase, self).setUp()

pact/verify.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Methods to verify previously created pacts."""
22
import sys
3-
from os.path import isfile
3+
from os import listdir
4+
from os.path import isfile, isdir, join
45

56
import click
67

@@ -20,7 +21,7 @@
2021
@click.option(
2122
'pact_url', '--pact-url',
2223
help='The URI of the pact to verify.'
23-
' Can be an HTTP URI or a local file path.'
24+
' Can be an HTTP URI, a local file or directory path. '
2425
' It can be specified multiple times to verify several pacts.',
2526
multiple=True)
2627
@click.option(
@@ -79,6 +80,7 @@ def main(base_url, pact_url, pact_urls, states_url, states_setup_url, username,
7980
+ ' At least one of --pact-url or --pact-urls is required.')
8081
raise click.Abort()
8182

83+
all_pact_urls = expand_directories(all_pact_urls)
8284
missing_files = [path for path in all_pact_urls if not path_exists(path)]
8385
if missing_files:
8486
click.echo(
@@ -103,6 +105,30 @@ def main(base_url, pact_url, pact_urls, states_url, states_setup_url, username,
103105
sys.exit(p.returncode)
104106

105107

108+
def expand_directories(paths):
109+
"""
110+
Iterate over paths and expand any that are directories into file paths.
111+
112+
:param paths: A list of file paths to expand.
113+
:type paths: list
114+
:return: A list of file paths with any directory paths replaced with the
115+
JSON files in those directories.
116+
:rtype: list
117+
"""
118+
paths_ = []
119+
for path in paths:
120+
if path.startswith('http://') or path.startswith('https://'):
121+
paths_.append(path)
122+
elif isdir(path):
123+
paths_.extend(
124+
[join(path, p) for p in listdir(path) if p.endswith('.json')])
125+
else:
126+
paths_.append(path)
127+
128+
# Ruby pact verifier expects forward slashes regardless of OS
129+
return [p.replace('\\', '/') for p in paths_]
130+
131+
106132
def path_exists(path):
107133
"""
108134
Determine if a particular path exists.

0 commit comments

Comments
 (0)