Module: Radius::Spec::Tempfile

Defined in:
lib/radius/spec/tempfile.rb

Overview

Temporary file helpers

These helpers are meant to ease the creation of temporary files to either stub the data out or provide a location for data to be saved then verified.

In the case of file stubs, using these helpers allows you to co-locate the file data with the specs. This makes it easy for someone to read the spec and understand the test case; instead of having to find a fixture file and look at its data. This also makes it easy to change the data between specs, allowing them to focus on just what they need.

To make these helpers available require them after the gem:

require 'radius/spec'
require 'radius/spec/tempfile'

Including Helpers in Specs

There are multiple ways you can use these helpers. Which method you choose depends on how much perceived magic/syntactic sugar you want:

  • call the helpers directly on the module
  • manually include the helper methods in the specs
  • use metadata to auto load this feature and include it in the specs

When using the metadata option you do not need to explicitly require the module. This gem registers metadata with the RSpec configuration when it loads and RSpec is defined. When the matching metadata is first used it will automatically require and include the helpers.

Any of following metadata will include the factory helpers:

  • :tempfile
  • :tmpfile

Examples:

use a helper directly in specs

require 'radius/spec/tempfile'

RSpec.describe AnyClass do
  it "includes the file helpers" do
    Radius::Spec::Tempfile.using_tempfile do |pathname|
      code_under_test pathname
      expect(pathname.read).to eq "Any written data"
    end
  end
end

manually include the helpers

require 'radius/spec/tempfile'

RSpec.describe AnyClass do
  include Radius::Spec::Tempfile
  it "includes the file helpers" do
    using_tempfile do |pathname|
      code_under_test pathname
      expect(pathname.read).to eq "Any written data"
    end
  end
end

use metadata to auto include the helpers

RSpec.describe AnyClass do
  it "includes the file helpers", :tempfile do
    using_tempfile do |pathname|
      code_under_test pathname
      expect(pathname.read).to eq "Any written data"
    end
  end
end

Since:

  • 0.5.0

Class Method Summary collapse

Class Method Details

.using_tempfile(*args, data: nil, **kwargs) {|pathname| ... } ⇒ Object

Note:

A block must be provided

Convenience wrapper for managaing temporary files.

This creates a temporary file and yields its path to the provided block. When the block returns the temporary file is deleted.

Optional Parameters

The block is required. All other parameters are optional. All parameters except data are Ruby version dependent and will be forwarded directly to the stdlib's Tempfile.create. The when the data parameter is provided it's contents will be written to the temporary file prior to yielding to the block.

Examples:

creating a tempfile to pass to code

def write_hello_world(filepath)
  File.write filepath, "Hello World"
end

Radius::Spec::Tempfile.using_tempfile do |pathname|
  write_hello_world pathname
end

creating a file stub

stub_data = "Any file stub data text."
Radius::Spec::Tempfile.using_tempfile(data: stub_data) do |stubpath|
  # File.read(stubpath)
  # => "Any file stub data text."
  code_under_test stubpath
end

creating a file stub inline

Radius::Spec::Tempfile.using_tempfile(data: <<~TEXT) do |stubpath|
  Any file stub data text.
TEXT
  # File.read(stubpath)
  # => "Any file stub data text.\n"
  code_under_test stubpath
end

creating a file stub inline without trailing newline

Radius::Spec::Tempfile.using_tempfile(data: <<~TEXT.chomp) do |stubpath|
  Any file stub data text.
TEXT
  # File.read(stubpath)
  # => "Any file stub data text."
  code_under_test stubpath
end

writing binary data inline

Radius::Spec::Tempfile.using_tempfile(encoding: Encoding::BINARY, data: <<~BIN.chomp) do |binpath|
  \xC8\x90\xC5\x9D\xE1\xB9\x95\xC4\x93\xC4\x89
BIN
  # File.read(binpath)
  # => "Ȑŝṕēĉ"
  code_under_test binpath
end

Parameters:

  • args (Object)

    addition file creation options

    Passed directly to Tempfile.create; see the stdlib docs for details on available options.

  • data (String) (defaults to: nil)

    stub data to write to the file before yielding

  • kwargs (Hash{Symbol => Object})

    addition file creation options

    Passed directly to Tempfile.create; see the stdlib docs for details on available options.

Yield Parameters:

  • pathname (Pathname)

    path of the created temporary file

See Also:

Since:

  • 0.5.0



152
153
154
155
156
157
158
159
# File 'lib/radius/spec/tempfile.rb', line 152

def using_tempfile(*args, data: nil, **kwargs)
  args << 'tmpfile' if args.empty?
  ::Tempfile.create(*args, **kwargs) do |f|
    f.write(data)
    f.close
    yield Pathname(f.path)
  end
end