From 14aba8d9a5947fb6fd5b0a957019e18c23fce7e3 Mon Sep 17 00:00:00 2001 From: Nick LaMuro Date: Mon, 13 Jul 2020 18:55:19 -0500 Subject: [PATCH] [Foreman::Procfile#load] Fail when empty Includes logic to cause loading an empty Procfile to raise a Foreman::Procfile::EmptyFileError. Keeps existing logic/error message for `foreman check`, just rescues this new error when doing so. --- lib/foreman/cli.rb | 3 ++- lib/foreman/procfile.rb | 8 +++++++- spec/foreman/procfile_spec.rb | 8 ++++++++ 3 files changed, 17 insertions(+), 2 deletions(-) diff --git a/lib/foreman/cli.rb b/lib/foreman/cli.rb index 67d697ab..9cd51786 100644 --- a/lib/foreman/cli.rb +++ b/lib/foreman/cli.rb @@ -69,8 +69,9 @@ def export(format, location=nil) def check check_procfile! engine.load_procfile(procfile) - error "no processes defined" unless engine.processes.length > 0 puts "valid procfile detected (#{engine.process_names.join(', ')})" + rescue Foreman::Procfile::EmptyFileError + error "no processes defined" end desc "run COMMAND [ARGS...]", "Run a command using your application's environment" diff --git a/lib/foreman/procfile.rb b/lib/foreman/procfile.rb index b896dab3..74887212 100644 --- a/lib/foreman/procfile.rb +++ b/lib/foreman/procfile.rb @@ -10,6 +10,8 @@ # class Foreman::Procfile + EmptyFileError = Class.new(StandardError) + # Initialize a Procfile # # @param [String] filename (nil) An optional filename to read from @@ -60,7 +62,11 @@ def delete(name) # @param [String] filename The filename of the +Procfile+ to load # def load(filename) - @entries.replace parse(filename) + parse_data = parse(filename) + + raise EmptyFileError if parse_data.empty? + + @entries.replace parse_data end # Save a Procfile to a file diff --git a/spec/foreman/procfile_spec.rb b/spec/foreman/procfile_spec.rb index 71c15ea9..ae604245 100644 --- a/spec/foreman/procfile_spec.rb +++ b/spec/foreman/procfile_spec.rb @@ -22,6 +22,14 @@ expect(procfile["foo_bar"]).to eq("./foo_bar") end + it "raises an error if Procfile is empty" do + write_file "Procfile" do |procfile| + procfile.puts + end + + expect { Foreman::Procfile.new("Procfile") }.to raise_error described_class::EmptyFileError + end + it 'only creates Procfile entries for lines matching regex' do write_procfile procfile = Foreman::Procfile.new("Procfile")