diff --git a/spec/lib/capistrano/application_spec.rb b/spec/lib/capistrano/application_spec.rb index 41e5ce63..f326c76d 100644 --- a/spec/lib/capistrano/application_spec.rb +++ b/spec/lib/capistrano/application_spec.rb @@ -5,46 +5,40 @@ describe Capistrano::Application do it "provides a --format option which enables the choice of output formatting" - let(:help_output) do - out, _err = capture_io do - flags "--help", "-h" - end - out - end - - it "displays documentation URL as help banner" do - expect(help_output.lines.first).to match(/capistranorb.com/) + it "displays documentation URL as help banner", capture_io: true do + flags "--help", "-h" + expect($stdout.string.each_line.first).to match(/capistranorb.com/) end %w(quiet silent verbose).each do |switch| - it "doesn't include --#{switch} in help" do - expect(help_output).not_to match(/--#{switch}/) + it "doesn't include --#{switch} in help", capture_io: true do + flags "--help", "-h" + expect($stdout.string).not_to match(/--#{switch}/) end end - it "overrides the rake method, but still prints the rake version" do - out, _err = capture_io do - flags "--version", "-V" - end + it "overrides the rake method, but still prints the rake version", capture_io: true do + flags "--version", "-V" + out = $stdout.string expect(out).to match(/\bCapistrano Version\b/) expect(out).to match(/\b#{Capistrano::VERSION}\b/) expect(out).to match(/\bRake Version\b/) expect(out).to match(/\b#{Rake::VERSION}\b/) end - it "overrides the rake method, and sets the sshkit_backend to SSHKit::Backend::Printer" do - capture_io do - flags "--dry-run", "-n" - end + it "overrides the rake method, and sets the sshkit_backend to SSHKit::Backend::Printer", capture_io: true do + flags "--dry-run", "-n" sshkit_backend = Capistrano::Configuration.fetch(:sshkit_backend) expect(sshkit_backend).to eq(SSHKit::Backend::Printer) end - it "enables printing all config variables on command line parameter" do - capture_io do + it "enables printing all config variables on command line parameter", capture_io: true do + begin flags "--print-config-variables", "-p" + expect(Capistrano::Configuration.fetch(:print_config_variables)).to be true + ensure + Capistrano::Configuration.reset! end - expect(Capistrano::Configuration.fetch(:print_config_variables)).to be true end def flags(*sets) @@ -63,22 +57,4 @@ describe Capistrano::Application do subject.run subject.options end - - def capture_io - require "stringio" - - orig_stdout = $stdout - orig_stderr = $stderr - captured_stdout = StringIO.new - captured_stderr = StringIO.new - $stdout = captured_stdout - $stderr = captured_stderr - - yield - - return captured_stdout.string, captured_stderr.string - ensure - $stdout = orig_stdout - $stderr = orig_stderr - end end diff --git a/spec/lib/capistrano/configuration/plugin_installer_spec.rb b/spec/lib/capistrano/configuration/plugin_installer_spec.rb index 3f5d2738..c218b81e 100644 --- a/spec/lib/capistrano/configuration/plugin_installer_spec.rb +++ b/spec/lib/capistrano/configuration/plugin_installer_spec.rb @@ -49,7 +49,7 @@ module Capistrano expect(task.prerequisites).to eq([:example_prerequisite]) end - it "sets defaults when load:defaults is invoked" do + it "sets defaults when load:defaults is invoked", capture_io: true do expect(fetch(:example_variable)).to be_nil invoke "load:defaults" expect(fetch(:example_variable)).to eq("foo") diff --git a/spec/lib/capistrano/configuration/question_spec.rb b/spec/lib/capistrano/configuration/question_spec.rb index 4ad36b37..985ae60a 100644 --- a/spec/lib/capistrano/configuration/question_spec.rb +++ b/spec/lib/capistrano/configuration/question_spec.rb @@ -58,7 +58,7 @@ module Capistrano end end - context "tty unavailable" do + context "tty unavailable", capture_io: true do before do $stdin.expects(:gets).never $stdin.expects(:tty?).returns(false) diff --git a/spec/lib/capistrano/configuration/scm_resolver_spec.rb b/spec/lib/capistrano/configuration/scm_resolver_spec.rb index 1e0dc5c5..bcbd28a2 100644 --- a/spec/lib/capistrano/configuration/scm_resolver_spec.rb +++ b/spec/lib/capistrano/configuration/scm_resolver_spec.rb @@ -1,4 +1,5 @@ require "spec_helper" +require "capistrano/scm" module Capistrano class Configuration @@ -24,12 +25,12 @@ module Capistrano expect { resolver.resolve }.to output(/will not load the git scm/i).to_stderr end - it "activates the git scm" do + it "activates the git scm", capture_io: true do resolver.resolve expect(Rake::Task["git:wrapper"]).not_to be_nil end - it "sets :scm to :git" do + it "sets :scm to :git", capture_io: true do resolver.resolve expect(fetch(:scm)).to eq(:git) end diff --git a/spec/lib/capistrano/doctor/environment_doctor_spec.rb b/spec/lib/capistrano/doctor/environment_doctor_spec.rb index 9b4ffc24..7774fcd8 100644 --- a/spec/lib/capistrano/doctor/environment_doctor_spec.rb +++ b/spec/lib/capistrano/doctor/environment_doctor_spec.rb @@ -29,7 +29,7 @@ module Capistrano Rake::Task.clear end - it "has an doctor:environment task that calls EnvironmentDoctor" do + it "has an doctor:environment task that calls EnvironmentDoctor", capture_io: true do EnvironmentDoctor.any_instance.expects(:call) Rake::Task["doctor:environment"].invoke end diff --git a/spec/lib/capistrano/doctor/gems_doctor_spec.rb b/spec/lib/capistrano/doctor/gems_doctor_spec.rb index c31d4d89..99c4d791 100644 --- a/spec/lib/capistrano/doctor/gems_doctor_spec.rb +++ b/spec/lib/capistrano/doctor/gems_doctor_spec.rb @@ -53,7 +53,7 @@ module Capistrano Rake::Task.clear end - it "has an doctor:gems task that calls GemsDoctor" do + it "has an doctor:gems task that calls GemsDoctor", capture_io: true do GemsDoctor.any_instance.expects(:call) Rake::Task["doctor:gems"].invoke end diff --git a/spec/lib/capistrano/doctor/servers_doctor_spec.rb b/spec/lib/capistrano/doctor/servers_doctor_spec.rb index 67a8b5e5..24be7ba2 100644 --- a/spec/lib/capistrano/doctor/servers_doctor_spec.rb +++ b/spec/lib/capistrano/doctor/servers_doctor_spec.rb @@ -71,7 +71,7 @@ module Capistrano Rake::Task.clear end - it "has an doctor:servers task that calls ServersDoctor" do + it "has an doctor:servers task that calls ServersDoctor", capture_io: true do ServersDoctor.any_instance.expects(:call) Rake::Task["doctor:servers"].invoke end diff --git a/spec/lib/capistrano/doctor/variables_doctor_spec.rb b/spec/lib/capistrano/doctor/variables_doctor_spec.rb index 3bca4f03..6d46977d 100644 --- a/spec/lib/capistrano/doctor/variables_doctor_spec.rb +++ b/spec/lib/capistrano/doctor/variables_doctor_spec.rb @@ -74,7 +74,7 @@ module Capistrano Rake::Task.clear end - it "has an doctor:variables task that calls VariablesDoctor" do + it "has an doctor:variables task that calls VariablesDoctor", capture_io: true do VariablesDoctor.any_instance.expects(:call) Rake::Task["doctor:variables"].invoke end diff --git a/spec/lib/capistrano/dsl/task_enhancements_spec.rb b/spec/lib/capistrano/dsl/task_enhancements_spec.rb index bb41fc3c..54bf8e0c 100644 --- a/spec/lib/capistrano/dsl/task_enhancements_spec.rb +++ b/spec/lib/capistrano/dsl/task_enhancements_spec.rb @@ -35,7 +35,7 @@ module Capistrano end end - it "invokes in proper order if define after than before" do + it "invokes in proper order if define after than before", capture_io: true do task_enhancements.after("task", "after_task") task_enhancements.before("task", "before_task") @@ -44,7 +44,7 @@ module Capistrano expect(order).to eq(%w(before_task task after_task)) end - it "invokes in proper order if define before than after" do + it "invokes in proper order if define before than after", capture_io: true do task_enhancements.before("task", "before_task") task_enhancements.after("task", "after_task") @@ -53,7 +53,7 @@ module Capistrano expect(order).to eq(%w(before_task task after_task)) end - it "invokes in proper order when referring to as-yet undefined tasks" do + it "invokes in proper order when referring to as-yet undefined tasks", capture_io: true do task_enhancements.after("task", "not_loaded_task") Rake::Task.define_task("not_loaded_task") do @@ -65,7 +65,7 @@ module Capistrano expect(order).to eq(%w(task not_loaded_task)) end - it "invokes in proper order and with arguments and block" do + it "invokes in proper order and with arguments and block", capture_io: true do task_enhancements.after("task", "after_task_custom", :order) do |_t, _args| order.push "after_task" end @@ -79,7 +79,7 @@ module Capistrano expect(order).to eq(%w(before_task task after_task)) end - it "invokes using the correct namespace when defined within a namespace" do + it "invokes using the correct namespace when defined within a namespace", capture_io: true do Rake.application.in_namespace("namespace") do Rake::Task.define_task("task") do |t| order.push(t.name) @@ -99,7 +99,7 @@ module Capistrano ) end - it "raises a sensible error if the task isn't found" do + it "raises a sensible error if the task isn't found", capture_io: true do task_enhancements.after("task", "non_existent_task") expect { Rake::Task["task"].invoke order }.to raise_error(ArgumentError, 'Task "non_existent_task" not found') end diff --git a/spec/lib/capistrano/dsl_spec.rb b/spec/lib/capistrano/dsl_spec.rb index c58b0df6..751a3f23 100644 --- a/spec/lib/capistrano/dsl_spec.rb +++ b/spec/lib/capistrano/dsl_spec.rb @@ -60,7 +60,7 @@ module Capistrano end end - it "prints helpful message to stderr" do + it "prints helpful message to stderr", capture_io: true do expect do expect do task.invoke @@ -72,7 +72,7 @@ module Capistrano describe "#invoke" do context "reinvoking" do - it "will not reenable invoking task" do + it "will not reenable invoking task", capture_io: true do counter = 0 Rake::Task.define_task("A") do @@ -85,7 +85,7 @@ module Capistrano end.to change { counter }.by(1) end - it "will print a message on stderr" do + it "will print a message on stderr", capture_io: true do Rake::Task.define_task("B") expect do @@ -98,7 +98,7 @@ module Capistrano describe "#invoke!" do context "reinvoking" do - it "will reenable invoking task" do + it "will reenable invoking task", capture_io: true do counter = 0 Rake::Task.define_task("C") do @@ -111,7 +111,7 @@ module Capistrano end.to change { counter }.by(2) end - it "will not print a message on stderr" do + it "will not print a message on stderr", capture_io: true do Rake::Task.define_task("D") expect do diff --git a/spec/lib/capistrano/plugin_spec.rb b/spec/lib/capistrano/plugin_spec.rb index f30ba792..7f3b2baf 100644 --- a/spec/lib/capistrano/plugin_spec.rb +++ b/spec/lib/capistrano/plugin_spec.rb @@ -62,14 +62,14 @@ module Capistrano dummy.expects(:set_defaults).never end - it "calls set_defaults during load:defaults" do + it "calls set_defaults during load:defaults", capture_io: true do dummy = DummyPlugin.new dummy.expects(:set_defaults).once install_plugin(dummy) Rake::Task["load:defaults"].invoke end - it "is able to load tasks from a .rake file" do + it "is able to load tasks from a .rake file", capture_io: true do install_plugin(ExternalTasksPlugin) Rake::Task["plugin_test"].invoke expect(fetch(:plugin_result)).to eq("hello") diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 8db58936..c6b48840 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -13,4 +13,17 @@ RSpec.configure do |config| config.raise_errors_for_deprecations! config.mock_framework = :mocha config.order = "random" + + config.around(:example, capture_io: true) do |example| + begin + Rake.application.options.trace_output = StringIO.new + $stdout = StringIO.new + $stderr = StringIO.new + example.run + ensure + Rake.application.options.trace_output = STDERR + $stdout = STDOUT + $stderr = STDERR + end + end end