Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 306 lines (245 sloc) 12.491 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305
require 'spec_helper'
require 'guard/guard'

describe Guard::Watcher do

  describe "#initialize" do
    it "requires a pattern parameter" do
      expect { described_class.new }.to raise_error(ArgumentError)
    end

    context "with a pattern parameter" do
      context "that is a string" do
        it "keeps the string pattern unmodified" do
          described_class.new('spec_helper.rb').pattern.should == 'spec_helper.rb'
        end
      end

      context "that is a regexp" do
        it "keeps the regex pattern unmodified" do
          described_class.new(/spec_helper\.rb/).pattern.should == /spec_helper\.rb/
        end
      end

      context "that is a string looking like a regex (deprecated)" do
        before(:each) { Guard::UI.should_receive(:info).any_number_of_times }

        it "converts the string automatically to a regex" do
          described_class.new('^spec_helper.rb').pattern.should == /^spec_helper.rb/
          described_class.new('spec_helper.rb$').pattern.should == /spec_helper.rb$/
          described_class.new('spec_helper\.rb').pattern.should == /spec_helper\.rb/
          described_class.new('.*_spec.rb').pattern.should == /.*_spec.rb/
        end
      end
    end
  end

  describe "#action" do
    it "sets the action to nothing by default" do
      described_class.new(/spec_helper\.rb/).action.should be_nil
    end

    it "sets the action to the supplied block" do
      action = lambda { |m| "spec/#{m[1]}_spec.rb" }
      described_class.new(%r{^lib/(.*).rb}, action).action.should == action
    end
  end

  describe ".match_files" do
    before(:all) do
      @guard = Guard::Guard.new
      @guard_any_return = Guard::Guard.new
      @guard_any_return.options[:any_return] = true
    end

    context "with a watcher without action" do
      context "that is a regex pattern" do
        before(:all) { @guard.watchers = [described_class.new(/.*_spec\.rb/)] }

        it "returns the paths that matches the regex" do
          described_class.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
        end
      end

      context "that is a string pattern" do
        before(:all) { @guard.watchers = [described_class.new('guard_rocks_spec.rb')] }

        it "returns the path that matches the string" do
          described_class.match_files(@guard, ['guard_rocks_spec.rb', 'guard_rocks.rb']).should == ['guard_rocks_spec.rb']
        end
      end
    end
    
   context "with a watcher action without parameter for a watcher that matches file strings" do
      before(:all) do
        @guard.watchers = [
          described_class.new('spec_helper.rb', lambda { 'spec' }),
          described_class.new('addition.rb', lambda { 1 + 1 }),
          described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
          described_class.new('array.rb', lambda { ['foo', 'bar'] }),
          described_class.new('blank.rb', lambda { '' }),
          described_class.new(/^uptime\.rb/, lambda { `uptime > /dev/null` })
        ]
      end

      it "returns a single file specified within the action" do
        described_class.match_files(@guard, ['spec_helper.rb']).should == ['spec']
      end

      it "returns multiple files specified within the action" do
        described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
      end

      it "returns multiple files by combining the results of different actions" do
        described_class.match_files(@guard, ['spec_helper.rb', 'array.rb']).should == ['spec', 'foo', 'bar']
      end

      it "returns nothing if the action returns something other than a string or an array of strings" do
        described_class.match_files(@guard, ['addition.rb']).should == []
      end

      it "returns nothing if the action response is empty" do
        described_class.match_files(@guard, ['blank.rb']).should == []
      end

      it "returns nothing if the action returns nothing" do
        described_class.match_files(@guard, ['uptime.rb']).should == []
      end
    end

    context 'with a watcher action without parameter for a watcher that matches information objects' do
      before(:all) do
        @guard_any_return.watchers = [
          described_class.new('spec_helper.rb', lambda { 'spec' }),
          described_class.new('addition.rb', lambda { 1 + 1 }),
          described_class.new('hash.rb', lambda { Hash[:foo, 'bar'] }),
          described_class.new('array.rb', lambda { ['foo', 'bar'] }),
          described_class.new('blank.rb', lambda { '' }),
          described_class.new(/^uptime\.rb/, lambda { `uptime > /dev/null` })
        ]
      end
  
      it "returns a single file specified within the action" do
        described_class.match_files(@guard_any_return, ['spec_helper.rb']).class.should == Array
        described_class.match_files(@guard_any_return, ['spec_helper.rb']).empty?.should == false
      end

      it "returns multiple files specified within the action" do
        described_class.match_files(@guard_any_return, ['hash.rb']).should == [{:foo => 'bar'}]
      end

      it "returns multiple files by combining the results of different actions" do
        described_class.match_files(@guard_any_return, ['spec_helper.rb', 'array.rb']).should == ['spec', ['foo', 'bar']]
      end

      it "returns the evaluated addition argument in an array" do
        described_class.match_files(@guard_any_return, ['addition.rb']).class.should == Array
        described_class.match_files(@guard_any_return, ['addition.rb'])[0].should == 2
      end

      it "returns nothing if the action response is empty string" do
        described_class.match_files(@guard_any_return, ['blank.rb']).should == ['']
      end

      it "returns nothing if the action returns empty string" do
        described_class.match_files(@guard_any_return, ['uptime.rb']).should == ['']
      end
    end
    
    context "with a watcher action that takes a parameter for a watcher that matches file strings" do
       before(:all) do
         @guard.watchers = [
           described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
           described_class.new(/addition(.*)\.rb/, lambda { |m| 1 + 1 }),
           described_class.new('hash.rb', lambda { |m| Hash[:foo, 'bar'] }),
           described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar'] }),
           described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
           described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
         ]
       end

       it "returns a substituted single file specified within the action" do
         described_class.match_files(@guard, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
       end

       it "returns multiple files specified within the action" do
         described_class.match_files(@guard, ['hash.rb']).should == ['foo', 'bar']
       end

       it "returns multiple files by combining the results of different actions" do
         described_class.match_files(@guard, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', 'foo', 'bar']
       end

       it "returns nothing if the action returns something other than a string or an array of strings" do
         described_class.match_files(@guard, ['addition.rb']).should == []
       end

       it "returns nothing if the action response is empty" do
         described_class.match_files(@guard, ['blank.rb']).should == []
       end

       it "returns nothing if the action returns nothing" do
         described_class.match_files(@guard, ['uptime.rb']).should == []
       end
    end
   
    context "with a watcher action that takes a parameter for a watcher that matches information objects" do
      before(:all) do
        @guard_any_return.watchers = [
          described_class.new(%r{lib/(.*)\.rb}, lambda { |m| "spec/#{m[1]}_spec.rb" }),
          described_class.new(/addition(.*)\.rb/, lambda { |m| (1 + 1).to_s + m[0] }),
          described_class.new('hash.rb', lambda { |m| Hash[:foo, 'bar', :file_name, m[0]] }),
          described_class.new(/array(.*)\.rb/, lambda { |m| ['foo', 'bar', m[0]] }),
          described_class.new(/blank(.*)\.rb/, lambda { |m| '' }),
          described_class.new(/uptime(.*)\.rb/, lambda { |m| `uptime > /dev/null` })
        ]
      end

      it "returns a substituted single file specified within the action" do
        described_class.match_files(@guard_any_return, ['lib/my_wonderful_lib.rb']).should == ['spec/my_wonderful_lib_spec.rb']
      end

      it "returns a hash specified within the action" do
        described_class.match_files(@guard_any_return, ['hash.rb']).should == [{:foo => 'bar', :file_name => 'hash.rb'}]
      end

      it "returns multiple files by combining the results of different actions" do
        described_class.match_files(@guard_any_return, ['lib/my_wonderful_lib.rb', 'array.rb']).should == ['spec/my_wonderful_lib_spec.rb', ['foo', 'bar', "array.rb"]]
      end

      it "returns the evaluated addition argument + the path" do
        described_class.match_files(@guard_any_return, ['addition.rb']).should == ["2addition.rb"]
      end

      it "returns nothing if the action response is empty string" do
        described_class.match_files(@guard_any_return, ['blank.rb']).should == ['']
      end

      it "returns nothing if the action returns empty string" do
        described_class.match_files(@guard_any_return, ['uptime.rb']).should == ['']
      end
    end

    context "with an exception that is raised" do
       before(:all) { @guard.watchers = [described_class.new('evil.rb', lambda { raise "EVIL" })] }

       it "displays the error and backtrace" do
         Guard::UI.should_receive(:error) do |msg|
           msg.should include("Problem with watch action!")
           msg.should include("EVIL")
         end

         described_class.match_files(@guard, ['evil.rb'])
       end
     end
   end

  describe ".match_files?" do
    before(:all) do
      @guard1 = Guard::Guard.new([described_class.new(/.*_spec\.rb/)])
      @guard2 = Guard::Guard.new([described_class.new('spec_helper.rb', 'spec')])
      @guards = [@guard1, @guard2]
    end

    context "with a watcher that matches a file" do
      specify { described_class.match_files?(@guards, ['lib/my_wonderful_lib.rb', 'guard_rocks_spec.rb']).should be_true }
    end

    context "with no watcher that matches a file" do
      specify { described_class.match_files?(@guards, ['lib/my_wonderful_lib.rb']).should be_false }
    end
  end

  describe "#match_file?" do
    context "with a string pattern" do
      context "that is a normal string" do
        subject { described_class.new('guard_rocks_spec.rb') }

        context "with a watcher that matches a file" do
          specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
        end

        context "with no watcher that matches a file" do
          specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false }
        end
      end

      context "that is a string representing a regexp (deprecated)" do
        subject { described_class.new('^guard_rocks_spec\.rb$') }

        context "with a watcher that matches a file" do
          specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
        end

        context "with no watcher that matches a file" do
          specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false }
        end
      end
    end

    context "that is a regexp pattern" do
      subject { described_class.new(/.*_spec\.rb/) }

      context "with a watcher that matches a file" do
        specify { subject.match_file?('guard_rocks_spec.rb').should be_true }
      end

      context "with no watcher that matches a file" do
        specify { subject.match_file?('lib/my_wonderful_lib.rb').should be_false }
      end
    end
  end

  describe ".match_guardfile?" do
    before(:all) { Guard::Dsl.stub(:guardfile_path) { Dir.pwd + '/Guardfile' } }

    context "with files that match the Guardfile" do
      specify { described_class.match_guardfile?(['Guardfile', 'guard_rocks_spec.rb']).should be_true }
    end

    context "with no files that match the Guardfile" do
      specify { described_class.match_guardfile?(['guard_rocks.rb', 'guard_rocks_spec.rb']).should be_false }
    end
  end

end
Something went wrong with that request. Please try again.