Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add Runtime#available?

  • Loading branch information...
commit b22082fc860f1ba27811963b64ce33120b841938 1 parent c371fb4
@sstephenson authored
View
6 lib/execjs/runtime.rb
@@ -20,6 +20,12 @@ def eval(source)
end
end
+ def available?
+ command = @command.split(/\s+/).first
+ `which #{command}`
+ $? == 0
+ end
+
protected
def compile(source)
runner_source.sub('#{source}', source)
View
8 test/test_execjs.rb
@@ -9,4 +9,12 @@ def test_exec
def test_eval
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
end
+
+ def test_runtime_available
+ runtime = ExecJS::Runtime.new(:command => "nonexistent")
+ assert_equal false, runtime.available?
+
+ runtime = ExecJS::Runtime.new(:command => "ruby")
+ assert_equal true, runtime.available?
+ end
end
View
44 test/test_runtimes.rb
@@ -39,34 +39,26 @@ def test_thrown_exception
end
end
-class TestJSCRuntime < Test::Unit::TestCase
- include TestRuntime
+def test_runtime(name)
+ runtime = ExecJS::Runtimes.const_get(name)
+ if runtime.available?
+ Class.new(Test::Unit::TestCase) do
+ (class << self; self end).send(:define_method, :name) do
+ "#{name}Test"
+ end
- def setup
- @runtime = ExecJS::Runtimes::JSC
- end
-end
-
-class TestNodeRuntime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::Node
- end
-end
+ include TestRuntime
-class TestSpidermonkeyRuntime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::Spidermonkey
+ define_method(:setup) do
+ instance_variable_set(:@runtime, runtime)
+ end
+ end
+ else
+ warn "#{name} runtime is unavailable, skipping"
end
end
-class TestV8Runtime < Test::Unit::TestCase
- include TestRuntime
-
- def setup
- @runtime = ExecJS::Runtimes::V8
- end
-end
+test_runtime :JSC
+test_runtime :Node
+test_runtime :Spidermonkey
+test_runtime :V8
Please sign in to comment.
Something went wrong with that request. Please try again.