Skip to content

Commit

Permalink
Use a singleton to keep track of Koality options
Browse files Browse the repository at this point in the history
  • Loading branch information
jdpace committed Jun 5, 2012
1 parent b7f4572 commit 4b75e5d
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 35 deletions.
18 changes: 11 additions & 7 deletions lib/koality.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,32 @@
module Koality

class << self
def run(options)
setup_environment(options)
def run
setup_environment

run_rails_bp(options) if options.rails_bp_enabled?
run_rails_bp if options.rails_bp_enabled?

success = run_cane(options)
success = run_cane
abort if options[:abort_on_failure] && !success
end

def run_rails_bp(options)
def run_rails_bp
rails_bp = Koality::Runner::RailsBestPractices.new(options)
rails_bp.run
end

def run_cane(options)
def run_cane
cane = Koality::Runner::Cane.new(options)
cane.run
end

def options
@options ||= Koality::Options.new
end

private

def setup_environment(options)
def setup_environment
FileUtils.mkdir_p options.output_directory
end
end
Expand Down
12 changes: 3 additions & 9 deletions lib/koality/rake_task.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
require 'rake'
require 'rake/tasklib'
require 'koality/options'
require 'koality'

module Koality
class RakeTask < ::Rake::TaskLib

attr_accessor :options


def initialize(task_name = :koality)
self.options = Koality::Options.new

yield options if block_given?
yield Koality.options if block_given?

define_task task_name
end
Expand All @@ -22,8 +17,7 @@ def define_task(task_name)
end

task task_name do
require 'koality'
Koality.run(options)
Koality.run
end
end

Expand Down
22 changes: 21 additions & 1 deletion lib/koality/runner/cane.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,38 @@ module Koality
module Runner
class Cane

attr_reader :cane_options
attr_reader :cane_options, :violations

def initialize(options)
@cane_options = translate_options(options)
@violations = {}
end

def run
::Cane.run(cane_options)
end

#def run
#violations.clear

#checkers.each do |type, klass|
#checker = klass.new(cane_options[type])
#violations[type] = checker.violations
#end

#violations_count <= cane_options[:max_violations]
#end

#def checkers
#::Cane::Runner::CHECKERS.select { |type, _| cane_options.key?(type) }
#end

private

def violations_count
violations.values.flatten.count
end

def translate_options(options)
Hash.new.tap do |cane_opts|
cane_opts[:max_violations] = options[:total_violations_threshold]
Expand Down
10 changes: 3 additions & 7 deletions spec/koality/rake_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,15 @@
describe '.new' do
it 'allows you to configure options' do
task = Koality::RakeTask.new do |options|
options.should be_instance_of(Koality::Options)
options.should == Koality.options
end
end

it 'defines a task that runs Koality with the options' do
task_options = nil
Koality::RakeTask.new do |opts|
task_options = opts
end

Koality::RakeTask.new
task = Rake::Task[:koality]

Koality.expects(:run).with(task_options)
Koality.expects(:run)
task.invoke
end
end
Expand Down
26 changes: 15 additions & 11 deletions spec/koality_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,55 +8,59 @@
})
end

before do
Koality.stubs(:options).returns(options)
end

describe '.run' do
before do
Koality.stubs(:run_rails_bp => true, :run_cane => true)
end

it 'makes sure the output_directory exists' do
FileUtils.expects(:mkdir_p).with('quality')
Koality.run(options)
Koality.run
end

it 'runs RailsBestPractices with the passed options if enabled' do
options.stubs(:rails_bp_enabled?).returns(true)
Koality.expects(:run_rails_bp).with(options)
Koality.run(options)
Koality.expects(:run_rails_bp)
Koality.run
end

it 'does not run RailsBestPractices if disabled' do
options.stubs(:rails_bp_enabled?).returns(false)
Koality.expects(:run_rails_bp).never
Koality.run(options)
Koality.run
end

it 'runs Cane with the passed options' do
Koality.expects(:run_cane).with(options).returns(true)
Koality.run(options)
Koality.expects(:run_cane).returns(true)
Koality.run
end

it 'aborts if the abort_on_failure flag is set and the run was not successful' do
options[:abort_on_failure] = true
Koality.stubs(:run_cane).returns(false)
Koality.expects(:abort)

Koality.run(options)
Koality.run
end

it 'does not abort if the abort_on_failure flag is set and the run was successful' do
options[:abort_on_failure] = true
Koality.stubs(:run_cane).returns(true)
Koality.expects(:abort).never

Koality.run(options)
Koality.run
end

it 'does not abort if the abort_on_failure flag is set to false' do
options[:abort_on_failure] = false
Koality.stubs(:run_cane).returns(false)
Koality.expects(:abort).never

Koality.run(options)
Koality.run
end
end

Expand All @@ -65,7 +69,7 @@
rbp = mock('RBP', :run => true)
Koality::Runner::RailsBestPractices.expects(:new).with(options).returns(rbp)

Koality.run_rails_bp(options)
Koality.run_rails_bp
end
end

Expand All @@ -74,7 +78,7 @@
cane = mock('cane', :run => true)
Koality::Runner::Cane.expects(:new).with(options).returns(cane)

Koality.run_cane(options)
Koality.run_cane
end
end

Expand Down

0 comments on commit 4b75e5d

Please sign in to comment.