Skip to content

Commit

Permalink
Handle exceptions and add more negative tests
Browse files Browse the repository at this point in the history
  • Loading branch information
betesh committed May 4, 2020
1 parent 7cfe77d commit 7c0a62b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
19 changes: 16 additions & 3 deletions lib/rjgit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,17 @@ def self.version

module Porcelain

import 'java.io.IOException'
import 'org.eclipse.jgit.lib.Constants'
import 'org.eclipse.jgit.api.AddCommand'
import 'org.eclipse.jgit.api.CommitCommand'
import 'org.eclipse.jgit.api.BlameCommand'
import 'org.eclipse.jgit.api.errors.RefNotFoundException'
import 'org.eclipse.jgit.blame.BlameGenerator'
import 'org.eclipse.jgit.blame.BlameResult'
import 'org.eclipse.jgit.errors.IncorrectObjectTypeException'
import 'org.eclipse.jgit.errors.InvalidPatternException'
import 'org.eclipse.jgit.errors.MissingObjectException'
import 'org.eclipse.jgit.treewalk.CanonicalTreeParser'
import 'org.eclipse.jgit.diff.DiffFormatter'

Expand Down Expand Up @@ -168,12 +173,20 @@ def self.describe(repository, ref, options = {})
repo = RJGit.repository_type(repository)
git = RubyGit.new(repo).jgit
command = git.describe.
set_target(ref).
set_always(options[:always]).
set_long(options[:long]).
set_tags(options[:tags])
command = options[:match].each_with_object(command) do |match|
command.set_match(match)
begin
command = command.set_target(ref)
rescue IncorrectObjectTypeException, IOException, MissingObjectException, RefNotFoundException
return nil
end
options[:match].each do |match|
begin
command = command.set_match(match)
rescue InvalidPatternException
return nil
end
end
command.call
end
Expand Down
7 changes: 7 additions & 0 deletions spec/rjgit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,13 @@
expect(Porcelain.describe(@repo, id, match: ['v0.0'])).to match(/\Av0\.0-4-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.1'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.0', 'v0.1'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v0.*'])).to match(/\Av0\.1-1-g[0-9a-f]{7}\z/)
expect(Porcelain.describe(@repo, id, match: ['v99'])).to be_nil
expect(Porcelain.describe(@repo, id, match: ['*[a'])).to be_nil # Swallows NoClosingBracketException, a type of InvalidPatternException
end

it "returns nil when the ref doesn't exist" do
expect(Porcelain.describe(@repo, 'abcdef123456')).to be_nil # Swallows RefNotFoundException
end
end

Expand Down

0 comments on commit 7c0a62b

Please sign in to comment.