4
4
require 'test/unit'
5
5
require 'tmpdir'
6
6
7
-
8
7
# Setup a temporary directory
9
8
TMP_DIR = File . join ( TEST_TMP_DIR , "index_test" )
10
9
11
- `rm -rf #{ TMP_DIR } `
12
- FileUtils . mkdir_p ( TMP_DIR )
13
-
14
10
def do_git ( cmd )
15
11
puts "Running: #{ cmd } "
16
12
`cd #{ TMP_DIR } && #{ cmd } `
17
13
end
18
14
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
20
20
21
21
class IndexTest < Test ::Unit ::TestCase
22
22
23
23
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 )
27
28
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 ) ;
29
37
end
30
38
39
+ # Run the default run loop, for up to 2 seconds
31
40
def run_loop
32
41
@finished = false
33
42
runloop = NSRunLoop . currentRunLoop
@@ -41,8 +50,8 @@ def run_loop
41
50
return true
42
51
end
43
52
44
- def refreshFinished ( notification )
45
- puts "Refresh finished!"
53
+ # Callback method to stop run loop
54
+ def stopRunLoop ( notification )
46
55
@finished = true
47
56
end
48
57
@@ -51,17 +60,18 @@ def wait_for_refresh
51
60
assert ( run_loop , "Refresh finishes in 2 seconds" )
52
61
end
53
62
54
- def test_a
55
- NSNotificationCenter . defaultCenter . addObserver ( self ,
56
- selector :"refreshFinished:" ,
57
- name :"PBGitIndexFinishedIndexRefresh" ,
58
- object :@controller ) ;
59
63
64
+
65
+
66
+
67
+ def test_refresh
60
68
wait_for_refresh
61
69
assert ( @controller . indexChanges . empty? , "No changes" )
70
+
62
71
do_git ( 'rm a' )
63
72
wait_for_refresh
64
73
assert ( @controller . indexChanges . count == 1 , "One change" )
74
+
65
75
do_git ( 'touch a' )
66
76
wait_for_refresh
67
77
assert ( @controller . indexChanges . empty? , "No changes anymore" )
@@ -77,7 +87,9 @@ def test_a
77
87
# 2 == DELETED, see PBChangedFile.h
78
88
assert_equal ( @controller . indexChanges [ 0 ] . status , 2 , "File status has changed" )
79
89
do_git ( 'git checkout a' )
90
+ end
80
91
92
+ def test_refresh_new_file
81
93
do_git ( 'touch c' )
82
94
wait_for_refresh
83
95
assert ( @controller . indexChanges . count == 1 )
@@ -86,9 +98,50 @@ def test_a
86
98
87
99
do_git ( 'git add c' )
88
100
wait_for_refresh
89
- assert ( @controller . indexChanges . count == 1 )
101
+ assert_equal ( 1 , @controller . indexChanges . count , "Just one file changed" )
90
102
assert_equal ( file , @controller . indexChanges [ 0 ] , "Still the same file" )
91
103
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 )
92
145
end
93
146
94
147
end
0 commit comments