Skip to content

Commit

Permalink
Merge pull request #64 from makaroni4/feature/thresholds
Browse files Browse the repository at this point in the history
Added threshold check for each rule from config.yml file
  • Loading branch information
makaroni4 committed May 13, 2015
2 parents 17f42b3 + c1977c3 commit 1a51476
Show file tree
Hide file tree
Showing 8 changed files with 99 additions and 18 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
14 changes: 11 additions & 3 deletions lib/sandi_meter/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,21 +75,29 @@ 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
class << self
def execute
cli = CommandParser.new
cli.parse_options

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])

create_config_file(cli.config[:output_path], '.sandi_meter', %w(db vendor).join("\n"))
create_config_file(cli.config[:output_path], 'config.yml', YAML.dump({ threshold: 90 }))
create_config_file(cli.config[:output_path], 'config.yml', YAML.dump({ thresholds: [90, 90, 90, 90] }))
end

if cli.config[:version]
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
{ threshold: 90 }
{ thresholds: cli.config[:rule_thresholds] }
end

if RulesChecker.new(data, config).ok?
Expand Down
10 changes: 8 additions & 2 deletions lib/sandi_meter/rules_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@ def initialize(data, config)
end

def ok?
@rules.reduce(:+) / 4 > @config[:threshold]
if @config[:threshold]
puts "DEPRECATION WARNING: sandi_meter threshold will be deprecated. Set thresholds for each rule in sandi_meter config.yml"

@rules.reduce(:+) / 4 > @config[:threshold]
elsif @config[:thresholds]
@rules.each_with_index.map { |percentage, index| percentage >= @config[:thresholds][index].to_f }.reduce(:&)
end
end

private
def percentage(amount, total)
total > 0 ? (amount / total.to_f)*100 : 100
total > 0 ? (amount / total.to_f) * 100 : 100
end
end
end
6 changes: 5 additions & 1 deletion sandi_meter/config.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
---
:threshold: 80
:thresholds:
- 80
- 50
- 90
- 90
31 changes: 25 additions & 6 deletions spec/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
FakeFS.deactivate!
end

describe '#execute' do
describe '#execute', silent_cli: true do
before do
@original_argv = ARGV
ARGV.clear
Expand All @@ -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
4 changes: 2 additions & 2 deletions spec/rules_checker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@

describe "#ok?" do
it "returns false with 100 threshold" do
expect(SandiMeter::RulesChecker.new(conditions, {threshold: 100})).to_not be_ok
expect(SandiMeter::RulesChecker.new(conditions, {thresholds: [100, 100, 100, 100]})).to_not be_ok
end

it "returns true with threshold less than 100" do
expect(SandiMeter::RulesChecker.new(conditions, {threshold: 50})).to be_ok
expect(SandiMeter::RulesChecker.new(conditions, {thresholds: [50, 100, 100, 100]})).to be_ok
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
6 changes: 4 additions & 2 deletions spec/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@
RSpec.configure do |config|
original_stderr = $stderr
original_stdout = $stdout
config.before(:all) do

config.before silent_cli: true do
# Redirect stderr and stdout
$stderr = File.open(File::NULL, "w")
$stdout = File.open(File::NULL, "w")
end
config.after(:all) do

config.after silent_cli: true do
$stderr = original_stderr
$stdout = original_stdout
end
Expand Down

0 comments on commit 1a51476

Please sign in to comment.