Skip to content

Commit

Permalink
Merge pull request #305 from github/fix-ruby-2.4-ci
Browse files Browse the repository at this point in the history
Fix CI for ruby 2.4
  • Loading branch information
jonabc authored Sep 23, 2020
2 parents 248e470 + 5c3134b commit 3d1a11c
Show file tree
Hide file tree
Showing 25 changed files with 159 additions and 154 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
ruby: [ 2.4.x, 2.5.x, 2.6.x ]
ruby: [ 2.4.x, 2.5.x, 2.6.x, 2.7.x ]
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
Expand Down
4 changes: 2 additions & 2 deletions lib/licensed/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def cache
method_option :sources, aliases: "-s", type: :array,
desc: "Individual source(s) to evaluate. Must also be enabled via configuration."
def status
run Licensed::Commands::Status.new(config: config), sources: options[:sources], format: options[:format]
run Licensed::Commands::Status.new(config: config), sources: options[:sources], reporter: options[:format]
end

desc "list", "List dependencies"
Expand Down Expand Up @@ -59,7 +59,7 @@ def version
method_option :config, aliases: "-c", type: :string,
desc: "Path to licensed configuration file"
def env
run Licensed::Commands::Environment.new(config: config), format: options[:format]
run Licensed::Commands::Environment.new(config: config), reporter: options[:format]
end

desc "migrate", "Migrate from a previous version of licensed"
Expand Down
6 changes: 3 additions & 3 deletions lib/licensed/commands/cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
module Licensed
module Commands
class Cache < Command
# Create a reporter to use during a command run
# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Raises a Licensed::Reporters::CacheReporter
def create_reporter(options)
# Returns a Licensed::Reporters::CacheReporter
def default_reporter(options)
Licensed::Reporters::CacheReporter.new
end

Expand Down
22 changes: 19 additions & 3 deletions lib/licensed/commands/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,29 @@ def run(**options)
result
end

# Create a reporter to use during a command run
# Creates a reporter to use during a command run
#
# options - The options the command was run with
#
# Raises an error
# Returns the reporter to use during the command run
def create_reporter(options)
raise "`create_reporter` must be implemented by commands"
return options[:reporter] if options[:reporter].is_a?(Licensed::Reporters::Reporter)

if options[:reporter].is_a?(String)
klass = "#{options[:reporter].capitalize}Reporter"
return Licensed::Reporters.const_get(klass).new if Licensed::Reporters.const_defined?(klass)
end

default_reporter(options)
end

# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Raises an error
def default_reporter(options)
raise "`default_reporter` must be implemented by commands"
end

protected
Expand Down
14 changes: 7 additions & 7 deletions lib/licensed/commands/environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def run(**options)
end
end

def create_reporter(options)
case options[:format]
when "json"
Licensed::Reporters::JsonReporter.new
else
Licensed::Reporters::YamlReporter.new
end
# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Returns a Licensed::Reporters::StatusReporter
def default_reporter(options)
Licensed::Reporters::YamlReporter.new
end

protected
Expand Down
4 changes: 2 additions & 2 deletions lib/licensed/commands/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
module Licensed
module Commands
class List < Command
# Create a reporter to use during a command run
# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Returns a Licensed::Reporters::ListReporter
def create_reporter(options)
def default_reporter(options)
Licensed::Reporters::ListReporter.new
end

Expand Down
6 changes: 3 additions & 3 deletions lib/licensed/commands/notices.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
module Licensed
module Commands
class Notices < Command
# Create a reporter to use during a command run
# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Raises a Licensed::Reporters::CacheReporter
def create_reporter(options)
# Returns a Licensed::Reporters::CacheReporter
def default_reporter(options)
Licensed::Reporters::NoticesReporter.new
end

Expand Down
13 changes: 3 additions & 10 deletions lib/licensed/commands/status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,13 @@
module Licensed
module Commands
class Status < Command
# Create a reporter to use during a command run
# Returns the default reporter to use during the command run
#
# options - The options the command was run with
#
# Returns a Licensed::Reporters::StatusReporter
def create_reporter(options)
case options[:format]
when "json"
Licensed::Reporters::JsonReporter.new
when "yaml"
Licensed::Reporters::YamlReporter.new
else
Licensed::Reporters::StatusReporter.new
end
def default_reporter(options)
Licensed::Reporters::StatusReporter.new
end

protected
Expand Down
1 change: 0 additions & 1 deletion licensed.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,4 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "rubocop", "~> 0.49", "< 0.67"
spec.add_development_dependency "rubocop-github", "~> 0.6"
spec.add_development_dependency "byebug", "~> 10.0.0"
spec.add_development_dependency "spy", "~> 1.0.0"
end
53 changes: 26 additions & 27 deletions test/commands/cache_test.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
# frozen_string_literal: true
require "test_helper"
require "test_helpers/command_test_helpers"

describe Licensed::Commands::Cache do
include CommandTestHelpers

let(:cache_path) { Dir.mktmpdir }
let(:reporter) { TestReporter.new }
let(:apps) { [] }
let(:source_config) { {} }
let(:config) { Licensed::Configuration.new("apps" => apps, "cache_path" => cache_path, "sources" => { "test" => true }, "test" => source_config) }
let(:generator) do
command = Licensed::Commands::Cache.new(config: config)
Spy.on(command, :create_reporter).and_return(reporter)
command
end
let(:reporter) { TestReporter.new }
let(:command) { Licensed::Commands::Cache.new(config: config) }
let(:fixtures) { File.expand_path("../../fixtures", __FILE__) }

after do
Expand All @@ -31,7 +30,7 @@
enabled = Dir.chdir(app.source_path) { app.sources.any? { |source| source.enabled? } }
next unless enabled

generator.run
run_command

expected_dependency = app["expected_dependency"]
expected_dependency_name = app["expected_dependency_name"] || expected_dependency
Expand All @@ -51,7 +50,7 @@
File.write app.cache_path.join("test/old_dep.#{Licensed::DependencyRecord::EXTENSION}"), ""
end

generator.run
run_command

config.apps.each do |app|
refute app.cache_path.join("test/old_dep.#{Licensed::DependencyRecord::EXTENSION}").exist?
Expand All @@ -65,7 +64,7 @@
app.ignore "type" => "test", "name" => "dependency"
end

generator.run
run_command

config.apps.each do |app|
refute app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}").exist?
Expand All @@ -76,7 +75,7 @@
apps << { "source_path" => Dir.pwd, "cache_path" => cache_path, "test" => { name: 1 } }
apps << { "source_path" => Dir.pwd, "cache_path" => cache_path, "test" => { name: 2 } }

generator.run
run_command

files = Dir.glob(File.join(cache_path, "**/*.#{Licensed::DependencyRecord::EXTENSION}"))
.map(&File.method(:basename))
Expand All @@ -92,7 +91,7 @@
File.write app.cache_path.join("other", "dep.#{Licensed::DependencyRecord::EXTENSION}"), ""
end

generator.run
run_command

config.apps.each do |app|
assert app.cache_path.join("root.#{Licensed::DependencyRecord::EXTENSION}").exist?
Expand All @@ -101,7 +100,7 @@
end

it "uses cached record if license text does not change" do
generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -111,7 +110,7 @@
record.save(path)
end

generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -122,7 +121,7 @@
end

it "requires re-review after license text changes" do
generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -135,7 +134,7 @@
app.review(record)
end

generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -146,7 +145,7 @@
end

it "does not reuse nil record version" do
generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -156,7 +155,7 @@
record.save(path)
end

generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -167,7 +166,7 @@
end

it "does not reuse empty record version" do
generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -177,7 +176,7 @@
record.save(path)
end

generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -188,7 +187,7 @@
end

it "does not include ignored dependencies in dependency counts" do
generator.run
run_command
count = reporter.report.all_reports.size

config.apps.each do |app|
Expand All @@ -197,22 +196,22 @@
app.ignore "type" => "test", "name" => "dependency"
end

generator.run
run_command
ignored_count = reporter.report.all_reports.size
assert_equal count - config.apps.size, ignored_count
end

it "reports a warning when a dependency doesn't exist" do
source_config[:path] = File.join(Dir.pwd, "non-existant")
generator.run
run_command
report = reporter.report.all_reports.find { |r| r.name&.include?("dependency") }
refute_empty report.warnings
assert report.warnings.any? { |w| w =~ /expected dependency path .*? does not exist/ }
end

it "reports an error when a dependency's path is empty" do
source_config[:path] = nil
generator.run
run_command
report = reporter.report.all_reports.find { |r| r.name&.include?("dependency") }
assert_includes report.errors, "dependency path not found"
end
Expand All @@ -222,7 +221,7 @@
app["source_path"] = fixtures
end

generator.run
run_command

config.apps.each do |app|
path = app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}")
Expand All @@ -232,7 +231,7 @@
end

it "skips a dependency sources not specified in optional :sources argument" do
generator.run(sources: "alternate")
run_command(sources: "alternate")

report = reporter.report.all_reports.find { |r| r.target.is_a?(Licensed::Sources::Source) }
refute_empty report.warnings
Expand Down Expand Up @@ -261,7 +260,7 @@
end

it "caches metadata for all apps" do
generator.run
run_command
config.apps.each do |app|
assert app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}").exist?
assert app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}").exist?
Expand All @@ -273,7 +272,7 @@
let(:source_config) { { name: "dependency/path" } }

it "caches metadata at the given file path" do
generator.run
run_command
config.apps.each do |app|
assert app.cache_path.join("test/dependency/path.#{Licensed::DependencyRecord::EXTENSION}").exist?
end
Expand Down
19 changes: 19 additions & 0 deletions test/commands/command_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,23 @@
report = command.reporter.report.all_reports.find { |r| r.target.is_a?(Licensed::Dependency) }
refute_equal true, report["evaluated"]
end

describe "#create_reporter" do
it "uses a YAML reporter when reporter is set to yaml" do
assert command.create_reporter(reporter: "yaml").is_a?(Licensed::Reporters::YamlReporter)
end

it "uses a JSON reporter when reporter is set to json" do
assert command.create_reporter(reporter: "json").is_a?(Licensed::Reporters::JsonReporter)
end

it "uses a passed in reporter if given" do
reporter = Licensed::Reporters::StatusReporter.new
assert_equal reporter, command.create_reporter(reporter: reporter)
end

it "uses the commands default_reporter by default" do
assert command.create_reporter({}).is_a?(TestReporter)
end
end
end
Loading

0 comments on commit 3d1a11c

Please sign in to comment.