class QUnited::Server
Constants
- ASSETS_PATH
- COFFEESCRIPT_EXTENSIONS
- DEFAULT_PORT
- QUNITED_ASSET_FILE_PREFIX
- SOURCE_FILE_PREFIX
- TEST_FILE_PREFIX
Attributes
fixture_files[RW]
source_files[RW]
test_files[RW]
Public Class Methods
new(opts={})
click to toggle source
# File lib/qunited/server.rb, line 21 def initialize(opts={}) @source_files = opts[:source_files] @test_files = opts[:test_files] @fixture_files = opts[:fixture_files] @port = opts[:port] || DEFAULT_PORT server_options = { :Port => @port } unless opts[:verbose] server_options[:AccessLog] = [] dev_null = '/dev/null' if File.exist?(dev_null) && File.writable?(dev_null) server_options[:Logger] = WEBrick::Log.new(dev_null) end end @server = create_server(server_options) end
Public Instance Methods
start()
click to toggle source
# File lib/qunited/server.rb, line 43 def start ['INT', 'TERM'].each do |signal| trap(signal) { @server.shutdown } end $stderr.puts "Serving QUnit test suite on port #{@port}\nCtrl-C to shutdown" @server.start end
Private Instance Methods
compile_coffeescript(file)
click to toggle source
Compile the CoffeeScript file with the given filename to JavaScript. Returns the compiled code as a string. Returns failing test JavaScript if CoffeeScript support is not installed. Also adds a failing test on compilation failure.
# File lib/qunited/server.rb, line 117 def compile_coffeescript(file) begin require 'coffee-script' rescue LoadError $stderr.puts <<-ERROR_MSG You must install an additional gem to use CoffeeScript source or test files. Run the following command (with sudo if necessary): gem install coffee-script ERROR_MSG return <<-ERROR_MSG_SCRIPT module('CoffeeScript'); test('coffee-script gem must be installed to compile this file: #{file}', function() { ok(false, 'Install CoffeeScript support with `gem install coffee-script`') }); ERROR_MSG_SCRIPT end previously_compiled_file = compiled_coffeescript_files[file] if previously_compiled_file && File.mtime(file) < File.mtime(previously_compiled_file.path) return File.read previously_compiled_file.path end compiled_js_file = Tempfile.new(["compiled_#{File.basename(file).gsub('.', '_')}", '.js']) begin contents = CoffeeScript.compile(File.read(file)) rescue => e return <<-COMPILATION_ERROR_SCRIPT module('CoffeeScript'); test('CoffeeScript compilation error', function() { ok(false, "#{e.message.gsub('"', '\"')}") }); COMPILATION_ERROR_SCRIPT end compiled_js_file.write contents compiled_js_file.close compiled_coffeescript_files[file] = compiled_js_file contents end
compiled_coffeescript_files()
click to toggle source
Hash that maps CoffeeScript file paths to temporary compiled JavaScript files. This is used partially because we need to keep around references to the temporary files or else they could be deleted.
# File lib/qunited/server.rb, line 163 def compiled_coffeescript_files @compiled_coffeescript_files ||= {} end
create_server(options)
click to toggle source
# File lib/qunited/server.rb, line 54 def create_server(options) server = ::WEBrick::HTTPServer.new(options) server.mount_proc '/' do |request, response| response.status = 200 case request.path when /^\/#{SOURCE_FILE_PREFIX}\/(.*)/, /^\/#{TEST_FILE_PREFIX}\/(.*)/ response['Content-Type'] = 'application/javascript' response.body = js_file_contents($1) when /^\/#{QUNITED_ASSET_FILE_PREFIX}\/(.*)/ filename = $1 response['Content-Type'] = (filename =~ /\.js$/) ? 'application/javascript' : 'text/css' response.body = IO.read(qunited_asset_path filename) else response['Content-Type'] = 'text/html' test_suite_template = ::ERB.new(IO.read(qunited_asset_path 'test_suite.html.erb')) response.body = test_suite_template.result(binding) end end server end
include_fixture_file(file_path)
click to toggle source
# File lib/qunited/server.rb, line 86 def include_fixture_file(file_path) IO.read(file_path) end
js_file_contents(file)
click to toggle source
# File lib/qunited/server.rb, line 106 def js_file_contents(file) if COFFEESCRIPT_EXTENSIONS.include? File.extname(file).sub(/^\./, '') compile_coffeescript(file) else IO.read(file) end end
qunited_asset_css_tag(filename)
click to toggle source
# File lib/qunited/server.rb, line 94 def qunited_asset_css_tag(filename) %{<link rel="stylesheet" type="text/css" href="#{QUNITED_ASSET_FILE_PREFIX}/#{filename}">} end
qunited_asset_path(filename)
click to toggle source
# File lib/qunited/server.rb, line 98 def qunited_asset_path(filename) File.join(ASSETS_PATH, filename) end
qunited_asset_script_tag(filename)
click to toggle source
# File lib/qunited/server.rb, line 90 def qunited_asset_script_tag(filename) script_tag "#{QUNITED_ASSET_FILE_PREFIX}/#{filename}" end
script_tag(src)
click to toggle source
# File lib/qunited/server.rb, line 102 def script_tag(src) %{<script type="text/javascript" src="#{src}"></script>} end
source_script_tag(file_path)
click to toggle source
# File lib/qunited/server.rb, line 78 def source_script_tag(file_path) script_tag "#{SOURCE_FILE_PREFIX}/#{file_path}" end
test_script_tag(file_path)
click to toggle source
# File lib/qunited/server.rb, line 82 def test_script_tag(file_path) script_tag "#{TEST_FILE_PREFIX}/#{file_path}" end