Skip to content

Commit b6f554e

Browse files
authored
Allow Skunk to generate reports in a given path with the --out flag (#131)
* chore: update rubocop todo configuration Update rubocop todo file with latest offense counts and configurations * 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. * Use same name on JSON, Console and HTML reports Skunk_report (txt,json,html) * test: fix share report sharing output * refactor: apply review feedback on --out flag - Write reporter output with $stdout.puts directly instead of a private print wrapper in the application - Memoize the SHARE_URL lookups in StatusSharer and use String#empty? - Simplify Skunk::Config#root= now that to_s covers the nil case - Assert the default root against an explicit RubyCritic::Config.root instead of a path regex, restoring the prior value afterwards * style: restore RubyCritic root via an after hook in argv test RuboCop derives TargetRubyVersion from the gemspec's required_ruby_version (>= 2.4.0), so a begin/ensure spanning the whole block body trips Style/RedundantBegin on the RuboCop 1.31/1.50 resolved for Ruby 2.6 and 2.7, and the block level ensure that would replace it is 2.6+ syntax that RuboCop 1.81 rejects under the 2.4 parser. Move the restore into a minitest after hook, which is valid on every supported Ruby. * build: pin rubocop and drop stray sample report Every CI job resolves its own Gemfile.lock, so an unpinned rubocop meant Ruby 2.7 linted with 1.31, 2.6 with 1.50 and 3.x with 1.81, and a cop whose behavior changed between them failed the build on the old rubies only. Pin the version so the matrix lints against one rubocop. Rubies older than 3.1 cannot install the pin: reek and rubycritic hold parser below 3.3 there, while rubocop 1.60 and up require it. The Gemfile leaves rubocop out on those rubies and the Rakefile drops the lint task when the require fails, so 2.6, 2.7 and 3.0 still run the tests and reek. The gemspec listed rubocop as an unpinned development dependency, which is what drove the resolution, so drop it and let the Gemfile hold the pin. It stays a Gemfile-only dev tool, like it already was in practice. Also drop samples/rubycritic/compare/skunk_report.txt, a stray "test output" file no test reads. * Fix typo * fix(cli): keep --out separate from RubyCritic root --out now sets only Skunk::Config.root. When it is absent, Skunk reads RubyCritic::Config.root at the moment it is needed instead of copying it on reset, so .rubycritic.yml is honored and a root left over from an earlier compare run is never reused. Compare mode wrote build_details.txt to tmp/rubycritic/compare even with --out. It now goes to <out>/compare/build_details.txt. --out rejects an empty path, which made RubyCritic write reports into the current directory, and an existing file, the old -o FILE usage. Both exit 1 with an error. skunk_report.txt is written only with --out, so a plain skunk run still writes no files. SHARE_URL="" now falls back to the default share URL. bin/console passes its formats through argv because Application#execute resets the config. CHANGELOG marks --out as a breaking change and notes the skunk_report.html rename. * test: reset Skunk::Config after --out tests The application share test runs with --out=tmp and left Skunk::Config.root set. With some seeds (24866 in CI) the compare test ran next, so build_details went to tmp/compare instead of the stubbed directory and the assertion failed.
1 parent 7841c88 commit b6f554e

21 files changed

Lines changed: 287 additions & 110 deletions

File tree

‎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+
* [BREAKING: `-o/--out` now takes an output directory instead of a file. Reports, and `skunk_report.txt` with the console output, are written there. The HTML report is renamed from `skunk_overview.html` to `skunk_report.html`](https://github.com/fastruby/skunk/pull/131)
1011
* [FEATURE: Add sortable columns to the Skunk HTML report](https://github.com/fastruby/skunk/pull/142)
1112
* [ENHANCEMENT: Support SimpleCov 1.0](https://github.com/fastruby/skunk/pull/140)
1213
* BUGFIX: Pin path_expander < 2.0 for Ruby 2.7 compatibility

‎Gemfile‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@ git_source(:github) { |repo_name| "https://github.com/#{repo_name}" }
88
gemspec
99

1010
gem "reek"
11-
gem "rubocop"
11+
12+
# Pinned so the CI matrix lints against one RuboCop instead of each Ruby
13+
# resolving whatever version it happens to support. Rubies older than 3.1 cannot
14+
# install it: reek and rubycritic hold parser below 3.3 there, while RuboCop
15+
# 1.60 and up require it, so those rubies get no RuboCop and the Rakefile drops
16+
# the lint task for them. They still run the tests and reek.
17+
gem "rubocop", "~> 1.81.0" if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("3.1")

‎README.md‎

Lines changed: 5 additions & 3 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 directory for reports
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+
When `--out PATH` is given, Skunk also writes the console report to `skunk_report.txt` in that directory.
131133

132134
### Comparing feature branches
133135

@@ -171,7 +173,7 @@ You can choose one or more formats from the command line:
171173

172174
```
173175
skunk --formats=json
174-
skunk --f json,html
176+
skunk -f json,html
175177
skunk --formats console,json
176178
```
177179

‎Rakefile‎

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

33
require "bundler/gem_tasks"
44
require "rake/testtask"
5-
require "rubocop/rake_task"
65
require "reek/rake/task"
76
require "rubycritic/rake_task"
87

@@ -12,12 +11,20 @@ Rake::TestTask.new do |task|
1211
task.pattern = "test/**/*_test.rb"
1312
end
1413

15-
RuboCop::RakeTask.new
16-
1714
Reek::Rake::Task.new
1815

1916
RubyCritic::RakeTask.new do |task|
2017
task.paths = FileList["lib/**/*.rb"]
2118
end
2219

23-
task default: %i[test reek rubocop]
20+
lint_tasks = []
21+
22+
begin
23+
require "rubocop/rake_task"
24+
RuboCop::RakeTask.new
25+
lint_tasks << :rubocop
26+
rescue LoadError
27+
# RuboCop is not installed on the rubies the Gemfile skips the pin for.
28+
end
29+
30+
task default: %i[test reek] + lint_tasks

‎bin/console‎

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

16-
Skunk::Config.formats = %i[json console html]
17-
Skunk::Cli::Application.new(ARGV).execute
16+
argv = ARGV.any? { |arg| arg.start_with?("-f", "--formats") } ? ARGV : ["--formats=json,console,html", *ARGV]
17+
Skunk::Cli::Application.new(argv).execute

‎lib/skunk/cli/application.rb‎

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,14 @@ def execute
3131
command = Skunk::CommandFactory.create(@parsed_options)
3232
reporter = command.execute
3333

34-
print(reporter.status_message)
34+
$stdout.puts(reporter.status_message)
3535
if command.sharing?
3636
share_status_message = command.share(reporter)
37-
print(share_status_message)
37+
$stdout.puts(share_status_message)
3838
end
3939

4040
reporter.status
41-
rescue OptionParser::InvalidOption => e
41+
rescue OptionParser::ParseError => e
4242
warn "Error: #{e}"
4343
STATUS_ERROR
4444
end
@@ -49,16 +49,6 @@ def warn_coverage_info
4949
warn "warning: Couldn't find coverage info at #{COVERAGE_FILE}."
5050
warn "warning: Having no coverage metrics will make your SkunkScore worse."
5151
end
52-
53-
# :reek:NilCheck
54-
def print(message)
55-
filename = @parsed_options[:output_filename]
56-
if filename.nil?
57-
$stdout.puts(message)
58-
else
59-
File.open(filename, "a") { |file| file << message }
60-
end
61-
end
6252
end
6353
end
6454
end

‎lib/skunk/cli/options/argv.rb‎

Lines changed: 27 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,42 @@ 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"
19-
add_branch_option(opts)
20-
add_output_option(opts)
21-
add_formats_option(opts)
22-
add_tail_options(opts)
16+
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
17+
self.base_branch = String(branch)
18+
set_current_branch
19+
self.mode = :compare_branches
20+
end
21+
22+
opts.on("-o", "--out PATH", "Output directory for reports") do |path|
23+
self.output_path = path
24+
end
25+
26+
opts.on("-f", "--formats json,html,console", Array, "Output formats: json,html,console") do |list|
27+
Skunk::Config.formats = Array(list).map(&:to_sym)
28+
end
29+
30+
opts.on_tail("-v", "--version", "Show gem's version") do
31+
self.mode = :version
32+
end
33+
34+
opts.on_tail("-h", "--help", "Show this message") do
35+
self.mode = :help
36+
end
2337
end.parse!(@argv)
2438
end
2539

26-
def to_h
27-
super.merge(output_filename: output_filename)
28-
end
29-
3040
private
3141

32-
def add_branch_option(opts)
33-
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
34-
self.base_branch = String(branch)
35-
set_current_branch
36-
self.mode = :compare_branches
37-
end
38-
end
39-
40-
def add_output_option(opts)
41-
opts.on("-o", "--out FILE", "Output report to file") do |filename|
42-
self.output_filename = filename
43-
end
44-
end
45-
46-
def add_formats_option(opts)
47-
opts.on(
48-
"-f",
49-
"--formats json,html,console",
50-
Array,
51-
"Output formats: json,html,console (default: console)"
52-
) do |list|
53-
Skunk::Config.formats = Array(list).map(&:to_sym)
54-
end
55-
end
56-
57-
def add_tail_options(opts)
58-
opts.on_tail("-v", "--version", "Show gem's version") do
59-
self.mode = :version
60-
end
42+
# Moves only Skunk's reports. RubyCritic::Config.root stays under
43+
# RubyCritic's control, and Skunk::Config.root falls back to it
44+
def output_path=(path)
45+
raise OptionParser::InvalidArgument, "expects a directory" if path.to_s.strip.empty?
46+
raise OptionParser::InvalidArgument, "expects a directory, but #{path} is a file" if File.file?(path)
6147

62-
opts.on_tail("-h", "--help", "Show this message") do
63-
self.mode = :help
64-
end
48+
Skunk::Config.root = path
6549
end
6650
end
6751
end

‎lib/skunk/commands/compare.rb‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "skunk/commands/output"
66
require "skunk/commands/shareable"
77
require "skunk/commands/compare_score"
8+
require "skunk/config"
89

910
# nodoc #
1011
module Skunk
@@ -51,13 +52,22 @@ def build_details
5152
::RubyCritic::Config.feature_branch_score.to_f.round(2)
5253
).message
5354

54-
Skunk::Command::Output.create_directory(::RubyCritic::Config.compare_root_directory)
55+
Skunk::Command::Output.create_directory(compare_directory)
5556
File.open(build_details_path, "w") { |file| file.write(details) }
5657
puts details
5758
end
5859

5960
def build_details_path
60-
"#{::RubyCritic::Config.compare_root_directory}/build_details.txt"
61+
"#{compare_directory}/build_details.txt"
62+
end
63+
64+
# Uses --out (Skunk::Config.root) when given, otherwise RubyCritic's
65+
# compare directory
66+
# :reek:UtilityFunction
67+
def compare_directory
68+
return ::RubyCritic::Config.compare_root_directory unless Skunk::Config.root_set?
69+
70+
File.join(Skunk::Config.root, "compare")
6171
end
6272
end
6373
end

‎lib/skunk/commands/status_sharer.rb‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,8 @@ def share
3333

3434
private
3535

36-
# :reek:UtilityFunction
3736
def base_url
38-
ENV["SHARE_URL"] || DEFAULT_URL
37+
share_url_empty? ? DEFAULT_URL : share_url
3938
end
4039

4140
def json_summary
@@ -73,7 +72,11 @@ def share_enabled?
7372

7473
# @return [Boolean] Check if share URL is empty
7574
def share_url_empty?
76-
ENV["SHARE_URL"].to_s == ""
75+
share_url.empty?
76+
end
77+
78+
def share_url
79+
@share_url ||= ENV["SHARE_URL"].to_s
7780
end
7881

7982
def payload

‎lib/skunk/config.rb‎

Lines changed: 22 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 = nil
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,23 @@ def formats=(format_list)
4650
@formats = [DEFAULT_FORMAT] if @formats.empty?
4751
end
4852

53+
# Path where reports are written. An explicit value (from --out) wins;
54+
# otherwise it follows RubyCritic::Config.root, which RubyCritic sets on
55+
# every run, so a stale value from an earlier run is never reused.
56+
def root
57+
@root || RubyCritic::Config.root || File.expand_path("tmp/rubycritic", Dir.pwd)
58+
end
59+
60+
# @return [Boolean] True when the output path was set explicitly
61+
def root_set?
62+
!!@root
63+
end
64+
65+
def root=(path)
66+
path_str = path.to_s
67+
@root = path_str.empty? ? nil : File.expand_path(path_str)
68+
end
69+
4970
# Add a format to the existing list
5071
# @param format [Symbol] Format to add
5172
def add_format(format)
@@ -77,6 +98,7 @@ def supported_formats
7798
# Reset to default configuration
7899
def reset
79100
@formats = [DEFAULT_FORMAT]
101+
@root = nil
80102
end
81103
end
82104

0 commit comments

Comments
 (0)