mirror of
https://github.com/rails/execjs
synced 2023-03-27 23:21:20 -04:00
Add Runtime#available?
This commit is contained in:
parent
c371fb474f
commit
b22082fc86
3 changed files with 33 additions and 27 deletions
|
@ -20,6 +20,12 @@ module ExecJS
|
|||
end
|
||||
end
|
||||
|
||||
def available?
|
||||
command = @command.split(/\s+/).first
|
||||
`which #{command}`
|
||||
$? == 0
|
||||
end
|
||||
|
||||
protected
|
||||
def compile(source)
|
||||
runner_source.sub('#{source}', source)
|
||||
|
|
|
@ -9,4 +9,12 @@ class TestExecJS < Test::Unit::TestCase
|
|||
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
|
||||
|
|
|
@ -39,34 +39,26 @@ module TestRuntime
|
|||
end
|
||||
end
|
||||
|
||||
class TestJSCRuntime < Test::Unit::TestCase
|
||||
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
|
||||
|
||||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::JSC
|
||||
define_method(:setup) do
|
||||
instance_variable_set(:@runtime, runtime)
|
||||
end
|
||||
end
|
||||
else
|
||||
warn "#{name} runtime is unavailable, skipping"
|
||||
end
|
||||
end
|
||||
|
||||
class TestNodeRuntime < Test::Unit::TestCase
|
||||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::Node
|
||||
end
|
||||
end
|
||||
|
||||
class TestSpidermonkeyRuntime < Test::Unit::TestCase
|
||||
include TestRuntime
|
||||
|
||||
def setup
|
||||
@runtime = ExecJS::Runtimes::Spidermonkey
|
||||
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
|
||||
|
|
Loading…
Reference in a new issue