From 189f22f0aaa3fba5fa890989c975d26c2062bcc2 Mon Sep 17 00:00:00 2001 From: takahashim Date: Mon, 26 Feb 2018 01:56:02 +0900 Subject: [PATCH 1/6] add Catalog#validate! raise FileNotFound error when `*.re` file does not exist --- lib/review/book/base.rb | 11 +++++++++-- lib/review/catalog.rb | 17 +++++++++++++++++ test/book_test_helper.rb | 1 + test/test_catalog.rb | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 2 deletions(-) diff --git a/lib/review/book/base.rb b/lib/review/book/base.rb index 171a9832c..aa9d7aa07 100644 --- a/lib/review/book/base.rb +++ b/lib/review/book/base.rb @@ -185,6 +185,9 @@ def catalog catalogfile_path = "#{basedir}/#{config['catalogfile']}" @catalog = File.open(catalogfile_path, 'r:BOM|utf-8') { |f| Catalog.new(f) } if File.file? catalogfile_path + if @catalog + @catalog.validate!(basedir) + end @catalog end @@ -247,10 +250,14 @@ def bib_exist? end def prefaces - return mkpart_from_namelist(catalog.predef.split("\n")) if catalog + if catalog + return mkpart_from_namelist(catalog.predef.split("\n")) + end begin - mkpart_from_namelistfile("#{@basedir}/#{config['predef_file']}") if File.file?("#{@basedir}/#{config['predef_file']}") + if File.file?("#{@basedir}/#{config['predef_file']}") + mkpart_from_namelistfile("#{@basedir}/#{config['predef_file']}") + end rescue FileNotFound => err raise FileNotFound, "preface #{err.message}" end diff --git a/lib/review/catalog.rb b/lib/review/catalog.rb index da6047599..d54ea6805 100644 --- a/lib/review/catalog.rb +++ b/lib/review/catalog.rb @@ -48,5 +48,22 @@ def postdef return '' unless @yaml['POSTDEF'] @yaml['POSTDEF'].join("\n") end + + def validate!(basedir) + %w[PREDEF CHAPS POSTDEF APPENDIX].each do |cat_name| + next unless @yaml[cat_name] + @yaml[cat_name].each do |item| + filenames = [item] + if item.is_a?(Hash) + filenames = item.values + end + filenames.each do |filename| + unless File.exist?(File.join(basedir, filename)) + raise FileNotFound, "file not found in catalog.yml: #{basedir}/#{filename}" + end + end + end + end + end end end diff --git a/test/book_test_helper.rb b/test/book_test_helper.rb index 010d14048..f36e79a20 100644 --- a/test/book_test_helper.rb +++ b/test/book_test_helper.rb @@ -1,4 +1,5 @@ require 'test_helper' +require 'book_test_helper' require 'review/book' require 'stringio' diff --git a/test/test_catalog.rb b/test/test_catalog.rb index 6083e748d..efebd2bef 100644 --- a/test/test_catalog.rb +++ b/test/test_catalog.rb @@ -3,6 +3,7 @@ class CatalogTest < Test::Unit::TestCase include ReVIEW + include BookTestHelper def test_predef sut = Catalog.new(yaml) @@ -90,6 +91,40 @@ def test_from_object assert_equal(exp.chomp, sut.chaps) end + def test_validate + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re ch02.re post01.re post02.re back01.re back02.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + + def test_validate_fail_ch02 + assert_raise FileNotFound do + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + end + + def test_validate_fail_back02 + assert_raise FileNotFound do + mktmpbookdir do |dir, _book, _files| + %w[pre01.re pre02.re ch01.re ch02.re post01.re post02.re back01.re back03.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_hash) + cat.validate!(dir) + end + end + end + private def yaml From ee35e413b1a13ba2936512973f0ff70e2d6f970f Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 27 Feb 2018 02:31:42 +0900 Subject: [PATCH 2/6] handle Errors in *maker --- lib/review/epubmaker.rb | 3 +++ lib/review/pdfmaker.rb | 3 +++ lib/review/webmaker.rb | 3 +++ 3 files changed, 9 insertions(+) diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index b5ddab683..b01069d82 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -134,6 +134,9 @@ def produce(yamlfile, bookname = nil) log('Call ePUB producer.') @producer.produce("#{bookname}.epub", basetmpdir, epubtmpdir) log('Finished.') + rescue ApplicationError => e + raise if $DEBUG + error(e.message) ensure FileUtils.remove_entry_secure basetmpdir unless @config['debug'] end diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index 0992843c4..2475f74e6 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -227,6 +227,9 @@ def generate_pdf(yamlfile) build_pdf FileUtils.cp(File.join(@path, 'book.pdf'), pdf_filepath) + rescue ApplicationError => e + raise if $DEBUG + error(e.message) ensure remove_entry_secure @path unless @config['debug'] end diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index b325c2e70..4a3f85418 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -96,6 +96,9 @@ def generate_html_files(yamlfile) copy_resources('covers', "#{@path}/#{@config['imagedir']}") copy_resources('adv', "#{@path}/#{@config['imagedir']}") copy_resources(@config['fontdir'], "#{@path}/fonts", @config['font_ext']) + rescue ApplicationError => e + raise if $DEBUG + error(e.message) end def build_body(basetmpdir, _yamlfile) From 2013d5066b87c6d2cc172eab52e4c2739c16cf06 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 27 Feb 2018 13:32:18 +0900 Subject: [PATCH 3/6] move `rescue` blocks to avoid exiting from deeper methods --- lib/review/pdfmaker.rb | 11 +++++++---- lib/review/textmaker.rb | 7 ++++++- lib/review/webmaker.rb | 10 ++++++---- 3 files changed, 19 insertions(+), 9 deletions(-) diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index 850ffc97f..c573165dc 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -126,7 +126,13 @@ def execute(*args) rescue ReVIEW::ConfigError => e warn e.message end - generate_pdf(yamlfile) + + begin + generate_pdf(yamlfile) + rescue ApplicationError => e + raise if $DEBUG + error(e.message) + end end def make_input_files(book, yamlfile) @@ -233,9 +239,6 @@ def generate_pdf(yamlfile) build_pdf FileUtils.cp(File.join(@path, 'book.pdf'), pdf_filepath) - rescue ApplicationError => e - raise if $DEBUG - error(e.message) ensure remove_entry_secure @path unless @config['debug'] end diff --git a/lib/review/textmaker.rb b/lib/review/textmaker.rb index 0d8a2f25c..289fb03c2 100644 --- a/lib/review/textmaker.rb +++ b/lib/review/textmaker.rb @@ -83,7 +83,12 @@ def execute(*args) # YAML configs will be overridden by command line options. @config.deep_merge!(cmd_config) I18n.setup(@config['language']) - generate_text_files(yamlfile) + begin + generate_text_files(yamlfile) + rescue ApplicationError => e + raise if $DEBUG + error(e.message) + end end def generate_text_files(yamlfile) diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index bb9b48eb0..dea3233f2 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -90,7 +90,12 @@ def execute(*args) @config.deep_merge!(cmd_config) @config['htmlext'] = 'html' I18n.setup(@config['language']) - generate_html_files(yamlfile) + begin + generate_html_files(yamlfile) + rescue ApplicationError => e + raise if $DEBUG + error(e.message) + end end def generate_html_files(yamlfile) @@ -112,9 +117,6 @@ def generate_html_files(yamlfile) copy_resources('covers', "#{@path}/#{@config['imagedir']}") copy_resources('adv', "#{@path}/#{@config['imagedir']}") copy_resources(@config['fontdir'], "#{@path}/fonts", @config['font_ext']) - rescue ApplicationError => e - raise if $DEBUG - error(e.message) end def build_body(basetmpdir, _yamlfile) From 6b7753c6b03fa37bfd60014ac8240e4f45c41143 Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 27 Feb 2018 15:16:47 +0900 Subject: [PATCH 4/6] use @config["debug"] instead of $DEBUG --- lib/review/epubmaker.rb | 2 +- lib/review/pdfmaker.rb | 2 +- lib/review/textmaker.rb | 2 +- lib/review/webmaker.rb | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/review/epubmaker.rb b/lib/review/epubmaker.rb index e0b2c405a..556ea3656 100644 --- a/lib/review/epubmaker.rb +++ b/lib/review/epubmaker.rb @@ -141,7 +141,7 @@ def produce(yamlfile, bookname = nil) @producer.produce("#{bookname}.epub", basetmpdir, epubtmpdir) log('Finished.') rescue ApplicationError => e - raise if $DEBUG + raise if @config['debug'] error(e.message) ensure FileUtils.remove_entry_secure(basetmpdir) unless @config['debug'] diff --git a/lib/review/pdfmaker.rb b/lib/review/pdfmaker.rb index c573165dc..0df3e8bfd 100644 --- a/lib/review/pdfmaker.rb +++ b/lib/review/pdfmaker.rb @@ -130,7 +130,7 @@ def execute(*args) begin generate_pdf(yamlfile) rescue ApplicationError => e - raise if $DEBUG + raise if @config['debug'] error(e.message) end end diff --git a/lib/review/textmaker.rb b/lib/review/textmaker.rb index 289fb03c2..a74ba02b0 100644 --- a/lib/review/textmaker.rb +++ b/lib/review/textmaker.rb @@ -86,7 +86,7 @@ def execute(*args) begin generate_text_files(yamlfile) rescue ApplicationError => e - raise if $DEBUG + raise if @config['debug'] error(e.message) end end diff --git a/lib/review/webmaker.rb b/lib/review/webmaker.rb index dea3233f2..ddb3fe421 100644 --- a/lib/review/webmaker.rb +++ b/lib/review/webmaker.rb @@ -93,7 +93,7 @@ def execute(*args) begin generate_html_files(yamlfile) rescue ApplicationError => e - raise if $DEBUG + raise if @config['debug'] error(e.message) end end From 04e7d547854d3ae0630cbd7f54120c64a3c8392f Mon Sep 17 00:00:00 2001 From: takahashim Date: Tue, 27 Feb 2018 20:28:38 +0900 Subject: [PATCH 5/6] support parts --- lib/review/catalog.rb | 33 +++++++++++++++++++++++---------- test/test_catalog.rb | 10 ++++++++++ 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/lib/review/catalog.rb b/lib/review/catalog.rb index d54ea6805..c888664fb 100644 --- a/lib/review/catalog.rb +++ b/lib/review/catalog.rb @@ -50,18 +50,31 @@ def postdef end def validate!(basedir) - %w[PREDEF CHAPS POSTDEF APPENDIX].each do |cat_name| - next unless @yaml[cat_name] - @yaml[cat_name].each do |item| - filenames = [item] - if item.is_a?(Hash) - filenames = item.values - end - filenames.each do |filename| - unless File.exist?(File.join(basedir, filename)) - raise FileNotFound, "file not found in catalog.yml: #{basedir}/#{filename}" + filenames = [] + if predef.present? + filenames.concat(predef.split(/\n/)) + end + parts_with_chaps.each do |chap| + if chap.is_a?(Hash) + chap.each_key do |part| + if File.extname(part) == '.re' + filenames.push(part) end end + filenames.concat(chap.values.flatten) + else + filenames.push(chap) + end + end + if appendix.present? + filenames.concat(appendix.split(/\n/)) + end + if postdef.present? + filenames.concat(postdef.split(/\n/)) + end + filenames.each do |filename| + unless File.exist?(File.join(basedir, filename)) + raise FileNotFound, "file not found in catalog.yml: #{basedir}/#{filename}" end end end diff --git a/test/test_catalog.rb b/test/test_catalog.rb index efebd2bef..1981d8ea5 100644 --- a/test/test_catalog.rb +++ b/test/test_catalog.rb @@ -101,6 +101,16 @@ def test_validate end end + def test_validate_with_parts + mktmpbookdir do |dir, _book, _files| + %w[ch01.re part1.re ch02.re ch03.re part2.re ch04.re ch05.re].each do |file| + FileUtils.touch(file) + end + cat = Catalog.new(yaml_with_parts) + cat.validate!(dir) + end + end + def test_validate_fail_ch02 assert_raise FileNotFound do mktmpbookdir do |dir, _book, _files| From 934d274273f407e6da85eb101778e335f1810ba1 Mon Sep 17 00:00:00 2001 From: Kenshi Muto Date: Tue, 27 Feb 2018 21:07:36 +0900 Subject: [PATCH 6/6] check file existence in review-compile. --- bin/review-compile | 1 + 1 file changed, 1 insertion(+) diff --git a/bin/review-compile b/bin/review-compile index d0ba754e3..5fcc77d74 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -82,6 +82,7 @@ def _main book = ReVIEW::Book::Base.load(@basedir) book.config = @config # needs only at the first time ARGV.each do |item| + error("file not found: #{item}") unless File.exist?(item) chap_name = File.basename(item, '.*') chap = book.chapter(chap_name) compiler = ReVIEW::Compiler.new(load_strategy_class(@target, @check_only))