Skip to content

Commit

Permalink
Apply linting
Browse files Browse the repository at this point in the history
  • Loading branch information
ZaikoXander committed May 1, 2024
1 parent ef65994 commit b697c7e
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 45 deletions.
13 changes: 7 additions & 6 deletions lib/file_to_tempfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@

require_relative "file_to_tempfile/version"

require 'tempfile'
require "tempfile"

# This module provides a method to convert a File object to a Tempfile object.
module FileToTempfile
def self.convert(file, using_path = false)
raise ArgumentError, 'Expected a File object as the first argument' unless file.is_a? File
raise ArgumentError, 'Expected a boolean as the second argument' unless [true, false].include?(using_path)
raise IOError, 'The file must be open before it can be converted' if file.closed?
def self.convert(file, using_path: false)
raise ArgumentError, "Expected a File object as the first argument" unless file.is_a? File
raise ArgumentError, "Expected a boolean as the second argument" unless [true, false].include?(using_path)
raise IOError, "The file must be open before it can be converted" if file.closed?

file.rewind if file.eof?

tempfile = Tempfile.new(using_path ? file.path : '').tap do |t|
tempfile = Tempfile.new(using_path ? file.path : "").tap do |t|
t.write(file.read)
t.rewind
end
Expand Down
76 changes: 37 additions & 39 deletions spec/file_to_tempfile_spec.rb
Original file line number Diff line number Diff line change
@@ -1,64 +1,62 @@
# frozen_string_literal: true

RSpec.describe FileToTempfile do
RSpec.describe FileToTempfile do # rubocop:disable Metrics/BlockLength
it "has a version number" do
expect(FileToTempfile::VERSION).not_to be nil
end

describe '#self.convert' do
context 'when file is not a File object' do
it 'raises an ArgumentError' do
describe "#self.convert" do # rubocop:disable Metrics/BlockLength
let(:file) { File.open ".gitignore" }

context "when file is not a File object" do
it "raises an ArgumentError" do
expect do
FileToTempfile.convert('file')
end.to raise_error ArgumentError, 'Expected a File object as the first argument'
FileToTempfile.convert("file")
end.to raise_error ArgumentError, "Expected a File object as the first argument"
end
end

context 'when file is a File object' do
let(:file) { File.open '.gitignore' }

context 'when file is open' do
context 'when file has not been read' do
it 'converts a file to a Tempfile' do
tempfile = FileToTempfile.convert(file)
context "when file is open" do
context "when file has not been read" do
it "converts a file to a Tempfile" do
tempfile = FileToTempfile.convert(file)

expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/"
end
expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/"
end
end

context 'when file has been read' do
before { file.read }
context "when file has been read" do
before { file.read }

it 'converts a file to a Tempfile' do
tempfile = FileToTempfile.convert(file)
it "converts a file to a Tempfile" do
tempfile = FileToTempfile.convert(file)

expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/"
end
expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/"
end
end

context 'when using_path is true' do
it 'converts a file to a Tempfile' do
tempfile = FileToTempfile.convert(file, true)
context "when using_path is true" do
it "converts a file to a Tempfile" do
tempfile = FileToTempfile.convert(file, using_path: true)

expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/#{file.path}"
end
expect(tempfile).to be_a Tempfile
expect(file.read).to eq tempfile.read
expect(tempfile.path).to start_with "/tmp/#{file.path}"
end
end
end

context 'when file has been closed' do
before { file.close }
context "when file has been closed" do
before { file.close }

it 'raises an error' do
expect do
FileToTempfile.convert(file)
end.to raise_error IOError, 'The file must be open before it can be converted'
end
it "raises an error" do
expect do
FileToTempfile.convert(file)
end.to raise_error IOError, "The file must be open before it can be converted"
end
end
end
Expand Down

0 comments on commit b697c7e

Please sign in to comment.