Skip to content

Commit

Permalink
Merge pull request #47 from fastruby/output-to-file
Browse files Browse the repository at this point in the history
Add output to file feature
  • Loading branch information
etagwerker authored Sep 29, 2020
2 parents e038ce3 + ead3f3d commit 7f8a482
Show file tree
Hide file tree
Showing 11 changed files with 86 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased] [(commits)](https://github.com/fastruby/skunk/compare/v0.4.2...HEAD)

* [FEATURE] Rename the tech debt metric: StinkScore => SkunkScore. It's a little friendlier.
* [FEATURE] Add `--out=file.txt` support to the command line (by [@manuca]())

## [0.4.2] [(commits)](https://github.com/fastruby/skunk/compare/v0.4.1...v0.4.2)
* [BUGFIX] Fixes table width issues by rounding values (by [@etagwerker][])
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ GEM
minitest (5.8.5)
minitest-around (0.5.0)
minitest (~> 5.0)
minitest-stub_any_instance (1.0.2)
parallel (1.19.2)
parser (2.6.5.0)
ast (~> 2.4.0)
Expand Down Expand Up @@ -110,6 +111,7 @@ DEPENDENCIES
codecov (~> 0.1.16)
minitest (~> 5.8.4)
minitest-around (~> 0.5.0)
minitest-stub_any_instance (~> 1.0.2)
rake (~> 13.0)
reek (~> 5.4.0)
rubocop (< 1.0)
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ There are not that many options but here they are:
skunk -h
Usage: skunk [options] [paths]
-b, --branch BRANCH Set branch to compare
-o, --out FILE Output report to file
-v, --version Show gem's version
-h, --help Show this message
```
Expand Down
17 changes: 15 additions & 2 deletions lib/skunk/cli/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
module Skunk
module Cli
# Knows how to execute command line commands
# :reek:InstanceVariableAssumption
class Application < RubyCritic::Cli::Application
COVERAGE_FILE = "coverage/.resultset.json"

Expand All @@ -21,8 +22,10 @@ def initialize(argv)
def execute
warn_coverage_info unless File.exist?(COVERAGE_FILE)

parsed_options = @options.parse.to_h
reporter = Skunk::Cli::CommandFactory.create(parsed_options).execute
# :reek:NilCheck
@parsed_options = @options.parse.to_h
reporter = Skunk::Cli::CommandFactory.create(@parsed_options).execute

print(reporter.status_message)
reporter.status
rescue OptionParser::InvalidOption => error
Expand All @@ -36,6 +39,16 @@ def warn_coverage_info
warn "warning: Couldn't find coverage info at #{COVERAGE_FILE}."
warn "warning: Having no coverage metrics will make your SkunkScore worse."
end

# :reek:NilCheck
def print(message)
filename = @parsed_options[:output_filename]
if filename.nil?
$stdout.puts(message)
else
File.open(filename, "w") { |file| file.puts(message) }
end
end
end
end
end
11 changes: 11 additions & 0 deletions lib/skunk/cli/options/argv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ class Options
# Extends RubyCritic::Cli::Options::Argv to parse a subset of the
# parameters accepted by RubyCritic
class Argv < RubyCritic::Cli::Options::Argv
# :reek:Attribute
attr_accessor :output_filename

def parse # rubocop:disable Metrics/MethodLength
parser.new do |opts|
opts.banner = "Usage: skunk [options] [paths]\n"
Expand All @@ -19,6 +22,10 @@ def parse # rubocop:disable Metrics/MethodLength
self.mode = :compare_branches
end

opts.on("-o", "--out FILE", "Output report to file") do |filename|
self.output_filename = filename
end

opts.on_tail("-v", "--version", "Show gem's version") do
self.mode = :version
end
Expand All @@ -28,6 +35,10 @@ def parse # rubocop:disable Metrics/MethodLength
end
end.parse!(@argv)
end

def to_h
super.merge(output_filename: output_filename)
end
end
end
end
Expand Down
1 change: 1 addition & 0 deletions skunk.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "codecov", "~> 0.1.16"
spec.add_development_dependency "minitest", "~> 5.8.4"
spec.add_development_dependency "minitest-around", "~> 0.5.0"
spec.add_development_dependency "minitest-stub_any_instance", "~> 1.0.2"
spec.add_development_dependency "rake", "~> 13.0"
spec.add_development_dependency "reek", "~> 5.4.0"
spec.add_development_dependency "rubocop", "< 1.0"
Expand Down
23 changes: 23 additions & 0 deletions test/lib/skunk/application_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require "test_helper"
require "skunk/cli/application"
require "rubycritic/core/analysed_module"

describe Skunk::Cli::Application do
describe "#execute" do
Expand All @@ -27,5 +28,27 @@
_(result).must_equal success_code
end
end

context "when passing --out option with a file" do
require "fileutils"

let(:argv) { ["--out=tmp/generated_report.txt", "samples/rubycritic"] }
let(:success_code) { 0 }

it "writes output to the file" do
FileUtils.rm("tmp/generated_report.txt", force: true)
FileUtils.mkdir_p("tmp")

RubyCritic::AnalysedModule.stub_any_instance(:churn, 1) do
RubyCritic::AnalysedModule.stub_any_instance(:coverage, 100.0) do
result = application.execute
_(result).must_equal success_code
end
end

_(File.read("tmp/generated_report.txt"))
.must_equal File.read("test/samples/console_output.txt")
end
end
end
end
1 change: 1 addition & 0 deletions test/lib/skunk/cli/commands/help_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
MSG = <<~HELP
Usage: skunk [options] [paths]
-b, --branch BRANCH Set branch to compare
-o, --out FILE Output report to file
-v, --version Show gem's version
-h, --help Show this message
HELP
Expand Down
27 changes: 27 additions & 0 deletions test/lib/skunk/cli/options/argv_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# frozen_string_literal: true

require "test_helper"

require "skunk/cli/options/argv"

describe Skunk::Cli::Options::Argv do
describe "#output_filename" do
context "passing --out=FILE options" do
let(:argv) { ["--out=file.txt"] }

it "parses passed filename" do
parser = Skunk::Cli::Options::Argv.new(argv)
parser.parse
_(parser.output_filename).must_equal "file.txt"
end
end

context "not passing the --out option" do
it "is nil" do
parser = Skunk::Cli::Options::Argv.new([])
parser.parse
_(parser.output_filename).must_be_nil
end
end
end
end
2 changes: 2 additions & 0 deletions test/samples/console_output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ SkunkScore Total: 0.59
Modules Analysed: 1
SkunkScore Average: 0.59
Worst SkunkScore: 0.59 (samples/rubycritic/analysed_module.rb)

Generated with Skunk v0.4.2
2 changes: 2 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
require "minitest/autorun"
require "minitest/pride"
require "minitest/around/spec"
require "minitest/stub_any_instance"

require "skunk/rubycritic/analysed_module"

def context(*args, &block)
Expand Down

0 comments on commit 7f8a482

Please sign in to comment.