From b22082fc860f1ba27811963b64ce33120b841938 Mon Sep 17 00:00:00 2001 From: Sam Stephenson Date: Sun, 6 Feb 2011 20:35:37 -0600 Subject: [PATCH] Add Runtime#available? --- lib/execjs/runtime.rb | 6 ++++++ test/test_execjs.rb | 8 ++++++++ test/test_runtimes.rb | 46 ++++++++++++++++++------------------------- 3 files changed, 33 insertions(+), 27 deletions(-) diff --git a/lib/execjs/runtime.rb b/lib/execjs/runtime.rb index 9e432c0..de0b3f2 100644 --- a/lib/execjs/runtime.rb +++ b/lib/execjs/runtime.rb @@ -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) diff --git a/test/test_execjs.rb b/test/test_execjs.rb index 095181a..3ec4610 100644 --- a/test/test_execjs.rb +++ b/test/test_execjs.rb @@ -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 diff --git a/test/test_runtimes.rb b/test/test_runtimes.rb index 7633941..07c5e4c 100644 --- a/test/test_runtimes.rb +++ b/test/test_runtimes.rb @@ -39,34 +39,26 @@ module TestRuntime 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 + include TestRuntime + + 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