diff options
author | sersut <serdar@opscode.com> | 2014-03-19 17:01:57 -0700 |
---|---|---|
committer | sersut <serdar@opscode.com> | 2014-03-19 17:42:58 -0700 |
commit | 41658bb4fbae8c1e54fe9bed1c6b0839e8b4e3e3 (patch) | |
tree | aa1738792912ab2ead4a9731dc272a83dcd472f7 /spec | |
parent | 3328f4749faea190b4d5c17302aa83a6591f6305 (diff) | |
download | chef-41658bb4fbae8c1e54fe9bed1c6b0839e8b4e3e3.tar.gz |
Remove should_exit out of parameters and make ui.confirm less ugly.
Diffstat (limited to 'spec')
-rw-r--r-- | spec/unit/knife/client_bulk_delete_spec.rb | 4 | ||||
-rw-r--r-- | spec/unit/knife/core/ui_spec.rb | 161 |
2 files changed, 102 insertions, 63 deletions
diff --git a/spec/unit/knife/client_bulk_delete_spec.rb b/spec/unit/knife/client_bulk_delete_spec.rb index f490851cbc..7df7d02e9b 100644 --- a/spec/unit/knife/client_bulk_delete_spec.rb +++ b/spec/unit/knife/client_bulk_delete_spec.rb @@ -28,6 +28,7 @@ describe Chef::Knife::ClientBulkDelete do k.config = option_args k.ui.stub(:stdout).and_return(stdout_io) k.ui.stub(:confirm).and_return(knife_confirm) + k.ui.stub(:confirm_without_exit).and_return(knife_confirm) k } @@ -132,7 +133,8 @@ describe Chef::Knife::ClientBulkDelete do end it "should confirm twice" do - knife.ui.should_receive(:confirm).twice + knife.ui.should_receive(:confirm).once + knife.ui.should_receive(:confirm_without_exit).once knife.run end diff --git a/spec/unit/knife/core/ui_spec.rb b/spec/unit/knife/core/ui_spec.rb index 74b3906067..9044bc2f2f 100644 --- a/spec/unit/knife/core/ui_spec.rb +++ b/spec/unit/knife/core/ui_spec.rb @@ -406,95 +406,132 @@ EOM end describe "confirm" do - before(:each) do - @question = "monkeys rule" - @stdout = StringIO.new - @ui.stub(:stdout).and_return(@stdout) - @ui.stdin.stub(:readline).and_return("y") + let(:stdout) {StringIO.new} + let(:output) {stdout.string} + + let(:question) { "monkeys rule" } + let(:answer) { 'y' } + + let(:default_choice) { nil } + let(:append_instructions) { true } + + def run_confirm + @ui.stub(:stdout).and_return(stdout) + @ui.stdin.stub(:readline).and_return(answer) + @ui.confirm(question, append_instructions, default_choice) end - it "should return true if you answer Y" do - @ui.stdin.stub(:readline).and_return("Y") - @ui.confirm(@question).should == true + def run_confirm_without_exit + @ui.stub(:stdout).and_return(stdout) + @ui.stdin.stub(:readline).and_return(answer) + @ui.confirm_without_exit(question, append_instructions, default_choice) end - it "should return true if you answer y" do - @ui.stdin.stub(:readline).and_return("y") - @ui.confirm(@question).should == true + shared_examples_for "confirm with positive answer" do + it "confirm should return true" do + run_confirm.should be_true + end + + it "confirm_without_exit should return true" do + run_confirm_without_exit.should be_true + end end - it "should exit 3 if you answer N" do - @ui.stdin.stub(:readline).and_return("N") - lambda { - @ui.confirm(@question) - }.should raise_error(SystemExit) { |e| e.status.should == 3 } + shared_examples_for "confirm with negative answer" do + it "confirm should exit 3" do + lambda { + run_confirm + }.should raise_error(SystemExit) { |e| e.status.should == 3 } + end + + it "confirm_without_exit should return false" do + run_confirm_without_exit.should be_false + end end - it "should exit 3 if you answer n" do - @ui.stdin.stub(:readline).and_return("n") - lambda { - @ui.confirm(@question) - }.should raise_error(SystemExit) { |e| e.status.should == 3 } + describe "with default choice set to true" do + let(:default_choice) { true } + + it "should show 'Y/n' in the instructions" do + run_confirm + output.should include("Y/n") + end + + describe "with empty answer" do + let(:answer) { "" } + + it_behaves_like "confirm with positive answer" + end + + describe "with answer N " do + let(:answer) { "N" } + + it_behaves_like "confirm with negative answer" + end end - describe "with 'Y' as a default choice" do - it 'should return true if you answer default' do - @ui.stdin.stub(:readline).and_return("\n") - @ui.confirm(@question, false, 'Y').should == true + describe "with default choice set to false" do + let(:default_choice) { false } + + it "should show 'y/N' in the instructions" do + run_confirm + output.should include("y/N") + end + + describe "with empty answer" do + let(:answer) { "" } + + it_behaves_like "confirm with negative answer" end - it "should show 'Y' as the default in the instructions" do - out = StringIO.new - @ui.stdin.stub(:readline).and_return("\n") - @ui.stub(:stdout).and_return(out) - @ui.confirm(@question, true, 'Y').should == true - out.string.should == "#{@question}? (Y/n)" + describe "with answer N " do + let(:answer) { "Y" } + + it_behaves_like "confirm with positive answer" end end - describe "with 'N' as a default choice" do - it 'should exit 3 if you answer default' do - @ui.stdin.stub(:readline).and_return("\n") - lambda { - @ui.confirm(@question, false, 'N') - }.should raise_error(SystemExit) { |e| e.status.should == 3 } + ["Y", "y"].each do |answer| + describe "with answer #{answer}" do + let(:answer) { answer } + + it_behaves_like "confirm with positive answer" end + end - it "should show 'N' as the default in the instructions" do - out = StringIO.new - @ui.stdin.stub(:readline).and_return("\n") - @ui.stub(:stdout).and_return(out) - lambda { - @ui.confirm(@question, true, 'N') - }.should raise_error(SystemExit) { |e| e.status.should == 3 } - out.string.should == "#{@question}? (y/N)You said no, so I'm done here.\n" + ["N", "n"].each do |answer| + describe "with answer #{answer}" do + let(:answer) { answer } + + it_behaves_like "confirm with negative answer" end end describe "with --y or --yes passed" do it "should return true" do @ui.config[:yes] = true - @ui.confirm(@question).should == true + run_confirm.should be_true + output.should eq("") end end + end - describe "when asking for free-form user input" do - it "asks a question and returns the answer provided by the user" do - out = StringIO.new - @ui.stub(:stdout).and_return(out) - @ui.stub(:stdin).and_return(StringIO.new("http://mychefserver.example.com\n")) - @ui.ask_question("your chef server URL?").should == "http://mychefserver.example.com" - out.string.should == "your chef server URL?" - end - - it "suggests a default setting and returns the default when the user's response only contains whitespace" do - out = StringIO.new - @ui.stub(:stdout).and_return(out) - @ui.stub(:stdin).and_return(StringIO.new(" \n")) - @ui.ask_question("your chef server URL? ", :default => 'http://localhost:4000').should == "http://localhost:4000" - out.string.should == "your chef server URL? [http://localhost:4000] " - end + describe "when asking for free-form user input" do + it "asks a question and returns the answer provided by the user" do + out = StringIO.new + @ui.stub(:stdout).and_return(out) + @ui.stub(:stdin).and_return(StringIO.new("http://mychefserver.example.com\n")) + @ui.ask_question("your chef server URL?").should == "http://mychefserver.example.com" + out.string.should == "your chef server URL?" end + it "suggests a default setting and returns the default when the user's response only contains whitespace" do + out = StringIO.new + @ui.stub(:stdout).and_return(out) + @ui.stub(:stdin).and_return(StringIO.new(" \n")) + @ui.ask_question("your chef server URL? ", :default => 'http://localhost:4000').should == "http://localhost:4000" + out.string.should == "your chef server URL? [http://localhost:4000] " + end end + end |