1
0
Fork 0
mirror of https://github.com/rails/execjs synced 2023-03-27 23:21:20 -04:00

Add Runtime#available?

This commit is contained in:
Sam Stephenson 2011-02-06 20:35:37 -06:00
parent c371fb474f
commit b22082fc86
3 changed files with 33 additions and 27 deletions

View file

@ -20,6 +20,12 @@ module ExecJS
end end
end end
def available?
command = @command.split(/\s+/).first
`which #{command}`
$? == 0
end
protected protected
def compile(source) def compile(source)
runner_source.sub('#{source}', source) runner_source.sub('#{source}', source)

View file

@ -9,4 +9,12 @@ class TestExecJS < Test::Unit::TestCase
def test_eval def test_eval
assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')") assert_equal ["red", "yellow", "blue"], ExecJS.eval("'red yellow blue'.split(' ')")
end 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 end

View file

@ -39,34 +39,26 @@ module TestRuntime
end end
end end
class TestJSCRuntime < Test::Unit::TestCase def test_runtime(name)
include TestRuntime 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 include TestRuntime
@runtime = ExecJS::Runtimes::JSC
define_method(:setup) do
instance_variable_set(:@runtime, runtime)
end
end
else
warn "#{name} runtime is unavailable, skipping"
end end
end end
class TestNodeRuntime < Test::Unit::TestCase test_runtime :JSC
include TestRuntime test_runtime :Node
test_runtime :Spidermonkey
def setup test_runtime :V8
@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