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.



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

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
    }

    Opal::Config.arity_check_enabled = arity_checking?

    # 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
      version_output = begin
        `phantomjs -v`.strip
      rescue
        warn 'Could not find phantomjs command on path!'
        exit 1
      end
      if version_output.to_f < 2
        warn "PhantomJS >= 2.0 is required but version #{version_output} is installed!"
        exit 1
      end
    end

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

Instance Attribute Details

#arity_checkingObject

Returns the value of attribute arity_checking.



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

def arity_checking
  @arity_checking
end

#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

#arity_checking?Boolean

Returns:

  • (Boolean)


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

def arity_checking?
  setting = @arity_checking || :enabled
  setting == :enabled
end

#get_load_asset_code(server) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/opal/rspec/rake_task.rb', line 34

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

#launch_node(server) ⇒ Object

TODO: Avoid the Rack server and compile directly



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/opal/rspec/rake_task.rb', line 43

def launch_node(server)
  compiled = Tempfile.new 'opal_rspec.js'
  begin
    require 'net/http'
    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



21
22
23
24
25
26
27
28
# File 'lib/opal/rspec/rake_task.rb', line 21

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



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/opal/rspec/rake_task.rb', line 65

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