Class: Opal::RSpec::RakeTask

Inherits:
Object
  • Object
show all
Includes:
Rake::DSL
Defined in:
lib/opal/rspec/rake_task.rb

Constant Summary collapse

RUNNER =
File.expand_path('../../../../vendor/spec_runner.js', __FILE__)
PORT =
9999
URL =
"http://localhost:#{PORT}/"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = 'opal:rspec', &block) ⇒ RakeTask

Returns a new instance of RakeTask.



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
# File 'lib/opal/rspec/rake_task.rb', line 81

def initialize(name = 'opal:rspec', &block)
  desc 'Run opal specs in phantomjs/node'
  task name do
    require 'rack'
    require 'webrick'

    sprockets_env = Opal::RSpec::SprocketsEnvironment.new
    app = Opal::Server.new(sprockets: sprockets_env) { |s|
      s.main = 'opal/rspec/sprockets_runner'
      s.debug = false

      block.call s, self if block
      sprockets_env.spec_pattern = self.pattern if self.pattern
      sprockets_env.spec_exclude_pattern = self.exclude_pattern
      sprockets_env.spec_files = self.files
      sprockets_env.default_path = self.default_path if self.default_path
      raise 'Cannot supply both a pattern and files!' if self.files and self.pattern
      sprockets_env.add_spec_paths_to_sprockets
    }

    # TODO: Once Opal 0.9 compatibility is established, if we're running node, add in the node stdlib requires in so RSpec can use them, also add NODE_PATH to the runner command above

    server = Thread.new do
      Thread.current.abort_on_exception = true
      Rack::Server.start(
          :app => app,
          :Port => PORT,
          :AccessLog => [],
          :Logger => WEBrick::Log.new("/dev/null"),
      )
    end

    wait_for_server
    is_phantom = runner == :phantom
    if is_phantom
      if `phantomjs -v`.nil?
        warn "Could not find phantomjs command"
        exit 1
      end
    end

    begin
      is_phantom ? launch_phantom(timeout) : launch_node(app)
    ensure
      server.kill
    end
  end
end

Instance Attribute Details

#default_pathObject

Returns the value of attribute default_path.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def default_path
  @default_path
end

#exclude_patternObject

Returns the value of attribute exclude_pattern.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def exclude_pattern
  @exclude_pattern
end

#filesObject

Returns the value of attribute files.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def files
  @files
end

#patternObject

Returns the value of attribute pattern.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def pattern
  @pattern
end

#runnerObject

Returns the value of attribute runner.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def runner
  @runner
end

#timeoutObject

Returns the value of attribute timeout.



14
15
16
# File 'lib/opal/rspec/rake_task.rb', line 14

def timeout
  @timeout
end

Instance Method Details

#get_load_asset_code(server) ⇒ Object



29
30
31
32
33
34
35
# File 'lib/opal/rspec/rake_task.rb', line 29

def get_load_asset_code(server)
  sprockets = server.sprockets
  name = server.main
  asset = sprockets[name]
  raise "Cannot find asset: #{name}" if asset.nil?
  Opal::Processor.load_asset_code(sprockets, name)
end

#launch_node(server) ⇒ Object

TODO: Avoid the Rack server and compile directly



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/opal/rspec/rake_task.rb', line 38

def launch_node(server)
  compiled = Tempfile.new 'opal_rspec.js'
  begin
    uri = URI(URL)
    Net::HTTP.start uri.hostname, uri.port do |http|
      resp = http.get File.join('/assets', server.main)
      compiled.write resp.body
      load_asset_code = get_load_asset_code server
      compiled.write load_asset_code
      compiled.close
    end
    command_line = "node #{compiled.path} 2>&1"
    puts "Running #{command_line}"
    system command_line
    exit 1 unless $?.success?
  ensure
    compiled.close unless compiled.closed?
    compiled.unlink
  end
end

#launch_phantom(timeout_value) ⇒ Object



16
17
18
19
20
21
22
23
# File 'lib/opal/rspec/rake_task.rb', line 16

def launch_phantom(timeout_value)
  command_line = %Q{phantomjs #{RUNNER} "#{URL}"#{timeout_value ? " #{timeout_value}" : ''}}
  puts "Running #{command_line}"
  system command_line
  success = $?.success?

  exit 1 unless success
end

#wait_for_serverObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/opal/rspec/rake_task.rb', line 59

def wait_for_server
  # avoid retryable dependency
  tries = 0
  up = false
  uri = URI(URL)
  while tries < 4 && !up
    tries += 1
    sleep 0.1
    begin
      # Using TCPSocket, not net/http open because executing the HTTP GET / will incur a decent delay just to check if the server is up
      # in order to better communicate to the user what is going on, save the actual HTTP request for the phantom/node run
      # the only objective here is to see if the Rack server has started
      socket = TCPSocket.new uri.hostname, uri.port
      up = true
      socket.close
    rescue Errno::ECONNREFUSED
      # server not up yet
    end
  end
  raise 'Tried 4 times to contact Rack server and not up!' unless up
end