1
+ #!/usr/bin/env arch -i386 macruby
2
+
3
+ require 'framework.rb'
4
+ require 'test/unit'
5
+ require 'tmpdir'
6
+
7
+
8
+ # Setup a temporary directory
9
+ TMP_DIR = File . join ( TEST_TMP_DIR , "index_test" )
10
+
11
+ `rm -rf #{ TMP_DIR } `
12
+ FileUtils . mkdir_p ( TMP_DIR )
13
+
14
+ def do_git ( cmd )
15
+ puts "Running: #{ cmd } "
16
+ `cd #{ TMP_DIR } && #{ cmd } `
17
+ end
18
+
19
+ do_git ( 'git init && touch a && touch b && git add a b && git commit -m"First Commit"' )
20
+
21
+ class IndexTest < Test ::Unit ::TestCase
22
+
23
+ def setup
24
+ @finished = false
25
+ path = NSURL . alloc . initFileURLWithPath ( TMP_DIR )
26
+ @repo = PBGitRepository . alloc . initWithURL ( path )
27
+ assert ( @repo , "Repository creation failed" )
28
+ @controller = PBGitIndex . alloc . initWithRepository ( @repo , workingDirectory :path )
29
+ end
30
+
31
+ def run_loop
32
+ @finished = false
33
+ runloop = NSRunLoop . currentRunLoop
34
+ now = NSDate . date
35
+ date = runloop . limitDateForMode ( "kCFRunLoopDefaultMode" )
36
+
37
+ while date = runloop . limitDateForMode ( "kCFRunLoopDefaultMode" ) && !@finished
38
+ date = runloop . limitDateForMode ( "kCFRunLoopDefaultMode" )
39
+ return false if ( date . timeIntervalSinceDate ( now ) ) > 2.0
40
+ end
41
+ return true
42
+ end
43
+
44
+ def refreshFinished ( notification )
45
+ puts "Refresh finished!"
46
+ @finished = true
47
+ end
48
+
49
+ def wait_for_refresh
50
+ @controller . refresh
51
+ assert ( run_loop , "Refresh finishes in 2 seconds" )
52
+ end
53
+
54
+ def test_a
55
+ NSNotificationCenter . defaultCenter . addObserver ( self ,
56
+ selector :"refreshFinished:" ,
57
+ name :"PBGitIndexFinishedIndexRefresh" ,
58
+ object :@controller ) ;
59
+
60
+ wait_for_refresh
61
+ assert ( @controller . indexChanges . empty? , "No changes" )
62
+ do_git ( 'rm a' )
63
+ wait_for_refresh
64
+ assert ( @controller . indexChanges . count == 1 , "One change" )
65
+ do_git ( 'touch a' )
66
+ wait_for_refresh
67
+ assert ( @controller . indexChanges . empty? , "No changes anymore" )
68
+
69
+ do_git ( 'echo "waa" > a' )
70
+ wait_for_refresh
71
+ assert ( @controller . indexChanges . count == 1 , "Another change" )
72
+ previous_state = @controller . indexChanges [ 0 ] . status
73
+
74
+ do_git ( 'rm a' )
75
+ wait_for_refresh
76
+ assert ( @controller . indexChanges . count == 1 , "Still one change" )
77
+ # 2 == DELETED, see PBChangedFile.h
78
+ assert_equal ( @controller . indexChanges [ 0 ] . status , 2 , "File status has changed" )
79
+ do_git ( 'git checkout a' )
80
+
81
+ do_git ( 'touch c' )
82
+ wait_for_refresh
83
+ assert ( @controller . indexChanges . count == 1 )
84
+ file = @controller . indexChanges [ 0 ]
85
+ assert_equal ( file . status , 0 , "File is new" )
86
+
87
+ do_git ( 'git add c' )
88
+ wait_for_refresh
89
+ assert ( @controller . indexChanges . count == 1 )
90
+ assert_equal ( file , @controller . indexChanges [ 0 ] , "Still the same file" )
91
+ assert_equal ( file . status , 0 , "Still new" )
92
+ end
93
+
94
+ end
0 commit comments