1
0
Fork 0
mirror of https://github.com/capistrano/capistrano synced 2023-03-27 23:21:18 -04:00

Silence console output in tests

* Rspec callback that assigns IO objects to collect and silence all output of the SUT and Rake, driven by example metadata.
* Consistent cleanup in the test for print-config-variables option.
* Requiring necessary lib, which was missing in scm-resolver spec file.
This commit is contained in:
Nikolay Vashchenko 2018-10-19 03:13:05 +02:00
parent 86362cf727
commit 80133b6770
12 changed files with 51 additions and 61 deletions

View file

@ -5,46 +5,40 @@ describe Capistrano::Application do
it "provides a --format option which enables the choice of output formatting" it "provides a --format option which enables the choice of output formatting"
let(:help_output) do it "displays documentation URL as help banner", capture_io: true do
out, _err = capture_io do
flags "--help", "-h" flags "--help", "-h"
end expect($stdout.string.each_line.first).to match(/capistranorb.com/)
out
end
it "displays documentation URL as help banner" do
expect(help_output.lines.first).to match(/capistranorb.com/)
end end
%w(quiet silent verbose).each do |switch| %w(quiet silent verbose).each do |switch|
it "doesn't include --#{switch} in help" do it "doesn't include --#{switch} in help", capture_io: true do
expect(help_output).not_to match(/--#{switch}/) flags "--help", "-h"
expect($stdout.string).not_to match(/--#{switch}/)
end end
end end
it "overrides the rake method, but still prints the rake version" do it "overrides the rake method, but still prints the rake version", capture_io: true do
out, _err = capture_io do
flags "--version", "-V" flags "--version", "-V"
end out = $stdout.string
expect(out).to match(/\bCapistrano Version\b/) expect(out).to match(/\bCapistrano Version\b/)
expect(out).to match(/\b#{Capistrano::VERSION}\b/) expect(out).to match(/\b#{Capistrano::VERSION}\b/)
expect(out).to match(/\bRake Version\b/) expect(out).to match(/\bRake Version\b/)
expect(out).to match(/\b#{Rake::VERSION}\b/) expect(out).to match(/\b#{Rake::VERSION}\b/)
end end
it "overrides the rake method, and sets the sshkit_backend to SSHKit::Backend::Printer" do it "overrides the rake method, and sets the sshkit_backend to SSHKit::Backend::Printer", capture_io: true do
capture_io do
flags "--dry-run", "-n" flags "--dry-run", "-n"
end
sshkit_backend = Capistrano::Configuration.fetch(:sshkit_backend) sshkit_backend = Capistrano::Configuration.fetch(:sshkit_backend)
expect(sshkit_backend).to eq(SSHKit::Backend::Printer) expect(sshkit_backend).to eq(SSHKit::Backend::Printer)
end end
it "enables printing all config variables on command line parameter" do it "enables printing all config variables on command line parameter", capture_io: true do
capture_io do begin
flags "--print-config-variables", "-p" flags "--print-config-variables", "-p"
end
expect(Capistrano::Configuration.fetch(:print_config_variables)).to be true expect(Capistrano::Configuration.fetch(:print_config_variables)).to be true
ensure
Capistrano::Configuration.reset!
end
end end
def flags(*sets) def flags(*sets)
@ -63,22 +57,4 @@ describe Capistrano::Application do
subject.run subject.run
subject.options subject.options
end 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 end

View file

@ -49,7 +49,7 @@ module Capistrano
expect(task.prerequisites).to eq([:example_prerequisite]) expect(task.prerequisites).to eq([:example_prerequisite])
end 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 expect(fetch(:example_variable)).to be_nil
invoke "load:defaults" invoke "load:defaults"
expect(fetch(:example_variable)).to eq("foo") expect(fetch(:example_variable)).to eq("foo")

View file

@ -58,7 +58,7 @@ module Capistrano
end end
end end
context "tty unavailable" do context "tty unavailable", capture_io: true do
before do before do
$stdin.expects(:gets).never $stdin.expects(:gets).never
$stdin.expects(:tty?).returns(false) $stdin.expects(:tty?).returns(false)

View file

@ -1,4 +1,5 @@
require "spec_helper" require "spec_helper"
require "capistrano/scm"
module Capistrano module Capistrano
class Configuration class Configuration
@ -24,12 +25,12 @@ module Capistrano
expect { resolver.resolve }.to output(/will not load the git scm/i).to_stderr expect { resolver.resolve }.to output(/will not load the git scm/i).to_stderr
end end
it "activates the git scm" do it "activates the git scm", capture_io: true do
resolver.resolve resolver.resolve
expect(Rake::Task["git:wrapper"]).not_to be_nil expect(Rake::Task["git:wrapper"]).not_to be_nil
end end
it "sets :scm to :git" do it "sets :scm to :git", capture_io: true do
resolver.resolve resolver.resolve
expect(fetch(:scm)).to eq(:git) expect(fetch(:scm)).to eq(:git)
end end

View file

@ -29,7 +29,7 @@ module Capistrano
Rake::Task.clear Rake::Task.clear
end 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) EnvironmentDoctor.any_instance.expects(:call)
Rake::Task["doctor:environment"].invoke Rake::Task["doctor:environment"].invoke
end end

View file

@ -53,7 +53,7 @@ module Capistrano
Rake::Task.clear Rake::Task.clear
end 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) GemsDoctor.any_instance.expects(:call)
Rake::Task["doctor:gems"].invoke Rake::Task["doctor:gems"].invoke
end end

View file

@ -71,7 +71,7 @@ module Capistrano
Rake::Task.clear Rake::Task.clear
end 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) ServersDoctor.any_instance.expects(:call)
Rake::Task["doctor:servers"].invoke Rake::Task["doctor:servers"].invoke
end end

View file

@ -74,7 +74,7 @@ module Capistrano
Rake::Task.clear Rake::Task.clear
end 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) VariablesDoctor.any_instance.expects(:call)
Rake::Task["doctor:variables"].invoke Rake::Task["doctor:variables"].invoke
end end

View file

@ -35,7 +35,7 @@ module Capistrano
end end
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.after("task", "after_task")
task_enhancements.before("task", "before_task") task_enhancements.before("task", "before_task")
@ -44,7 +44,7 @@ module Capistrano
expect(order).to eq(%w(before_task task after_task)) expect(order).to eq(%w(before_task task after_task))
end 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.before("task", "before_task")
task_enhancements.after("task", "after_task") task_enhancements.after("task", "after_task")
@ -53,7 +53,7 @@ module Capistrano
expect(order).to eq(%w(before_task task after_task)) expect(order).to eq(%w(before_task task after_task))
end 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") task_enhancements.after("task", "not_loaded_task")
Rake::Task.define_task("not_loaded_task") do Rake::Task.define_task("not_loaded_task") do
@ -65,7 +65,7 @@ module Capistrano
expect(order).to eq(%w(task not_loaded_task)) expect(order).to eq(%w(task not_loaded_task))
end 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| task_enhancements.after("task", "after_task_custom", :order) do |_t, _args|
order.push "after_task" order.push "after_task"
end end
@ -79,7 +79,7 @@ module Capistrano
expect(order).to eq(%w(before_task task after_task)) expect(order).to eq(%w(before_task task after_task))
end 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.application.in_namespace("namespace") do
Rake::Task.define_task("task") do |t| Rake::Task.define_task("task") do |t|
order.push(t.name) order.push(t.name)
@ -99,7 +99,7 @@ module Capistrano
) )
end 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") task_enhancements.after("task", "non_existent_task")
expect { Rake::Task["task"].invoke order }.to raise_error(ArgumentError, 'Task "non_existent_task" not found') expect { Rake::Task["task"].invoke order }.to raise_error(ArgumentError, 'Task "non_existent_task" not found')
end end

View file

@ -60,7 +60,7 @@ module Capistrano
end end
end end
it "prints helpful message to stderr" do it "prints helpful message to stderr", capture_io: true do
expect do expect do
expect do expect do
task.invoke task.invoke
@ -72,7 +72,7 @@ module Capistrano
describe "#invoke" do describe "#invoke" do
context "reinvoking" do context "reinvoking" do
it "will not reenable invoking task" do it "will not reenable invoking task", capture_io: true do
counter = 0 counter = 0
Rake::Task.define_task("A") do Rake::Task.define_task("A") do
@ -85,7 +85,7 @@ module Capistrano
end.to change { counter }.by(1) end.to change { counter }.by(1)
end 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") Rake::Task.define_task("B")
expect do expect do
@ -98,7 +98,7 @@ module Capistrano
describe "#invoke!" do describe "#invoke!" do
context "reinvoking" do context "reinvoking" do
it "will reenable invoking task" do it "will reenable invoking task", capture_io: true do
counter = 0 counter = 0
Rake::Task.define_task("C") do Rake::Task.define_task("C") do
@ -111,7 +111,7 @@ module Capistrano
end.to change { counter }.by(2) end.to change { counter }.by(2)
end 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") Rake::Task.define_task("D")
expect do expect do

View file

@ -62,14 +62,14 @@ module Capistrano
dummy.expects(:set_defaults).never dummy.expects(:set_defaults).never
end end
it "calls set_defaults during load:defaults" do it "calls set_defaults during load:defaults", capture_io: true do
dummy = DummyPlugin.new dummy = DummyPlugin.new
dummy.expects(:set_defaults).once dummy.expects(:set_defaults).once
install_plugin(dummy) install_plugin(dummy)
Rake::Task["load:defaults"].invoke Rake::Task["load:defaults"].invoke
end 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) install_plugin(ExternalTasksPlugin)
Rake::Task["plugin_test"].invoke Rake::Task["plugin_test"].invoke
expect(fetch(:plugin_result)).to eq("hello") expect(fetch(:plugin_result)).to eq("hello")

View file

@ -13,4 +13,17 @@ RSpec.configure do |config|
config.raise_errors_for_deprecations! config.raise_errors_for_deprecations!
config.mock_framework = :mocha config.mock_framework = :mocha
config.order = "random" 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 end