Skip to content

Commit 3612d41

Browse files
committed
refactor(config): centralize output path configuration and simplify printing
Move output path configuration to Skunk::Config and remove file output logic from Application#print Update tests and documentation to reflect changes in output path handling Even if the default path where the JSON and HTML reports are generated are `tmp/rubycritic` you can now modify them.
1 parent 5e6896d commit 3612d41

File tree

14 files changed

+107
-37
lines changed

14 files changed

+107
-37
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
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

88
## main [(unreleased)](https://github.com/fastruby/skunk/compare/v0.5.4...HEAD)
99

10+
* [FEATURE: Indicate `--out PATH` location](https://github.com/fastruby/skunk/pull/131)
1011
* [FEATURE: Add `--formats` CLI flag to select report formats (json, html, console)](https://github.com/fastruby/skunk/pull/130)
1112
* [REFACTOR: Move Console Report](https://github.com/fastruby/skunk/pull/128)
1213
* [BUGFIX: Set the right content type in the share HTTP request](https://github.com/fastruby/skunk/pull/129)

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ Run `skunk -h` to check out the help options:
6565
```
6666
Usage: skunk [options] [paths]
6767
-b, --branch BRANCH Set branch to compare
68-
-o, --out FILE Output report to file
68+
-o, --out PATH Output report path
6969
-v, --version Show gem's version
7070
-h, --help Show this message
7171
```
@@ -127,7 +127,9 @@ To only run skunk on specific folders, pass a list of directories in the command
127127

128128
### Generate JSON report in background
129129

130-
When the Skunk command is run, it will generate a JSON report file in the `RubyCritic::Config.root` location.
130+
When the Skunk command is run, it will generate a JSON report file in the configured output path.
131+
132+
Skunk also writes the console report to `skunk_console.txt` under the same output path.
131133

132134
### Comparing feature branches
133135

bin/console

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ puts ARGV.inspect
1313
require "skunk/cli/application"
1414
require "skunk/config"
1515

16-
Skunk::Config.formats = %i[json console html]
16+
Skunk::Config.formats = %i[json console html] # supported output formats
17+
Skunk::Config.root = "tmp/rubycritic" # default path to store generated JSON and HTML reports.
1718
Skunk::Cli::Application.new(ARGV).execute

lib/skunk/cli/application.rb

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,14 +49,8 @@ def warn_coverage_info
4949
warn "warning: Having no coverage metrics will make your SkunkScore worse."
5050
end
5151

52-
# :reek:NilCheck
5352
def print(message)
54-
filename = @parsed_options[:output_filename]
55-
if filename.nil?
56-
$stdout.puts(message)
57-
else
58-
File.open(filename, "a") { |file| file << message }
59-
end
53+
$stdout.puts(message)
6054
end
6155
end
6256
end

lib/skunk/cli/options/argv.rb

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ class Options
1010
# Extends RubyCritic::Cli::Options::Argv to parse a subset of the
1111
# parameters accepted by RubyCritic
1212
class Argv < RubyCritic::Cli::Options::Argv
13-
# :reek:Attribute
14-
attr_accessor :output_filename
15-
1613
def parse
1714
parser.new do |opts|
1815
opts.banner = "Usage: skunk [options] [paths]\n"
@@ -23,8 +20,8 @@ def parse
2320
self.mode = :compare_branches
2421
end
2522

26-
opts.on("-o", "--out FILE", "Output report to file") do |filename|
27-
self.output_filename = filename
23+
opts.on("-o", "--out PATH", "Output report path") do |path|
24+
Skunk::Config.root = path
2825
end
2926

3027
opts.on("-f", "--formats json,html,console", Array, "Output formats: json,html,console") do |list|
@@ -40,10 +37,6 @@ def parse
4037
end
4138
end.parse!(@argv)
4239
end
43-
44-
def to_h
45-
super.merge(output_filename: output_filename)
46-
end
4740
end
4841
end
4942
end

lib/skunk/config.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require "rubycritic/configuration"
4+
35
module Skunk
46
# Utility module for format validation
57
module FormatValidator
@@ -28,10 +30,12 @@ class Configuration
2830

2931
def initialize
3032
@formats = [DEFAULT_FORMAT]
33+
@root = RubyCritic::Config.root
3134
end
3235

3336
def set(options = {})
3437
self.formats = options[:formats] if options.key?(:formats)
38+
self.root = options[:root] if options.key?(:root)
3539
end
3640

3741
# Get the configured formats
@@ -46,6 +50,14 @@ def formats=(format_list)
4650
@formats = [DEFAULT_FORMAT] if @formats.empty?
4751
end
4852

53+
def root
54+
@root || File.expand_path("tmp/rubycritic", Dir.pwd)
55+
end
56+
57+
def root=(path)
58+
@root = path.nil? || path.to_s.empty? ? nil : File.expand_path(path.to_s)
59+
end
60+
4961
# Add a format to the existing list
5062
# @param format [Symbol] Format to add
5163
def add_format(format)
@@ -77,6 +89,7 @@ def supported_formats
7789
# Reset to default configuration
7890
def reset
7991
@formats = [DEFAULT_FORMAT]
92+
@root = RubyCritic::Config.root
8093
end
8194
end
8295

lib/skunk/generators/console_report.rb

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
require "erb"
44
require "terminal-table"
5+
require "pathname"
6+
require "fileutils"
57

68
require "skunk/generators/console/simple"
9+
require "skunk/config"
710

811
module Skunk
912
module Generator
@@ -14,14 +17,25 @@ def initialize(analysed_modules)
1417
end
1518

1619
def generate_report
17-
puts generator.render
20+
content = generator.render
21+
puts content
22+
FileUtils.mkdir_p(file_directory)
23+
File.write(file_pathname, content)
1824
end
1925

2026
private
2127

2228
def generator
2329
@generator ||= Skunk::Generator::Console::Simple.new(@analysed_modules)
2430
end
31+
32+
def file_directory
33+
@file_directory ||= Pathname.new(Skunk::Config.root)
34+
end
35+
36+
def file_pathname
37+
Pathname.new(file_directory).join("skunk_console.txt")
38+
end
2539
end
2640
end
2741
end

lib/skunk/generators/html/overview.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ def files
4949
FileData.new(module_data)
5050
end
5151
end
52+
53+
def root_directory
54+
@root_directory ||= Pathname.new(Skunk::Config.root)
55+
end
5256
end
5357
end
5458
end

lib/skunk/generators/json/simple.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
require "pathname"
44

5-
require "rubycritic/configuration"
5+
require "skunk/config"
66
require "skunk/rubycritic/analysed_modules_collection"
77

88
module Skunk
@@ -25,7 +25,7 @@ def data
2525
end
2626

2727
def file_directory
28-
@file_directory ||= Pathname.new(RubyCritic::Config.root)
28+
@file_directory ||= Pathname.new(Skunk::Config.root)
2929
end
3030

3131
def file_pathname

test/lib/skunk/cli/options/argv_test.rb

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,21 @@
55
require "skunk/cli/options/argv"
66

77
describe Skunk::Cli::Options::Argv do
8-
describe "#output_filename" do
9-
context "passing --out=FILE options" do
10-
let(:argv) { ["--out=file.txt"] }
8+
describe "--out path" do
9+
after do
10+
Skunk::Config.reset
11+
end
1112

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
13+
it "sets Skunk::Config.root to the provided path" do
14+
parser = Skunk::Cli::Options::Argv.new(["--out=tmp/custom"])
15+
parser.parse
16+
_(Skunk::Config.root).must_match(/tmp\/custom$/)
1717
end
1818

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
19+
it "defaults to tmp/rubycritic when not provided" do
20+
parser = Skunk::Cli::Options::Argv.new([])
21+
parser.parse
22+
_(Skunk::Config.root).must_match(/tmp\/rubycritic$/)
2523
end
2624
end
2725

0 commit comments

Comments
 (0)