From 6d3cf50a9c20e1609cc4ac4ef2ba2ee8bcc2526a Mon Sep 17 00:00:00 2001 From: Adam Pohorecki Date: Wed, 8 Jun 2011 01:24:07 +0200 Subject: [PATCH] Split Runner into RuntimeConfig and a bunch of commands. Reorganized application to use manual dependency injection. --- lib/js_test_driver.rb | 30 +++- lib/js_test_driver/application.rb | 47 ++++++ lib/js_test_driver/cli/capture_browsers.rb | 34 ++++ lib/js_test_driver/cli/run.rb | 54 ++++++ lib/js_test_driver/cli/run_tests.rb | 22 +++ lib/js_test_driver/cli/start_server.rb | 20 +++ .../commands/capture_browsers_command.rb | 22 --- .../commands/generate_coverage_report.rb | 19 ++- .../commands/jstd_jar_command.rb | 21 ++- lib/js_test_driver/runner.rb | 154 +----------------- lib/js_test_driver/runtime_config.rb | 105 ++++++++++++ test/integration/sample_test.rb | 6 +- test/test_helper.rb | 49 +++++- test/unit/capture_browsers_test.rb | 30 ++++ test/unit/run_test.rb | 29 ++++ test/unit/run_tests_test.rb | 20 +++ test/unit/runner_test.rb | 109 ------------- test/unit/runtime_config_test.rb | 21 +++ test/unit/start_server_test.rb | 15 ++ 19 files changed, 500 insertions(+), 307 deletions(-) create mode 100644 lib/js_test_driver/application.rb create mode 100644 lib/js_test_driver/cli/capture_browsers.rb create mode 100644 lib/js_test_driver/cli/run.rb create mode 100644 lib/js_test_driver/cli/run_tests.rb create mode 100644 lib/js_test_driver/cli/start_server.rb delete mode 100644 lib/js_test_driver/commands/capture_browsers_command.rb create mode 100644 lib/js_test_driver/runtime_config.rb create mode 100644 test/unit/capture_browsers_test.rb create mode 100644 test/unit/run_test.rb create mode 100644 test/unit/run_tests_test.rb delete mode 100644 test/unit/runner_test.rb create mode 100644 test/unit/runtime_config_test.rb create mode 100644 test/unit/start_server_test.rb diff --git a/lib/js_test_driver.rb b/lib/js_test_driver.rb index e7b3b76..7507b07 100644 --- a/lib/js_test_driver.rb +++ b/lib/js_test_driver.rb @@ -1,11 +1,25 @@ require 'json' require 'fileutils' -require File.expand_path(File.join('..', 'js_test_driver', 'config'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'runner'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'html_fixture'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'missing_dependency_error'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'commands', 'base_command'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'commands', 'jstd_jar_command'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'commands', 'capture_browsers_command'), __FILE__) -require File.expand_path(File.join('..', 'js_test_driver', 'commands', 'generate_coverage_report'), __FILE__) +module JsTestDriver + autoload :Application, 'js_test_driver/application' + autoload :Config, 'js_test_driver/config' + autoload :HtmlFixture, 'js_test_driver/html_fixture' + autoload :MissingDependencyError, 'js_test_driver/missing_dependency_error' + autoload :Runner, 'js_test_driver/runner' + autoload :RuntimeConfig, 'js_test_driver/runtime_config' + autoload :VERSION, 'js_test_driver/version' + + module CLI + autoload :CaptureBrowsers, 'js_test_driver/cli/capture_browsers' + autoload :Run, 'js_test_driver/cli/run' + autoload :RunTests, 'js_test_driver/cli/run_tests' + autoload :StartServer, 'js_test_driver/cli/start_server' + end + + module Commands + autoload :BaseCommand, 'js_test_driver/commands/base_command' + autoload :GenerateCoverageReport, 'js_test_driver/commands/generate_coverage_report' + autoload :JstdJarCommand, 'js_test_driver/commands/jstd_jar_command' + end +end diff --git a/lib/js_test_driver/application.rb b/lib/js_test_driver/application.rb new file mode 100644 index 0000000..f02003e --- /dev/null +++ b/lib/js_test_driver/application.rb @@ -0,0 +1,47 @@ +module JsTestDriver + + class Application + + attr_reader :runtime_config + + def initialize(opts = {}) + @runtime_config = JsTestDriver::RuntimeConfig.new(opts) + end + + def config + runtime_config.config + end + + def start_server(opts = {}) + JsTestDriver::CLI::StartServer.new(jstd_jar_command, runner).run(opts) + end + + def capture_browsers(opts = {}) + JsTestDriver::CLI::CaptureBrowsers.new(config, runner).run(opts) + end + + def run_tests(opts = {}) + JsTestDriver::CLI::RunTests.new(jstd_jar_command, runner).run(opts) + end + + def run(opts = {}) + JsTestDriver::CLI::Run.new(jstd_jar_command, runner, config, coverage_command).run(opts) + end + + protected + + def runner + JsTestDriver::Runner.new + end + + def jstd_jar_command + JsTestDriver::Commands::JstdJarCommand.new(runtime_config, config) + end + + def coverage_command + JsTestDriver::Commands::GenerateCoverageReport.new(runtime_config) + end + + end + +end diff --git a/lib/js_test_driver/cli/capture_browsers.rb b/lib/js_test_driver/cli/capture_browsers.rb new file mode 100644 index 0000000..f4662dc --- /dev/null +++ b/lib/js_test_driver/cli/capture_browsers.rb @@ -0,0 +1,34 @@ +module JsTestDriver + module CLI + + class CaptureBrowsers + + attr_reader :config, :runner + + def initialize(config, runner) + @config = config + @runner = runner + end + + def run(opts = {}) + browsers = opts[:browsers] || '' + browsers = browsers.split(',') + browsers = config.browsers if browsers.empty? + + url = config.server + "/capture?strict" + + browsers.each do |name| + runner.runbg(browser_command(name, url)) + end + end + + protected + + def browser_command(name, url) + JsTestDriver::Commands::BaseCommand.new(name).arg(url).to_s + end + + end + + end +end diff --git a/lib/js_test_driver/cli/run.rb b/lib/js_test_driver/cli/run.rb new file mode 100644 index 0000000..f3e9971 --- /dev/null +++ b/lib/js_test_driver/cli/run.rb @@ -0,0 +1,54 @@ +module JsTestDriver + module CLI + + class Run + + attr_reader :command, :runner, :config, :coverage_command + + def initialize(jstd_jar_command, runner, config, coverage_command) + @command = jstd_jar_command + @runner = runner + @config = config + @coverage_command = coverage_command + end + + def run(opts = {}) + result = run_jstd_command(opts) + generate_coverage_report(opts) + return result + end + + protected + + def run_jstd_command(opts) + command + .start_server + .with_config + .capture_browsers(opts[:browsers]) + .run_tests(opts[:tests]) + + command.output_directory(opts[:output_xml_path]) if opts[:output_xml_path] + command.capture_console if opts[:capture_console] + command.verbose if opts[:verbose] + + return runner.run(command.to_s) + end + + def generate_coverage_report(opts) + return unless config.measure_coverage? && opts[:output_xml_path] + + if genhtml_installed? + runner.run(coverage_command.output_path(opts[:output_xml_path]).to_s) + else + puts "Could not find genhtml. You must install lcov (sudo apt-get install lcov)" + end + end + + def genhtml_installed? + !%x[which genhtml].strip.empty? + end + + end + + end +end diff --git a/lib/js_test_driver/cli/run_tests.rb b/lib/js_test_driver/cli/run_tests.rb new file mode 100644 index 0000000..26a78ef --- /dev/null +++ b/lib/js_test_driver/cli/run_tests.rb @@ -0,0 +1,22 @@ +module JsTestDriver + module CLI + + class RunTests + + attr_reader :jstd_jar_command, :runner + + def initialize(jstd_jar_command, runner) + @jstd_jar_command = jstd_jar_command + @runner = runner + end + + def run(opts = {}) + jstd_jar_command.with_config.run_tests(opts[:tests]) + + runner.run(jstd_jar_command.to_s) + end + + end + + end +end diff --git a/lib/js_test_driver/cli/start_server.rb b/lib/js_test_driver/cli/start_server.rb new file mode 100644 index 0000000..08987d1 --- /dev/null +++ b/lib/js_test_driver/cli/start_server.rb @@ -0,0 +1,20 @@ +module JsTestDriver + module CLI + + class StartServer + + attr_reader :jstd_jar_command, :runner + + def initialize(jstd_jar_command, runner) + @jstd_jar_command = jstd_jar_command + @runner = runner + end + + def run(opts = {}) + runner.run(jstd_jar_command.start_server.to_s) + end + + end + + end +end diff --git a/lib/js_test_driver/commands/capture_browsers_command.rb b/lib/js_test_driver/commands/capture_browsers_command.rb deleted file mode 100644 index 91b7c68..0000000 --- a/lib/js_test_driver/commands/capture_browsers_command.rb +++ /dev/null @@ -1,22 +0,0 @@ -module JsTestDriver - module Commands - - class CaptureBrowsersCommand - - include Enumerable - - def initialize(browsers, url) - @browsers = browsers - @url = url - end - - def each(&block) - @browsers.each do |browser| - yield(JsTestDriver::Commands::BaseCommand.new(browser).arg(@url).to_s) - end - end - - end - - end -end diff --git a/lib/js_test_driver/commands/generate_coverage_report.rb b/lib/js_test_driver/commands/generate_coverage_report.rb index a6647f5..b4dd453 100644 --- a/lib/js_test_driver/commands/generate_coverage_report.rb +++ b/lib/js_test_driver/commands/generate_coverage_report.rb @@ -3,23 +3,26 @@ module Commands class GenerateCoverageReport < BaseCommand - attr_reader :runtime_config, :output_path + attr_reader :runtime_config - def initialize(runtime_config, output_path) + def initialize(runtime_config) super('genhtml') - @runtime_config = runtime_config - @output_path = File.expand_path(output_path) + end - option('-o', output_dir) - arg(data_file_path) + def output_path(value) + value = File.expand_path(value) + option('-o', output_dir(value)) + arg(data_file_path(value)) end - def output_dir + protected + + def output_dir(output_path) File.join(output_path, 'coverage') end - def data_file_path + def data_file_path(output_path) config_path = File.basename(runtime_config.config_yml_path) file_name = config_path + '-coverage.dat' File.join(output_path, file_name) diff --git a/lib/js_test_driver/commands/jstd_jar_command.rb b/lib/js_test_driver/commands/jstd_jar_command.rb index aab70e0..bf2bdcc 100644 --- a/lib/js_test_driver/commands/jstd_jar_command.rb +++ b/lib/js_test_driver/commands/jstd_jar_command.rb @@ -3,22 +3,22 @@ module Commands class JstdJarCommand < BaseCommand - attr_reader :runtime_config + attr_reader :runtime_config, :config - def initialize(runtime_config) + def initialize(runtime_config, config) super('java') @runtime_config = runtime_config + @config = config option('-jar', runtime_config.jar_path) end def with_config - runtime_config.config.save(runtime_config.config_yml_path) return option('--config', runtime_config.config_yml_path) end def start_server - return option('--port', runtime_config.config.port) + return option('--port', config.port) end def run_tests(tests = nil) @@ -26,7 +26,7 @@ def run_tests(tests = nil) end def capture_browsers(browsers = nil) - browsers ||= runtime_config.config.browsers.join(',') + browsers ||= config.browsers.join(',') raise ArgumentError.new("No browsers defined!") if browsers == "" return option('--browser', browsers) end @@ -41,6 +41,17 @@ def capture_console return option('--captureConsole') end + def verbose + return option('--verbose') + end + + def runner_mode(mode) + return option('--runnerMode', mode) + end + + def browser_timeout(timeout) + return option('--browserTimeout', timeout) + end end end diff --git a/lib/js_test_driver/runner.rb b/lib/js_test_driver/runner.rb index 4a20c31..05f7d7a 100644 --- a/lib/js_test_driver/runner.rb +++ b/lib/js_test_driver/runner.rb @@ -1,161 +1,13 @@ module JsTestDriver - # The Runner class is used - class Runner - - def initialize(attributes = {}) - self.attributes = attributes - end - - # configuration, by default it's parsed from config_path - attr_writer :config - - def config - @config ||= parse_config - end - - attr_writer :generated_files_dir - - # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver - # - # the generated files are the config yml file and the fixture files - def generated_files_dir - @generated_files_dir || default_generated_files_dir - end - - # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb - attr_writer :config_path - - def config_path - @config_path ||= default_config_path - end - - # this is the path to the js test driver jar file, by default it's stored relatively to this file - attr_writer :jar_path - - def jar_path - @jar_path ||= default_jar_path - end - - # this is where the config yml file will be saved, by default its saved in the generated files - # directory under the name jsTestDriver.conf - attr_writer :tmp_path - - def config_yml_path - @tmp_path ||= default_config_yml_path - end - - # starts the server on the default port specified in the config file - def start_server - run(jstd.start_server) - end - - # captures the browsers - # - # by default it will capture the browsers specified in the config, - # but you can pass an argument like 'opera,chrome,firefox' to capture opera, chrome and firefox - def capture_browsers(browsers = nil) - browsers ||= '' - browsers = browsers.split(',') - browsers = config.browsers if browsers.empty? - - url = config.server + "/capture?strict" - - JsTestDriver::Commands::CaptureBrowsersCommand.new(browsers, url).each do |cmd| - spawn(cmd) - end - end - - # runs the tests specified by the argument - # - # by default it will run all the tests, but you can pass a string like: - # 'TestCase' or 'TestCase.test' - # to run either a single test case or a single test - def run_tests(tests = nil) - command = jstd.with_config.run_tests(tests) - - run(command) - end - - def start_server_capture_and_run(tests = nil, browsers = nil, output_xml_path = nil, console = nil) - command = jstd - .start_server - .with_config - .capture_browsers(browsers) - .run_tests(tests) - - command.output_directory(output_xml_path) if output_xml_path - command.capture_console if console - - result = run(command) - - if config.measure_coverage? && output_xml_path - generate_html_coverage_report(output_xml_path) - end - - return result - end - def generate_html_coverage_report(output_path) - unless genhtml_installed? - puts "Could not find genhtml. You must install lcov (sudo apt-get install lcov)" - return - end - - command = JsTestDriver::Commands::GenerateCoverageReport.new(self, output_path) - run(command) - end - - def jstd - JsTestDriver::Commands::JstdJarCommand.new(self) - end - - protected + class Runner def run(command) system(command.to_s) end - def genhtml_installed? - !%x[which genhtml].strip.empty? - end - - def parse_config - source = "" - if File.exist?(config_path) - source = File.read(config_path) - else - warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!") - end - config = JsTestDriver::Config.parse(source) - config.config_dir = generated_files_dir - return config - end - - def default_config_path - root = defined?(::Rails) ? ::Rails.root.to_s : '.' - return File.expand_path(File.join(root, 'config', 'js_test_driver.rb')) - end - - def default_jar_path - current_dir = File.dirname(__FILE__) - path = File.join(current_dir, '..', '..', 'vendor', 'js_test_driver.jar') - return File.expand_path(path) - end - - def default_config_yml_path - return File.expand_path("jsTestDriver.conf", generated_files_dir) - end - - def default_generated_files_dir - return File.expand_path(".js_test_driver") - end - - private - - def attributes=(values) - values.each do |attr, value| - self.send("#{attr}=", value) - end + def runbg(command) + spawn(command.to_s) end end diff --git a/lib/js_test_driver/runtime_config.rb b/lib/js_test_driver/runtime_config.rb new file mode 100644 index 0000000..cab9fbf --- /dev/null +++ b/lib/js_test_driver/runtime_config.rb @@ -0,0 +1,105 @@ +module JsTestDriver + # The RuntimeConfig class holds the various paths that the application + # uses, as well as the actual JsTestDriver configuration + class RuntimeConfig + + def initialize(attributes = {}) + self.attributes = attributes + end + + # configuration, by default it's parsed from config_path + attr_writer :config + + def config + @config ||= parse_config + end + + attr_writer :generated_files_dir + + # this is where the generated files will be saved, by default this is `pwd`/.js_test_driver + # + # the generated files are the config yml file and the fixture files + def generated_files_dir + dir(@generated_files_dir || default_generated_files_dir) + end + + # this is the path of the config file, by default its `pwd`/config/js_test_driver.rb + attr_writer :config_path + + def config_path + file(@config_path ||= default_config_path) + end + + # this is the path to the js test driver jar file, by default it's stored relatively to this file + attr_writer :jar_path + + def jar_path + file(@jar_path ||= default_jar_path) + end + + # this is where the config yml file will be saved, by default its saved in the generated files + # directory under the name jsTestDriver.conf + attr_writer :tmp_path + + def config_yml_path + file(@tmp_path ||= default_config_yml_path) + end + + protected + + def parse_config + source = "" + if File.exist?(config_path) + source = File.read(config_path) + else + warn("Could not find JS Test Driver config: '#{config_path}', assuming empty config file!") + end + config = JsTestDriver::Config.parse(source) + config.config_dir = generated_files_dir + config.save(config_yml_path) + return config + end + + def default_config_path + root = defined?(::Rails) ? ::Rails.root.to_s : '.' + return File.expand_path(File.join(root, 'config', 'js_test_driver.rb')) + end + + def default_jar_path + current_dir = File.dirname(__FILE__) + path = File.join(current_dir, '..', '..', 'vendor', 'js_test_driver.jar') + return File.expand_path(path) + end + + def default_config_yml_path + return file("jsTestDriver.conf", generated_files_dir) + end + + def default_generated_files_dir + return file(".js_test_driver") + end + + private + + def dir(name, parent = nil) + path = File.expand_path(name, parent) + FileUtils.mkdir_p(path) unless File.exist?(path) + return path + end + + def file(name, parent = nil) + path = File.expand_path(name, parent) + dir = File.dirname(path) + FileUtils.mkdir_p(dir) unless File.exist?(dir) + return path + end + + def attributes=(values) + values.each do |attr, value| + self.send("#{attr}=", value) + end + end + + end + +end diff --git a/test/integration/sample_test.rb b/test/integration/sample_test.rb index 9680249..c7d7f5b 100644 --- a/test/integration/sample_test.rb +++ b/test/integration/sample_test.rb @@ -4,11 +4,11 @@ module JsTestDriver class SampleTest < Test::Unit::TestCase def test_running_sample_specs - config = JsTestDriver::Runner.new(:config_path => File.expand_path('../../sample/config/js_test_driver.rb', __FILE__)) - output_path = File.expand_path('../../../.js_test_driver/', __FILE__) + config_path = File.expand_path('../../sample/config/js_test_driver.rb', __FILE__) + app = JsTestDriver::Application.new(:config_path => config_path) - assert config.start_server_capture_and_run(nil, nil, output_path), 'running tests should return a success' + assert app.run(:output_xml_path => output_path), 'running tests should return a success' end end diff --git a/test/test_helper.rb b/test/test_helper.rb index a195986..72f45e8 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -8,9 +8,40 @@ require 'test/unit' require 'mocha' +class FakeRunner + + attr_reader :commands_run, :commands_run_in_bg + + def initialize + @commands_run = [] + @commands_run_in_bg = [] + end + + def run(command) + @commands_run << command.to_s + end + + def runbg(command) + @commands_run_in_bg << command.to_s + end + + def has_run?(command) + @commands_run.include?(command.to_s) + end + + def has_run_in_bg?(command) + @commands_run_in_bg.include?(command.to_s) + end +end + class Test::Unit::TestCase + attr_reader :application, :runner + def setup + @application = JsTestDriver::Application.new + @runner = FakeRunner.new + @application.stubs(:runner => @runner) clean_up_saved_config_files end @@ -18,6 +49,22 @@ def fixture_dir File.expand_path('fixtures', File.dirname(__FILE__)) end + def config + application.config + end + + def runtime_config + application.runtime_config + end + + def assert_run(cmd) + assert runner.has_run?(cmd), "Expected to have run:\n#{cmd}\nbut only commands run were:\n#{runner.commands_run.join("\n")}" + end + + def assert_run_in_bg(cmd) + assert runner.has_run_in_bg?(cmd), "Expected to have run in bg:\n#{cmd}\nbut only commands run were:\n#{runner.commands_run_in_bg.join("\n")}" + end + def clean_up_saved_config_files root_dir = File.expand_path('..', File.dirname(__FILE__)) Dir["#{root_dir}/**/.js_test_driver"].each do |file| @@ -25,4 +72,4 @@ def clean_up_saved_config_files end end -end \ No newline at end of file +end diff --git a/test/unit/capture_browsers_test.rb b/test/unit/capture_browsers_test.rb new file mode 100644 index 0000000..d929b0d --- /dev/null +++ b/test/unit/capture_browsers_test.rb @@ -0,0 +1,30 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +module JsTestDriver + class CaptureBrowsersTest < Test::Unit::TestCase + + def test_should_capture_default_browsers + config.browser 'foo', 'bar', 'baz' + JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!) + + application.capture_browsers + + ['foo', 'bar', 'baz'].each do |browser| + assert_run_in_bg("#{browser} http://localhost:4224/capture?strict") + end + end + + def test_should_capture_given_browsers + config.browser 'foo', 'bar', 'baz' + JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!) + + application.capture_browsers(:browsers => 'aaa,bbb') + + ['aaa', 'bbb'].each do |browser| + assert_run_in_bg("#{browser} http://localhost:4224/capture?strict") + end + end + + end +end + diff --git a/test/unit/run_test.rb b/test/unit/run_test.rb new file mode 100644 index 0000000..309ce43 --- /dev/null +++ b/test/unit/run_test.rb @@ -0,0 +1,29 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +module JsTestDriver + class RunTest < Test::Unit::TestCase + +:wa + def test_should_run_with_all_arguments_at_once + config.browser 'foo', 'bar', 'baz' + + application.run(:tests => 'TestCase', + :browsers => 'aaa,bbb', + :output_xml_path => '.js_test_driver', + :capture_console => true) + + assert_run("java -jar #{runtime_config.jar_path} --port 4224 --config #{runtime_config.config_yml_path} --browser aaa,bbb --tests TestCase --testOutput #{File.expand_path('.js_test_driver')} --captureConsole") + end + + def test_when_measuring_coverage + config.measure_coverage + config.browser 'foo' + + application.run(:output_xml_path => '.js_test_driver') + + assert_run("genhtml -o #{File.expand_path('.js_test_driver/coverage')} #{File.expand_path('.js_test_driver/jsTestDriver.conf-coverage.dat')}") + end + + end +end + diff --git a/test/unit/run_tests_test.rb b/test/unit/run_tests_test.rb new file mode 100644 index 0000000..4d81e60 --- /dev/null +++ b/test/unit/run_tests_test.rb @@ -0,0 +1,20 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +module JsTestDriver + class RunTestsTest < Test::Unit::TestCase + + def test_should_run_all_tests_by_default + application.run_tests + + assert_run("java -jar #{runtime_config.jar_path} --config #{runtime_config.config_yml_path} --tests all") + end + + def test_should_run_selected_tests + application.run_tests(:tests => 'MyTestCase.some_test') + + assert_run("java -jar #{runtime_config.jar_path} --config #{runtime_config.config_yml_path} --tests MyTestCase.some_test") + end + + end +end + diff --git a/test/unit/runner_test.rb b/test/unit/runner_test.rb deleted file mode 100644 index 17b7c5e..0000000 --- a/test/unit/runner_test.rb +++ /dev/null @@ -1,109 +0,0 @@ -require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) - -module JsTestDriver - class RunnerTest < Test::Unit::TestCase - - def given_a_runner(opts = {}) - return JsTestDriver::Runner.new(opts) - end - - def test_should_have_default_config_path - # given - runner = given_a_runner - - # then - assert runner.config_path - end - - def test_should_have_default_jar_path - # given - runner = given_a_runner - - # then - assert runner.jar_path - end - - def test_should_have_default_tmp_path - # given - runner = given_a_runner - - # then - assert runner.config_yml_path - end - - def expect_command_to_be_executed(runner, cmd) - runner.expects(:system).with(cmd) - end - - def expect_spawn(runner, cmd) - runner.expects(:spawn).with(cmd) - end - - def test_should_run_server_with_given_port_number - config = JsTestDriver::Config.new(:port => 6666) - runner = given_a_runner(:config => config) - - expect_command_to_be_executed(runner, "java -jar #{runner.jar_path} --port #{config.port}") - - runner.start_server - end - - def test_should_run_all_tests_by_default - runner = given_a_runner(:config => JsTestDriver::Config.new) - - expect_command_to_be_executed(runner, "java -jar #{runner.jar_path} --config #{runner.config_yml_path} --tests all") - - runner.run_tests - end - - def test_should_run_selected_tests - runner = given_a_runner(:config => JsTestDriver::Config.new) - - expect_command_to_be_executed(runner, "java -jar #{runner.jar_path} --config #{runner.config_yml_path} --tests MyTestCase.some_test") - - runner.run_tests('MyTestCase.some_test') - end - - def test_should_capture_default_browsers - runner = given_a_runner(:config => JsTestDriver::Config.new(:browsers => ['foo', 'bar', 'baz'])) - JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!) - - ['foo', 'bar', 'baz'].each do |browser| - expect_spawn(runner, "#{browser} http://localhost:4224/capture?strict") - end - - runner.capture_browsers - end - - def test_should_capture_given_browsers - runner = given_a_runner(:config => JsTestDriver::Config.new(:browsers => ['foo', 'bar', 'baz'])) - JsTestDriver::Commands::BaseCommand.any_instance.stubs(:ensure_installed!) - - ['aaa', 'bbb'].each do |browser| - expect_spawn(runner, "#{browser} http://localhost:4224/capture?strict") - end - - runner.capture_browsers('aaa,bbb') - end - - def test_should_run_with_all_arguments_at_once - runner = given_a_runner(:config => JsTestDriver::Config.new(:browsers => ['foo', 'bar', 'baz'])) - - expect_command_to_be_executed(runner, "java -jar #{runner.jar_path} --port 4224 --config #{runner.config_yml_path} --browser aaa,bbb --tests TestCase --testOutput #{File.expand_path('.js_test_driver')} --captureConsole") - - runner.start_server_capture_and_run('TestCase', 'aaa,bbb', '.js_test_driver', true) - end - - def test_when_measuring_coverage - config = JsTestDriver::Config.new - config.measure_coverage - runner = given_a_runner(:config => config) - - runner.expects(:system) - runner.expects(:system).with("genhtml -o #{File.expand_path('.js_test_driver/coverage')} #{File.expand_path('.js_test_driver/jsTestDriver.conf-coverage.dat')}") - - runner.start_server_capture_and_run(nil, 'aaa', '.js_test_driver') - end - - end -end diff --git a/test/unit/runtime_config_test.rb b/test/unit/runtime_config_test.rb new file mode 100644 index 0000000..749b1dd --- /dev/null +++ b/test/unit/runtime_config_test.rb @@ -0,0 +1,21 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +module JsTestDriver + class RuntimeConfigTest < Test::Unit::TestCase + + attr_reader :runtime_config + + def test_should_have_default_config_path + assert application.runtime_config.config_path + end + + def test_should_have_default_jar_path + assert application.runtime_config.jar_path + end + + def test_should_have_default_tmp_path + assert application.runtime_config.config_yml_path + end + + end +end diff --git a/test/unit/start_server_test.rb b/test/unit/start_server_test.rb new file mode 100644 index 0000000..d3aa23c --- /dev/null +++ b/test/unit/start_server_test.rb @@ -0,0 +1,15 @@ +require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper')) + +module JsTestDriver + class StartServerTest < Test::Unit::TestCase + + def test_should_run_server_with_given_port_number + application.config.port(6666) + + application.start_server + + assert_run("java -jar #{runtime_config.jar_path} --port #{config.port}") + end + + end +end