Skip to content

Commit 11a8eee

Browse files
committed
Collection integration test output
Previously, integration tests (i.e. the tests that were run using Open3.popen3) did not have their output governed by the :show_output flag the way the other tests did. In addition, turning the :show_output flag on for a test could potentially cause the test to fail if it needed to examine the output for some value. This was because collection and console output were mutually exclusive. This change fixes both issues. First, output is now tee'd into the stdout and stderr StringIO streams and optionally the STDOUT and STDERR streams as well depending on the :show_output flag. An integration_helper was also introduced that encapsulated the behaviors for starting, running, capturing the output of, and returning the exit status of integration tests.
1 parent fe7c8c3 commit 11a8eee

8 files changed

Lines changed: 84 additions & 30 deletions

File tree

Gemfile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ end
1414
group :test do
1515
gem 'codeclimate-test-reporter'
1616
gem 'simplecov'
17+
gem 'tee'
1718
gem 'webmock'
1819
end
1920

Gemfile.lock

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ GEM
77
simplecov (>= 0.7.1, < 1.0.0)
88
crack (0.4.1)
99
safe_yaml (~> 0.9.0)
10-
debugger-ruby_core_source (1.2.3)
10+
debugger-ruby_core_source (1.2.4)
1111
diff-lcs (1.2.5)
1212
docile (1.1.0)
1313
multi_json (1.8.2)
@@ -46,6 +46,7 @@ GEM
4646
simplecov-html (~> 0.8.0)
4747
simplecov-html (0.8.0)
4848
slop (3.4.7)
49+
tee (1.0.0)
4950
webmock (1.16.0)
5051
addressable (>= 2.2.7)
5152
crack (>= 0.3.2)
@@ -63,5 +64,6 @@ DEPENDENCIES
6364
ruby-debug-base19x (>= 0.11.30.pre)
6465
ruby-debug-ide
6566
simplecov
67+
tee
6668
webmock
6769
yard

java-buildpack.iml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
<orderEntry type="library" scope="PROVIDED" name="bundler (v1.3.5, rbenv: 1.9.3-p448) [gem]" level="application" />
1919
<orderEntry type="library" scope="PROVIDED" name="codeclimate-test-reporter (v0.2.0, rbenv: 1.9.3-p448) [gem]" level="application" />
2020
<orderEntry type="library" scope="PROVIDED" name="crack (v0.4.1, rbenv: 1.9.3-p448) [gem]" level="application" />
21-
<orderEntry type="library" scope="PROVIDED" name="debugger-ruby_core_source (v1.2.3, rbenv: 1.9.3-p448) [gem]" level="application" />
21+
<orderEntry type="library" scope="PROVIDED" name="debugger-ruby_core_source (v1.2.4, rbenv: 1.9.3-p448) [gem]" level="application" />
2222
<orderEntry type="library" scope="PROVIDED" name="diff-lcs (v1.2.5, rbenv: 1.9.3-p448) [gem]" level="application" />
2323
<orderEntry type="library" scope="PROVIDED" name="docile (v1.1.0, rbenv: 1.9.3-p448) [gem]" level="application" />
2424
<orderEntry type="library" scope="PROVIDED" name="multi_json (v1.8.2, rbenv: 1.9.3-p448) [gem]" level="application" />
@@ -39,6 +39,7 @@
3939
<orderEntry type="library" scope="PROVIDED" name="simplecov (v0.8.2, rbenv: 1.9.3-p448) [gem]" level="application" />
4040
<orderEntry type="library" scope="PROVIDED" name="simplecov-html (v0.8.0, rbenv: 1.9.3-p448) [gem]" level="application" />
4141
<orderEntry type="library" scope="PROVIDED" name="slop (v3.4.7, rbenv: 1.9.3-p448) [gem]" level="application" />
42+
<orderEntry type="library" scope="PROVIDED" name="tee (v1.0.0, rbenv: 1.9.3-p448) [gem]" level="application" />
4243
<orderEntry type="library" scope="PROVIDED" name="webmock (v1.16.0, rbenv: 1.9.3-p448) [gem]" level="application" />
4344
<orderEntry type="library" scope="PROVIDED" name="yard (v0.8.7.3, rbenv: 1.9.3-p448) [gem]" level="application" />
4445
</component>

spec/bin/compile_spec.rb

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,22 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18-
require 'application_helper'
19-
require 'open3'
18+
require 'integration_helper'
2019

2120
describe 'compile script', :integration do
22-
include_context 'application_helper'
21+
include_context 'integration_helper'
2322

2423
it 'should return zero if success',
2524
app_fixture: 'integration_valid' do
2625

27-
Open3.popen3("bin/compile #{app_dir} #{app_dir}") do |stdin, stdout, stderr, wait_thr|
28-
expect(wait_thr.value).to be_success
29-
end
26+
run("bin/compile #{app_dir} #{app_dir + '.cache'}") { |status| expect(status).to be_success }
3027
end
3128

3229
it 'should fail to compile when no containers detect' do
33-
error = Open3.capture3("bin/compile #{app_dir} #{app_dir}")[1]
34-
expect(error).to match /No container can run the application/
30+
run("bin/compile #{app_dir} #{app_dir + '.cache'}") do |status|
31+
expect(status).not_to be_success
32+
expect(stderr.string).to match /No container can run the application/
33+
end
3534
end
3635

3736
end

spec/bin/detect_spec.rb

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,21 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18-
require 'application_helper'
19-
require 'open3'
18+
require 'integration_helper'
2019

2120
describe 'detect script', :integration do
22-
include_context 'application_helper'
21+
include_context 'integration_helper'
2322

2423
it 'should return zero if success',
2524
app_fixture: 'integration_valid' do
2625

27-
Open3.popen3("bin/detect #{app_dir}") do |stdin, stdout, stderr, wait_thr|
28-
expect(wait_thr.value).to be_success
29-
end
26+
run("bin/detect #{app_dir}") { |status| expect(status).to be_success }
27+
3028
end
3129

3230
it 'should fail to detect when no containers detect' do
33-
Open3.popen3("bin/detect #{app_dir}") do |stdin, stdout, stderr, wait_thr|
34-
expect(wait_thr.value).to_not be_success
31+
run("bin/detect #{app_dir}") do |status|
32+
expect(status).not_to be_success
3533
end
3634
end
3735

spec/bin/release_spec.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,22 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18-
require 'application_helper'
19-
require 'open3'
18+
require 'integration_helper'
2019

2120
describe 'release script', :integration do
22-
include_context 'application_helper'
21+
include_context 'integration_helper'
2322

2423
it 'should return zero if success',
2524
app_fixture: 'integration_valid' do
2625

27-
Open3.popen3("bin/release #{app_dir}") do |stdin, stdout, stderr, wait_thr|
28-
expect(wait_thr.value).to be_success
29-
end
30-
26+
run("bin/release #{app_dir}") { |status| expect(status).to be_success }
3127
end
3228

3329
it 'should fail to release when no containers detect' do
34-
error = Open3.capture3("bin/release #{app_dir}")[1]
35-
expect(error).to match /No container can run the application/
30+
run("bin/release #{app_dir}") do |status|
31+
expect(status).not_to be_success
32+
expect(stderr.string).to match /No container can run the application/
33+
end
3634
end
3735

3836
end

spec/console_helper.rb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,23 @@
1515
# limitations under the License.
1616

1717
require 'spec_helper'
18+
require 'tee'
1819

1920
shared_context 'console_helper' do
2021

22+
STDOUT.sync
23+
STDERR.sync
24+
2125
let(:stdout) { StringIO.new }
2226
let(:stderr) { StringIO.new }
2327

2428
before do |example|
25-
unless example.metadata[:show_output]
26-
$stdout = stdout
27-
$stderr = stderr
29+
$stdout = Tee.open(stdout, stdout: nil)
30+
$stderr = Tee.open(stderr, stdout: nil)
31+
32+
if example.metadata[:show_output]
33+
$stdout.add STDOUT
34+
$stderr.add STDERR
2835
end
2936
end
3037

@@ -33,4 +40,19 @@
3340
$stdout = STDOUT
3441
end
3542

43+
def capture_output(out, err)
44+
t_out = Thread.new { copy_stream(out, $stdout) }
45+
t_err = Thread.new { copy_stream(err, $stderr) }
46+
47+
t_out.join
48+
t_err.join
49+
end
50+
51+
def copy_stream(source, destination)
52+
while (buff = source.read(1))
53+
destination.write(buff)
54+
destination.flush
55+
end
56+
end
57+
3658
end

spec/integration_helper.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Encoding: utf-8
2+
# Cloud Foundry Java Buildpack
3+
# Copyright 2013 the original author or authors.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
require 'spec_helper'
18+
require 'application_helper'
19+
require 'console_helper'
20+
require 'open3'
21+
22+
shared_context 'integration_helper' do
23+
include_context 'application_helper'
24+
include_context 'console_helper'
25+
26+
def run(command)
27+
Open3.popen3(command) do |stdin, stdout, stderr, wait_thr|
28+
capture_output stdout, stderr
29+
yield wait_thr.value if block_given?
30+
end
31+
end
32+
33+
end

0 commit comments

Comments
 (0)