Skip to content

Commit

Permalink
[Foreman::Procfile#load] Fail when empty
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
NickLaMuro authored and ddollar committed Apr 12, 2024
1 parent 0ebcb08 commit 14aba8d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/foreman/cli.rb
Expand Up @@ -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"
Expand Down
8 changes: 7 additions & 1 deletion lib/foreman/procfile.rb
Expand Up @@ -10,6 +10,8 @@
#
class Foreman::Procfile

EmptyFileError = Class.new(StandardError)

# Initialize a Procfile
#
# @param [String] filename (nil) An optional filename to read from
Expand Down Expand Up @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/foreman/procfile_spec.rb
Expand Up @@ -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")
Expand Down

0 comments on commit 14aba8d

Please sign in to comment.