Skip to content

Commit

Permalink
Split Runner into RuntimeConfig and a bunch of commands. Reorganized …
Browse files Browse the repository at this point in the history
…application to use manual dependency injection.
  • Loading branch information
Adam Pohorecki committed Jun 7, 2011
1 parent f4d7581 commit 6d3cf50
Show file tree
Hide file tree
Showing 19 changed files with 500 additions and 307 deletions.
30 changes: 22 additions & 8 deletions 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
47 changes: 47 additions & 0 deletions 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
34 changes: 34 additions & 0 deletions 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
54 changes: 54 additions & 0 deletions 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
22 changes: 22 additions & 0 deletions 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
20 changes: 20 additions & 0 deletions 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
22 changes: 0 additions & 22 deletions lib/js_test_driver/commands/capture_browsers_command.rb

This file was deleted.

19 changes: 11 additions & 8 deletions lib/js_test_driver/commands/generate_coverage_report.rb
Expand Up @@ -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)
Expand Down
21 changes: 16 additions & 5 deletions lib/js_test_driver/commands/jstd_jar_command.rb
Expand Up @@ -3,30 +3,30 @@ 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)
return option('--tests', tests || "all")
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
Expand All @@ -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
Expand Down

0 comments on commit 6d3cf50

Please sign in to comment.