qunited 0.4.3 → 0.4.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,18 +1,18 @@
1
1
  <!DOCTYPE html>
2
2
  <html>
3
3
  <head>
4
- <title>QUnit Test Suite</title>
5
- <%= asset_css_tag 'qunit.css' %>
4
+ <title>QUnit Test Suite</title>
5
+ <%= qunited_asset_css_tag 'qunit.css' %>
6
6
  </head>
7
7
  <body>
8
- <h1 id="qunit-header">QUnit Test Suite</h1>
9
- <h2 id="qunit-banner"></h2>
10
- <div id="qunit-testrunner-toolbar"></div>
11
- <h2 id="qunit-userAgent"></h2>
12
- <ol id="qunit-tests"></ol>
13
- <div id="qunit-fixture"></div>
8
+ <h1 id="qunit-header">QUnit Test Suite</h1>
9
+ <h2 id="qunit-banner"></h2>
10
+ <div id="qunit-testrunner-toolbar"></div>
11
+ <h2 id="qunit-userAgent"></h2>
12
+ <ol id="qunit-tests"></ol>
13
+ <div id="qunit-fixture"></div>
14
14
  </body>
15
- <%= asset_script_tag 'qunit.js' %>
15
+ <%= qunited_asset_script_tag 'qunit.js' %>
16
16
 
17
17
  <% source_files.each do |file| %>
18
18
  <%= source_script_tag file %>
@@ -1,6 +1,7 @@
1
1
  require 'webrick'
2
2
  require 'erb'
3
3
  require 'pathname'
4
+ require 'tempfile'
4
5
 
5
6
  module QUnited
6
7
  class Server
@@ -13,6 +14,8 @@ module QUnited
13
14
 
14
15
  QUNITED_ASSET_FILE_PREFIX = 'qunited-asset'
15
16
 
17
+ COFFEESCRIPT_EXTENSIONS = ['coffee', 'cs']
18
+
16
19
  attr_accessor :source_files, :test_files
17
20
 
18
21
  def initialize(opts={})
@@ -32,40 +35,42 @@ module QUnited
32
35
  end
33
36
  end
34
37
 
35
- @server = ::WEBrick::HTTPServer.new(server_options)
38
+ @server = create_server(server_options)
39
+ end
40
+
41
+ def start
42
+ ['INT', 'TERM'].each do |signal|
43
+ trap(signal) { @server.shutdown }
44
+ end
45
+
46
+ $stderr.puts "Serving QUnit test suite on port #{@port}\nCtrl-C to shutdown"
47
+ @server.start
48
+ end
36
49
 
37
- @server.mount_proc '/' do |request, response|
50
+ private
51
+
52
+ def create_server(options)
53
+ server = ::WEBrick::HTTPServer.new(options)
54
+
55
+ server.mount_proc '/' do |request, response|
38
56
  response.status = 200
39
57
 
40
58
  case request.path
41
59
  when /^\/#{SOURCE_FILE_PREFIX}\/(.*)/, /^\/#{TEST_FILE_PREFIX}\/(.*)/
42
60
  response['Content-Type'] = 'application/javascript'
43
- response.body = IO.read($1)
61
+ response.body = js_file_contents($1)
44
62
  when /^\/#{QUNITED_ASSET_FILE_PREFIX}\/(.*)/
45
63
  filename = $1
46
64
  response['Content-Type'] = (filename =~ /\.js$/) ? 'application/javascript' : 'text/css'
47
- response.body = IO.read(asset_path filename)
65
+ response.body = IO.read(qunited_asset_path filename)
48
66
  else
49
67
  response['Content-Type'] = 'text/html'
50
- test_suite_template = ::ERB.new(IO.read(asset_path 'test_suite.html.erb'))
68
+ test_suite_template = ::ERB.new(IO.read(qunited_asset_path 'test_suite.html.erb'))
51
69
  response.body = test_suite_template.result(binding)
52
70
  end
53
71
  end
54
72
 
55
- ['INT', 'TERM'].each do |signal|
56
- trap(signal) { @server.shutdown }
57
- end
58
- end
59
-
60
- def start
61
- $stderr.puts "Serving QUnit test suite on port #{@port}\nCtrl-C to shutdown"
62
- @server.start
63
- end
64
-
65
- private
66
-
67
- def asset_path(filename)
68
- File.join(ASSETS_PATH, filename)
73
+ server
69
74
  end
70
75
 
71
76
  def source_script_tag(file_path)
@@ -76,16 +81,81 @@ module QUnited
76
81
  script_tag "#{TEST_FILE_PREFIX}/#{file_path}"
77
82
  end
78
83
 
79
- def asset_script_tag(filename)
84
+ def qunited_asset_script_tag(filename)
80
85
  script_tag "#{QUNITED_ASSET_FILE_PREFIX}/#{filename}"
81
86
  end
82
87
 
83
- def asset_css_tag(filename)
88
+ def qunited_asset_css_tag(filename)
84
89
  %{<link rel="stylesheet" type="text/css" href="#{QUNITED_ASSET_FILE_PREFIX}/#{filename}">}
85
90
  end
86
91
 
92
+ def qunited_asset_path(filename)
93
+ File.join(ASSETS_PATH, filename)
94
+ end
95
+
87
96
  def script_tag(src)
88
97
  %{<script type="text/javascript" src="#{src}"></script>}
89
98
  end
99
+
100
+ def js_file_contents(file)
101
+ if COFFEESCRIPT_EXTENSIONS.include? File.extname(file).sub(/^\./, '')
102
+ compile_coffeescript(file)
103
+ else
104
+ IO.read(file)
105
+ end
106
+ end
107
+
108
+ # Compile the CoffeeScript file with the given filename to JavaScript. Returns the compiled
109
+ # code as a string. Returns failing test JavaScript if CoffeeScript support is not installed.
110
+ # Also adds a failing test on compilation failure.
111
+ def compile_coffeescript(file)
112
+ begin
113
+ require 'coffee-script'
114
+ rescue LoadError
115
+ $stderr.puts <<-ERROR_MSG
116
+ You must install an additional gem to use CoffeeScript source or test files.
117
+ Run the following command (with sudo if necessary): gem install coffee-script
118
+ ERROR_MSG
119
+
120
+ return <<-ERROR_MSG_SCRIPT
121
+ module('CoffeeScript');
122
+ test('coffee-script gem must be installed to compile this file: #{file}', function() {
123
+ ok(false, 'Install CoffeeScript support with `gem install coffee-script`')
124
+ });
125
+ ERROR_MSG_SCRIPT
126
+ end
127
+
128
+ previously_compiled_file = compiled_coffeescript_files[file]
129
+ if previously_compiled_file && File.mtime(file) < File.mtime(previously_compiled_file.path)
130
+ return File.read previously_compiled_file.path
131
+ end
132
+
133
+ compiled_js_file = Tempfile.new(["compiled_#{File.basename(file).gsub('.', '_')}", '.js'])
134
+
135
+ begin
136
+ contents = CoffeeScript.compile(File.read(file))
137
+ rescue => e
138
+ return <<-COMPILATION_ERROR_SCRIPT
139
+ module('CoffeeScript');
140
+ test('CoffeeScript compilation error', function() {
141
+ ok(false, "#{e.message.gsub('"', '\"')}")
142
+ });
143
+ COMPILATION_ERROR_SCRIPT
144
+ end
145
+
146
+ compiled_js_file.write contents
147
+ compiled_js_file.close
148
+
149
+ compiled_coffeescript_files[file] = compiled_js_file
150
+
151
+ contents
152
+ end
153
+
154
+ # Hash that maps CoffeeScript file paths to temporary compiled JavaScript files. This is
155
+ # used partially because we need to keep around references to the temporary files or else
156
+ # they could be deleted.
157
+ def compiled_coffeescript_files
158
+ @compiled_coffeescript_files ||= {}
159
+ end
90
160
  end
91
161
  end
@@ -1,3 +1,3 @@
1
1
  module QUnited
2
- VERSION = '0.4.3'
2
+ VERSION = '0.4.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: qunited
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-04-12 00:00:00.000000000 Z
12
+ date: 2013-04-20 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: QUnited runs headless QUnit tests as part of your normal build
15
15
  email: