diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e49b5013..698a2e04 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -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 diff --git a/lib/licensed/cli.rb b/lib/licensed/cli.rb index bbca0a46..40f1a462 100644 --- a/lib/licensed/cli.rb +++ b/lib/licensed/cli.rb @@ -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" @@ -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" diff --git a/lib/licensed/commands/cache.rb b/lib/licensed/commands/cache.rb index 53f9dbef..cd29fba6 100644 --- a/lib/licensed/commands/cache.rb +++ b/lib/licensed/commands/cache.rb @@ -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 diff --git a/lib/licensed/commands/command.rb b/lib/licensed/commands/command.rb index c2a23531..3a829c77 100644 --- a/lib/licensed/commands/command.rb +++ b/lib/licensed/commands/command.rb @@ -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 diff --git a/lib/licensed/commands/environment.rb b/lib/licensed/commands/environment.rb index 71616f7d..22481851 100644 --- a/lib/licensed/commands/environment.rb +++ b/lib/licensed/commands/environment.rb @@ -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 diff --git a/lib/licensed/commands/list.rb b/lib/licensed/commands/list.rb index 0c8447e6..64a54b83 100644 --- a/lib/licensed/commands/list.rb +++ b/lib/licensed/commands/list.rb @@ -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 diff --git a/lib/licensed/commands/notices.rb b/lib/licensed/commands/notices.rb index c1d10d52..1ecd972d 100644 --- a/lib/licensed/commands/notices.rb +++ b/lib/licensed/commands/notices.rb @@ -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 diff --git a/lib/licensed/commands/status.rb b/lib/licensed/commands/status.rb index 8c3501b6..e41c8a8b 100644 --- a/lib/licensed/commands/status.rb +++ b/lib/licensed/commands/status.rb @@ -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 diff --git a/licensed.gemspec b/licensed.gemspec index 69f12bb5..5810e19e 100644 --- a/licensed.gemspec +++ b/licensed.gemspec @@ -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 diff --git a/test/commands/cache_test.rb b/test/commands/cache_test.rb index afbb38d2..7fcce99b 100644 --- a/test/commands/cache_test.rb +++ b/test/commands/cache_test.rb @@ -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 @@ -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 @@ -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? @@ -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? @@ -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)) @@ -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? @@ -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}") @@ -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}") @@ -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}") @@ -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}") @@ -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}") @@ -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}") @@ -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}") @@ -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}") @@ -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| @@ -197,14 +196,14 @@ 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/ } @@ -212,7 +211,7 @@ 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 @@ -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}") @@ -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 @@ -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? @@ -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 diff --git a/test/commands/command_test.rb b/test/commands/command_test.rb index 7af818ec..851c337f 100644 --- a/test/commands/command_test.rb +++ b/test/commands/command_test.rb @@ -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 diff --git a/test/commands/environment_test.rb b/test/commands/environment_test.rb index 00de2aa3..e10db860 100644 --- a/test/commands/environment_test.rb +++ b/test/commands/environment_test.rb @@ -1,7 +1,10 @@ # frozen_string_literal: true require "test_helper" +require "test_helpers/command_test_helpers" describe Licensed::Commands::Environment do + include CommandTestHelpers + let(:apps) { [ { @@ -20,19 +23,15 @@ describe "#run" do let(:reporter) { TestReporter.new } - before do - Spy.on(command, :create_reporter).and_return(reporter) - end - it "reports environment information" do - command.run + run_command report = reporter.report assert_equal Licensed::Git.git_repo?, report["git_repo"] end it "reports information for each app" do - command.run + run_command config.apps.each do |app| report = reporter.report.all_reports.find { |r| r.target == app } @@ -44,18 +43,4 @@ end end end - - describe "#create_reporter" do - it "uses a YAML reporter by default" do - assert command.create_reporter({}).is_a?(Licensed::Reporters::YamlReporter) - end - - it "uses a YAML reporter when format is set to yaml" do - assert command.create_reporter(format: "yaml").is_a?(Licensed::Reporters::YamlReporter) - end - - it "uses a JSON reporter when format is set to json" do - assert command.create_reporter(format: "json").is_a?(Licensed::Reporters::JsonReporter) - end - end end diff --git a/test/commands/list_test.rb b/test/commands/list_test.rb index d3d9e731..25b4ac66 100644 --- a/test/commands/list_test.rb +++ b/test/commands/list_test.rb @@ -1,7 +1,10 @@ # frozen_string_literal: true require "test_helper" +require "test_helpers/command_test_helpers" describe Licensed::Commands::List do + include CommandTestHelpers + let(:reporter) { TestReporter.new } let(:apps) { [] } let(:source_config) { {} } @@ -9,10 +12,6 @@ let(:command) { Licensed::Commands::List.new(config: config) } let(:fixtures) { File.expand_path("../../fixtures", __FILE__) } - before do - Spy.on(command, :create_reporter).and_return(reporter) - end - each_source do |source_class| describe "with #{source_class.type}" do let(:source_type) { source_class.type } @@ -24,7 +23,7 @@ enabled = Dir.chdir(app.source_path) { app.sources.any? { |source| source.enabled? } } next unless enabled - command.run + run_command app_report = reporter.report.reports.find { |r| r.target == app } assert app_report @@ -41,7 +40,7 @@ end it "does not include ignored dependencies" do - command.run + run_command dependencies = reporter.report.all_reports assert dependencies.any? { |dependency| dependency.name == "licensed.test.dependency" } count = dependencies.size @@ -50,7 +49,7 @@ app.ignore("type" => "test", "name" => "dependency") end - command.run + run_command dependencies = reporter.report.all_reports refute dependencies.any? { |dependency| dependency.name == "licensed.test.dependency" } ignored_count = dependencies.size @@ -63,7 +62,7 @@ app["source_path"] = fixtures end - command.run + run_command reports = reporter.report.all_reports dependency_report = reports.find { |report| report.target.is_a?(Licensed::Dependency) } @@ -72,7 +71,7 @@ end it "skips a dependency sources not specified in optional :sources argument" do - command.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 @@ -98,7 +97,7 @@ end it "lists dependencies for all apps" do - command.run + run_command config.apps.each do |app| assert reporter.report.reports.find { |report| report.name == app["name"] } end diff --git a/test/commands/notices_test.rb b/test/commands/notices_test.rb index 35c7402d..cc7267a6 100644 --- a/test/commands/notices_test.rb +++ b/test/commands/notices_test.rb @@ -1,19 +1,19 @@ # frozen_string_literal: true require "test_helper" +require "test_helpers/command_test_helpers" describe Licensed::Commands::Notices do + include CommandTestHelpers + let(:cache_path) { Dir.mktmpdir } let(:reporter) { TestReporter.new } let(:config) { Licensed::Configuration.new("cache_path" => cache_path, "sources" => { "test" => true }) } let(:command) { Licensed::Commands::Notices.new(config: config) } before do - Spy.on(command, :create_reporter).and_return(reporter) - generator_config = Marshal.load(Marshal.dump(config)) generator = Licensed::Commands::Cache.new(config: generator_config) - Spy.on(generator, :create_reporter).and_return(TestReporter.new) - generator.run(force: true) + generator.run(force: true, reporter: TestReporter.new) end after do @@ -33,7 +33,7 @@ def dependency_report(app, source, dependency_name = "dependency") end it "reports cached records found for dependencies" do - command.run + run_command config.apps.each do |app| app.sources.each do |source| @@ -50,7 +50,7 @@ def dependency_report(app, source, dependency_name = "dependency") it "reports a warning on missing cached records" do config.apps.each { |app| FileUtils.rm_rf app.cache_path } - command.run + run_command config.apps.each do |app| app.sources.each do |source| @@ -67,7 +67,7 @@ def dependency_report(app, source, dependency_name = "dependency") end it "skips dependency sources not specified in optional :sources argument" do - command.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 diff --git a/test/commands/status_test.rb b/test/commands/status_test.rb index 865a0946..a97d1b29 100644 --- a/test/commands/status_test.rb +++ b/test/commands/status_test.rb @@ -1,23 +1,22 @@ # frozen_string_literal: true require "test_helper" +require "test_helpers/command_test_helpers" describe Licensed::Commands::Status 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(:verifier) { Licensed::Commands::Status.new(config: config) } let(:fixtures) { File.expand_path("../../fixtures", __FILE__) } let(:command) { Licensed::Commands::Status.new(config: config) } before do - Spy.on(verifier, :create_reporter).and_return(reporter) - generator_config = Marshal.load(Marshal.dump(config)) generator = Licensed::Commands::Cache.new(config: generator_config) - Spy.on(generator, :create_reporter).and_return(TestReporter.new) - generator.run(force: true) + generator.run(force: true, reporter: TestReporter.new) end after do @@ -38,7 +37,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "warns if license is not allowed" do - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "license needs review: mit" @@ -54,7 +53,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(path) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes \ @@ -69,7 +68,7 @@ def dependency_errors(app, source, dependency_name = "dependency") app.allow "mit" end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| refute_includes dependency_errors(app, source), "license needs review: mit" @@ -78,7 +77,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "does not warn if dependency is ignored" do - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).any? @@ -86,7 +85,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| @@ -96,7 +95,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "does not warn if dependency is reviewed" do - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).any? @@ -104,7 +103,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).empty? @@ -119,7 +118,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(filename) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "missing license text" @@ -134,7 +133,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(filename) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "missing license text" @@ -149,7 +148,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(filename) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| refute_includes dependency_errors(app, source), "missing license text" @@ -165,7 +164,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(filename) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "cached dependency record out of date" @@ -178,7 +177,7 @@ def dependency_errors(app, source, dependency_name = "dependency") FileUtils.rm app.cache_path.join("test/dependency.#{Licensed::DependencyRecord::EXTENSION}") end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "cached dependency record not found" @@ -192,7 +191,7 @@ def dependency_errors(app, source, dependency_name = "dependency") app.ignore "type" => "test", "name" => "dependency" end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| refute_includes dependency_errors(app, source), "cached dependency record not found" @@ -201,14 +200,14 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "does not include ignored dependencies in dependency counts" do - verifier.run + run_command count = reporter.report.all_reports.size config.apps.each do |app| app.ignore "type" => "test", "name" => "dependency" end - verifier.run + run_command ignored_count = reporter.report.all_reports.size assert_equal count - config.apps.size, ignored_count @@ -219,7 +218,7 @@ def dependency_errors(app, source, dependency_name = "dependency") app["source_path"] = fixtures end - verifier.run + run_command reports = reporter.report.all_reports dependency_report = reports.find { |report| report.target.is_a?(Licensed::Dependency) } @@ -228,7 +227,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "skips a dependency sources not specified in optional :sources argument" do - verifier.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 @@ -254,7 +253,7 @@ def dependency_errors(app, source, dependency_name = "dependency") end it "verifies dependencies for all apps" do - verifier.run + run_command apps.each do |app| assert reporter.report.reports.find { |report| report.name == app["name"] } end @@ -271,7 +270,7 @@ def dependency_errors(app, source, dependency_name = "dependency") record.save(filename) end - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source, "dependency/path"), "missing license text" @@ -309,7 +308,7 @@ def update_records(classification, *licenses) # because the top level license field is allowed update_records("mit", mit, agpl_3) - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).empty? @@ -322,7 +321,7 @@ def update_records(classification, *licenses) # them when the record's top level license field is set to other update_records("agpl-3.0", mit, bsd_3) - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "license needs review: agpl-3.0" @@ -335,7 +334,7 @@ def update_records(classification, *licenses) # and will be checked because the top level license field is set to other update_records("other", mit, agpl_3) - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert_includes dependency_errors(app, source), "license needs review: other" @@ -348,7 +347,7 @@ def update_records(classification, *licenses) # when the top level license field is set to other update_records("other", mit, bsd_3) - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).empty? @@ -361,7 +360,7 @@ def update_records(classification, *licenses) # but not as part of a LICENSE file update_records("other", readme_mit, bsd_3) - verifier.run + run_command config.apps.each do |app| app.sources.each do |source| assert dependency_errors(app, source).empty? @@ -369,18 +368,4 @@ def update_records(classification, *licenses) end end end - - describe "#create_reporter" do - it "uses a status reporter by default" do - assert command.create_reporter({}).is_a?(Licensed::Reporters::StatusReporter) - end - - it "uses a YAML reporter when format is set to yaml" do - assert command.create_reporter(format: "yaml").is_a?(Licensed::Reporters::YamlReporter) - end - - it "uses a JSON reporter when format is set to json" do - assert command.create_reporter(format: "json").is_a?(Licensed::Reporters::JsonReporter) - end - end end diff --git a/test/reporters/cache_reporter_test.rb b/test/reporters/cache_reporter_test.rb index c8e8fbdc..80594523 100644 --- a/test/reporters/cache_reporter_test.rb +++ b/test/reporters/cache_reporter_test.rb @@ -7,7 +7,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_app" do it "runs a block" do diff --git a/test/reporters/json_reporter_test.rb b/test/reporters/json_reporter_test.rb index 90b2a82d..095c9108 100644 --- a/test/reporters/json_reporter_test.rb +++ b/test/reporters/json_reporter_test.rb @@ -8,7 +8,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_run" do it "runs a block" do diff --git a/test/reporters/list_reporter_test.rb b/test/reporters/list_reporter_test.rb index 5c614a8c..dabdf4d7 100644 --- a/test/reporters/list_reporter_test.rb +++ b/test/reporters/list_reporter_test.rb @@ -7,7 +7,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_app" do it "runs a block" do diff --git a/test/reporters/notices_reporter_test.rb b/test/reporters/notices_reporter_test.rb index 7d58f776..3ee92846 100644 --- a/test/reporters/notices_reporter_test.rb +++ b/test/reporters/notices_reporter_test.rb @@ -7,7 +7,7 @@ let(:reporter) { Licensed::Reporters::NoticesReporter.new(shell) } let(:app) { Licensed::AppConfiguration.new("source_path" => Dir.pwd, "cache_path" => cache_path) } let(:source) { TestSource.new(app) } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } after do FileUtils.rm_rf app.cache_path diff --git a/test/reporters/reporter_test.rb b/test/reporters/reporter_test.rb index 1e06f3b0..57bbc981 100644 --- a/test/reporters/reporter_test.rb +++ b/test/reporters/reporter_test.rb @@ -6,7 +6,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_run" do it "runs a block" do diff --git a/test/reporters/status_reporter_test.rb b/test/reporters/status_reporter_test.rb index ce72369b..c52cfb63 100644 --- a/test/reporters/status_reporter_test.rb +++ b/test/reporters/status_reporter_test.rb @@ -7,7 +7,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_app" do it "runs a block" do diff --git a/test/reporters/yaml_reporter_test.rb b/test/reporters/yaml_reporter_test.rb index 819d2184..18962558 100644 --- a/test/reporters/yaml_reporter_test.rb +++ b/test/reporters/yaml_reporter_test.rb @@ -7,7 +7,7 @@ let(:app) { Licensed::AppConfiguration.new({ "source_path" => Dir.pwd }) } let(:source) { TestSource.new(app) } let(:dependency) { source.dependencies.first } - let(:command) { TestCommand.new(config: nil, reporter: reporter) } + let(:command) { TestCommand.new(config: nil) } describe "#report_run" do it "runs a block" do diff --git a/test/test_helper.rb b/test/test_helper.rb index 62168777..4c0aee37 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -3,7 +3,6 @@ require "minitest/autorun" require "mocha/minitest" require "byebug" -require "spy" require "licensed" require "English" require "test_helpers/test_command" diff --git a/test/test_helpers/command_test_helpers.rb b/test/test_helpers/command_test_helpers.rb new file mode 100644 index 00000000..0a947729 --- /dev/null +++ b/test/test_helpers/command_test_helpers.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +module CommandTestHelpers + def run_command(**options) + if defined?(reporter) && !options.key?(:reporter) + # automatically set the defined reporter if a reporter is not explicitly set + options = options.merge(reporter: reporter) + end + + # expects the test class to define "command" + command.run(**options) + end +end diff --git a/test/test_helpers/test_command.rb b/test/test_helpers/test_command.rb index 1a7f6dee..1029ad60 100644 --- a/test/test_helpers/test_command.rb +++ b/test/test_helpers/test_command.rb @@ -1,21 +1,19 @@ # frozen_string_literal: true class TestCommand < Licensed::Commands::Command - def initialize(config:, reporter: TestReporter.new) - super(config: config) - @test_reporter = reporter - end - def reporter @test_reporter end def create_reporter(options) - @test_reporter + @test_reporter ||= super(options) + end + + def default_reporter(options) + TestReporter.new end def run(**options) super do |report| - # byebug report["extra"] = true next :skip if options[:skip_run] end