Skip to content

Commit

Permalink
#301 return non-zero exit code on fatal errors
Browse files Browse the repository at this point in the history
  • Loading branch information
CAMOBAP committed Feb 11, 2024
1 parent a390565 commit 32bb378
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 12 deletions.
11 changes: 7 additions & 4 deletions lib/metanorma/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ def self.start(arguments)
Metanorma::Cli::Command.start(arguments)
rescue Interrupt, SignalException => _e
UI.say("Process cancelled, exiting.")
rescue Errors::FileNotFoundError => error
UI.say("Error: #{error}. \nNot sure what to run? try: metanorma help")
rescue Errors::FileNotFoundError => e
UI.say("Error: #{e}. \nNot sure what to run? try: metanorma help")
exit(Errno::ENOENT::Errno)
rescue Errors::FatalCompilationError => e
UI.error(e.message)
exit(-1)
end

def self.root
Expand Down Expand Up @@ -72,11 +75,11 @@ def self.config_path(global = false)
def self.writable_templates_path?
parent_directory = templates_path.join("..", "..")

unless parent_directory && parent_directory.writable?
unless parent_directory&.writable?
raise Errno::EACCES, "No permission to write in this directory"
end

return true
true
end

def self.root_path
Expand Down
18 changes: 18 additions & 0 deletions lib/metanorma/cli/errors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,24 @@ class DuplicateTemplateError < StandardError; end

class FileNotFoundError < StandardError; end
class InvalidManifestFileError < StandardError; end

class FatalCompilationError < StandardError
attr_reader :fatals

def initialize(fatals)
super()
@fatals = fatals
end

def message
<<~MSG
Fatal compilation error(s):
#{fatals.map { |f| "- #{f}" }.join("\n")}
Look at error.log for more details
MSG
end
end
end
end
end
7 changes: 6 additions & 1 deletion lib/metanorma/cli/site_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,12 @@ def self.generate(source, options = {}, compile_options = {})

def generate
site_directory = asset_directory.join("..")
select_source_files.each { |source| compile(source) }

fatals = select_source_files.map { |source| compile(source) }
.flatten
.compact

raise Errors::FatalCompilationError, fatals unless fatals.empty?

Dir.chdir(site_directory) do
build_collection_file(collection_name)
Expand Down
12 changes: 5 additions & 7 deletions spec/acceptance/compile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@

describe "failure" do
it "returns the correct status code" do
begin
command = %w(compile -t iso invalid-file)
capture_stdout { Metanorma::Cli.start(command) }

rescue SystemExit => error
expect(error.status).to eq(Errno::ENOENT::Errno)
end
command = %w(compile -t iso invalid-file)
expect { Metanorma::Cli.start(command) }
.to raise_error(SystemExit) { |e|
expect(e.status).to eq(Errno::ENOENT::Errno)
}
end
end

Expand Down
29 changes: 29 additions & 0 deletions spec/acceptance/generate_site_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,35 @@
end
end

describe "failure" do
before do
fatals = [
"Fatal error 1",
"Fatal error 2",
"Fatal error 3",
]
allow(Metanorma::Cli::Compiler).to receive(:compile)
.and_return(fatals, [])
end

it "returns non-zero status code on fatal error" do
command = %W(site generate #{source_dir})

expect { Metanorma::Cli.start(command) }
.to raise_error(SystemExit) { |e|
expect(e.status).to eq(-1)
}
end

it "print UI.error on fatal errors" do
command = %W(site generate #{source_dir})

output = capture_stderr { Metanorma::Cli.start(command) }

expect(output).to include("Fatal compilation error(s)")
end
end

def source_dir
@source_dir ||= File.expand_path(File.join(File.dirname(__dir__), "fixtures"))
end
Expand Down
18 changes: 18 additions & 0 deletions spec/metanorma/cli/site_generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,24 @@
end
end

it "raise error on fatal errors" do
stub_external_interface_calls
fatals = [
"Fatal error 1",
"Fatal error 2",
"Fatal error 3",
]

allow(Metanorma::Cli::Compiler).to receive(:compile).and_return(fatals)
expect do
Metanorma::Cli::SiteGenerator.generate(
source_path,
{ output_dir: output_directory },
continue_without_fonts: false,
)
end.to raise_error(Metanorma::Cli::Errors::FatalCompilationError)
end

def stub_external_interface_calls
allow(File).to receive(:rename)
allow(Metanorma::Cli::Compiler).to receive(:compile)
Expand Down

0 comments on commit 32bb378

Please sign in to comment.