diff options
Diffstat (limited to 'spec/unit/knife/core/ui_spec.rb')
-rw-r--r-- | spec/unit/knife/core/ui_spec.rb | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/spec/unit/knife/core/ui_spec.rb b/spec/unit/knife/core/ui_spec.rb index ed1037ebd5..83fff4c082 100644 --- a/spec/unit/knife/core/ui_spec.rb +++ b/spec/unit/knife/core/ui_spec.rb @@ -67,16 +67,16 @@ describe Chef::Knife::UI do @ui.config[:disable_editing] = false @ui.config[:editor] = my_editor @mock = double('Tempfile') - @mock.should_receive(:sync=).with(true) - @mock.should_receive(:puts).with(json_from_ruby) - @mock.should_receive(:close) - @mock.should_receive(:path).at_least(:once).and_return(temp_path) - Tempfile.should_receive(:open).with([ 'knife-edit-', '.json' ]).and_yield(@mock) + expect(@mock).to receive(:sync=).with(true) + expect(@mock).to receive(:puts).with(json_from_ruby) + expect(@mock).to receive(:close) + expect(@mock).to receive(:path).at_least(:once).and_return(temp_path) + expect(Tempfile).to receive(:open).with([ 'knife-edit-', '.json' ]).and_yield(@mock) end context "and the editor works" do before do - @ui.should_receive(:system).with("#{my_editor} #{temp_path}").and_return(true) - IO.should_receive(:read).with(temp_path).and_return(json_from_editor) + expect(@ui).to receive(:system).with("#{my_editor} #{temp_path}").and_return(true) + expect(IO).to receive(:read).with(temp_path).and_return(json_from_editor) end context "when parse_output is false" do @@ -93,8 +93,8 @@ describe Chef::Knife::UI do end context "when running the editor fails with nil" do before do - @ui.should_receive(:system).with("#{my_editor} #{temp_path}").and_return(nil) - IO.should_not_receive(:read) + expect(@ui).to receive(:system).with("#{my_editor} #{temp_path}").and_return(nil) + expect(IO).not_to receive(:read) end it "throws an exception" do expect{ subject }.to raise_error(RuntimeError) @@ -102,8 +102,8 @@ describe Chef::Knife::UI do end context "when running the editor fails with false" do before do - @ui.should_receive(:system).with("#{my_editor} #{temp_path}").and_return(false) - IO.should_not_receive(:read) + expect(@ui).to receive(:system).with("#{my_editor} #{temp_path}").and_return(false) + expect(IO).not_to receive(:read) end it "throws an exception" do expect{ subject }.to raise_error(RuntimeError) @@ -115,13 +115,13 @@ describe Chef::Knife::UI do @ui.config[:disable_editing] = false @ui.config[:editor] = my_editor @tempfile = Tempfile.new([ 'knife-edit-', '.json' ]) - Tempfile.should_receive(:open).with([ 'knife-edit-', '.json' ]).and_yield(@tempfile) + expect(Tempfile).to receive(:open).with([ 'knife-edit-', '.json' ]).and_yield(@tempfile) end context "and the editor works" do before do - @ui.should_receive(:system).with("#{my_editor} #{@tempfile.path}").and_return(true) - IO.should_receive(:read).with(@tempfile.path).and_return(json_from_editor) + expect(@ui).to receive(:system).with("#{my_editor} #{@tempfile.path}").and_return(true) + expect(IO).to receive(:read).with(@tempfile.path).and_return(json_from_editor) end context "when parse_output is false" do @@ -153,33 +153,33 @@ describe Chef::Knife::UI do describe "format_list_for_display" do it "should print the full hash if --with-uri is true" do @ui.config[:with_uri] = true - @ui.format_list_for_display({ :marcy => :playground }).should == { :marcy => :playground } + expect(@ui.format_list_for_display({ :marcy => :playground })).to eq({ :marcy => :playground }) end it "should print only the keys if --with-uri is false" do @ui.config[:with_uri] = false - @ui.format_list_for_display({ :marcy => :playground }).should == [ :marcy ] + expect(@ui.format_list_for_display({ :marcy => :playground })).to eq([ :marcy ]) end end shared_examples "an output mehthod handling IO exceptions" do |method| it "should throw Errno::EIO exceptions" do - @out.stub(:puts).and_raise(Errno::EIO) - @err.stub(:puts).and_raise(Errno::EIO) - lambda {@ui.send(method, "hi")}.should raise_error(Errno::EIO) + allow(@out).to receive(:puts).and_raise(Errno::EIO) + allow(@err).to receive(:puts).and_raise(Errno::EIO) + expect {@ui.send(method, "hi")}.to raise_error(Errno::EIO) end it "should ignore Errno::EPIPE exceptions (CHEF-3516)" do - @out.stub(:puts).and_raise(Errno::EPIPE) - @err.stub(:puts).and_raise(Errno::EPIPE) - lambda {@ui.send(method, "hi")}.should raise_error(SystemExit) + allow(@out).to receive(:puts).and_raise(Errno::EPIPE) + allow(@err).to receive(:puts).and_raise(Errno::EPIPE) + expect {@ui.send(method, "hi")}.to raise_error(SystemExit) end it "should throw Errno::EPIPE exceptions with -VV (CHEF-3516)" do @config[:verbosity] = 2 - @out.stub(:puts).and_raise(Errno::EPIPE) - @err.stub(:puts).and_raise(Errno::EPIPE) - lambda {@ui.send(method, "hi")}.should raise_error(Errno::EPIPE) + allow(@out).to receive(:puts).and_raise(Errno::EPIPE) + allow(@err).to receive(:puts).and_raise(Errno::EPIPE) + expect {@ui.send(method, "hi")}.to raise_error(Errno::EPIPE) end end @@ -188,12 +188,12 @@ describe Chef::Knife::UI do it "formats strings appropriately" do @ui.output("hi") - @out.string.should == "hi\n" + expect(@out.string).to eq("hi\n") end it "formats hashes appropriately" do @ui.output({'hi' => 'a', 'lo' => 'b' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM hi: a lo: b EOM @@ -201,12 +201,12 @@ EOM it "formats empty hashes appropriately" do @ui.output({}) - @out.string.should == "\n" + expect(@out.string).to eq("\n") end it "formats arrays appropriately" do @ui.output([ 'a', 'b' ]) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a b EOM @@ -214,22 +214,22 @@ EOM it "formats empty arrays appropriately" do @ui.output([ ]) - @out.string.should == "\n" + expect(@out.string).to eq("\n") end it "formats single-member arrays appropriately" do @ui.output([ 'a' ]) - @out.string.should == "a\n" + expect(@out.string).to eq("a\n") end it "formats nested single-member arrays appropriately" do @ui.output([ [ 'a' ] ]) - @out.string.should == "a\n" + expect(@out.string).to eq("a\n") end it "formats nested arrays appropriately" do @ui.output([ [ 'a', 'b' ], [ 'c', 'd' ]]) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a b @@ -240,7 +240,7 @@ EOM it "formats nested arrays with single- and empty subarrays appropriately" do @ui.output([ [ 'a', 'b' ], [ 'c' ], [], [ 'd', 'e' ]]) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a b @@ -254,7 +254,7 @@ EOM it "formats arrays of hashes with extra lines in between for readability" do @ui.output([ { 'a' => 'b', 'c' => 'd' }, { 'x' => 'y' }, { 'm' => 'n', 'o' => 'p' }]) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: b c: d @@ -267,7 +267,7 @@ EOM it "formats hashes with empty array members appropriately" do @ui.output({ 'a' => [], 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: b: c EOM @@ -275,7 +275,7 @@ EOM it "formats hashes with single-member array values appropriately" do @ui.output({ 'a' => [ 'foo' ], 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: foo b: c EOM @@ -283,7 +283,7 @@ EOM it "formats hashes with array members appropriately" do @ui.output({ 'a' => [ 'foo', 'bar' ], 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: foo bar @@ -293,7 +293,7 @@ EOM it "formats hashes with single-member nested array values appropriately" do @ui.output({ 'a' => [ [ 'foo' ] ], 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: foo b: c @@ -304,12 +304,12 @@ EOM @ui.output({ 'a' => [ [ 'foo', 'bar' ], [ 'baz', 'bjork' ] ], 'b' => 'c' }) # XXX: using a HEREDOC at this point results in a line with required spaces which auto-whitespace removal settings # on editors will remove and will break this test. - @out.string.should == "a:\n foo\n bar\n \n baz\n bjork\nb: c\n" + expect(@out.string).to eq("a:\n foo\n bar\n \n baz\n bjork\nb: c\n") end it "formats hashes with hash values appropriately" do @ui.output({ 'a' => { 'aa' => 'bb', 'cc' => 'dd' }, 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: aa: bb cc: dd @@ -319,7 +319,7 @@ EOM it "formats hashes with empty hash values appropriately" do @ui.output({ 'a' => { }, 'b' => 'c' }) - @out.string.should == <<EOM + expect(@out.string).to eq <<EOM a: b: c EOM @@ -341,20 +341,20 @@ EOM describe "format_for_display" do it "should return the raw data" do input = { :gi => :go } - @ui.format_for_display(input).should == input + expect(@ui.format_for_display(input)).to eq(input) end describe "with --attribute passed" do it "should return the deeply nested attribute" do input = { "gi" => { "go" => "ge" }, "id" => "sample-data-bag-item" } @ui.config[:attribute] = "gi.go" - @ui.format_for_display(input).should == { "sample-data-bag-item" => { "gi.go" => "ge" } } + expect(@ui.format_for_display(input)).to eq({ "sample-data-bag-item" => { "gi.go" => "ge" } }) end it "should return multiple attributes" do input = { "gi" => "go", "hi" => "ho", "id" => "sample-data-bag-item" } @ui.config[:attribute] = ["gi", "hi"] - @ui.format_for_display(input).should == { "sample-data-bag-item" => { "gi" => "go", "hi"=> "ho" } } + expect(@ui.format_for_display(input)).to eq({ "sample-data-bag-item" => { "gi" => "go", "hi"=> "ho" } }) end end @@ -365,8 +365,8 @@ EOM input.run_list("role[monkey]", "role[churchmouse]") @ui.config[:run_list] = true response = @ui.format_for_display(input) - response["sample-node"]["run_list"][0].should == "role[monkey]" - response["sample-node"]["run_list"][1].should == "role[churchmouse]" + expect(response["sample-node"]["run_list"][0]).to eq("role[monkey]") + expect(response["sample-node"]["run_list"][1]).to eq("role[churchmouse]") end end end @@ -388,7 +388,7 @@ EOM it "should return an array of the cookbooks with versions" do expected_response = [ "cookbook_name 3.0.0 2.0.0 1.0.0" ] response = @ui.format_cookbook_list_for_display(@item) - response.should == expected_response + expect(response).to eq(expected_response) end describe "with --with-uri" do @@ -400,15 +400,15 @@ EOM "3.0.0" => "http://url/cookbooks/3.0.0"} } @ui.config[:with_uri] = true - @ui.format_cookbook_list_for_display(@item).should == response + expect(@ui.format_cookbook_list_for_display(@item)).to eq(response) end end context "when running on Windows" do before(:each) do stdout = double('StringIO', :tty? => true) - @ui.stub(:stdout).and_return(stdout) - Chef::Platform.stub(:windows?) { true } + allow(@ui).to receive(:stdout).and_return(stdout) + allow(Chef::Platform).to receive(:windows?) { true } Chef::Config.reset end @@ -444,36 +444,36 @@ EOM let(:append_instructions) { true } def run_confirm - @ui.stub(:stdout).and_return(stdout) - @ui.stdin.stub(:readline).and_return(answer) + allow(@ui).to receive(:stdout).and_return(stdout) + allow(@ui.stdin).to receive(:readline).and_return(answer) @ui.confirm(question, append_instructions, default_choice) end def run_confirm_without_exit - @ui.stub(:stdout).and_return(stdout) - @ui.stdin.stub(:readline).and_return(answer) + allow(@ui).to receive(:stdout).and_return(stdout) + allow(@ui.stdin).to receive(:readline).and_return(answer) @ui.confirm_without_exit(question, append_instructions, default_choice) end shared_examples_for "confirm with positive answer" do it "confirm should return true" do - run_confirm.should be_true + expect(run_confirm).to be_truthy end it "confirm_without_exit should return true" do - run_confirm_without_exit.should be_true + expect(run_confirm_without_exit).to be_truthy end end shared_examples_for "confirm with negative answer" do it "confirm should exit 3" do - lambda { + expect { run_confirm - }.should raise_error(SystemExit) { |e| e.status.should == 3 } + }.to raise_error(SystemExit) { |e| expect(e.status).to eq(3) } end it "confirm_without_exit should return false" do - run_confirm_without_exit.should be_false + expect(run_confirm_without_exit).to be_falsey end end @@ -482,7 +482,7 @@ EOM it "should show 'Y/n' in the instructions" do run_confirm - output.should include("Y/n") + expect(output).to include("Y/n") end describe "with empty answer" do @@ -503,7 +503,7 @@ EOM it "should show 'y/N' in the instructions" do run_confirm - output.should include("y/N") + expect(output).to include("y/N") end describe "with empty answer" do @@ -538,8 +538,8 @@ EOM describe "with --y or --yes passed" do it "should return true" do @ui.config[:yes] = true - run_confirm.should be_true - output.should eq("") + expect(run_confirm).to be_truthy + expect(output).to eq("") end end end @@ -547,18 +547,18 @@ EOM 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?" + allow(@ui).to receive(:stdout).and_return(out) + allow(@ui).to receive(:stdin).and_return(StringIO.new("http://mychefserver.example.com\n")) + expect(@ui.ask_question("your chef server URL?")).to eq("http://mychefserver.example.com") + expect(out.string).to eq("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] " + allow(@ui).to receive(:stdout).and_return(out) + allow(@ui).to receive(:stdin).and_return(StringIO.new(" \n")) + expect(@ui.ask_question("your chef server URL? ", :default => 'http://localhost:4000')).to eq("http://localhost:4000") + expect(out.string).to eq("your chef server URL? [http://localhost:4000] ") end end |