Skip to content

Commit

Permalink
Merge pull request #1560 from kmuto/file-write
Browse files Browse the repository at this point in the history
use File.write
  • Loading branch information
takahashim authored Aug 29, 2020
2 parents 21cbbf2 + 43a38be commit aefdcfa
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 62 deletions.
10 changes: 3 additions & 7 deletions bin/review-compile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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}"
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion lib/review/converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 3 additions & 9 deletions lib/review/init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -147,7 +146,6 @@ def generate_catalog_file(dir)
POSTDEF:
EOS
end
end

def generate_images_dir(dir)
Expand Down Expand Up @@ -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'))
Expand All @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion test/test_book.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/test_epub3maker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ def test_detect_mathml
</body>
</html>
EOT
File.open(path, 'w') { |f| f.write(html) }
File.write(path, html)
assert_equal ['mathml'], epubmaker.detect_properties(path)
end
end
Expand All @@ -656,7 +656,7 @@ def test_detect_mathml_ns
</body>
</html>
EOT
File.open(path, 'w') { |f| f.write(html) }
File.write(path, html)
assert_equal ['mathml'], epubmaker.detect_properties(path)
end
end
Expand Down
20 changes: 10 additions & 10 deletions test/test_htmlbuilder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand All @@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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 <<EOF }
File.write(file1, '')
File.write(filet1, '')
File.write(file2, '')
File.write(file3, '')
File.write(cat, "CHAPS:\n - sample1.re\n")
File.write(re1, <<-EOF)
= test
tbl1 is @<table>{tbl1}.
Expand Down
56 changes: 28 additions & 28 deletions test/test_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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')
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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])
Expand Down
8 changes: 4 additions & 4 deletions test/test_pdfmaker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ def test_make_custom_page
Dir.mktmpdir do |dir|
coverfile = 'cover.html'
content = '<html><body>test</body></html>'
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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit aefdcfa

Please sign in to comment.