Skip to content

Commit 7f8a482

Browse files
authored
Merge pull request #47 from fastruby/output-to-file
Add output to file feature
2 parents e038ce3 + ead3f3d commit 7f8a482

File tree

11 files changed

+86
-2
lines changed

11 files changed

+86
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77
## [Unreleased] [(commits)](https://github.com/fastruby/skunk/compare/v0.4.2...HEAD)
88

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

1112
## [0.4.2] [(commits)](https://github.com/fastruby/skunk/compare/v0.4.1...v0.4.2)
1213
* [BUGFIX] Fixes table width issues by rounding values (by [@etagwerker][])

Gemfile.lock

+2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ GEM
4747
minitest (5.8.5)
4848
minitest-around (0.5.0)
4949
minitest (~> 5.0)
50+
minitest-stub_any_instance (1.0.2)
5051
parallel (1.19.2)
5152
parser (2.6.5.0)
5253
ast (~> 2.4.0)
@@ -110,6 +111,7 @@ DEPENDENCIES
110111
codecov (~> 0.1.16)
111112
minitest (~> 5.8.4)
112113
minitest-around (~> 0.5.0)
114+
minitest-stub_any_instance (~> 1.0.2)
113115
rake (~> 13.0)
114116
reek (~> 5.4.0)
115117
rubocop (< 1.0)

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ There are not that many options but here they are:
5353
skunk -h
5454
Usage: skunk [options] [paths]
5555
-b, --branch BRANCH Set branch to compare
56+
-o, --out FILE Output report to file
5657
-v, --version Show gem's version
5758
-h, --help Show this message
5859
```

lib/skunk/cli/application.rb

+15-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
module Skunk
1212
module Cli
1313
# Knows how to execute command line commands
14+
# :reek:InstanceVariableAssumption
1415
class Application < RubyCritic::Cli::Application
1516
COVERAGE_FILE = "coverage/.resultset.json"
1617

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

24-
parsed_options = @options.parse.to_h
25-
reporter = Skunk::Cli::CommandFactory.create(parsed_options).execute
25+
# :reek:NilCheck
26+
@parsed_options = @options.parse.to_h
27+
reporter = Skunk::Cli::CommandFactory.create(@parsed_options).execute
28+
2629
print(reporter.status_message)
2730
reporter.status
2831
rescue OptionParser::InvalidOption => error
@@ -36,6 +39,16 @@ def warn_coverage_info
3639
warn "warning: Couldn't find coverage info at #{COVERAGE_FILE}."
3740
warn "warning: Having no coverage metrics will make your SkunkScore worse."
3841
end
42+
43+
# :reek:NilCheck
44+
def print(message)
45+
filename = @parsed_options[:output_filename]
46+
if filename.nil?
47+
$stdout.puts(message)
48+
else
49+
File.open(filename, "w") { |file| file.puts(message) }
50+
end
51+
end
3952
end
4053
end
4154
end

lib/skunk/cli/options/argv.rb

+11
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ class Options
99
# Extends RubyCritic::Cli::Options::Argv to parse a subset of the
1010
# parameters accepted by RubyCritic
1111
class Argv < RubyCritic::Cli::Options::Argv
12+
# :reek:Attribute
13+
attr_accessor :output_filename
14+
1215
def parse # rubocop:disable Metrics/MethodLength
1316
parser.new do |opts|
1417
opts.banner = "Usage: skunk [options] [paths]\n"
@@ -19,6 +22,10 @@ def parse # rubocop:disable Metrics/MethodLength
1922
self.mode = :compare_branches
2023
end
2124

25+
opts.on("-o", "--out FILE", "Output report to file") do |filename|
26+
self.output_filename = filename
27+
end
28+
2229
opts.on_tail("-v", "--version", "Show gem's version") do
2330
self.mode = :version
2431
end
@@ -28,6 +35,10 @@ def parse # rubocop:disable Metrics/MethodLength
2835
end
2936
end.parse!(@argv)
3037
end
38+
39+
def to_h
40+
super.merge(output_filename: output_filename)
41+
end
3142
end
3243
end
3344
end

skunk.gemspec

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ Gem::Specification.new do |spec|
4545
spec.add_development_dependency "codecov", "~> 0.1.16"
4646
spec.add_development_dependency "minitest", "~> 5.8.4"
4747
spec.add_development_dependency "minitest-around", "~> 0.5.0"
48+
spec.add_development_dependency "minitest-stub_any_instance", "~> 1.0.2"
4849
spec.add_development_dependency "rake", "~> 13.0"
4950
spec.add_development_dependency "reek", "~> 5.4.0"
5051
spec.add_development_dependency "rubocop", "< 1.0"

test/lib/skunk/application_test.rb

+23
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
require "test_helper"
44
require "skunk/cli/application"
5+
require "rubycritic/core/analysed_module"
56

67
describe Skunk::Cli::Application do
78
describe "#execute" do
@@ -27,5 +28,27 @@
2728
_(result).must_equal success_code
2829
end
2930
end
31+
32+
context "when passing --out option with a file" do
33+
require "fileutils"
34+
35+
let(:argv) { ["--out=tmp/generated_report.txt", "samples/rubycritic"] }
36+
let(:success_code) { 0 }
37+
38+
it "writes output to the file" do
39+
FileUtils.rm("tmp/generated_report.txt", force: true)
40+
FileUtils.mkdir_p("tmp")
41+
42+
RubyCritic::AnalysedModule.stub_any_instance(:churn, 1) do
43+
RubyCritic::AnalysedModule.stub_any_instance(:coverage, 100.0) do
44+
result = application.execute
45+
_(result).must_equal success_code
46+
end
47+
end
48+
49+
_(File.read("tmp/generated_report.txt"))
50+
.must_equal File.read("test/samples/console_output.txt")
51+
end
52+
end
3053
end
3154
end

test/lib/skunk/cli/commands/help_test.rb

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
MSG = <<~HELP
1111
Usage: skunk [options] [paths]
1212
-b, --branch BRANCH Set branch to compare
13+
-o, --out FILE Output report to file
1314
-v, --version Show gem's version
1415
-h, --help Show this message
1516
HELP
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# frozen_string_literal: true
2+
3+
require "test_helper"
4+
5+
require "skunk/cli/options/argv"
6+
7+
describe Skunk::Cli::Options::Argv do
8+
describe "#output_filename" do
9+
context "passing --out=FILE options" do
10+
let(:argv) { ["--out=file.txt"] }
11+
12+
it "parses passed filename" do
13+
parser = Skunk::Cli::Options::Argv.new(argv)
14+
parser.parse
15+
_(parser.output_filename).must_equal "file.txt"
16+
end
17+
end
18+
19+
context "not passing the --out option" do
20+
it "is nil" do
21+
parser = Skunk::Cli::Options::Argv.new([])
22+
parser.parse
23+
_(parser.output_filename).must_be_nil
24+
end
25+
end
26+
end
27+
end

test/samples/console_output.txt

+2
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,5 @@ SkunkScore Total: 0.59
88
Modules Analysed: 1
99
SkunkScore Average: 0.59
1010
Worst SkunkScore: 0.59 (samples/rubycritic/analysed_module.rb)
11+
12+
Generated with Skunk v0.4.2

test/test_helper.rb

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
require "minitest/autorun"
2020
require "minitest/pride"
2121
require "minitest/around/spec"
22+
require "minitest/stub_any_instance"
23+
2224
require "skunk/rubycritic/analysed_module"
2325

2426
def context(*args, &block)

0 commit comments

Comments
 (0)