From 43a38be4d5404d4b4ec736fba2ddde52072f9f9b Mon Sep 17 00:00:00 2001 From: takahashim Date: Sat, 29 Aug 2020 03:09:44 +0900 Subject: [PATCH] use File.write --- bin/review-compile | 10 +++---- lib/review/converter.rb | 2 +- lib/review/init.rb | 12 +++------ test/test_book.rb | 2 +- test/test_epub3maker.rb | 4 +-- test/test_htmlbuilder.rb | 20 +++++++------- test/test_i18n.rb | 56 ++++++++++++++++++++-------------------- test/test_pdfmaker.rb | 8 +++--- 8 files changed, 52 insertions(+), 62 deletions(-) diff --git a/bin/review-compile b/bin/review-compile index 35efe7fcb..13110121d 100755 --- a/bin/review-compile +++ b/bin/review-compile @@ -88,7 +88,7 @@ def _main compiler = ReVIEW::Compiler.new(load_builder_class(@target, @check_only)) result = compiler.compile(chap) if @output_filename - write(@output_filename, result) + File.write(@output_filename, result) else puts result unless @check_only end @@ -99,12 +99,12 @@ def _main compiler = ReVIEW::Compiler.new(load_builder_class(@target, @check_only)) book.chapters.each do |chap| str = compiler.compile(chap) - write("#{chap.name}#{compiler.builder.extname}", str) unless @check_only + File.write("#{chap.name}#{compiler.builder.extname}", str) unless @check_only end # PART book.parts_in_file.each do |part| str = compiler.compile(part) - write("#{part.name}#{compiler.builder.extname}", str) unless @check_only + File.write("#{part.name}#{compiler.builder.extname}", str) unless @check_only end else raise "must not happen: #{@mode}" @@ -183,8 +183,4 @@ def load_builder_class(target, strict) ReVIEW.const_get("#{target.upcase}Builder").new(strict) end -def write(path, str) - File.open(path, 'w') { |f| f.puts str } -end - main diff --git a/lib/review/converter.rb b/lib/review/converter.rb index 11cc6e209..38c0bb0fa 100644 --- a/lib/review/converter.rb +++ b/lib/review/converter.rb @@ -17,7 +17,7 @@ def convert(file, output_path) chap_name = File.basename(file, '.*') chap = @book.chapter(chap_name) result = @compiler.compile(chap) - File.open(output_path, 'w') { |f| f.puts result } + File.write(output_path, result) end end end diff --git a/lib/review/init.rb b/lib/review/init.rb index 21616ed7d..0790d2920 100644 --- a/lib/review/init.rb +++ b/lib/review/init.rb @@ -135,8 +135,7 @@ def generate_sample(dir) end def generate_catalog_file(dir) - File.open(File.join(dir, 'catalog.yml'), 'w') do |file| - file.write <<-EOS + File.write(File.join(dir, 'catalog.yml'), <<-EOS) PREDEF: CHAPS: @@ -147,7 +146,6 @@ def generate_catalog_file(dir) POSTDEF: EOS - end end def generate_images_dir(dir) @@ -223,13 +221,11 @@ def generate_texmacro(dir) def generate_rakefile(dir) FileUtils.mkdir_p(File.join(dir, 'lib/tasks')) - File.open(File.join(dir, 'Rakefile'), 'w') do |file| - file.write <<-EOS + File.write(File.join(dir, 'Rakefile'), <<-EOS) Dir.glob('lib/tasks/*.rake').sort.each do |file| load(file) end EOS - end FileUtils.cp(File.join(@review_dir, 'samples/sample-book/src/lib/tasks/review.rake'), File.join(dir, 'lib/tasks/review.rake')) @@ -240,14 +236,12 @@ def generate_locale(dir) end def generate_gemfile(dir) - File.open(File.join(dir, 'Gemfile'), 'w') do |file| - file.write <<-EOS + File.write(File.join(dir, 'Gemfile'), <<-EOS) source 'https://rubygems.org' gem 'rake' gem 'review', '#{ReVIEW::VERSION}' EOS - end end def generate_doc(dir) diff --git a/test/test_book.rb b/test/test_book.rb index b44386a32..28bb343f4 100644 --- a/test/test_book.rb +++ b/test/test_book.rb @@ -23,7 +23,7 @@ def test_update_rubyenv test_const = "ReVIEW__BOOK__TEST__#{num}" begin Dir.mktmpdir do |dir| - File.open(File.join(dir, 'review-ext.rb'), 'w') { |o| o.puts "#{test_const} = #{num}" } + File.write(File.join(dir, 'review-ext.rb'), "#{test_const} = #{num}") Book::Base.load(dir) assert_equal num, (Object.class_eval { const_get(test_const) }) end diff --git a/test/test_epub3maker.rb b/test/test_epub3maker.rb index 23840cfae..4262d9ebe 100644 --- a/test/test_epub3maker.rb +++ b/test/test_epub3maker.rb @@ -631,7 +631,7 @@ def test_detect_mathml EOT - File.open(path, 'w') { |f| f.write(html) } + File.write(path, html) assert_equal ['mathml'], epubmaker.detect_properties(path) end end @@ -656,7 +656,7 @@ def test_detect_mathml_ns EOT - File.open(path, 'w') { |f| f.write(html) } + File.write(path, html) assert_equal ['mathml'], epubmaker.detect_properties(path) end end diff --git a/test/test_htmlbuilder.rb b/test/test_htmlbuilder.rb index b146c9af2..2562ac6f0 100644 --- a/test/test_htmlbuilder.rb +++ b/test/test_htmlbuilder.rb @@ -61,7 +61,7 @@ def test_headline_postdef_roman Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write "locale: ja\nappendix: 付録%pR" } + File.write(file, "locale: ja\nappendix: 付録%pR") I18n.setup('ja') @chapter.instance_eval do def on_appendix? @@ -82,7 +82,7 @@ def test_headline_postdef_alpha Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write "locale: ja\nappendix: 付録%pA" } + File.write(file, "locale: ja\nappendix: 付録%pA") I18n.setup('ja') @chapter.instance_eval do def on_appendix? @@ -272,7 +272,7 @@ def test_inline_hd_chap_postdef_roman Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write "locale: ja\nappendix: 付録%pR" } + File.write(file, "locale: ja\nappendix: 付録%pR") I18n.setup('ja') @chapter.instance_eval do def on_appendix? @@ -301,7 +301,7 @@ def test_inline_hd_chap_postdef_alpha Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write "locale: ja\nappendix: 付録%pA" } + File.write(file, "locale: ja\nappendix: 付録%pA") I18n.setup('ja') @chapter.instance_eval do def on_appendix? @@ -425,12 +425,12 @@ def test_inline_imgref3 re1 = File.join(dir, 'sample1.re') cat = File.join(dir, 'catalog.yml') FileUtils.mkdir_p(File.join(dir, 'images')) - File.open(file1, 'w') { |f| f.write '' } - File.open(filet1, 'w') { |f| f.write '' } - File.open(file2, 'w') { |f| f.write '' } - File.open(file3, 'w') { |f| f.write '' } - File.open(cat, 'w') { |f| f.write "CHAPS:\n - sample1.re\n" } - File.open(re1, 'w') { |f| f.write <{tbl1}. diff --git a/test/test_i18n.rb b/test/test_i18n.rb index 55ea218d5..90c35d080 100644 --- a/test/test_i18n.rb +++ b/test/test_i18n.rb @@ -9,7 +9,7 @@ def test_load_locale_yml Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nfoo: "bar"\n) } + File.write(file, %Q(locale: ja\nfoo: "bar"\n)) I18n.setup assert_equal 'bar', I18n.t('foo') end @@ -20,7 +20,7 @@ def test_load_locale_yaml Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yaml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nfoo: "bar"\n) } + File.write(file, %Q(locale: ja\nfoo: "bar"\n)) assert_raise ReVIEW::ConfigError do I18n.setup end @@ -32,7 +32,7 @@ def test_load_foo_yaml Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'foo.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nfoo: "bar"\n) } + File.write(file, %Q(locale: ja\nfoo: "bar"\n)) I18n.setup('ja', 'foo.yml') assert_equal 'bar', I18n.t('foo') end @@ -43,7 +43,7 @@ def test_update_foo_yaml Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'foo.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nfoo: "bar"\n) } + File.write(file, %Q(locale: ja\nfoo: "bar"\n)) i18n = ReVIEW::I18n.new('ja') i18n.update_localefile(File.join(Dir.pwd, 'foo.yml')) assert_equal 'bar', i18n.t('foo') @@ -55,7 +55,7 @@ def test_update_foo_yaml_i18nclass Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'foo.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nfoo: "bar"\n) } + File.write(file, %Q(locale: ja\nfoo: "bar"\n)) I18n.setup('ja', 'foo.yml') assert_equal 'bar', I18n.t('foo') end @@ -66,7 +66,7 @@ def test_load_locale_yml_i18n Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write %Q(ja:\n foo: "bar"\nen:\n foo: "buz"\n) } + File.write(file, %Q(ja:\n foo: "bar"\nen:\n foo: "buz"\n)) I18n.setup assert_equal 'bar', I18n.t('foo') assert_equal '図', I18n.t('image') @@ -81,7 +81,7 @@ def test_load_locale_invalid_yml Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write %Q(local: ja\nfoo: "bar"\n) } + File.write(file, %Q(local: ja\nfoo: "bar"\n)) assert_raises(ReVIEW::KeyError) do I18n.setup end @@ -93,59 +93,59 @@ def test_custom_format Dir.mktmpdir do |dir| Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pa章" } + File.write(file, "locale: ja\nchapter: 第%pa章") I18n.setup('ja') assert_equal '第a章', I18n.t('chapter', 1) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pA章" } + File.write(file, "locale: ja\nchapter: 第%pA章") I18n.setup('ja') assert_equal '第B章', I18n.t('chapter', 2) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pAW章" } + File.write(file, "locale: ja\nchapter: 第%pAW章") I18n.setup('ja') assert_equal '第B章', I18n.t('chapter', 2) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%paW章" } + File.write(file, "locale: ja\nchapter: 第%paW章") I18n.setup('ja') assert_equal '第b章', I18n.t('chapter', 2) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pR章" } + File.write(file, "locale: ja\nchapter: 第%pR章") I18n.setup('ja') assert_equal '第I章', I18n.t('chapter', 1) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pr章" } + File.write(file, "locale: ja\nchapter: 第%pr章") I18n.setup('ja') assert_equal '第ii章', I18n.t('chapter', 2) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pRW章" } + File.write(file, "locale: ja\nchapter: 第%pRW章") I18n.setup('ja') assert_equal '第Ⅻ章', I18n.t('chapter', 12) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pJ章" } + File.write(file, "locale: ja\nchapter: 第%pJ章") I18n.setup('ja') assert_equal '第二十七章', I18n.t('chapter', 27) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pdW章" } + File.write(file, "locale: ja\nchapter: 第%pdW章") I18n.setup('ja') assert_equal '第1章', I18n.t('chapter', 1) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pdW章" } + File.write(file, "locale: ja\nchapter: 第%pdW章") I18n.setup('ja') assert_equal '第27章', I18n.t('chapter', 27) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pDW章" } + File.write(file, "locale: ja\nchapter: 第%pDW章") I18n.setup('ja') assert_equal '第1章', I18n.t('chapter', 1) - File.open(file, 'w') { |f| f.write "locale: ja\nchapter: 第%pDW章" } + File.write(file, "locale: ja\nchapter: 第%pDW章") I18n.setup('ja') assert_equal '第27章', I18n.t('chapter', 27) - File.open(file, 'w') { |f| f.write "locale: ja\npart: Part %pRW" } + File.write(file, "locale: ja\npart: Part %pRW") I18n.setup('ja') assert_equal 'Part 0', I18n.t('part', 0) - File.open(file, 'w') { |f| f.write "locale: ja\npart: 第%pJ部" } + File.write(file, "locale: ja\npart: 第%pJ部") I18n.setup('ja') assert_equal '第一部', I18n.t('part', 1) end @@ -157,23 +157,23 @@ def test_custom_format_numbers Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%s-%pA:") } + File.write(file, %Q(locale: ja\nformat_number_header: "%s-%pA:")) I18n.setup('ja') assert_equal '1-B:', I18n.t('format_number_header', [1, 2]) - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%s.%pa:") } + File.write(file, %Q(locale: ja\nformat_number_header: "%s.%pa:")) I18n.setup('ja') assert_equal '2.c:', I18n.t('format_number_header', [2, 3]) - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%pA,%pAW:") } + File.write(file, %Q(locale: ja\nformat_number_header: "%pA,%pAW:")) I18n.setup('ja') assert_equal 'C,D:', I18n.t('format_number_header', [3, 4]) - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%pJ・%pJ:") } + File.write(file, %Q(locale: ja\nformat_number_header: "%pJ・%pJ:")) I18n.setup('ja') assert_equal '十二・二十六:', I18n.t('format_number_header', [12, 26]) - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%pdW―%pdW:") } + File.write(file, %Q(locale: ja\nformat_number_header: "%pdW―%pdW:")) I18n.setup('ja') assert_equal '3―12:', I18n.t('format_number_header', [3, 12]) end @@ -185,11 +185,11 @@ def test_format_with_mismatched_number_of_arguments Dir.chdir(dir) do file = File.join(dir, 'locale.yml') - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%2$d") } + File.write(file, %Q(locale: ja\nformat_number_header: "%2$d")) I18n.setup('ja') assert_equal '10', I18n.t('format_number_header', [1, 10]) - File.open(file, 'w') { |f| f.write %Q(locale: ja\nformat_number_header: "%2$d-%1$d") } + File.write(file, %Q(locale: ja\nformat_number_header: "%2$d-%1$d")) I18n.setup('ja') # ERROR: returns raw format assert_equal '%2$d-%1$d', I18n.t('format_number_header', [1]) diff --git a/test/test_pdfmaker.rb b/test/test_pdfmaker.rb index 7041430d6..a319a9cff 100644 --- a/test/test_pdfmaker.rb +++ b/test/test_pdfmaker.rb @@ -76,7 +76,7 @@ def test_make_custom_page Dir.mktmpdir do |dir| coverfile = 'cover.html' content = 'test' - File.open(File.join(dir, 'cover.tex'), 'w') { |f| f.write(content) } + File.write(File.join(dir, 'cover.tex'), content) page = @maker.make_custom_page(File.join(dir, coverfile)) assert_equal(content, page) end @@ -176,11 +176,11 @@ def test_gettemplate_with_backmatter Dir.mktmpdir do |dir| Dir.chdir(dir) do profile = "\\thispagestyle{empty}\\chapter*{Profile}\nsome profile\n" - File.open(File.join(dir, 'profile.tex'), 'w') { |f| f.write(profile) } + File.write(File.join(dir, 'profile.tex'), profile) advfile = "\\thispagestyle{empty}\\chapter*{Ad}\nsome ad content\n" - File.open(File.join(dir, 'advfile.tex'), 'w') { |f| f.write(advfile) } + File.write(File.join(dir, 'advfile.tex'), advfile) backcover = "\\clearpage\n\\thispagestyle{empty}\\AddToShipoutPictureBG{%\n\\AtPageLowerLeft{\\includegraphics[width=\\paperwidth,height=\\paperheight]{images/backcover.png}}\n}\n\\null" - File.open(File.join(dir, 'backcover.tex'), 'w') { |f| f.write(backcover) } + File.write(File.join(dir, 'backcover.tex'), backcover) expect = File.read(File.join(assets_dir, 'test_template_backmatter.tex')) expect.gsub!(/\\def\\review@reviewversion{[^}]+}/, "\\def\\review@reviewversion{#{ReVIEW::VERSION}}") @maker.basedir = Dir.pwd