Skip to content

Commit e2f8d37

Browse files
committedOct 23, 2014
Merge pull request #634 from opal/node-mspec-runner
Node mspec runner
2 parents b60c4bd + 79dc800 commit e2f8d37

File tree

18 files changed

+132
-50
lines changed

18 files changed

+132
-50
lines changed
 

‎.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ build/
88
/cdn
99
/node_modules
1010
/gh-pages
11+
/tmp

‎Rakefile

+36-1
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,44 @@ MSpec::Opal::RakeTask.new(:mspec) do |config|
1818
config.basedir = ENV['MSPEC_BASEDIR'] if ENV['MSPEC_BASEDIR']
1919
end
2020

21-
task :default => [:rspec, :mspec]
21+
task :default => [:rspec, :mspec, :mspec_node]
2222

2323

24+
task :mspec_node do
25+
rubyspecs = File.read('spec/rubyspecs').lines.reject do |l|
26+
l.strip!; l.start_with?('#') || l.empty?
27+
end.flat_map do |path|
28+
path = "spec/#{path}"
29+
File.directory?(path) ? Dir[path+'/*.rb'] : "#{path}.rb"
30+
end
31+
32+
filters = Dir['spec/filters/**/*.rb']
33+
shared = Dir['spec/{opal,lib/parser}/**/*_spec.rb'] + ['spec/lib/lexer_spec.rb']
34+
35+
p [rubyspecs.size, :rubyspecs]
36+
p [filters.size, :filters]
37+
p [shared.size, :shared]
38+
39+
specs = []
40+
specs += filters
41+
specs += rubyspecs
42+
specs += shared
43+
44+
requires = specs.map{|s| "require '#{s.sub(/^spec\//,'')}'"}
45+
filename = 'tmp/mspec_node.rb'
46+
mkdir_p File.dirname(filename)
47+
File.write filename, <<-RUBY
48+
require 'spec_helper'
49+
#{requires.join(" \n")}
50+
OSpecRunner.main.did_finish
51+
RUBY
52+
53+
stubs = " -smspec/helpers/tmp -smspec/helpers/environment -smspec/guards/block_device -smspec/guards/endian"
54+
55+
exec 'RUBYOPT="-rbundler/setup -rmspec/opal/special_calls" '\
56+
"bin/opal -Ispec -Ilib -gmspec #{stubs} -rnodejs -Dwarning -A #{filename}"
57+
end
58+
2459
require 'opal/version'
2560
desc <<-DESC
2661
Build *corelib* and *stdlib* to "build/"

‎lib/mspec/opal/rake_task.rb

+7-30
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,6 @@
1-
require 'opal/nodes'
2-
class Opal::Nodes::CallNode
3-
# Rubyspec uses this call to load in language specific features at runtime.
4-
# We can't do this at runtime, so handle it during compilation
5-
add_special :language_version do
6-
if meth == :language_version and scope.top?
7-
lang_type = arglist[2][1]
8-
target = "corelib/language/versions/#{lang_type}_1.9"
9-
10-
if File.exist?(target)
11-
compiler.requires << target
12-
end
13-
14-
push fragment("nil")
15-
end
16-
end
17-
18-
add_special :not_supported_on do
19-
unless meth == :not_supported_on and arglist[1][1] == :opal
20-
compile_default!
21-
end
22-
end
23-
end
24-
25-
261
require 'rack'
272
require 'webrick'
3+
require 'mspec/opal/special_calls'
284

295
module MSpec
306
module Opal
@@ -205,16 +181,17 @@ def files_to_run(pattern=nil)
205181

206182
if pattern
207183
# add custom opal specs from spec/
208-
add_files paths_from_glob(pattern) & rubyspec_white_list, :rubyspec_custom_pattern
209-
add_files paths_from_glob(pattern).grep(/(?!spec\/(corelib|stdlib)\/)/), :other_custom_pattern
184+
add_files paths_from_glob(pattern) & rubyspec_white_list, :rubyspec_custom
185+
add_files paths_from_glob(pattern).grep(/(?!spec\/(corelib|stdlib)\/)/), :other_custom
210186

211187
else
212188
# add opal specific specs
213-
add_files paths_from_glob("#{basedir}/opal/**/*_spec.rb"), 'opal/*'
214-
add_files paths_from_glob("#{basedir}/lib/{lexer_spec.rb,parser/**/*_spec.rb}"), 'lib/{lexer,parser}'
189+
add_files paths_from_glob("#{basedir}/opal/**/*_spec.rb"), :shared
190+
add_files paths_from_glob("#{basedir}/lib/lexer_spec.rb"), :lexer
191+
add_files paths_from_glob("#{basedir}/lib/parser/**/*_spec.rb"), :parser
215192

216193
# add any rubyspecs we want to run (defined in spec/rubyspecs)
217-
add_files rubyspec_white_list, :rubyspec_white_list
194+
add_files rubyspec_white_list, :rubyspecs
218195
end
219196
end
220197

‎lib/mspec/opal/runner.rb

+32
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,38 @@ def after(state)
144144
end
145145
end
146146

147+
class NodeJSFormatter < BrowserFormatter
148+
def green(str)
149+
`process.stdout.write("\033[32m"+#{str}+"\033[0m")`
150+
end
151+
152+
def red(str)
153+
`process.stdout.write("\033[31m"+#{str}+"\033[0m")`
154+
end
155+
156+
def log(str)
157+
puts str
158+
end
159+
160+
def after(state)
161+
super
162+
unless exception?
163+
green('.')
164+
else
165+
red(failure? ? 'F' : 'E')
166+
end
167+
end
168+
169+
def finish_with_code(code)
170+
`global.OPAL_SPEC_CODE = code;`
171+
end
172+
173+
def finish
174+
super
175+
puts "\n\n"
176+
end
177+
end
178+
147179
class PhantomDebugFormatter < PhantomFormatter
148180
def after(state = nil)
149181
(@exception && state) ? red(state.description) : green(state.description)

‎lib/mspec/opal/special_calls.rb

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
require 'opal/nodes'
2+
class Opal::Nodes::CallNode
3+
# Rubyspec uses this call to load in language specific features at runtime.
4+
# We can't do this at runtime, so handle it during compilation
5+
add_special :language_version do
6+
if meth == :language_version and scope.top?
7+
lang_type = arglist[2][1]
8+
target = "corelib/language/versions/#{lang_type}_1.9"
9+
10+
if File.exist?(target)
11+
compiler.requires << target
12+
end
13+
14+
push fragment("nil")
15+
end
16+
end
17+
18+
add_special :not_supported_on do
19+
unless meth == :not_supported_on and arglist[1][1] == :opal
20+
compile_default!
21+
end
22+
end
23+
end
24+
25+

‎lib/opal/cli.rb

+5-5
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def initialize options = nil
4848
@filename = options.fetch(:filename) { @file && @file.path }; options.delete(:filename)
4949
@skip_opal_require = options.delete(:skip_opal_require)
5050
@compiler_options = Hash[
51-
*processor_option_names.map do |option|
51+
*compiler_option_names.map do |option|
5252
key = option.to_sym
5353
next unless options.has_key? key
5454
value = options.delete(key)
@@ -94,7 +94,7 @@ def compiled_source
9494

9595
# REQUIRES: -r
9696
requires.each do |local_require|
97-
builder.build_str("require #{local_require.inspect}", 'require')
97+
builder.build(local_require)
9898
end
9999

100100
if evals.any?
@@ -129,10 +129,10 @@ def map
129129
compiler.source_map
130130
end
131131

132-
def processor_option_names
132+
def compiler_option_names
133133
%w[
134-
method_missing_enabled
135-
arity_check_enabled
134+
method_missing
135+
arity_check
136136
dynamic_require_severity
137137
source_map_enabled
138138
irb_enabled

‎lib/opal/compiler.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def error(msg, line = nil)
108108
# method simply appends the filename and curent line number onto
109109
# the message and issues a warning.
110110
def warning(msg, line = nil)
111-
warn "WARNING: #{msg} :#{file}:#{line}"
111+
warn "WARNING: #{msg} -- #{file}:#{line}"
112112
end
113113

114114
# Instances of `Scope` can use this to determine the current

‎lib/opal/nodes/top.rb

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ def compile
3838

3939
def opening
4040
if compiler.requirable?
41-
line "Opal.modules[#{compiler.file.inspect}] = function($opal) {"
41+
path = Pathname(compiler.file).cleanpath.to_s
42+
line "Opal.modules[#{path.inspect}] = function($opal) {"
4243
else
4344
line "(function($opal) {"
4445
end

‎opal/corelib/runtime.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1072,6 +1072,7 @@
10721072

10731073
for (var i = 0, ii = parts.length; i < ii; i++) {
10741074
part = parts[i];
1075+
if (part == '') continue;
10751076
(part === '..') ? new_parts.pop() : new_parts.push(part)
10761077
}
10771078

@@ -1094,7 +1095,7 @@
10941095
Opal.LoadError ? Opal.LoadError.$new(message) : function(){throw message}();
10951096
}
10961097
else if (severity === "warning") {
1097-
Opal.gvars.stderr.$puts('WARNING: LoadError: ' + message);
1098+
console.warn('WARNING: LoadError: ' + message);
10981099
}
10991100
}
11001101

‎spec/spec_helper.rb

+10-5
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,16 @@ def at_exit(&block)
2626
end
2727
end
2828

29-
if `!!window.OPAL_SPEC_PHANTOM`
30-
require 'phantomjs'
31-
formatter_class = PhantomFormatter
32-
else
33-
formatter_class = BrowserFormatter
29+
case
30+
when defined?(NodeJS)
31+
formatter_class = NodeJSFormatter
32+
when `(typeof(window) !== 'undefined')`
33+
if `!!window.OPAL_SPEC_PHANTOM`
34+
require 'phantomjs'
35+
formatter_class = PhantomFormatter
36+
else
37+
formatter_class = BrowserFormatter
38+
end
3439
end
3540

3641
# Uncomment the following to see example titles when they're executed.

‎stdlib/fileutils.rb

Whitespace-only changes.

‎stdlib/iconv.rb

Whitespace-only changes.

‎stdlib/nodejs.rb

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
module NodeJS
2+
end
3+
14
require 'nodejs/runtime'
25
require 'nodejs/file'
36
require 'nodejs/dir'

‎stdlib/nodejs/dir.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Dir
2-
@__glob__ = NodeJS.require :glob
2+
@__glob__ = node_require :glob
33
`var __glob__ = #{@__glob__}`
44

55
def self.[] glob

‎stdlib/nodejs/file.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ class File < IO
22
include ::IO::Writable
33
include ::IO::Readable
44

5-
@__fs__ = NodeJS.require :fs
6-
@__path__ = NodeJS.require :path
5+
@__fs__ = node_require :fs
6+
@__path__ = node_require :path
77
`var __fs__ = #{@__fs__}`
88
`var __path__ = #{@__path__}`
99

‎stdlib/nodejs/io.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
$stdout.write_proc = -> (string) {`process.stdout.write(#{string})`}
1+
$stdout.write_proc = -> (string) {`process.stdout.write(string)`}
22
$stderr.write_proc = -> (string) {`process.stderr.write(string)`}

‎stdlib/nodejs/runtime.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
}).call(this);
1818
}
1919

20-
module NodeJS
21-
def self.require name
20+
21+
module Kernel
22+
def node_require name
2223
`OpalNode.node_require(#{name})`
2324
end
2425
end

‎stdlib/yaml.rb

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require 'nodejs/yaml' if defined? NodeJS

0 commit comments

Comments
 (0)
Please sign in to comment.