Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

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

* BUGFIX: Pin path_expander < 2.0 for Ruby 2.7 compatibility
* [FEATURE: Indicate `--out PATH` location](https://github.com/fastruby/skunk/pull/131)
* [FEATURE: Add `--formats` CLI flag to select report formats (json, html, console)](https://github.com/fastruby/skunk/pull/130)
* [REFACTOR: Move Console Report](https://github.com/fastruby/skunk/pull/128)
* [BUGFIX: Set the right content type in the share HTTP request](https://github.com/fastruby/skunk/pull/129)
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ Run `skunk -h` to check out the help options:
```
Usage: skunk [options] [paths]
-b, --branch BRANCH Set branch to compare
-o, --out FILE Output report to file
-o, --out PATH Output report path
-v, --version Show gem's version
-h, --help Show this message
```
Expand Down Expand Up @@ -127,7 +127,9 @@ To only run skunk on specific folders, pass a list of directories in the command

### Generate JSON report in background

When the Skunk command is run, it will generate a JSON report file in the `RubyCritic::Config.root` location.
When the Skunk command is run, it will generate a JSON report file in the configured output path.

Skunk also writes the console report to `skunk_report.txt` under the same output path.

### Comparing feature branches

Expand Down
3 changes: 2 additions & 1 deletion bin/console
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ puts ARGV.inspect
require "skunk/cli/application"
require "skunk/config"

Skunk::Config.formats = %i[json console html]
Skunk::Config.formats = %i[json console html] # supported output formats
Skunk::Config.root = "tmp/rubycritic" # default path to store generated JSON and HTML reports.
Skunk::Cli::Application.new(ARGV).execute
14 changes: 2 additions & 12 deletions lib/skunk/cli/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ def execute
command = Skunk::CommandFactory.create(@parsed_options)
reporter = command.execute

print(reporter.status_message)
$stdout.puts(reporter.status_message)
if command.sharing?
share_status_message = command.share(reporter)
print(share_status_message)
$stdout.puts(share_status_message)
end

reporter.status
Expand All @@ -49,16 +49,6 @@ 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, "a") { |file| file << message }
end
end
end
end
end
69 changes: 21 additions & 48 deletions lib/skunk/cli/options/argv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,59 +10,32 @@ 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
parser.new do |opts|
opts.banner = "Usage: skunk [options] [paths]\n"
add_branch_option(opts)
add_output_option(opts)
add_formats_option(opts)
add_tail_options(opts)
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
self.base_branch = String(branch)
set_current_branch
self.mode = :compare_branches
end

opts.on("-o", "--out PATH", "Output report path") do |path|
Skunk::Config.root = path
end

opts.on("-f", "--formats json,html,console", Array, "Output formats: json,html,console") do |list|
Skunk::Config.formats = Array(list).map(&:to_sym)
end

opts.on_tail("-v", "--version", "Show gem's version") do
self.mode = :version
end

opts.on_tail("-h", "--help", "Show this message") do
self.mode = :help
end
end.parse!(@argv)
end

def to_h
super.merge(output_filename: output_filename)
end

private

def add_branch_option(opts)
opts.on("-b", "--branch BRANCH", "Set branch to compare") do |branch|
self.base_branch = String(branch)
set_current_branch
self.mode = :compare_branches
end
end

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

def add_formats_option(opts)
opts.on(
"-f",
"--formats json,html,console",
Array,
"Output formats: json,html,console (default: console)"
) do |list|
Skunk::Config.formats = Array(list).map(&:to_sym)
end
end

def add_tail_options(opts)
opts.on_tail("-v", "--version", "Show gem's version") do
self.mode = :version
end

opts.on_tail("-h", "--help", "Show this message") do
self.mode = :help
end
end
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/skunk/commands/status_sharer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def share

# :reek:UtilityFunction
def base_url
ENV["SHARE_URL"] || DEFAULT_URL
@base_url ||= ENV["SHARE_URL"] || DEFAULT_URL
end

def json_summary
Expand Down Expand Up @@ -73,7 +73,11 @@ def share_enabled?

# @return [Boolean] Check if share URL is empty
def share_url_empty?
ENV["SHARE_URL"].to_s == ""
share_url.empty?
end

def share_url
@share_url ||= ENV["SHARE_URL"].to_s
end

def payload
Expand Down
14 changes: 14 additions & 0 deletions lib/skunk/config.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

require "rubycritic/configuration"

module Skunk
# Utility module for format validation
module FormatValidator
Expand Down Expand Up @@ -28,10 +30,12 @@ class Configuration

def initialize
@formats = [DEFAULT_FORMAT]
@root = RubyCritic::Config.root
end

def set(options = {})
self.formats = options[:formats] if options.key?(:formats)
self.root = options[:root] if options.key?(:root)
end

# Get the configured formats
Expand All @@ -46,6 +50,15 @@ def formats=(format_list)
@formats = [DEFAULT_FORMAT] if @formats.empty?
end

def root
@root || File.expand_path("tmp/rubycritic", Dir.pwd)
end

def root=(path)
path_str = path.to_s
@root = path_str.empty? ? nil : File.expand_path(path_str)
end

# Add a format to the existing list
# @param format [Symbol] Format to add
def add_format(format)
Expand Down Expand Up @@ -77,6 +90,7 @@ def supported_formats
# Reset to default configuration
def reset
@formats = [DEFAULT_FORMAT]
@root = RubyCritic::Config.root
end
end

Expand Down
16 changes: 15 additions & 1 deletion lib/skunk/generators/console_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

require "erb"
require "terminal-table"
require "pathname"
require "fileutils"

require "skunk/generators/console/simple"
require "skunk/config"

module Skunk
module Generator
Expand All @@ -14,14 +17,25 @@ def initialize(analysed_modules)
end

def generate_report
puts generator.render
content = generator.render
puts content
FileUtils.mkdir_p(file_directory)
File.write(file_pathname, content)
end

private

def generator
@generator ||= Skunk::Generator::Console::Simple.new(@analysed_modules)
end

def file_directory
@file_directory ||= Pathname.new(Skunk::Config.root)
end

def file_pathname
Pathname.new(file_directory).join("skunk_report.txt")
end
end
end
end
6 changes: 5 additions & 1 deletion lib/skunk/generators/html/overview.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def initialize(analysed_modules)
end

def file_name
"skunk_overview.html"
"skunk_report.html"
end

def render
Expand All @@ -49,6 +49,10 @@ def files
FileData.new(module_data)
end
end

def root_directory
@root_directory ||= Pathname.new(Skunk::Config.root)
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/skunk/generators/json/simple.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require "pathname"

require "rubycritic/configuration"
require "skunk/config"
require "skunk/rubycritic/analysed_modules_collection"

module Skunk
Expand All @@ -25,7 +25,7 @@ def data
end

def file_directory
@file_directory ||= Pathname.new(RubyCritic::Config.root)
@file_directory ||= Pathname.new(Skunk::Config.root)
end

def file_pathname
Expand Down
1 change: 1 addition & 0 deletions samples/rubycritic/compare/skunk_report.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
test output
35 changes: 19 additions & 16 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 "skunk/commands/default"
require "rubycritic/core/analysed_module"
require "minitest/stub_const"

Expand Down Expand Up @@ -46,14 +47,15 @@
end

context "when passing an environment variable SHARE=true" do
let(:argv) { ["--out=tmp/shared_report.txt", "samples/rubycritic"] }
let(:argv) { ["--out=tmp", "samples/rubycritic"] }
let(:success_code) { 0 }
let(:shared_message) do
"Shared at: https://skunk.fastruby.io/j"
end
let(:generated_message) { "Generated with Skunk" }
let(:shared_message) { "Shared at: https://skunk.fastruby.io/j" }
let(:share_url) { "https://skunk.fastruby.io" }
let(:report_path) { "tmp/skunk_report.txt" }

around do |example|
stub_request(:post, "https://skunk.fastruby.io/reports").to_return(
stub_request(:post, "#{share_url}/reports").to_return(
status: 200,
body: '{"id":"j"}',
headers: { "Content-Type" => "application/json" }
Expand All @@ -62,20 +64,21 @@
end

it "share report to default server" do
FileUtils.rm("tmp/shared_report.txt", force: true)
FileUtils.rm(report_path, force: true)
FileUtils.mkdir_p("tmp")

RubyCritic::AnalysedModule.stub_any_instance(:churn, 1) do
RubyCritic::AnalysedModule.stub_any_instance(:coverage, 100.0) do
Skunk::Command::Default.stub_any_instance(:share_enabled?, true) do
Skunk::Command::StatusSharer.stub_any_instance(:not_sharing?, false) do
Skunk::Command::StatusSharer.stub_any_instance(:share, "Shared at: https://skunk.fastruby.io/j") do
result = application.execute
_(result).must_equal success_code
output = File.read("tmp/shared_report.txt")
_(output).must_include(shared_message)
end
Skunk::Command::Default.stub_any_instance(:share_enabled?, true) do
Skunk::Command::StatusSharer.stub_any_instance(:share_url, share_url) do
Skunk::Command::StatusSharer.stub_any_instance(:share, "Shared at: #{share_url}/j") do
stdout = capture_stdout do
result = application.execute
_(result).must_equal success_code
end
_(File.exist?(report_path)).must_equal true
file_output = File.read(report_path)

_(file_output).must_include(generated_message)
_(stdout).must_include(shared_message)
end
end
end
Expand Down
Loading
Loading