Sha256: 99e32417db38b3c7935082731476afc13d2d42690c6871bfb434a9ebe236de9c

Contents?: true

Size: 1.87 KB

Versions: 23

Compression:

Stored size: 1.87 KB

Contents

require "spec_helper"
require "tempfile"

RSpec.describe Files::File, :with_test_folder do
  describe "#read" do
    before do
      Files::File.open(test_folder.join("read.txt").to_s, 'w', options) do |f|
        f.write("contents")
      end
    end

    it "returns the body of the file" do
      file = Files::File.find(test_folder.join("read.txt").to_s, {}, options)
      expect(file.read).to eq("contents")
    end
  end

  describe "#read_io" do
    before do
      Files::File.open(test_folder.join("read.txt").to_s, 'w', options) do |f|
        f.write("contents")
      end
    end

    it "returns an IO Object" do
      file = Files::File.find(test_folder.join("read.txt").to_s, {}, options)
      expect(file.read_io.class).to eq(IO)
      expect(file.read_io.read).to eq("contents")
    end
  end

  describe "#write" do
    it "can take string" do
      Files::File.open(test_folder.join("write-as-string.txt").to_s, 'w', options) do |f|
        f.write("I am a string")
      end

      file = Files::File.find(test_folder.join("write-as-string.txt").to_s, {}, options)
      expect(file.read).to eq("I am a string")
    end

    it "can take IO object" do
      Files::File.open(test_folder.join("write-as-io.txt").to_s, 'w', options) do |f|
        f.write(StringIO.new("I am a string via IO"))
      end

      file = Files::File.find(test_folder.join("write-as-io.txt").to_s, {}, options)
      expect(file.read).to eq("I am a string via IO")
    end

    it "can take an IO without #size" do
      temp_file = Tempfile.new("testing_io.txt")

      temp_file.write("I am a string via IO")
      temp_file.rewind
      fd = IO.sysopen(temp_file.path)
      io = IO.new(fd)

      file = Files::File.open(test_folder.join("write-as-io.txt").to_s, 'w', options) do |f|
        f.write(io)
      end

      expect(file.read).to eq("I am a string via IO")
      temp_file.close
    end
  end
end

Version data entries

23 entries across 23 versions & 1 rubygems

Version Path
files.com-1.0.66 spec/models/file_spec.rb
files.com-1.0.65 spec/models/file_spec.rb
files.com-1.0.64 spec/models/file_spec.rb
files.com-1.0.63 spec/models/file_spec.rb
files.com-1.0.62 spec/models/file_spec.rb
files.com-1.0.61 spec/models/file_spec.rb
files.com-1.0.60 spec/models/file_spec.rb
files.com-1.0.59 spec/models/file_spec.rb
files.com-1.0.58 spec/models/file_spec.rb
files.com-1.0.57 spec/models/file_spec.rb
files.com-1.0.56 spec/models/file_spec.rb
files.com-1.0.55 spec/models/file_spec.rb
files.com-1.0.54 spec/models/file_spec.rb
files.com-1.0.53 spec/models/file_spec.rb
files.com-1.0.52 spec/models/file_spec.rb
files.com-1.0.51 spec/models/file_spec.rb
files.com-1.0.50 spec/models/file_spec.rb
files.com-1.0.49 spec/models/file_spec.rb
files.com-1.0.48 spec/models/file_spec.rb
files.com-1.0.47 spec/models/file_spec.rb