# The MIT License # Copyright © 2009 Magnus Bergmark # # Permission is hereby granted, free of charge, to any person obtaining a copy # of this software and associated documentation files (the "Software"), to deal # in the Software without restriction, including without limitation the rights # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell # copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in # all copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN # THE SOFTWARE. require File.dirname(__FILE__) + '/../spec_helper' describe GitRemoteMonitor::GitWrapper do before(:all) do # Shortcut :-D @wrapper = GitRemoteMonitor::GitWrapper end describe "system calls" do describe "to fetch" do it "should raise error if git gets an error" do lambda { $?.stub!(:success? => false) @wrapper.stub!(:`) @wrapper.send(:call_fetch) }.should raise_error end it "should return output" do $?.stub!(:success? => true) @wrapper.should_receive(:`).with(/git fetch/).and_return("Downloading stuff...") @wrapper.send(:call_fetch).should == "Downloading stuff..." end end it "should call git branch correctly" do @wrapper.should_receive(:`).with(/git branch/).and_return("Output") @wrapper.send(:call_get_local_branch).should == "Output" end it "should call remote listing correctly" do @wrapper.should_receive(:`).with(/git branch -r/).and_return("output") @wrapper.send(:call_remotes_list).should == "output" end it "should call command to discover git repo correctly and return exit status" do # We test a "impossible" value here as a shortcut. If the method returns this, we know the status is returned unchanged status = mock("Exit status") $?.stub!(:success? => status) @wrapper.should_receive(:`).with(/git show-ref/).and_return("") @wrapper.send(:call_repo_discovery).should == status end it "should call git config correctly to get remote branch" do @wrapper.should_receive(:`).with(/git config branch\.foobar\.remote/).and_return("Output") @wrapper.send(:call_get_remote, "foobar").should == "Output" end it "should call git rev-list correctly to get tracking information" do # This is a complex one... :-( # Hard to test this without a copy-paste @wrapper.should_receive(:`). with("git rev-list --left-right foo...bar | cut -c 1 | sort | uniq -c"). and_return("Output") @wrapper.send(:call_get_tracking_info, "foo", "bar").should == "Output" end end describe "discovering remote branches" do it "should return number of remotes when some are defined" do @wrapper.should_receive(:call_remotes_list).and_return(" origin/master\n friend/cool-branch\n") @wrapper.has_remotes?.should == 2 end it "should return nil when no remotes are defined" do @wrapper.should_receive(:call_remotes_list).and_return("\n") @wrapper.has_remotes?.should be_nil end end describe "discovering git repo" do it "should return true if inside repository" do @wrapper.stub!(:call_repo_discovery => true) @wrapper.in_repo?.should be_true end it "should return false if outside repository" do @wrapper.stub!(:call_repo_discovery => false) @wrapper.in_repo?.should be_false end end describe "fetch" do before(:each) do @wrapper.stub!(:has_remotes? => 1, :call_fetch => "") end it "should raise error if no remote repositories are defined" do @wrapper.should_receive(:has_remotes?).and_return(nil) lambda { @wrapper.fetch! }.should raise_error(/no remote/) end it "should know if a fetch didn't occur" do @wrapper.should_receive(:call_fetch).and_return "\n" @wrapper.fetch!.should == false end it "should know if a fetch occured" do @wrapper.should_receive(:call_fetch).and_return( "remote: Generating pack...\n" + "remote: Done counting 119 objects.\n" + "remote: Result has 90 objects.\n" + "remote: Deltifying 90 objects.\n" + "remote: 100% (90/90) done\n" + "remote: Total 90, written 90 (delta 55), reused 0 (delta 0)\n" + "Unpacking objects: 100% (90/90), done.\n" + "From git@foobar:baz\n" + " e871819..110f4bc master -> origin/master\n" ) @wrapper.fetch!.should == true end end describe "branch discovery" do before(:each) do @wrapper.stub!(:call_get_remote).and_return "" @wrapper.stub!(:call_get_local_branch).and_return " * master" @wrapper.stub!(:call_get_remote).with("master").and_return "upstream\n" end it "should find the current local branch" do @wrapper.local_branch.should == "master" end it "should find the current local branch, even if there are many branches" do @wrapper.stub!(:call_get_local_branch).and_return(" master\n* test\n other") @wrapper.local_branch.should == "test" end it "should find the remote branch" do @wrapper.remote_branch.should == "upstream/master" end it "should return nil when no remote branch exists" do @wrapper.stub!(:call_get_local_branch).and_return "local" @wrapper.remote_branch.should be_nil end end describe "getting tracking information" do def tracking_info_should_be(local_changes, remote_changes, options = {}) @wrapper.should_receive(:call_get_tracking_info).and_return(options[:for]) @wrapper.get_tracking_info("master", "origin/master").should == [local_changes, remote_changes] end before(:each) do # Stub away the system command to be safe @wrapper.stub!(:call_get_tracking_info => "") end it "should raise error on unexpected output from command" do lambda { @wrapper.stub!(:call_get_tracking_info => nil) @wrapper.get_tracking_info("master", "origin/master") }.should raise_error lambda { @wrapper.stub!(:call_get_tracking_info => " 1\n 2\n 3\n") @wrapper.get_tracking_info("master", "origin/master") }.should raise_error end it "should be able to get the correct number of commits when nothing has changed" do tracking_info_should_be 0, 0, :for => " 1 " end it "should be able to get the correct number of commits when diverged" do tracking_info_should_be 1, 7, :for => " 1 <\n 7 >" end it "should be able to get the correct number of commits when remote is ahead" do tracking_info_should_be 0, 7, :for => " 7 >" end it "should be able to get the correct number of commits when remote is behind" do tracking_info_should_be 132, 0, :for => " 132 <" end end end