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"
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

View file

@ -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")

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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")

View file

@ -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