File: test_index_ops.rb

package info (click to toggle)
ruby-git 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 4,124 kB
  • sloc: ruby: 5,385; sh: 507; perl: 64; makefile: 6
file content (168 lines) | stat: -rw-r--r-- 4,697 bytes parent folder | download
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#!/usr/bin/env ruby

require File.dirname(__FILE__) + '/../test_helper'

class TestIndexOps < Test::Unit::TestCase
  
  def setup
    set_file_paths
    @git = Git.open(@wdir)
  end
  
  def test_add
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'new')
      Dir.chdir('new') do
        assert_equal('100644', g.status['example.txt'].mode_index)
        
        new_file('test-file', 'blahblahblah')
        assert(g.status.untracked.assoc('test-file'))
        
        g.add
        assert(g.status.added.assoc('test-file'))
        assert(!g.status.untracked.assoc('test-file'))
        assert(!g.status.changed.assoc('example.txt'))
        
        new_file('example.txt', 'hahahaha')
        assert(g.status.changed.assoc('example.txt'))
        
        g.add
        assert(g.status.changed.assoc('example.txt'))
        
        g.commit('my message')
        assert(!g.status.changed.assoc('example.txt'))
        assert(!g.status.added.assoc('test-file'))
        assert(!g.status.untracked.assoc('test-file')) 
        assert_equal('hahahaha', g.status['example.txt'].blob.contents)
      end
    end
  end

  def test_clean
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'clean_me')
      Dir.chdir('clean_me') do
        new_file('test-file', 'blahblahbal')
        new_file('ignored_file', 'ignored file contents')
        new_file('.gitignore', 'ignored_file')

        g.add
        g.commit("first commit")

        FileUtils.mkdir_p("nested")
        Dir.chdir('nested') do
          Git.init
        end

        new_file('file-to-clean', 'blablahbla')
        FileUtils.mkdir_p("dir_to_clean")

        Dir.chdir('dir_to_clean') do
          new_file('clean-me-too', 'blablahbla')
        end

        assert(File.exist?('file-to-clean'))
        assert(File.exist?('dir_to_clean'))
        assert(File.exist?('ignored_file'))

        g.clean(:force => true)
        
        assert(!File.exist?('file-to-clean'))
        assert(File.exist?('dir_to_clean'))
        assert(File.exist?('ignored_file'))

        new_file('file-to-clean', 'blablahbla')
        
        g.clean(:force => true, :d => true)

        assert(!File.exist?('file-to-clean'))
        assert(!File.exist?('dir_to_clean'))
        assert(File.exist?('ignored_file'))

        g.clean(:force => true, :x => true)
        assert(!File.exist?('ignored_file'))

        assert(File.exist?('nested'))

        g.clean(:ff => true, :d => true)
        assert(!File.exist?('nested'))
      end
    end
  end
  
  def test_revert
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'new')
      Dir.chdir('new') do
        new_file('test-file', 'blahblahbal')
        g.add
        g.commit("first commit")
        first_commit = g.gcommit('HEAD')

        new_file('test-file2', 'blablahbla')
        g.add
        g.commit("second-commit")
        g.gcommit('HEAD')

        commits = g.log(10000).count
        g.revert(first_commit.sha)
        assert_equal(commits + 1, g.log(10000).count)
        assert(!File.exist?('test-file2'))
      end
    end
  end

  def test_add_array
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'new')
      Dir.chdir('new') do
        
        new_file('test-file1', 'blahblahblah1')
        new_file('test-file2', 'blahblahblah2')
        assert(g.status.untracked.assoc('test-file1'))
        
        g.add(['test-file1', 'test-file2'])
        assert(g.status.added.assoc('test-file1'))
        assert(g.status.added.assoc('test-file1'))
        assert(!g.status.untracked.assoc('test-file1'))
                
        g.commit('my message')
        assert(!g.status.added.assoc('test-file1'))
        assert(!g.status.untracked.assoc('test-file1')) 
        assert_equal('blahblahblah1', g.status['test-file1'].blob.contents)
      end
    end
  end
  
  def test_remove
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'remove_test')
      Dir.chdir('remove_test') do
        assert(g.status['example.txt'])
        g.remove('example.txt')
        assert(g.status.deleted.assoc('example.txt')) 
        g.commit('deleted file')
        assert(!g.status['example.txt'])
      end
    end
  end
  
  def test_reset
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'reset_test')
      Dir.chdir('reset_test') do
        new_file('test-file1', 'blahblahblah1')
        new_file('test-file2', 'blahblahblah2')
        assert(g.status.untracked.assoc('test-file1'))
        
        g.add(['test-file1', 'test-file2'])
        assert(!g.status.untracked.assoc('test-file1'))
        
        g.reset
        assert(g.status.untracked.assoc('test-file1'))
        assert(!g.status.added.assoc('test-file1'))
      end
    end
  end
  
end