Skip to content

Commit c5babe7

Browse files
committed
feat(stub-server): allow pacts to be loaded from a directory
Closes: pact-foundation#94
1 parent 486419b commit c5babe7

File tree

3 files changed

+97
-7
lines changed

3 files changed

+97
-7
lines changed

lib/pact/stub_service/cli.rb

+17-7
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ module Pact
1010
module StubService
1111
class CLI < Pact::MockService::CLI::CustomThor
1212

13-
desc 'PACT_URI ...', "Start a stub service with the given pact file(s)."
13+
desc 'PACT_URI ...', "Start a stub service with the given pact file(s) or directory."
1414
long_desc <<-DOC
15-
Start a stub service with the given pact file(s). Pact URIs may be local file paths or HTTP.
15+
Start a stub service with the given pact file(s) or directories. Pact URIs may be local
16+
file or directory paths, or HTTP.
1617
Include any basic auth details in the URL using the format https://USERNAME:PASSWORD@URI.
1718
Where multiple matching interactions are found, the interactions will be sorted by
1819
response status, and the first one will be returned. This may lead to some non-deterministic
1920
behaviour. If you are having problems with this, please raise it on the pact-dev google group,
2021
and we can discuss some potential enhancements.
2122
Note that only versions 1 and 2 of the pact specification are currently fully supported.
22-
Pacts using the v3 format may be used, however, any matching features added in v4 will
23+
Pacts using the v3 format may be used, however, any matching features added in v3 will
2324
currently be ignored.
2425
DOC
2526

@@ -33,13 +34,16 @@ class CLI < Pact::MockService::CLI::CustomThor
3334
method_option :stub_pactfile_paths, hide: true
3435
method_option :monkeypatch, hide: true
3536

36-
def service(*pactfiles)
37-
raise Thor::Error.new("Please provide an existing pact file to load") if pactfiles.empty?
37+
def service(*pact_files)
3838
require 'pact/mock_service/run'
39-
options.stub_pactfile_paths = pactfiles
39+
require 'pact/support/expand_file_list'
40+
41+
expanded_pact_files = file_list(pact_files)
42+
raise Thor::Error.new("Please provide at least one pact file to load") if expanded_pact_files.empty?
43+
4044
opts = Thor::CoreExt::HashWithIndifferentAccess.new
4145
opts.merge!(options)
42-
opts[:stub_pactfile_paths] = pactfiles
46+
opts[:stub_pactfile_paths] = expanded_pact_files
4347
opts[:pactfile_write_mode] = 'none'
4448
MockService::Run.(opts)
4549
end
@@ -52,6 +56,12 @@ def version
5256
end
5357

5458
default_task :service
59+
60+
no_commands do
61+
def file_list(pact_files)
62+
Pact::Support::ExpandFileList.call(pact_files)
63+
end
64+
end
5565
end
5666
end
5767
end

lib/pact/support/expand_file_list.rb

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module Pact
2+
module Support
3+
module ExpandFileList
4+
def self.call pact_files
5+
pact_files
6+
.collect{ |path| unixify_path(path) }
7+
.collect{ | path | expand_path(path) }
8+
.flatten
9+
end
10+
11+
def self.unixify_path(path)
12+
path.gsub(/\\+/, '/')
13+
end
14+
15+
def self.expand_path(path)
16+
if File.directory?(path)
17+
Dir.glob(File.join(path, "*.json"))
18+
elsif Dir.glob(path).any?
19+
Dir.glob(path)
20+
else
21+
path
22+
end
23+
end
24+
end
25+
end
26+
end
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
require 'pact/support/expand_file_list'
2+
3+
module Pact
4+
module Support
5+
module ExpandFileList
6+
describe "#call" do
7+
8+
subject { ExpandFileList.call(file_list) }
9+
10+
context "with a list of json files" do
11+
let(:file_list) { ["file1.json", "file2.json"] }
12+
13+
it "returns the list" do
14+
expect(subject).to eq file_list
15+
end
16+
end
17+
18+
context "with a list of json files that contains a windows path" do
19+
let(:file_list) { ["c:\\foo\\file1.json"] }
20+
21+
it "returns the list in Unix format" do
22+
expect(subject).to eq ["c:/foo/file1.json"]
23+
end
24+
end
25+
26+
context "with a directory" do
27+
let(:file_list) { ["spec/support/"] }
28+
29+
it "returns a list of the json files inside" do
30+
expect(subject.size).to be > 1
31+
32+
subject.each do | path |
33+
expect(path).to start_with("spec/support")
34+
expect(path).to end_with(".json")
35+
end
36+
end
37+
end
38+
39+
context "with a glob" do
40+
let(:file_list) { ["spec/support/*.json"] }
41+
42+
it "returns a list of the json files inside" do
43+
expect(subject.size).to be > 1
44+
45+
subject.each do | path |
46+
expect(path).to start_with("spec/support")
47+
expect(path).to end_with(".json")
48+
end
49+
end
50+
end
51+
end
52+
end
53+
end
54+
end

0 commit comments

Comments
 (0)