Skip to content

Commit

Permalink
Fix Style/FileWrite offenses
Browse files Browse the repository at this point in the history
Also refactor specs to use `join` instead of `+` because it's shorter, clearer, and uses less memory, even if it is slightly less performant
in terms of IPS

Ref: #276
  • Loading branch information
tagliala committed Sep 14, 2024
1 parent dfca266 commit b5c62ff
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 24 deletions.
7 changes: 0 additions & 7 deletions .rubocop_todo.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions bin/cvheathen
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ files = []
if in_file.file?
abort "Output is a directory, but expected a file" if out_file.directory?
content = converter.convert action, File.read(in_file), language
File.open(out_file, 'wb') { |f| f.write content }
File.binwrite(out_file, content)
elsif in_file.directory?
abort "Invalid output directory" unless out_file.directory?
Dir.glob(in_file + (recurse ? '**/*' : '*')).each do |file|
Expand All @@ -49,7 +49,7 @@ elsif in_file.directory?
content = converter.convert action, File.read(file), language
new_file = Heathen::Filename.suggest_in_new_dir file, content.mime_type, in_file.to_s, out_file.to_s
Pathname.new(new_file).parent.mkpath
File.open(new_file, 'wb') { |f| f.write content }
File.binwrite(new_file, content)
rescue StandardError => e
logger.error "Failed to convert #{file}: #{e.message}"
end
Expand Down
2 changes: 1 addition & 1 deletion lib/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def add_file(version, filename, body, author = nil)

body = StringIO.new(body) unless body.respond_to?(:read) # string -> IO
File.open(directory + version + filename, "wb") { |f| IO.copy_stream(body, f) }
File.open(directory + version + AUTHOR_FILE, 'w') { |f| f.write author }
File.write(directory + version + AUTHOR_FILE, author)
end

# Sets the specified version as current.
Expand Down
2 changes: 1 addition & 1 deletion lib/legacy_converter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def convert_file(action, orig_content, language = 'en')

# Stores the specified file in the legacy directory
def store_file(filename, content)
File.open(@legacy_dir + filename, 'wb') { |f| f.write content }
File.binwrite(@legacy_dir + filename, content)
end

# Loads and returns a legacy converted file
Expand Down
24 changes: 11 additions & 13 deletions spec/lib/document_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@

describe '#directory' do
it 'runs' do
dir = document.directory
expect(dir).not_to be_nil
expect(File.exist?(dir)).to be true
expect(document.directory).to exist
end
end

Expand Down Expand Up @@ -133,7 +131,7 @@
it 'runs' do
version = document.new_version
expect(version).not_to be_nil
expect(File.exist?(document.directory + version)).to be true
expect(document.directory.join(version)).to exist
new_doc = described_class.load storage_dir, doc_key
expect(new_doc.versions.include?(version)).to be true
end
Expand All @@ -144,31 +142,31 @@
file = __FILE__
body = File.read(file)
document.add_file 'v002', File.basename(file), body
expect(File.exist?(document.directory + 'v002' + File.basename(file))).to be true
expect(document.directory.join('v002', File.basename(file))).to exist
expect(document.directory.join('v002', described_class::AUTHOR_FILE).read.chomp).to eq ''
end

it 'runs with author' do
file = __FILE__
body = File.read(file)
document.add_file 'v002', File.basename(file), body, author
expect(File.exist?(document.directory + 'v002' + File.basename(file))).to be true
expect(File.exist?(document.directory + 'v002' + described_class::AUTHOR_FILE)).to be true
expect(File.read(document.directory + 'v002' + described_class::AUTHOR_FILE).chomp).to eq author
expect(document.directory.join('v002', File.basename(file))).to exist
expect(document.directory.join('v002', described_class::AUTHOR_FILE).read.chomp).to eq author
end

it 'runs with IO for body' do
file = __FILE__
body = File.open(file)
document.add_file 'v002', File.basename(file), body
expect(File.exist?(document.directory + 'v002' + File.basename(file))).to be true
expect(document.directory.join('v002', File.basename(file))).to exist
end
end

describe '#set_current' do
it 'runs' do
document.set_current 'v001'
st1 = File.stat(document.directory + 'current')
st2 = File.stat(document.directory + 'v001')
st1 = document.directory.join('current').stat
st2 = document.directory.join('v001').stat
expect(st1.ino).to eq st2.ino
end

Expand All @@ -188,7 +186,7 @@
describe '#delete_version' do
it 'runs' do
document.delete_version 'v001'
expect(File.exist?(document.directory + 'v001')).to be false
expect(document.directory.join('v001')).not_to exist
end

it 'refuses to delete "current"' do
Expand Down Expand Up @@ -266,7 +264,7 @@
describe '#save_metadata' do
it 'runs' do
document.save_metadata
expect(File.exist?(document.directory + 'metadata.json')).to be true
expect(document.directory.join('metadata.json')).to exist
# expect this to pass
JSON.parse File.read(document.directory + 'metadata.json')
end
Expand Down

0 comments on commit b5c62ff

Please sign in to comment.