Skip to content

Commit

Permalink
Merge pull request #278 from ifad/chore/fix-style-write
Browse files Browse the repository at this point in the history
Fix Style/FileWrite offenses
  • Loading branch information
tagliala authored Sep 14, 2024
2 parents 70de07a + 5148da5 commit b440ef0
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 39 deletions.
8 changes: 0 additions & 8 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
50 changes: 23 additions & 27 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,52 +131,48 @@
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
end

describe '#add_file' do
let(:file) { fixture('heathen/test.txt') }

it 'runs without author' do
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
document.add_file 'v002', File.basename(file), file.read
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
document.add_file 'v002', File.basename(file), file.read, 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
document.add_file 'v002', File.basename(file), file.read
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

it 'fails when you try an invalid version' do
it 'fails with a non-existing version' do
expect {
document.set_current 'v009'
}.to raise_error Colore::VersionNotFound
end

it 'fails when you try an invalid version name' do
it 'fails with an invalid version name' do
expect {
document.set_current 'title'
}.to raise_error Colore::InvalidVersion
Expand All @@ -188,7 +182,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 @@ -242,7 +236,7 @@

describe '#to_hash' do
it 'runs' do
testhash = JSON.parse(File.read(fixture('document.json')))
testhash = JSON.parse(fixture('document.json').read)
testhash = Colore::Utils.symbolize_keys testhash
dochash = Colore::Utils.symbolize_keys document.to_hash
dochash[:versions].each do |k, v|
Expand All @@ -266,9 +260,11 @@
describe '#save_metadata' do
it 'runs' do
document.save_metadata
expect(File.exist?(document.directory + 'metadata.json')).to be true
# expect this to pass
JSON.parse File.read(document.directory + 'metadata.json')
expect(document.directory.join('metadata.json')).to exist

expect do
JSON.parse document.directory.join('metadata.json').read
end.not_to raise_error
end
end
end

0 comments on commit b440ef0

Please sign in to comment.