diff --git a/features/features/cli/cli_spec.rb b/features/features/cli/cli_spec.rb index e77eb117a..4c75d5f89 100644 --- a/features/features/cli/cli_spec.rb +++ b/features/features/cli/cli_spec.rb @@ -98,4 +98,30 @@ expect(developer).to_not be_seeing('MIT') end end + + describe 'running project_roots' do + before do + @project = LicenseFinder::TestingDSL::CompositeProject.create + end + + context 'with --recursive flag' do + let(:license_finder_command) { 'license_finder project_roots --recursive' } + + specify 'returns all project paths' do + developer.execute_command(license_finder_command) + + expect(developer).to be_seeing_something_like %r{\"#{Regexp.escape(@project.project_dir.to_s)}/multi-module-gradle\"} + expect(developer).to be_seeing_something_like %r{\"#{Regexp.escape(@project.project_dir.to_s)}/single-module-gradle\"} + end + end + + context 'without flags' do + let(:license_finder_command) { 'license_finder project_roots' } + + specify 'returns current path' do + developer.execute_command(license_finder_command) + expect(developer).to be_seeing_something_like /^#{Regexp.escape(@project.project_dir.to_s)}$/ + end + end + end end diff --git a/lib/license_finder/cli/main.rb b/lib/license_finder/cli/main.rb index ae2ff7a78..f6f4b2b3b 100644 --- a/lib/license_finder/cli/main.rb +++ b/lib/license_finder/cli/main.rb @@ -80,6 +80,13 @@ def self.shared_options type: :array end + desc 'project_roots', 'List project directories to be scanned' + shared_options + def project_roots + config.strict_matching = true + aggregate_paths + end + desc 'action_items', 'List unapproved dependencies (the default action for `license_finder`)' shared_options format_option @@ -162,7 +169,8 @@ def aggregate_paths check_valid_project_path aggregate_paths = config.aggregate_paths project_path = config.project_path || Pathname.pwd - aggregate_paths = ProjectFinder.new(project_path).find_projects if config.recursive + aggregate_paths = ProjectFinder.new(project_path, config.strict_matching).find_projects if config.recursive + say(aggregate_paths || project_path) if config.strict_matching return aggregate_paths unless aggregate_paths.nil? || aggregate_paths.empty? [config.project_path] unless config.project_path.nil? end diff --git a/lib/license_finder/configuration.rb b/lib/license_finder/configuration.rb index 2b9b2a323..482b5babc 100644 --- a/lib/license_finder/configuration.rb +++ b/lib/license_finder/configuration.rb @@ -122,6 +122,10 @@ def sbt_include_groups get(:sbt_include_groups) end + attr_writer :strict_matching + + attr_reader :strict_matching + protected attr_accessor :primary_config diff --git a/lib/license_finder/package_managers/go_workspace.rb b/lib/license_finder/package_managers/go_workspace.rb index f7b967788..68fe150cf 100644 --- a/lib/license_finder/package_managers/go_workspace.rb +++ b/lib/license_finder/package_managers/go_workspace.rb @@ -1,5 +1,4 @@ require 'json' - module LicenseFinder class GoWorkspace < PackageManager Submodule = Struct.new :install_path, :revision @@ -8,6 +7,7 @@ class GoWorkspace < PackageManager def initialize(options = {}) super @full_version = options[:go_full_version] + @strict_matching = options[:strict_matching] end def self.package_management_command @@ -38,6 +38,7 @@ def possible_package_paths end def active? + return false if @strict_matching godep = LicenseFinder::GoDep.new(project_path: Pathname(project_path)) # go workspace is only active if GoDep wasn't. There are some projects # that will use the .envrc and have a Godep folder as well. diff --git a/lib/license_finder/project_finder.rb b/lib/license_finder/project_finder.rb index 92853e730..e7ac45ab5 100644 --- a/lib/license_finder/project_finder.rb +++ b/lib/license_finder/project_finder.rb @@ -1,7 +1,8 @@ module LicenseFinder class ProjectFinder - def initialize(main_project_path) + def initialize(main_project_path, strict_matching = false) @package_managers = LicenseFinder::Scanner::PACKAGE_MANAGERS + @strict_matching = strict_matching @main_project_path = main_project_path end @@ -41,7 +42,7 @@ def project_root?(pathname) def active_project?(project_path) active_project = @package_managers.map do |pm| - pm.new(project_path: project_path).active? + pm.new(project_path: project_path, strict_matching: @strict_matching).active? end active_project.include?(true) end diff --git a/spec/lib/license_finder/cli/main_spec.rb b/spec/lib/license_finder/cli/main_spec.rb index 15ebc658d..19fcc34c3 100644 --- a/spec/lib/license_finder/cli/main_spec.rb +++ b/spec/lib/license_finder/cli/main_spec.rb @@ -91,7 +91,7 @@ module CLI it 'passes the config options to the new LicenseFinder::LicenseAggregator instance' do stubs = { valid_project_path?: true, project_path: Pathname('../other_project'), - decisions_file_path: Pathname('../other_project'), aggregate_paths: nil, recursive: false, format: nil, columns: nil } + decisions_file_path: Pathname('../other_project'), aggregate_paths: nil, recursive: false, format: nil, columns: nil, strict_matching: false } configuration = double(:configuration, stubs) allow(LicenseFinder::Configuration).to receive(:with_optional_saved_config).and_return(configuration) expect(LicenseFinder::LicenseAggregator).to receive(:new).with(configuration, anything).and_return(license_finder_instance)