Skip to content

Commit 30621f1

Browse files
committed
test/index: add more tests
1 parent a3f02b2 commit 30621f1

File tree

1 file changed

+70
-17
lines changed

1 file changed

+70
-17
lines changed

test/index.rb

+70-17
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,39 @@
44
require 'test/unit'
55
require 'tmpdir'
66

7-
87
# Setup a temporary directory
98
TMP_DIR = File.join(TEST_TMP_DIR, "index_test")
109

11-
`rm -rf #{TMP_DIR}`
12-
FileUtils.mkdir_p(TMP_DIR)
13-
1410
def do_git(cmd)
1511
puts "Running: #{cmd}"
1612
`cd #{TMP_DIR} && #{cmd}`
1713
end
1814

19-
do_git('git init && touch a && touch b && git add a b && git commit -m"First Commit"')
15+
def setup_git_repository
16+
`rm -rf #{TMP_DIR}`
17+
FileUtils.mkdir_p(TMP_DIR)
18+
do_git('git init && touch a && touch b && git add a b && git commit -m"First Commit"')
19+
end
2020

2121
class IndexTest < Test::Unit::TestCase
2222

2323
def setup
24-
@finished = false
25-
path = NSURL.alloc.initFileURLWithPath(TMP_DIR)
26-
@repo = PBGitRepository.alloc.initWithURL(path)
24+
setup_git_repository
25+
26+
@path = NSURL.alloc.initFileURLWithPath(TMP_DIR)
27+
@repo = PBGitRepository.alloc.initWithURL(@path)
2728
assert(@repo, "Repository creation failed")
28-
@controller = PBGitIndex.alloc.initWithRepository(@repo, workingDirectory:path)
29+
@controller = PBGitIndex.alloc.initWithRepository(@repo, workingDirectory:@path)
30+
assert(@controller, "Controller creation failed")
31+
32+
# Setup escape from run loop
33+
NSNotificationCenter.defaultCenter.addObserver(self,
34+
selector:"stopRunLoop:",
35+
name:"PBGitIndexFinishedIndexRefresh",
36+
object:@controller);
2937
end
3038

39+
# Run the default run loop, for up to 2 seconds
3140
def run_loop
3241
@finished = false
3342
runloop = NSRunLoop.currentRunLoop
@@ -41,8 +50,8 @@ def run_loop
4150
return true
4251
end
4352

44-
def refreshFinished(notification)
45-
puts "Refresh finished!"
53+
# Callback method to stop run loop
54+
def stopRunLoop(notification)
4655
@finished = true
4756
end
4857

@@ -51,17 +60,18 @@ def wait_for_refresh
5160
assert(run_loop, "Refresh finishes in 2 seconds")
5261
end
5362

54-
def test_a
55-
NSNotificationCenter.defaultCenter.addObserver(self,
56-
selector:"refreshFinished:",
57-
name:"PBGitIndexFinishedIndexRefresh",
58-
object:@controller);
5963

64+
65+
66+
67+
def test_refresh
6068
wait_for_refresh
6169
assert(@controller.indexChanges.empty?, "No changes")
70+
6271
do_git('rm a')
6372
wait_for_refresh
6473
assert(@controller.indexChanges.count == 1, "One change")
74+
6575
do_git('touch a')
6676
wait_for_refresh
6777
assert(@controller.indexChanges.empty?, "No changes anymore")
@@ -77,7 +87,9 @@ def test_a
7787
# 2 == DELETED, see PBChangedFile.h
7888
assert_equal(@controller.indexChanges[0].status, 2, "File status has changed")
7989
do_git('git checkout a')
90+
end
8091

92+
def test_refresh_new_file
8193
do_git('touch c')
8294
wait_for_refresh
8395
assert(@controller.indexChanges.count == 1)
@@ -86,9 +98,50 @@ def test_a
8698

8799
do_git('git add c')
88100
wait_for_refresh
89-
assert(@controller.indexChanges.count == 1)
101+
assert_equal(1, @controller.indexChanges.count, "Just one file changed")
90102
assert_equal(file, @controller.indexChanges[0], "Still the same file")
91103
assert_equal(file.status, 0, "Still new")
104+
105+
do_git('git rm --cached c')
106+
wait_for_refresh
107+
assert_equal(1, @controller.indexChanges.count, "Shouldn't be tracked anymore, but still in other list")
108+
assert_equal(file, @controller.indexChanges[0], "Still the same file")
109+
assert_equal(file.status, 0, "Still new (but only local)")
110+
111+
# FIXME: The things below should actually be true / false, but macruby return 0 / 1
112+
assert(file.hasUnstagedChanges == 1, "Has unstaged changes")
113+
assert(@controller.indexChanges[0].hasStagedChanges == 0, "But no staged changes")
114+
115+
do_git('rm c')
116+
wait_for_refresh
117+
assert(@controller.indexChanges.empty?, "All files should be gone")
118+
119+
# Test an add -> git rm deletion
120+
do_git("touch d && git add d")
121+
wait_for_refresh
122+
assert_equal(1, @controller.indexChanges.count, "Just one changed file")
123+
124+
do_git("git rm -f d")
125+
wait_for_refresh
126+
assert(@controller.indexChanges.empty?, "Should be gone again")
127+
end
128+
129+
def test_remove_existing_file
130+
wait_for_refresh
131+
do_git("rm a")
132+
wait_for_refresh
133+
assert_equal(1, @controller.indexChanges.count, "Change was noticed")
134+
file = @controller.indexChanges[0]
135+
assert_equal(2, file.status, "File was DELETED")
136+
assert(file.hasUnstagedChanges == 1)
137+
assert(file.hasStagedChanges == 0)
138+
139+
do_git("git rm a")
140+
wait_for_refresh
141+
assert_equal(1, @controller.indexChanges.count, "File was removed")
142+
assert_equal(file, @controller.indexChanges[0], "Still the same")
143+
assert(file.hasUnstagedChanges == 0)
144+
assert(file.hasStagedChanges == 1)
92145
end
93146

94147
end

0 commit comments

Comments
 (0)