Skip to content

Commit

Permalink
Fix collection option related issue
Browse files Browse the repository at this point in the history
We have recently added support to provide collection options either
as an command line arguments or options in the collection file. But
that is only working on the site interface, and the collection cli
still expect the `output-folder` as required option.

This commit changes that, so it's not required anymore and it will
try to read the option from the file, and if none of those source
has this value, only then raise a missing option error.

Fixes #283
  • Loading branch information
abunashir committed Sep 11, 2022
1 parent 7f7cef2 commit 9e24967
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
9 changes: 8 additions & 1 deletion lib/metanorma/cli/collection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def self.render(filename, options = {})

def render
extract_options_from_file
verify_required_options_are_present
collection_file.render(collection_options.compact)
end

Expand All @@ -26,8 +27,14 @@ def collection_file
@collection_file ||= Metanorma::Collection.parse(file)
end

def verify_required_options_are_present
if collection_options[:output_folder].nil?
raise ArgumentError.new("Missing a required option '--output-folder'")
end
end

def collection_options
{
@collection_options ||= {
compile: @compile_options,
coverpage: options.fetch(:coverpage, nil),
output_folder: options.fetch(:output_folder, nil),
Expand Down
2 changes: 1 addition & 1 deletion lib/metanorma/cli/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def compile(file_name = nil)

desc "collection FILENAME", "Render HTML pages from XML/YAML colection"
option :format, aliases: "-x", type: :string, desc: "Formats to generate"
option :output_folder, aliases: "-w", required: true, desc: "Directory to save compiled files"
option :output_folder, aliases: "-w", desc: "Directory to save compiled files"
option :coverpage, aliases: "-c", desc: "Liquid template"
option :agree_to_terms, type: :boolean, desc: "Agree / Disagree with all third-party licensing terms "\
"presented (WARNING: do know what you are agreeing with!)"
Expand Down
7 changes: 7 additions & 0 deletions spec/acceptance/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,13 @@
run_metanorma_collection("collection1.xml")
expect_generated_files_to_match_expectations
end

it "only raises error when the file/cli doesn't include output option" do
command = %W(collection collection1.xml -x html --no-install-fonts )
output = capture_stdout { Metanorma::Cli.start(command) }

expect(output).to include("Missing a required option '--output-folder'")
end
end

around(:each) do |example|
Expand Down
20 changes: 18 additions & 2 deletions spec/metanorma/cli/collection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
context "with specified options" do
it "compiles and renders the collection files" do
collection = mock_collection_instance
Metanorma::Cli::Collection.render(collection_file, format: "html, pdf")

expect(collection).to have_received(:render).with(format: %I(html pdf))
Metanorma::Cli::Collection.render(
collection_file,
format: "html, pdf",
output_folder: "bilingual-brochure",
)

expect(collection).to have_received(:render).with(
format: %I(html pdf), output_folder: "bilingual-brochure"
)
end
end

Expand All @@ -25,6 +32,15 @@
)
end
end

it "validates the required attributes" do
collection = mock_collection_instance

expect {
Metanorma::Cli::Collection.render(collection_file)
}.to raise_error(ArgumentError)
expect(collection).not_to have_received(:render)
end
end

def collection_file(collection = "collection1.yml")
Expand Down

0 comments on commit 9e24967

Please sign in to comment.