Browse files

test gitrepo's tagging methods

  • Loading branch information...
1 parent 03244ec commit 2b0d5424aaf9d6eb82bb55fbce5969c954b8c9e1 @bronson bronson committed Oct 31, 2011
Showing with 61 additions and 27 deletions.
  1. +17 −25 lib/gitrepo.rb
  2. +2 −1 scraper
  3. +42 −1 spec/gitrepo_spec.rb
View
42 lib/gitrepo.rb
@@ -61,21 +61,15 @@ def git *args
end
end
- # i.e. remote_add 'rails', 'http://github.com/rails/rails.git'
def remote_add name, remote
- Dir.chdir(@root) {
- output = `git remote add #{name} #{remote} 2>&1`
- raise GitError.new("git remote add #{name} failed: #{output}") unless $?.success?
- }
+ git :remote, :add, name, remote
end
def remote_remove name
- Dir.chdir(@root) {
- output = `git remote rm #{name} 2>&1`
- raise GitError.new("git remote add #{name} failed: #{output}") unless $?.success?
- }
+ git :remote, :rm, name
end
+
# todo: get rid of this call, should be regular git add / git commit
def commit_all message
Dir.chdir(@root) {
@@ -105,28 +99,26 @@ def push *args
end
- def create_tag name, message, committer, branch = 'master'
- # todo: this blows away the environment, is there a better way of doing this?
+ # todo: get rid of branch since we should only ever produce commits on master.
+ def create_tag name, message, committer, branch='master'
+ # gitrb doesn't handle annotated tags so we call git directly
+ # todo: this blows away the environment, should set env after forking & before execing
ENV['GIT_COMMITTER_NAME'] = committer[:name]
ENV['GIT_COMMITTER_EMAIL'] = committer[:email]
- puts " tagging with #{gittagify(name)} from #{name}"
- Dir.chdir(@root) {
- output = `git tag -a #{gittagify(name)} -m 'tag #{name}' #{branch} 2>&1`
- raise GitError.new("create git tag failed: #{output}") unless $?.success?
- }
+ ENV['GIT_COMMITTER_DATE'] = (committer[:date] || Time.now).strftime("%s %z")
+
+ result = git :tag, '-a', name, '-m', message, branch
+
ENV.delete 'GIT_COMMITTER_NAME'
ENV.delete 'GIT_COMMITTER_EMAIL'
- end
+ ENV.delete 'GIT_COMMITTER_DATE'
+ result
+ end
- def read_tag tagname
- # gitrb doesn't handle annotated tags so we call git directly
- Dir.chdir(@root) {
- output = `git tag -l #{tagname} 2>&1`
- raise GitError.new("read git tag failed: #{output}") unless $?.success?
- output = nil if tag =~ /^\s*$/
- return output
- }
+ def find_tag tagname
+ tag = git :tag, '-l', tagname
+ return !tag || tag =~ /^\s*$/ ? nil : tag.chomp
end
View
3 scraper
@@ -1257,7 +1257,7 @@ def store_versions_in_repo repo, script
# todo: rather than bailing, we should bit-for-bit verify
tagname = gittagify(script_version(version))
- next if repo.read_tag(tagname)
+ next if repo.find_tag(tagname)
next if corrupted_package(script, version)
@@ -1275,6 +1275,7 @@ def store_versions_in_repo repo, script
copy_file(commit, 'README', "This is a mirror of #{script_id_to_url(script['script_id'])}\n\n" + script['description'] + "\n") unless repo.root['README']
end
sver = script_version(version)
+ puts " tagging with #{gittagify(sver)} from #{sver}"
repo.create_tag gittagify(sver), "tag #{sver}", committer, branch
count += 1
end
View
43 spec/gitrepo_spec.rb
@@ -1,5 +1,7 @@
# Ensures git operations can occur on repos.
# Avoids stubbing as much as possible so the testfile doesn't prescribe the implementation.
+#
+# to run specs with 'date' in the name and preserve the results: PRESERVE=1 bundle exec rspec spec/* -e date
require File.dirname(File.absolute_path(__FILE__)) + '/../lib/gitrepo'
@@ -12,7 +14,9 @@ def with_git_repo args={}, &block
stub = lambda { |tmpdir|
options = { :root => "#{tmpdir}/repo", :create => true }
options.merge! args
- block.call GitRepo.new options
+ repo = GitRepo.new options
+ repo.retryable_options :logger => lambda { |task,retries,error| } # turn off logging when testing
+ block.call repo
}
if ENV['PRESERVE']
@@ -56,4 +60,41 @@ def with_git_commit *args
repo.push :origin, :master
end
end
+
+
+ it "should create a tag at the right date" do
+ with_git_commit(:bare => true) do |repo|
+ date = Time.new 2010,10,10, 16,40,0, '-07:00'
+ committer = { :name => "test committer", :email => "testemail@example.com", :date => date }
+ repo.create_tag '1.2', 'tag 1.2', committer
+ # wish there was a better way of checking this but git show doesn't support --format for tags.
+ repo.git(:show, '1.2').should match /^tag 1.2\nTagger: test committer <testemail@example.com>\nDate:\s*Sun Oct 10 16:40:00 2010 -0700\n\ntag 1.2\n/
+ end
+ end
+
+ it "should not create an invalid tag" do
+ with_git_commit(:bare => true) do |repo|
+ committer = { :name => "test committer", :email => "testemail@example.com" }
+ lambda {
+ repo.create_tag '5 4.3', 'tag 5.4.3', committer
+ }.should raise_error(GitRepo::GitError, /is not a valid tag name/)
+ end
+ end
+
+ it "should find tags" do
+ with_git_commit(:bare => true) do |repo|
+ committer = { :name => "test committer", :email => "testemail@example.com" }
+ repo.create_tag '5.4.3', 'tag message for 5.4.3', committer
+ repo.find_tag('5.4.3').should == '5.4.3' # find an existing tag
+ repo.find_tag('5.4.4').should == nil # find a nonexistent tag
+ repo.find_tag('5 4.4').should == nil # find an invalid tag
+ end
+ end
+
+
+ # testing commits:
+ # committer same as author
+ # omit committer
+ # empty commit
+ # -700 in commits
end

0 comments on commit 2b0d542

Please sign in to comment.