Skip to content

Commit

Permalink
Added CLI thresholds for each rule
Browse files Browse the repository at this point in the history
  • Loading branch information
makaroni4 committed May 12, 2015
1 parent 6533cb2 commit 79bae34
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 8 deletions.
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ sandi_meter --help
-g, --graph HTML mode. Create folder, log data and output stats to HTML file.
--json Output as JSON
-l, --log Show syntax error and indentation log output
-p, --path PATH Path to folder or file to analyze (default is ".")
-o, --output_path PATH Path to save output files (default is "./sandi_meter")
-o, --output-path PATH Path for storing generated output files (default: ./sandi_meter/)
-p, --path PATH Path to folder or file to analyze
-q, --quiet Do not open HTML report for graph option in browser.
-t, --thresholds THRESHOLD Thresholds for each rule (default: "90,90,90,90" or in config.yml)
-r, --rules Show rules
-v, --version Gem version
-h, --help Help
cd ~/your/ruby/or/rails/project
Expand Down
10 changes: 9 additions & 1 deletion lib/sandi_meter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ class CommandParser
long: "--json",
description: "Output as JSON",
boolean: false

option :rule_thresholds,
short: "-t THRESHOLD",
long: "--thresholds THRESHOLD",
description: "Thresholds for each rule (default: 90,90,90,90) or in config.yml",
default: "90,90,90,90"
end

class CLI
Expand All @@ -85,6 +91,8 @@ def execute

cli.config[:output_path] ||= File.expand_path(File.join(cli.config[:path], 'sandi_meter'))

cli.config[:rule_thresholds] = cli.config[:rule_thresholds].split(",").map(&:to_i)

if cli.config[:graph]
FileUtils.mkdir_p(cli.config[:output_path]) unless Dir.exists?(cli.config[:output_path])

Expand Down Expand Up @@ -136,7 +144,7 @@ def execute
config = if File.exists?(config_file_path)
YAML.load(File.read(config_file_path))
else
{ thresholds: [90, 90, 90, 90] }
{ thresholds: cli.config[:rule_thresholds] }
end

if RulesChecker.new(data, config).ok?
Expand Down
29 changes: 24 additions & 5 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@

context 'with the graph flag passed in' do
before { ARGV.push('-g') }
after { ARGV.pop }

it 'opens the graph in a web browser' do
expect(cli).to receive(:open_in_browser)
Expand All @@ -43,10 +42,6 @@
ARGV.push('-q')
ARGV.push('-g')
end
after do
ARGV.pop
ARGV.pop
end

it 'does not open the browser' do
expect(cli).to_not receive(:open_in_browser)
Expand Down Expand Up @@ -82,5 +77,29 @@
expect(File.directory?(File.expand_path('/sandi_meter'))).to eq(true)
end
end

context 'makes account for rules thresholds' do
context 'for low thresholds' do
before do
ARGV.push('-t')
ARGV.push("1,1,1,1")
end

it 'terminates with 0 code' do
expect { cli.execute }.to terminate.with_code(0)
end
end

context 'for high thresholds' do
before do
ARGV.push('-t')
ARGV.push("99,99,99,99")
end

it 'terminates with 1 code' do
expect { cli.execute }.to terminate.with_code(1)
end
end
end
end
end
39 changes: 39 additions & 0 deletions spec/support/terminate_matcher.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#https://gist.github.com/mmasashi/58bd7e2668836a387856

RSpec::Matchers.define :terminate do |code|
actual = nil

def supports_block_expectations?
true
end

match do |block|
begin
block.call
rescue SystemExit => e
actual = e.status
end
actual and actual == status_code
end

chain :with_code do |status_code|
@status_code = status_code
end

failure_message do |block|
"expected block to call exit(#{status_code}) but exit" +
(actual.nil? ? " not called" : "(#{actual}) was called")
end

failure_message_when_negated do |block|
"expected block not to call exit(#{status_code})"
end

description do
"expect block to call exit(#{status_code})"
end

def status_code
@status_code ||= 0
end
end

0 comments on commit 79bae34

Please sign in to comment.