Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use a stub for $stdin during testing #2033

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 7 additions & 3 deletions lib/capistrano/configuration/question.rb
Expand Up @@ -36,12 +36,12 @@ def response
end

def gets
return unless $stdin.tty?
return unless stdin.tty?

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

def stdin
(options || {}).fetch(:stdin, $stdin)
end
end
end
end
8 changes: 5 additions & 3 deletions spec/integration/dsl_spec.rb
Expand Up @@ -356,14 +356,16 @@
end

describe "asking for a variable" do
let(:stdin) { stub(tty?: true) }

before do
dsl.ask(:scm, :svn)
dsl.ask(:scm, :svn, stdin: stdin)
$stdout.stubs(:print)
end

context "variable is provided" do
before do
$stdin.expects(:gets).returns("git")
stdin.expects(:gets).returns("git")
end

it "sets the input as the variable" do
Expand All @@ -373,7 +375,7 @@

context "variable is not provided" do
before do
$stdin.expects(:gets).returns("")
stdin.expects(:gets).returns("")
end

it "sets the variable as the default" do
Expand Down
24 changes: 12 additions & 12 deletions spec/lib/capistrano/configuration/question_spec.rb
Expand Up @@ -3,12 +3,12 @@
module Capistrano
class Configuration
describe Question do
let(:question) { Question.new(key, default, options) }
let(:question_without_echo) { Question.new(key, default, echo: false) }
let(:question_without_default) { Question.new(key, nil) }
let(:question) { Question.new(key, default, stdin: stdin) }
let(:question_without_echo) { Question.new(key, default, echo: false, stdin: stdin) }
let(:question_without_default) { Question.new(key, nil, stdin: stdin) }
let(:default) { :default }
let(:key) { :branch }
let(:options) { nil }
let(:stdin) { stub(tty?: true) }

describe ".new" do
it "takes a key, default, options" do
Expand All @@ -22,24 +22,24 @@ class Configuration

it "returns the echoed value" do
$stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:gets).returns(branch)
$stdin.expects(:noecho).never
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never

expect(question.call).to eq(branch)
end

it "returns the value but does not echo it" do
$stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:noecho).returns(branch)
stdin.expects(:noecho).returns(branch)
$stdout.expects(:print).with("\n")

expect(question_without_echo.call).to eq(branch)
end

it "returns the value but has no default between parenthesis" do
$stdout.expects(:print).with("Please enter branch: ")
$stdin.expects(:gets).returns(branch)
$stdin.expects(:noecho).never
stdin.expects(:gets).returns(branch)
stdin.expects(:noecho).never

expect(question_without_default.call).to eq(branch)
end
Expand All @@ -50,7 +50,7 @@ class Configuration

before do
$stdout.expects(:print).with("Please enter branch (default): ")
$stdin.expects(:gets).returns("")
stdin.expects(:gets).returns("")
end

it "returns the default as the value" do
Expand All @@ -60,8 +60,8 @@ class Configuration

context "tty unavailable", capture_io: true do
before do
$stdin.expects(:gets).never
$stdin.expects(:tty?).returns(false)
stdin.expects(:gets).never
stdin.expects(:tty?).returns(false)
end

it "returns the default as the value" do
Expand Down