Skip to content

Commit

Permalink
added rspec tests that at least give it a baseline of coverage
Browse files Browse the repository at this point in the history
also had to tweak the patch submitted for finding the .git directory
from looking for GIT_DIR to looking for GIT_WORKING_DIR
  • Loading branch information
schacon committed Mar 30, 2008
1 parent 41940a3 commit 67e3618
Show file tree
Hide file tree
Showing 6 changed files with 291 additions and 17 deletions.
21 changes: 9 additions & 12 deletions lib/ticgit/base.rb
Expand Up @@ -41,8 +41,8 @@ def initialize(git_dir, opts = {})

def find_repo(dir)
full = File.expand_path(dir)
ENV["GIT_DIR"] || loop do
return full if File.directory?(File.join(full,".git"))
ENV["GIT_WORKING_DIR"] || loop do
return full if File.directory?(File.join(full, ".git"))
raise NoRepoFound if full == full=File.dirname(full)
end
end
Expand Down Expand Up @@ -178,7 +178,6 @@ def ticket_revparse(ticket_id)
else
# partial or full sha
if ch = @tickets.select { |name, t| t['files'].assoc('TICKET_ID')[1] =~ /^#{ticket_id}/ }
p ch
return ch.first[0]
end
end
Expand All @@ -201,9 +200,11 @@ def ticket_tag(tag, ticket_id = nil, options = {})

def ticket_change(new_state, ticket_id = nil)
if t = ticket_revparse(ticket_id)
ticket = TicGit::Ticket.open(self, t, @tickets[t])
ticket.change_state(new_state)
reset_ticgit
if tic_states.include?(new_state)
ticket = TicGit::Ticket.open(self, t, @tickets[t])
ticket.change_state(new_state)
reset_ticgit
end
end
end

Expand All @@ -220,11 +221,7 @@ def comment_add(ticket_id, comment, options = {})

def comment_list(ticket_id)
end


def checkout(ticket)
end


def tic_states
['open', 'resolved', 'invalid', 'hold']
end
Expand All @@ -249,7 +246,7 @@ def load_tickets
end

def init_ticgit_branch(ticgit_branch = false)
puts 'creating ticgit repo branch'
@logger.info 'creating ticgit repo branch'

in_branch(ticgit_branch) do
new_file('.hold', 'hold')
Expand Down
8 changes: 3 additions & 5 deletions lib/ticgit/ticket.rb
Expand Up @@ -73,7 +73,7 @@ def self.parse_ticket_name(name)
# write this ticket to the git database
def save_new
base.in_branch do |wd|
puts "saving #{ticket_name}"
base.logger.info "saving #{ticket_name}"

Dir.mkdir(ticket_name)
Dir.chdir(ticket_name) do
Expand All @@ -87,6 +87,7 @@ def save_new

# add initial tags
if opts[:tags] && opts[:tags].size > 0
opts[:tags] = opts[:tags].map { |t| t.strip }.compact
opts[:tags].each do |tag|
if tag.size > 0
tag_filename = 'TAG_' + Ticket.clean_string(tag)
Expand All @@ -95,10 +96,7 @@ def save_new
end
end
end
end

# !! TODO : add initial milestone

end
end

base.git.add
Expand Down
145 changes: 145 additions & 0 deletions spec/base_spec.rb
@@ -0,0 +1,145 @@
require File.dirname(__FILE__) + "/spec_helper"

describe TicGit::Base do
include TicGitSpecHelper

before(:all) do
@path = setup_new_git_repo
@orig_test_opts = test_opts
@ticgit = TicGit.open(@path, @orig_test_opts)
end

it "should have 4 ticket states" do
@ticgit.tic_states.size.should eql(4)
end

it "should be able to create new tickets" do
@ticgit.tickets.size.should eql(0)
@ticgit.ticket_new('my new ticket').should be_an_instance_of(TicGit::Ticket)
@ticgit.tickets.size.should eql(1)
end

it "should be able to list existing tickets" do
@ticgit.ticket_new('my second ticket').should be_an_instance_of(TicGit::Ticket)
list = @ticgit.ticket_list
list.first.should be_an_instance_of(TicGit::Ticket)
list.size.should eql(2)
end

it "should be able to change the state of a ticket" do
tic = @ticgit.ticket_list.first
@ticgit.ticket_change('resolved', tic.ticket_id)
tic = @ticgit.ticket_show(tic.ticket_id)
tic.state.should eql('resolved')
end

it "should not be able to change the state of a ticket to something invalid" do
tic = @ticgit.ticket_list.first
@ticgit.ticket_change('resolve', tic.ticket_id)
tic = @ticgit.ticket_show(tic.ticket_id)
tic.state.should_not eql('resolve')
end

it "should only show open tickets by default" do
@ticgit.ticket_new('my third ticket')
tics = @ticgit.ticket_list
states = tics.map { |t| t.state }.uniq
states.size.should eql(1)
states.first.should eql('open')
end

it "should be able to filter tickets on state" do
tics = @ticgit.ticket_list(:state => 'resolved')
tics.size.should eql(1)
tics = @ticgit.ticket_list(:state => 'open')
tics.size.should eql(2)
end

it "should be able to save and recall filtered ticket lists" do
tics = @ticgit.ticket_list(:state => 'resolved', :save => 'resolve')
tics.size.should eql(1)
rtics = @ticgit.ticket_list(:saved => 'resolve')
tics.size.should eql(1)
end

it "should be able to comment on tickets" do
t = @ticgit.ticket_new('my fourth ticket')
t.comments.size.should eql(0)

@ticgit.ticket_comment('my new comment', t.ticket_id)
t = @ticgit.ticket_show(t.ticket_id)
t.comments.size.should eql(1)
t.comments.first.comment.should eql('my new comment')
end

it "should retrieve specific tickets" do
tid = @ticgit.ticket_list.last.ticket_id
tic = @ticgit.ticket_show(tid)
tic.ticket_id.should eql(tid)
end

it "should be able to checkout a ticket" do
tid = @ticgit.ticket_list.last.ticket_id
@ticgit.ticket_checkout(tid)
@ticgit.ticket_show.ticket_id.should eql(tid)
end

it "should resolve partial shas into ticket" do
tid = @ticgit.ticket_list.last.ticket_id
@ticgit.ticket_checkout(tid[0, 5])
@ticgit.ticket_show.ticket_id.should eql(tid)

@ticgit.ticket_checkout(tid[0, 20])
@ticgit.ticket_show.ticket_id.should eql(tid)
end

it "should resolve order number from most recent list into ticket" do
tics = @ticgit.ticket_list(:state => 'open')
@ticgit.ticket_show('1').ticket_id.should eql(tics[0].ticket_id)
@ticgit.ticket_show('2').ticket_id.should eql(tics[1].ticket_id)
end

it "should be able to tag a ticket" do
t = @ticgit.ticket_list.last
t.tags.size.should eql(0)
@ticgit.ticket_tag('newtag', t.ticket_id)
t = @ticgit.ticket_show(t.ticket_id)
t.tags.size.should eql(1)
t.tags.first.should eql('newtag')
end

it "should not be able to tag a ticket with a blank tag" do
t = @ticgit.ticket_new('my fourth ticket', :tags => [' '])
t.tags.size.should eql(0)

@ticgit.ticket_tag(' ', t.ticket_id)
t = @ticgit.ticket_show(t.ticket_id)
t.tags.size.should eql(0)

@ticgit.ticket_tag('', t.ticket_id)
t = @ticgit.ticket_show(t.ticket_id)
t.tags.size.should eql(0)

@ticgit.ticket_tag(',mytag', t.ticket_id)
t = @ticgit.ticket_show(t.ticket_id)
t.tags.size.should eql(1)
t.tags.first.should eql('mytag')
end

it "should be able to remove a tag from a ticket" do
t = @ticgit.ticket_new('my next ticket', :tags => ['scotty', 'chacony'])
t.tags.size.should eql(2)

@ticgit.ticket_tag('scotty', t.ticket_id, :remove => true)
t.tags.size.should eql(2)
t.tags.first.should eql('chacony')
end

it "should save state to disk after a new ticket" do
time = File.stat(@ticgit.state).size
t = @ticgit.ticket_new('my next ticket', :tags => ['scotty', 'chacony'])
File.stat(@ticgit.state).size.should_not eql(time)
end

end

16 changes: 16 additions & 0 deletions spec/cli_spec.rb
@@ -0,0 +1,16 @@
require File.dirname(__FILE__) + "/spec_helper"

describe TicGit::CLI do
include TicGitSpecHelper

before(:all) do
@path = setup_new_git_repo
@orig_test_opts = test_opts
@ticgit = TicGit.open(@path, @orig_test_opts)
end

it "should list the tickets"

it "should show a ticket"

end
85 changes: 85 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,85 @@
require File.expand_path(File.dirname(__FILE__) + "/../lib/ticgit")
require 'fileutils'
require 'logger'

module TicGitSpecHelper

def setup_new_git_repo
temp = Tempfile.new('ticgit')
p = temp.path
temp.unlink
Dir.mkdir(p)
Dir.chdir(p) do
g = Git.init
new_file('test', 'content')
Dir.mkdir('subdir')
new_file('subdir/testfile', 'content2')
g.add
g.commit('first commit')
end
p
end

def setup_existing_ticgit_repo
end

def test_opts
temp = Tempfile.new('ticdir')
p = temp.path
temp.unlink
Dir.mkdir(p)
logger = Logger.new(Tempfile.new('ticgit-log'))
{ :tic_dir => p, :logger => logger }
end


def new_file(name, contents)
File.open(name, 'w') do |f|
f.puts contents
end
end

end



##
# rSpec Hash additions.
#
# From
# * http://wincent.com/knowledge-base/Fixtures_considered_harmful%3F
# * Neil Rahilly

class Hash

##
# Filter keys out of a Hash.
#
# { :a => 1, :b => 2, :c => 3 }.except(:a)
# => { :b => 2, :c => 3 }

def except(*keys)
self.reject { |k,v| keys.include?(k || k.to_sym) }
end

##
# Override some keys.
#
# { :a => 1, :b => 2, :c => 3 }.with(:a => 4)
# => { :a => 4, :b => 2, :c => 3 }

def with(overrides = {})
self.merge overrides
end

##
# Returns a Hash with only the pairs identified by +keys+.
#
# { :a => 1, :b => 2, :c => 3 }.only(:a)
# => { :a => 1 }

def only(*keys)
self.reject { |k,v| !keys.include?(k || k.to_sym) }
end

end
33 changes: 33 additions & 0 deletions spec/ticgit_spec.rb
@@ -0,0 +1,33 @@
require File.dirname(__FILE__) + "/spec_helper"

describe TicGit do
include TicGitSpecHelper

before(:all) do
@path = setup_new_git_repo
@orig_test_opts = test_opts
@ticgit = TicGit.open(@path, @orig_test_opts)
end

it "should create a new branch if it's not there" do
br = @ticgit.git.branches.map { |b| b.name }
br.should include('ticgit')
end

it "should find an existing ticgit branch if it's there" do
tg = TicGit.open(@path, test_opts)
@ticgit.git.branches.size.should eql(tg.git.branches.size)
end

it "should find the .git directory if it's there" do
@ticgit.git.dir.path.should eql(@path)
end

it "should look for the .git directory until it finds it" do
tg = TicGit.open(File.join(@path, 'subdir'), @orig_test_opts)
tg.git.dir.path.should eql(@path)
end

it "should add a .hold file to a new branch"

end

0 comments on commit 67e3618

Please sign in to comment.