File: test_branch.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 (106 lines) | stat: -rw-r--r-- 3,083 bytes parent folder | download | duplicates (2)
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
#!/usr/bin/env ruby

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

class TestBranch < Test::Unit::TestCase
  def setup
    set_file_paths
    @git = Git.open(@wdir)
    
    @commit = @git.object('1cc8667014381')
    @tree = @git.object('1cc8667014381^{tree}')
    @blob = @git.object('v2.5:example.txt')
    
    @branches = @git.branches
  end
  
  def test_branches_all
    assert(@git.branches[:master].is_a?(Git::Branch))
    assert(@git.branches.size > 5)
  end
  
  def test_branches_local
    bs = @git.branches.local
    assert(bs.size > 4)
  end

  def test_branches_remote
    bs = @git.branches.remote
    assert_equal(1, bs.size)
  end
  
  def test_branches_single
    branch = @git.branches[:test_object]
    assert_equal('test_object', branch.name)

    %w{working/master remotes/working/master}.each do |branch_name|
      branch = @git.branches[branch_name]
     
      assert_equal('master', branch.name)
      assert_equal('remotes/working/master', branch.full)
      assert_equal('working', branch.remote.name)
      assert_equal('+refs/heads/*:refs/remotes/working/*', branch.remote.fetch_opts)
      assert_equal('../working.git', branch.remote.url)
    end
  end
  
  def test_true_branch_contains?
    assert(@git.branch('git_grep').contains?('master'))
  end

  def test_false_branch_contains?
    assert(!@git.branch('master').contains?('git_grep'))
  end

  def test_branch_commit
    assert_equal(270, @git.branches[:test_branches].gcommit.size)
  end
  
  def test_branch_create_and_switch
    in_temp_dir do |path|
      g = Git.clone(@wbare, 'branch_test')
      Dir.chdir('branch_test') do
        assert(!g.branch('new_branch').current)
        g.branch('other_branch').create
        assert(!g.branch('other_branch').current)
        g.branch('new_branch').checkout
        assert(g.branch('new_branch').current)

        assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)

        new_file('test-file1', 'blahblahblah1')
        new_file('test-file2', 'blahblahblah2')
        new_file('.test-dot-file1', 'blahblahblahdot1')
        assert(g.status.untracked.assoc('test-file1'))
        assert(g.status.untracked.assoc('.test-dot-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'))

        assert_raise Git::GitExecuteError do
          g.branch('new_branch').delete 
        end
        assert_equal(1, g.branches.select { |b| b.name == 'new_branch' }.size)

        g.branch('master').checkout
        g.branch('new_branch').delete
        assert_equal(0, g.branches.select { |b| b.name == 'new_branch' }.size)
        
        g.checkout('other_branch')
        assert(g.branch('other_branch').current)

        g.checkout('master')
        assert(!g.branch('other_branch').current)

        g.checkout(g.branch('other_branch'))
        assert(g.branch('other_branch').current)
        
      end
    end
  end
  
end