Skip to content

Commit

Permalink
Use custom capture implementation in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
simi committed Apr 9, 2015
1 parent 1a82784 commit 2288502
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions core/spec/lib/refinery/cli_spec.rb
Expand Up @@ -13,7 +13,7 @@

context "when called with no args" do
it "shows info message" do
msg = capture(:stdout) { rake["refinery:override"].invoke }
msg = capture { rake["refinery:override"].invoke }

expect(msg).to match("You didn't specify anything valid to override. Here are some examples:")
expect(msg).to match("rake refinery:override view=pages/home")
Expand All @@ -38,7 +38,7 @@
it "shows message" do
ENV[env] = "non-existent"

msg = capture(:stdout) { rake["refinery:override"].invoke }
msg = capture { rake["refinery:override"].invoke }

expect(msg).to include(not_found_message)
end
Expand All @@ -57,7 +57,7 @@
it "copies file to app folder" do
ENV[env] = env_file_location

msg = capture(:stdout) { rake["refinery:override"].invoke }
msg = capture { rake["refinery:override"].invoke }

Array(spec_success_message).each do |message_fragment|
expect(msg).to include(message_fragment)
Expand Down Expand Up @@ -132,4 +132,31 @@
let(:copied_file_location) { "app/assets/stylesheets/refinery/#{file_name}" }
end
end

private

# From episode 029 of Ruby Tapas by Avdi
# https://rubytapas.dpdcart.com/subscriber/post?id=88
def capture(stream=STDOUT, &block)
old_stdout = stream.clone
pipe_r, pipe_w = IO.pipe
pipe_r.sync = true
output = ""
reader = Thread.new do
begin
loop do
output << pipe_r.readpartial(1024)
end
rescue EOFError
end
end
stream.reopen(pipe_w)
yield
ensure
stream.reopen(old_stdout)
pipe_w.close
reader.join
pipe_r.close
return output
end
end

0 comments on commit 2288502

Please sign in to comment.