public
Description: A tool to simplify working with remote branches
Homepage: http://grb.rubyforge.org/
Clone URL: git://github.com/webmat/git_remote_branch.git
Click here to lend your support to: git_remote_branch and make a donation at www.pledgie.com !
git_remote_branch / test / helpers / shoulda_functional_helpers.rb
100644 153 lines (127 sloc) 3.759 kb
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
module ShouldaFunctionalHelpers
  include CaptureFu
  include InDir
  
  GIT = GitRemoteBranch::GIT
  
  def self.ruby_prefix
    if ENV['RUBY']
      warn " Forcing execution of grb with ruby interpreter #{ENV['RUBY']}"
      ENV['RUBY'] + ' '
    elsif WINDOWS
      'ruby '
    else
      ''
    end
  end
  
  # Here we're only prepending with 'ruby'.
  # When run as a gem, RubyGems takes care of generating a batch file that does this stuff.
  GRB_COMMAND = ruby_prefix + File.expand_path(File.dirname(__FILE__) + '/../../bin/grb') unless defined?(GRB_COMMAND)
  
  def self.included(base)
    base.extend ClassMethods
    base.class_eval do
      include ::ShouldaFunctionalHelpers::InstanceMethods
    end
  end
  
  module InstanceMethods
    def current_dir
      @current_dir || raise("@current_dir is not set. Warning, Will Robinson!")
    end
    
    def current_dir=(value)
      @current_dir = value
    end
    
    # Switches to one of the directories created by GitHelper:
    # :local1, :local2, :non_git or :remote
    # This affects commands run with ``, system and so on.
    def in_directory_for(dir)
      # Just a reminder for my dumb head
      raise "'in_directory_for' depends on @gh being set" unless @gh
      
      @current_dir = eval("@gh.#{dir}")
    end
    
    def in_branch(branch)
      execute "#{GIT} checkout #{branch}"
    end
    
    
    def run_grb_with(params='')
      execute "#{GRB_COMMAND} #{params}"
    end
 
    def execute(command)
      in_dir current_dir do
        errno, returned_string = capture_process_output(command)
        returned_string
      end
    end
    
    private
      def get_branch_location(location)
        case location.to_sym
        when :local
          args = '-l'
        when :remote
          args = '-r'
        else
          raise ArgumentError, "Unknown branch location: #{location.inspect}"
        end
      end
  end
 
  module ClassMethods
    def should_have_branch(what_branch, *wheres)
      wheres.flatten.each do |where|
        should "have the branch '#{what_branch}' #{where == :local ? 'locally' : 'remotely'}" do
          args = get_branch_location(where)
          assert_match(/#{what_branch}/, execute("#{GIT} branch #{args}"))
        end
      end
    end
    
    def should_not_have_branch(what_branch, *wheres)
      wheres.flatten.each do |where|
        should "not have the branch '#{what_branch}' #{where == :local ? 'locally' : 'remotely'}" do
          args = get_branch_location(where)
          assert_no_match(/#{what_branch}/, execute("#{GIT} branch #{args}"))
        end
      end
    end
    
    def on_a_repository
      context "on a new repository" do
        setup do
          @gh = GitHelper.new
        end
 
        teardown do
          @gh.cleanup
        end
 
        context '' do
          yield
        end
      end
    end
    
    def in_a_non_git_directory
      context "on a non-git related directory" do
        setup do
          @temp_dir = TempDirHelper.new
          @current_dir = @temp_dir.directory
        end
        
        teardown do
          @temp_dir.cleanup
        end
        
        context '' do
          yield
        end
      end
    end
    
    def with_env_var(name, value)
      name = name.to_s
      
      context "with environment variable '#{name}' set to '#{value}'" do
        setup do
          @env_previous_value = ENV[name] if ENV.keys.include?(name)
          ENV[name] = value
        end
        
        teardown do
          if @env_previous_value
            ENV[name] = @env_previous_value
          else
            ENV.delete(name)
          end
        end
        
        context '' do
          yield
        end
      end
    end
  end
end