Module: Kitchen::Docker::Helpers::FileHelper

Included in:
Container
Defined in:
lib/kitchen/docker/helpers/file_helper.rb

Overview

Local temp-file handling.

Instance Method Summary collapse

Instance Method Details

#create_temp_file(file, contents) ⇒ void

This method returns an undefined value.

Writes a temp file, creating its parent directory if needed.

Written with File.write, which opens, writes and closes in one call. The previous implementation assigned the open file back over the file parameter and closed it in an ensure, so when opening failed -- a read-only directory, a parent that is not a directory, a full disk -- file was still the path String and the ensure raised "undefined method 'close' for an instance of String", replacing the real error with a Ruby one. Its rescue did not help either: it caught IOError, while opening a file fails with Errno classes, which are SystemCallError and not IOError.

Parameters:

  • file (String)

    path to write

  • contents (String)

    what to write

Raises:

  • (RuntimeError)

    if the file cannot be written, naming the path and the underlying cause



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kitchen/docker/helpers/file_helper.rb', line 39

def create_temp_file(file, contents)
  debug("[Docker] Creating temp file #{file}")
  debug("[Docker] --- Start Temp File Contents ---")
  debug(contents)
  debug("[Docker] --- End Temp File Contents ---")

  path = ::File.dirname(file)
  ::FileUtils.mkdir_p(path) unless ::Dir.exist?(path)
  ::File.write(file, contents)
rescue SystemCallError, IOError => e
  raise "Failed to write temp file #{file}. Error Details: #{e}"
end