diff --git a/lib/licensed/commands/command.rb b/lib/licensed/commands/command.rb index 3a829c77..c0644a27 100644 --- a/lib/licensed/commands/command.rb +++ b/lib/licensed/commands/command.rb @@ -72,6 +72,12 @@ def default_reporter(options) # Returns whether the command succeeded for the application. def run_app(app) reporter.report_app(app) do |report| + # ensure the app source path exists before evaluation + if !Dir.exist?(app.source_path) + report.errors << "No such directory #{app.source_path}" + next false + end + Dir.chdir app.source_path do begin # allow additional report data to be given by commands diff --git a/lib/licensed/configuration.rb b/lib/licensed/configuration.rb index 069a0cf0..83aa68d2 100644 --- a/lib/licensed/configuration.rb +++ b/lib/licensed/configuration.rb @@ -158,19 +158,22 @@ def initialize(options = {}) def self.expand_app_source_path(app_config) return app_config if app_config["source_path"].to_s.empty? + # check if the source path maps to an existing directory source_path = File.expand_path(app_config["source_path"], AppConfiguration.root_for(app_config)) + return app_config if Dir.exist?(source_path) + + # try to expand the source path for glob patterns expanded_source_paths = Dir.glob(source_path).select { |p| File.directory?(p) } - # return the original configuration if glob didn't result in multiple paths - return app_config if expanded_source_paths.size <= 1 + configs = expanded_source_paths.map { |path| app_config.merge("source_path" => path) } - # map the expanded paths to new application configurations - expanded_source_paths.map do |path| - config = app_config.merge("source_path" => path) + # if no directories are found for the source path, return the original config + return app_config if configs.size == 0 - # update configured values for name and cache_path for uniqueness. - # this is only needed when values are explicitly set, AppConfiguration - # will handle configurations that don't have these explicitly set - dir_name = File.basename(path) + # update configured values for name and cache_path for uniqueness. + # this is only needed when values are explicitly set, AppConfiguration + # will handle configurations that don't have these explicitly set + configs.each do |config| + dir_name = File.basename(config["source_path"]) config["name"] = "#{config["name"]}-#{dir_name}" if config["name"] # if a cache_path is set and is not marked as shared, append the app name @@ -178,9 +181,9 @@ def self.expand_app_source_path(app_config) if config["cache_path"] && config["shared_cache"] != true config["cache_path"] = File.join(config["cache_path"], dir_name) end - - config end + + configs end # Find a default configuration file in the given directory. diff --git a/test/commands/command_test.rb b/test/commands/command_test.rb index 851c337f..c8b19885 100644 --- a/test/commands/command_test.rb +++ b/test/commands/command_test.rb @@ -103,6 +103,19 @@ end end + it "catches and reports a non-existent app source path" do + nonexistent_path = File.join(Dir.pwd, "nonexistent") + apps.each { |app| app["source_path"] = nonexistent_path } + + refute command.run + + reports = command.reporter.report.all_reports.select { |r| r.target.is_a?(Licensed::AppConfiguration) } + refute_empty reports + reports.each do |r| + assert_includes r.errors, "No such directory #{nonexistent_path}" + end + end + it "allows implementations to add extra data to reports with a yielded block" do assert command.run diff --git a/test/configuration_test.rb b/test/configuration_test.rb index d6de7733..21053c55 100644 --- a/test/configuration_test.rb +++ b/test/configuration_test.rb @@ -111,6 +111,10 @@ assert_equal 2, config.apps.size assert_equal "app1", config.apps[0]["name"] assert_equal "app2", config.apps[1]["name"] + + config.apps.each do |app| + assert_equal File.expand_path("../../", __FILE__), app["source_path"] + end end it "includes default options" do @@ -140,6 +144,20 @@ end end + it "expands source_path patters that result in a single path" do + apps.clear + apps << { "source_path" => File.expand_path("../../lib/*", __FILE__) } + expected_source_paths = Dir.glob(apps[0]["source_path"]).select { |p| File.directory?(p) } + expected_source_paths.each do |source_path| + app = config.apps.find { |a| a["source_path"] == source_path } + assert app + dir_name = File.basename(source_path) + assert_equal app.root.join(Licensed::AppConfiguration::DEFAULT_CACHE_PATH, dir_name), + app.cache_path + assert_equal dir_name, app["name"] + end + end + it "assigns uniqueness to explicitly configured values" do cache_path = ".test_licenses" name = "test"