Module: Jasmine

Defined in:
lib/jasmine/base.rb,
lib/jasmine/page.rb,
lib/jasmine/config.rb,
lib/jasmine/result.rb,
lib/jasmine/server.rb,
lib/jasmine/railtie.rb,
lib/jasmine/version.rb,
lib/jasmine/ci_runner.rb,
lib/jasmine/application.rb,
lib/jasmine/path_mapper.rb,
lib/jasmine/dependencies.rb,
lib/jasmine/configuration.rb,
lib/jasmine/path_expander.rb,
lib/jasmine/asset_expander.rb,
lib/jasmine/formatters/multi.rb,
lib/jasmine/command_line_tool.rb,
lib/jasmine/core_configuration.rb,
lib/jasmine/formatters/console.rb,
lib/jasmine/runners/phantom_js.rb,
lib/jasmine/yaml_config_parser.rb,
lib/jasmine/formatters/exit_code.rb,
lib/jasmine/asset_pipeline_mapper.rb,
lib/jasmine/runners/chrome_headless.rb,
lib/generators/jasmine/install/install_generator.rb,
lib/generators/jasmine/examples/examples_generator.rb

Defined Under Namespace

Modules: Dependencies, Formatters, Generators, Runners Classes: Application, AssetExpander, AssetPipelineMapper, CiRunner, CommandLineTool, Configuration, CoreConfiguration, Page, PathExpander, PathMapper, Railtie, Result, Server, YamlConfigParser

Constant Summary collapse

ConfigNotFound =
Class.new(Exception)
VERSION =
"3.99.0"

Class Method Summary collapse

Class Method Details

.configObject



94
95
96
97
# File 'lib/jasmine/config.rb', line 94

def self.config
  initialize_config
  @config
end

.configure(&block) ⇒ Object



5
6
7
# File 'lib/jasmine/config.rb', line 5

def self.configure(&block)
  block.call(self.config)
end

.find_unused_portObject



15
16
17
18
19
20
# File 'lib/jasmine/base.rb', line 15

def self.find_unused_port
  socket = open_socket_on_unused_port
  port = socket.addr[1]
  socket.close
  port
end

.initialize_configObject



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
# File 'lib/jasmine/config.rb', line 9

def self.initialize_config
  return if @config
  @config = Jasmine::Configuration.new
  core_config = Jasmine::CoreConfiguration.new

  @config.add_path_mapper(Jasmine::PathMapper.method(:new))

  @config.jasmine_path = jasmine_path = '/__jasmine__'
  @config.src_path = src_path = '/'
  @config.spec_path = spec_path = '/__spec__'
  @config.boot_path = boot_path = '/__boot__'
  @config.runner_boot_path = runner_boot_path = '/__runner_boot__'
  @config.image_path = image_path = '/__images__'

  @config.jasmine_dir = core_config.path
  @config.boot_dir = core_config.boot_dir
  @config.boot_files = lambda { core_config.boot_files }
  @config.jasmine_files = lambda { core_config.js_files }
  @config.jasmine_css_files = lambda { core_config.css_files }
  @config.add_rack_path(jasmine_path, lambda { Rack::File.new(config.jasmine_dir) })
  @config.add_rack_path(boot_path, lambda { Rack::File.new(config.boot_dir) })
  @config.add_rack_path(runner_boot_path, lambda { Rack::File.new(config.runner_boot_dir) })
  if Jasmine::Dependencies.use_asset_pipeline?
    @config.add_rack_path(spec_path, lambda {
      sprockets_spec_env = Sprockets::Environment.new
      sprockets_spec_env.append_path @config.spec_dir
      Rails.application.assets.paths.each do |path|
        sprockets_spec_env.append_path(path)
      end
      sprockets_spec_env
    })
  else
    @config.add_rack_path(spec_path, lambda { Rack::File.new(config.spec_dir) })
  end
  @config.add_rack_path(image_path, lambda { Rack::File.new(core_config.images_dir) })
  @config.add_rack_path(src_path, lambda {
    Rack::Cascade.new([
      Rack::URLMap.new('/' => Rack::File.new(config.src_dir)),
      Rack::Jasmine::Runner.new(Jasmine::Page.new(config))
    ])
  })

  @config.add_rack_app(Rack::Head)
  @config.add_rack_app(Rack::Jasmine::CacheControl)

  if Jasmine::Dependencies.use_asset_pipeline?
    @config.add_path_mapper(lambda { |config|
      asset_expander = Jasmine::AssetExpander.new
      Jasmine::AssetPipelineMapper.new(config, asset_expander.method(:expand))
    })
    # In order to have asset helpers like asset_path and image_path, we need to require 'action_view/base'.  This
    # triggers run_load_hooks on action_view which, in turn, causes sprockets/railtie to load the Sprockets asset
    # helpers and set some configuration options.
    Rails.application.assets.context_class.instance_eval do
      if Jasmine::Dependencies.rails4?
        require 'action_view/base'
        Rails.application.assets.context_class.assets_prefix = Rails.application.config.assets.prefix
      end
    end
    @config.add_rack_path(Rails.application.config.assets.prefix, lambda {
      Rails.application.assets
    })
  end

  @config.runner = lambda do |formatter, jasmine_server_url|
    case @config.runner_browser
    when :phantomjs
      Jasmine::Runners::PhantomJs.new(formatter,
                                      jasmine_server_url,
                                      @config.prevent_phantom_js_auto_install,
                                      @config.show_console_log,
                                      @config.phantom_config_script,
                                      @config.show_full_stack_trace,
                                      @config.phantom_cli_options)
    when :chromeheadless
      Jasmine::Runners::ChromeHeadless.new(formatter,
                                        jasmine_server_url,
                                        @config)
    else
      raise "Jasmine.config.runner_browser should be either phantomjs or chromeheadless"
    end

  end
end

.load_configuration_from_yaml(path = nil) ⇒ Object



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
# File 'lib/jasmine/config.rb', line 99

def self.load_configuration_from_yaml(path = nil)
  path ||= File.join(Dir.pwd, 'spec', 'javascripts', 'support', 'jasmine.yml')
  if File.exist?(path)
    yaml_loader = lambda do |filepath|
      YAML::load(ERB.new(File.read(filepath)).result(binding)) if File.exist?(filepath)
    end
    yaml_config = Jasmine::YamlConfigParser.new(path, Dir.pwd, Jasmine::PathExpander.method(:expand), yaml_loader)
    Jasmine.configure do |config|
      config.jasmine_dir = yaml_config.jasmine_dir if yaml_config.jasmine_dir
      config.jasmine_files = lambda { yaml_config.jasmine_files } if yaml_config.jasmine_files.any?
      config.jasmine_css_files = lambda { yaml_config.jasmine_css_files } if yaml_config.jasmine_css_files.any?
      config.boot_dir = yaml_config.boot_dir if yaml_config.boot_dir
      config.boot_files = lambda { yaml_config.boot_files } if yaml_config.boot_files.any?

      config.src_dir = yaml_config.src_dir
      config.src_files = lambda { yaml_config.src_files }
      config.css_files = lambda { yaml_config.css_files }

      config.spec_dir = yaml_config.spec_dir
      config.helper_files = lambda { yaml_config.helpers }
      config.spec_files = lambda { yaml_config.spec_files }

      config.show_console_log = yaml_config.show_console_log
      config.stop_spec_on_expectation_failure = yaml_config.stop_spec_on_expectation_failure
      config.stop_on_spec_failure = yaml_config.stop_on_spec_failure
      config.random = yaml_config.random
      config.phantom_config_script = yaml_config.phantom_config_script
      config.phantom_cli_options = yaml_config.phantom_cli_options

      config.rack_options = yaml_config.rack_options
    end
    require yaml_config.spec_helper if File.exist?(yaml_config.spec_helper)
  else
   raise ConfigNotFound, "Unable to load jasmine config from #{path}"
  end
end

.load_spec(spec_path) ⇒ Object



136
137
138
139
140
141
# File 'lib/jasmine/config.rb', line 136

def self.load_spec(spec_path)
  return if spec_path.nil?
  Jasmine.configure do |c|
    c.spec_files = lambda { [spec_path] }
  end
end

.open_socket_on_unused_portObject

this seemingly-over-complex method is necessary to get an open port on at least some of our Macs



6
7
8
9
10
11
12
13
# File 'lib/jasmine/base.rb', line 6

def self.open_socket_on_unused_port
  infos = Socket::getaddrinfo("localhost", nil, Socket::AF_UNSPEC, Socket::SOCK_STREAM, 0, Socket::AI_PASSIVE)
  families = Hash[*infos.collect { |af, *_| af }.uniq.zip([]).flatten]

  return TCPServer.open('0.0.0.0', 0) if families.has_key?('AF_INET')
  return TCPServer.open('::', 0) if families.has_key?('AF_INET6')
  return TCPServer.open(0)
end

.root(*paths) ⇒ Object



50
51
52
# File 'lib/jasmine/base.rb', line 50

def self.root(*paths)
  File.expand_path(File.join(File.dirname(__FILE__), *paths))
end

.runner_filepathObject



42
43
44
# File 'lib/jasmine/base.rb', line 42

def self.runner_filepath
  File.expand_path(File.join(File.dirname(__FILE__), "run_specs.rb"))
end

.runner_templateObject



46
47
48
# File 'lib/jasmine/base.rb', line 46

def self.runner_template
  File.read(File.join(File.dirname(__FILE__), "run.html.erb"))
end

.server_is_listening_on(hostname, port) ⇒ Object



22
23
24
25
26
27
28
29
30
31
# File 'lib/jasmine/base.rb', line 22

def self.server_is_listening_on(hostname, port)
  require 'socket'
  begin
    socket = TCPSocket.open(hostname, port)
  rescue Errno::ECONNREFUSED, Errno::ENETUNREACH, Errno::EAFNOSUPPORT, Errno::EADDRNOTAVAIL, Errno::EHOSTUNREACH
    return false
  end
  socket.close
  true
end

.wait_for_listener(port, hostname = "localhost", seconds_to_wait = 20) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/jasmine/base.rb', line 33

def self.wait_for_listener(port, hostname = "localhost", seconds_to_wait = 20)
  time_out_at = Time.now + seconds_to_wait
  until server_is_listening_on hostname, port
    sleep 0.1
    puts "Waiting for server on #{hostname}:#{port}..."
    raise "jasmine server didn't show up on port #{hostname}:#{port} after #{seconds_to_wait} seconds." if Time.now > time_out_at
  end
end