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

Use a stub for $stdin during testing (#2033)

This allows the tests to run even in a situation where stdin isn't
available, for example when the test is run with `< /dev/null` as is the
case for some packaging scripts.

Fixes #2032
This commit is contained in:
Matt Brictson 2019-09-04 08:39:26 -07:00 committed by GitHub
parent af6ad87241
commit 5c21b7007b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 18 deletions

View file

@ -36,12 +36,12 @@ module Capistrano
end end
def gets def gets
return unless $stdin.tty? return unless stdin.tty?
if echo? if echo?
$stdin.gets stdin.gets
else else
$stdin.noecho(&:gets).tap { $stdout.print "\n" } stdin.noecho(&:gets).tap { $stdout.print "\n" }
end end
rescue Errno::EIO rescue Errno::EIO
# when stdio gets closed # when stdio gets closed
@ -59,6 +59,10 @@ module Capistrano
def echo? def echo?
(options || {}).fetch(:echo, true) (options || {}).fetch(:echo, true)
end end
def stdin
(options || {}).fetch(:stdin, $stdin)
end
end end
end end
end end

View file

@ -356,14 +356,16 @@ describe Capistrano::DSL do
end end
describe "asking for a variable" do describe "asking for a variable" do
let(:stdin) { stub(tty?: true) }
before do before do
dsl.ask(:scm, :svn) dsl.ask(:scm, :svn, stdin: stdin)
$stdout.stubs(:print) $stdout.stubs(:print)
end end
context "variable is provided" do context "variable is provided" do
before do before do
$stdin.expects(:gets).returns("git") stdin.expects(:gets).returns("git")
end end
it "sets the input as the variable" do it "sets the input as the variable" do
@ -373,7 +375,7 @@ describe Capistrano::DSL do
context "variable is not provided" do context "variable is not provided" do
before do before do
$stdin.expects(:gets).returns("") stdin.expects(:gets).returns("")
end end
it "sets the variable as the default" do it "sets the variable as the default" do

View file

@ -3,12 +3,12 @@ require "spec_helper"
module Capistrano module Capistrano
class Configuration class Configuration
describe Question do describe Question do
let(:question) { Question.new(key, default, options) } let(:question) { Question.new(key, default, stdin: stdin) }
let(:question_without_echo) { Question.new(key, default, echo: false) } let(:question_without_echo) { Question.new(key, default, echo: false, stdin: stdin) }
let(:question_without_default) { Question.new(key, nil) } let(:question_without_default) { Question.new(key, nil, stdin: stdin) }
let(:default) { :default } let(:default) { :default }
let(:key) { :branch } let(:key) { :branch }
let(:options) { nil } let(:stdin) { stub(tty?: true) }
describe ".new" do describe ".new" do
it "takes a key, default, options" do it "takes a key, default, options" do
@ -22,15 +22,15 @@ module Capistrano
it "returns the echoed value" do it "returns the echoed value" do
$stdout.expects(:print).with("Please enter branch (default): ") $stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:gets).returns(branch) stdin.expects(:gets).returns(branch)
$stdin.expects(:noecho).never stdin.expects(:noecho).never
expect(question.call).to eq(branch) expect(question.call).to eq(branch)
end end
it "returns the value but does not echo it" do it "returns the value but does not echo it" do
$stdout.expects(:print).with("Please enter branch (default): ") $stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:noecho).returns(branch) stdin.expects(:noecho).returns(branch)
$stdout.expects(:print).with("\n") $stdout.expects(:print).with("\n")
expect(question_without_echo.call).to eq(branch) expect(question_without_echo.call).to eq(branch)
@ -38,8 +38,8 @@ module Capistrano
it "returns the value but has no default between parenthesis" do it "returns the value but has no default between parenthesis" do
$stdout.expects(:print).with("Please enter branch: ") $stdout.expects(:print).with("Please enter branch: ")
$stdin.expects(:gets).returns(branch) stdin.expects(:gets).returns(branch)
$stdin.expects(:noecho).never stdin.expects(:noecho).never
expect(question_without_default.call).to eq(branch) expect(question_without_default.call).to eq(branch)
end end
@ -50,7 +50,7 @@ module Capistrano
before do before do
$stdout.expects(:print).with("Please enter branch (default): ") $stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:gets).returns("") stdin.expects(:gets).returns("")
end end
it "returns the default as the value" do it "returns the default as the value" do
@ -60,8 +60,8 @@ module Capistrano
context "tty unavailable", capture_io: true 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)
end end
it "returns the default as the value" do it "returns the default as the value" do