Skip to content

Instantly share code, notes, and snippets.

@bhameyie
Created May 28, 2013 17:08
Show Gist options
  • Save bhameyie/5664333 to your computer and use it in GitHub Desktop.
Save bhameyie/5664333 to your computer and use it in GitHub Desktop.
Mochy git with ruby mocha
require "test/unit"
require "mocha"
require_relative '../lib'
require "git"
class GitWrapTest < Test::Unit::TestCase
def test_fails_if_untrackedFiles_present_before_pull
git = mock()
fakeStatus= mock()
fakeStatusCount = mock()
git.expects(:status).returns(fakeStatus)
fakeStatus.expects(:added).returns(fakeStatusCount)
fakeStatusCount.expects(:count).returns(1000)
wrap = GitWrap.new(git)
assert_raise RuntimeError do
wrap.pull(".")
end
end
def test_commit_raisesexception_if_emptyMessage
git = mock()
wrap = GitWrap.new(git)
wrap.expects(:checkReadyForCheckin).returns(true)
assert_raise RuntimeError do
wrap.commit("")
end
end
end
class GitWrap
GITPATH= "."
def initialize(git=nil, path=nil)
@git = git || Git.open(path || GITPATH)
end
def pull(remote="origin", branch="master")
if !checkReadyForCheckin
raise RuntimeError, "Please check your git status for adds, deletes, changes or conflicts"
end
@git.pull remote, branch
end
def commit(message)
raise RuntimeError, "Please check your git status for adds, deletes, changes or conflicts" if !checkReadyForCheckin
raise RuntimeError, "Commit message is empty" if message.empty?
@git.commit message
end
def checkReadyForCheckin()
status = @git.status
return !(status.added.count > 0 || status.deleted.count > 0 || status.changed.count > 0 || status.conflicts.count > 0)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment