diff --git a/README.md b/README.md index ff0e1b9..605ade0 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/lib/sandi_meter/cli.rb b/lib/sandi_meter/cli.rb index 4b78433..ce7d69e 100644 --- a/lib/sandi_meter/cli.rb +++ b/lib/sandi_meter/cli.rb @@ -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 @@ -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]) @@ -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? diff --git a/spec/cli_spec.rb b/spec/cli_spec.rb index 8b0a5a0..bfda69c 100644 --- a/spec/cli_spec.rb +++ b/spec/cli_spec.rb @@ -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) @@ -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) @@ -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 diff --git a/spec/support/terminate_matcher.rb b/spec/support/terminate_matcher.rb new file mode 100644 index 0000000..6e14249 --- /dev/null +++ b/spec/support/terminate_matcher.rb @@ -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