Module: Guard::Jasmine::Util

Included in:
Guard::Jasmine, CLI, Runner, JasmineCoverage
Defined in:
lib/guard/jasmine/util.rb

Overview

Provider of some shared utility methods.

Instance Method Summary (collapse)

Instance Method Details

- (Integer) find_free_server_port

Finds a free server port to use

Returns:

  • (Integer)

    a free server port



88
89
90
91
92
93
94
95
96
# File 'lib/guard/jasmine/util.rb', line 88

def find_free_server_port
  server = TCPServer.new('127.0.0.1', 0)
  port   = server.addr[1]
  server.close

  port
rescue Errno::EADDRINUSE
  retry
end

- (Boolean) phantomjs_bin_valid?(bin)

Verifies that the phantomjs bin is available and the right version is installed.

Parameters:

  • bin (String)

    the location of the phantomjs bin

Returns:

  • (Boolean)

    when the runner is available



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/guard/jasmine/util.rb', line 59

def phantomjs_bin_valid?(bin)
  if bin && !bin.empty?
    version = `#{ bin } --version`

    if version
      # Remove all but version, e.g. from '1.5 (development)'
      cleaned_version = version.match(/(\d\.)*(\d)/)

      if cleaned_version
        if Gem::Version.new(cleaned_version[0]) < Gem::Version.new('1.3.0')
          ::Guard::Jasmine::Formatter.error "PhantomJS executable at #{ bin } must be at least version 1.3.0"
        else
          true
        end
      else
        ::Guard::Jasmine::Formatter.error "PhantomJS reports unknown version format: #{ version }"
      end
    else
      ::Guard::Jasmine::Formatter.error "PhantomJS executable doesn't exist at #{ bin }"
    end
  else
    ::Guard::Jasmine::Formatter.error 'PhantomJS executable couldn\'t be auto detected.'
  end
end

- (Boolean) runner_available?(options)

Verifies if the Jasmine test runner is available. If the runner is not available within 15 seconds, then the availability check will cancel.

Parameters:

  • options (Hash)

    the options for the Guard

Options Hash (options):

  • :server_timeout (Integer)

    the number of seconds to wait for the Jasmine spec server

  • :jasmine_url (String)

    the url of the Jasmine test runner

Returns:

  • (Boolean)

    when the runner is available



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
# File 'lib/guard/jasmine/util.rb', line 21

def runner_available?(options)
  url = URI.parse(options[:jasmine_url])

  begin
    ::Guard::Jasmine::Formatter.info "Waiting for Jasmine test runner at #{ url }"

    http = Net::HTTP.new(url.host, url.port)
    http.read_timeout = options[:server_timeout]
    http.start do
      response  = http.request(Net::HTTP::Get.new(url.path))
      available = response.code.to_i == 200

      unless available
        ::Guard::Jasmine::Formatter.error "Jasmine test runner failed with status #{ response.code }"
        if response.body
          ::Guard::Jasmine::Formatter.error 'Please open the Jasmine runner in your browser for more information.'
        end
      end

      available
    end

  rescue Timeout::Error => e
    ::Guard::Jasmine::Formatter.error 'Timeout waiting for the Jasmine test runner.'
    false

  rescue => e
    ::Guard::Jasmine::Formatter.error "Jasmine test runner isn't available: #{ e.message }"
    false
  end
end

- (String?) which(cmd)

Examples:

which('ruby') #=> /usr/bin/ruby

Parameters:

  • cmd (String)

    the executable to find

Returns:

  • (String, nil)

    the path to the executable



107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/guard/jasmine/util.rb', line 107

def which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']

  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = "#{ path }/#{ cmd }#{ ext }"
      return exe if File.file?(exe) && File.executable?(exe)
    end
  end

  nil
end