Sha256: 766b78d9d5a68719f9e369c3684298f3c464629fdf6fc3c167726eb69439d341

Contents?: true

Size: 1.78 KB

Versions: 1

Compression:

Stored size: 1.78 KB

Contents

require 'spec_helper'
require 'file_creator'

RSpec.describe FileCreator do
  describe '#create' do
    subject(:create) { FileCreator.new.create(file_name) }
    let(:file_name) { "pluginscan.scan" }

    it 'tries to create a file with the given name' do
      allow(File).to receive(:new)
      create
      expect(File).to have_received(:new).with(file_name, 'w')
    end

    it 'returns the file if it successfully created it' do
      file = double(File)
      allow(File).to receive(:new).and_return file
      expect(create).to eq file
    end

    context 'if the filename is not a writeable location' do
      before { allow(File).to receive(:new).and_raise(Errno::EACCES) }
      it 'raises an error' do
        expect{ create }.to raise_error(FileCreator::Error, "You do not have permission to write to that location (#{file_name})")
      end
      # Real example:
      # File.new('/etc/foo', 'w')
      # => Errno::EACCES: Permission denied @ rb_sysopen - /etc/foo
    end

    context 'if the filename is a directory name' do
      before { allow(File).to receive(:new).and_raise(Errno::EISDIR) }
      it 'raises an error' do
        expect{ create }.to raise_error(FileCreator::Error, "File name is a directory (#{file_name})")
      end
      # Real example:
      # File.new("/", 'w')
      # => Errno::EISDIR: Is a directory @ rb_sysopen - /
    end

    context 'if the filename is in a directory which does not exist' do
      before { allow(File).to receive(:new).and_raise(Errno::ENOTDIR) }
      it 'raises an error' do
        expect{ create }.to raise_error(FileCreator::Error, "File name refers to a directory which does not exist (#{file_name})")
      end
      # Real example:
      # File.new("foo/bar", 'w')
      # => Errno::ENOTDIR: Not a directory @ rb_sysopen - foo/bar
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pluginscan-0.9.0 spec/file_creator_spec.rb