forked from dgrijalva/gitx
-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathindex.rb
147 lines (116 loc) · 4.13 KB
/
index.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/env arch -i386 macruby
require 'framework.rb'
require 'test/unit'
require 'tmpdir'
# Setup a temporary directory
TMP_DIR = File.join(TEST_TMP_DIR, "index_test")
def do_git(cmd)
puts "Running: #{cmd}"
`cd #{TMP_DIR} && #{cmd}`
end
def setup_git_repository
`rm -rf #{TMP_DIR}`
FileUtils.mkdir_p(TMP_DIR)
do_git('git init && touch a && touch b && git add a b && git commit -m"First Commit"')
end
class IndexTest < Test::Unit::TestCase
def setup
setup_git_repository
@path = NSURL.alloc.initFileURLWithPath(TMP_DIR)
@repo = PBGitRepository.alloc.initWithURL(@path)
assert(@repo, "Repository creation failed")
@controller = PBGitIndex.alloc.initWithRepository(@repo, workingDirectory:@path)
assert(@controller, "Controller creation failed")
# Setup escape from run loop
NSNotificationCenter.defaultCenter.addObserver(self,
selector:"stopRunLoop:",
name:"PBGitIndexFinishedIndexRefresh",
object:@controller);
end
# Run the default run loop, for up to 2 seconds
def run_loop
@finished = false
runloop = NSRunLoop.currentRunLoop
now = NSDate.date
date = runloop.limitDateForMode("kCFRunLoopDefaultMode")
while date = runloop.limitDateForMode("kCFRunLoopDefaultMode") && !@finished
date = runloop.limitDateForMode("kCFRunLoopDefaultMode")
return false if (date.timeIntervalSinceDate(now)) > 2.0
end
return true
end
# Callback method to stop run loop
def stopRunLoop(notification)
@finished = true
end
def wait_for_refresh
@controller.refresh
assert(run_loop, "Refresh finishes in 2 seconds")
end
def test_refresh
wait_for_refresh
assert(@controller.indexChanges.empty?, "No changes")
do_git('rm a')
wait_for_refresh
assert(@controller.indexChanges.count == 1, "One change")
do_git('touch a')
wait_for_refresh
assert(@controller.indexChanges.empty?, "No changes anymore")
do_git('echo "waa" > a')
wait_for_refresh
assert(@controller.indexChanges.count == 1, "Another change")
previous_state = @controller.indexChanges[0].status
do_git('rm a')
wait_for_refresh
assert(@controller.indexChanges.count == 1, "Still one change")
# 2 == DELETED, see PBChangedFile.h
assert_equal(@controller.indexChanges[0].status, 2, "File status has changed")
do_git('git checkout a')
end
def test_refresh_new_file
do_git('touch c')
wait_for_refresh
assert(@controller.indexChanges.count == 1)
file = @controller.indexChanges[0]
assert_equal(file.status, 0, "File is new")
do_git('git add c')
wait_for_refresh
assert_equal(1, @controller.indexChanges.count, "Just one file changed")
assert_equal(file, @controller.indexChanges[0], "Still the same file")
assert_equal(file.status, 0, "Still new")
do_git('git rm --cached c')
wait_for_refresh
assert_equal(1, @controller.indexChanges.count, "Shouldn't be tracked anymore, but still in other list")
assert_equal(file, @controller.indexChanges[0], "Still the same file")
assert_equal(file.status, 0, "Still new (but only local)")
# FIXME: The things below should actually be true / false, but macruby return 0 / 1
assert(file.hasUnstagedChanges == 1, "Has unstaged changes")
assert(@controller.indexChanges[0].hasStagedChanges == 0, "But no staged changes")
do_git('rm c')
wait_for_refresh
assert(@controller.indexChanges.empty?, "All files should be gone")
# Test an add -> git rm deletion
do_git("touch d && git add d")
wait_for_refresh
assert_equal(1, @controller.indexChanges.count, "Just one changed file")
do_git("git rm -f d")
wait_for_refresh
assert(@controller.indexChanges.empty?, "Should be gone again")
end
def test_remove_existing_file
wait_for_refresh
do_git("rm a")
wait_for_refresh
assert_equal(1, @controller.indexChanges.count, "Change was noticed")
file = @controller.indexChanges[0]
assert_equal(2, file.status, "File was DELETED")
assert(file.hasUnstagedChanges == 1)
assert(file.hasStagedChanges == 0)
do_git("git rm a")
wait_for_refresh
assert_equal(1, @controller.indexChanges.count, "File was removed")
assert_equal(file, @controller.indexChanges[0], "Still the same")
assert(file.hasUnstagedChanges == 0)
assert(file.hasStagedChanges == 1)
end
end