Skip to content

Commit 5d48e4c

Browse files
committed
test: dynamically find available ports
1 parent c6d6a46 commit 5d48e4c

3 files changed

+16
-12
lines changed

spec/integration/cli_monkeypatch_spec.rb

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
require 'fileutils'
22
require 'support/integration_spec_support'
3+
require 'find_a_port'
34

45
describe "The pact-mock-service command line interface with a monkeypatch", mri_only: true do
56

67
include Pact::IntegrationTestSupport
78

89
before :all do
910
clear_dirs
10-
@pid = start_server 3234, "--monkeypatch #{Dir.pwd}/spec/support/monkeypatch.rb"
11+
@port = FindAPort.available_port
12+
@pid = start_server @port, "--monkeypatch #{Dir.pwd}/spec/support/monkeypatch.rb"
1113
end
1214

1315
let(:interaction) do
@@ -25,11 +27,11 @@
2527
end
2628

2729
it "starts up and responds with mocked responses" do
28-
response = setup_interaction 3234, interaction
30+
response = setup_interaction @port, interaction
2931

30-
`curl -H 'custom_header: bar' http://localhost:3234/greeting`
32+
`curl -H 'custom_header: bar' http://localhost:#{@port}/greeting`
3133

32-
response = verify 3234
34+
response = verify @port
3335
puts response.body unless response.status == 200
3436
expect(response.status).to eq 200
3537
end

spec/integration/cli_with_auto_pact_write_spec.rb

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
require 'fileutils'
22
require 'support/integration_spec_support'
3+
require 'find_a_port'
34

45
describe "The pact-mock-service command line interface", mri_only: true do
56

67
include Pact::IntegrationTestSupport
78

89
before :all do
910
clear_dirs
10-
@pid = start_server 1235, '--consumer Consumer --provider Provider'
11+
@port = FindAPort.available_port
12+
@pid = start_server @port, '--consumer Consumer --provider Provider'
1113
end
1214

1315
context "when the consumer and provider names are provided" do
1416
it "writes the pact to the specified directory on shutdown" do
1517
expect(File.exist?('tmp/pacts/consumer-provider.json')).to be false
16-
response = setup_interaction 1235
18+
response = setup_interaction @port
1719
expect(response.status).to eq 200
1820

19-
response = invoke_expected_request 1235
21+
response = invoke_expected_request @port
2022
expect(response.status).to eq 200
2123

2224
Process.kill "INT", @pid

spec/integration/stub_cli_with_multiple_pacts_spec.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,24 @@
11
require 'support/integration_spec_support'
2+
require 'find_a_port'
23

34
describe "The pact-stub-service command line interface with multiple pacts", mri_only: true do
45

56
include Pact::IntegrationTestSupport
67

7-
PORT = 5556
8-
98
before :all do
109
clear_dirs
11-
@pid = start_stub_server PORT, "spec/support/pact-for-stub-1.json spec/support/pact-for-stub-2.json"
10+
@port = FindAPort.available_port
11+
@pid = start_stub_server @port, "spec/support/pact-for-stub-1.json spec/support/pact-for-stub-2.json"
1212
end
1313

1414
it "includes the interactions from the first pact file" do
15-
response = Faraday.get "http://localhost:#{PORT}/path1"
15+
response = Faraday.get "http://localhost:#{@port}/path1"
1616
puts response.body if response.status != 200
1717
expect(response.status).to eq 200
1818
end
1919

2020
it "includes the interactions from the second pact file" do
21-
response = Faraday.get "http://localhost:#{PORT}/path2"
21+
response = Faraday.get "http://localhost:#{@port}/path2"
2222
puts response.body if response.status != 200
2323
expect(response.status).to eq 200
2424
end

0 commit comments

Comments
 (0)