Class: Cuprum::Cli::Dependencies::FileSystem::Mock

Inherits:
Cuprum::Cli::Dependencies::FileSystem show all
Defined in:
lib/cuprum/cli/dependencies/file_system/mock.rb

Overview

Mock implementation of FileSystem for testing purposes.

Defined Under Namespace

Classes: InvalidPathError, MockTempfile

Instance Attribute Summary collapse

Attributes inherited from Cuprum::Cli::Dependencies::FileSystem

#root_path

Instance Method Summary collapse

Constructor Details

#initialize(files: {}, root_path: Dir.pwd) ⇒ Mock

Returns a new instance of Mock.

Parameters:

  • files (Hash{String => Hash, IO}) (defaults to: {})

    the mocked directories and files. Must be a Hash with String keys representing file path segments; Hash values represent directories, while IO values represent files.

  • root_path (String) (defaults to: Dir.pwd)

    the path to the root directory. Defaults to the value of Dir.pwd.



32
33
34
35
36
37
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 32

def initialize(files: {}, root_path: Dir.pwd)
  super(root_path:)

  @files     = files
  @tempfiles = []
end

Instance Attribute Details

#filesHash{String => Hash, IO} (readonly)

Returns the mocked directories and files.

Returns:

  • (Hash{String => Hash, IO})

    the mocked directories and files.



40
41
42
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 40

def files
  @files
end

#tempfilesArray<String> (readonly)

Returns the contents of each generated tempfile.

Returns:

  • (Array<String>)

    the contents of each generated tempfile.



43
44
45
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 43

def tempfiles
  @tempfiles
end

Instance Method Details

#create_directory(path, recursive: false) ⇒ String Also known as: make_directory

Creates a directory at the requested path.

Parameters:

  • path (String)

    the path to the directory.

  • recursive (true, false) (defaults to: false)

    if true, creates any required intermediate directories, equivalent to the -p flag on mkdir. Defaults to false.

Returns:

  • (String)

    the path to the created directory.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 46

def create_directory(path, recursive: false) # rubocop:disable Metrics/MethodLength
  tools.assertions.validate_name(path, as: 'path')

  *dir_names, dir_name = split_path(resolve_path(path))

  directory = write_directory(
    *dir_names,
    action:       'create directory',
    file_or_path: path,
    recursive:
  )

  if io_stream?(directory[dir_name])
    raise DirectoryIsAFileError,
      "unable to create directory #{path} - directory is a file"
  end

  directory[dir_name] = {}

  path
end

#directory?(path) ⇒ true, false Also known as: directory_exists?

Checks if the requested directory exists.

Parameters:

  • path (String)

    the path to the requested directory.

Returns:

  • (true, false)

    true if the directory exists and is a directory, otherwise false.



70
71
72
73
74
75
76
77
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 70

def directory?(path)
  tools.assertions.validate_name(path, as: 'path')

  path = resolve_path(path)
  mock = resolve_mock(path)

  mock.is_a?(Hash)
end

#each_fileEnumerator<String> #each_file({ |file| }) {|the| ... } ⇒ nil

Overloads:

  • #each_fileEnumerator<String>

    Iterates over file names matching the given pattern.

    Parameters:

    • pattern (String)

      the file pattern to match.

    Returns:

    • (Enumerator<String>)

      an enumerator over the matching file names.

  • #each_file({ |file| }) {|the| ... } ⇒ nil

    Yields each file name matching the given pattern.

    Parameters:

    • pattern (String)

      the file pattern to match.

    Yield Parameters:

    • the (String)

      matching file name.

    Returns:

    • (nil)


81
82
83
84
85
86
87
88
89
90
91
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 81

def each_file(pattern, &)
  return enum_for(:each_file, pattern) unless block_given?

  flattened_files.each do |file_path|
    next unless matches_pattern?(file_path:, pattern:)

    yield File.join(root_path, file_path)
  end

  nil
end

#file?(path) ⇒ true, false Also known as: file_exists?

Checks if the requested directory exists.

Parameters:

  • path (String)

    the path to the requested directory.

Returns:

  • (true, false)

    true if the directory exists and is a directory, otherwise false.



94
95
96
97
98
99
100
101
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 94

def file?(path)
  tools.assertions.validate_name(path, as: 'path')

  path = resolve_path(path)
  mock = resolve_mock(path)

  io_stream?(mock)
end

#read_file(file) ⇒ String #read_file(path) ⇒ String Also known as: read

Overloads:

  • #read_file(file) ⇒ String

    Reads the contents of the given file or IO stream.

    Parameters:

    • file (IO)

      the file to read.

    Returns:

    • (String)

      the file contents.

  • #read_file(path) ⇒ String

    Reads the contents of the file at the given path.

    Parameters:

    • path (String)

      the file path to read.

    Returns:

    • (String)

      the file contents.

    Raises:

Raises:



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 105

def read_file(file_or_path) # rubocop:disable Metrics/MethodLength
  validate_file(file_or_path, as: 'file')

  return file_or_path.read if io_stream?(file_or_path)

  path = resolve_path(file_or_path)

  unless path.start_with?(root_path)
    raise FileNotFoundError,
      "unable to read file #{file_or_path} - file not found"
  end

  mock = resolve_mock(path)

  return mock.tap(&:rewind).read if io_stream?(mock)

  if mock
    raise FileIsADirectoryError,
      "unable to read file #{file_or_path} - file is a directory"
  end

  raise FileNotFoundError,
    "unable to read file #{file_or_path} - file not found"
end

#with_tempfile {|the| ... } ⇒ Object

Creates a tempfile and passes it to the block.

Yield Parameters:

  • the (File)

    generated tempfile.

Returns:

  • (Object)

    the value returned by the block.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 132

def with_tempfile(&block)
  file_name = SecureRandom.uuid
  file_path = File.join(root_path, 'tempfiles', file_name)

  tempfile = MockTempfile.new(file_path)

  (files['tempfiles'] ||= {})[file_name] = tempfile

  block.call(tempfile).tap do
    tempfiles << read_file(tempfile.tap(&:rewind))
  end
ensure
  files['tempfiles'].delete(file_name)
end

#write_file(file, data) ⇒ Integer #write_file(path, data) ⇒ Integer Also known as: write

Overloads:

  • #write_file(file, data) ⇒ Integer

    Writes the data to the given file or IO stream.

    Parameters:

    • file (IO)

      the file to write.

    • data (String)

      the data to write.

    Returns:

    • (Integer)

      the number of bytes written.

  • #write_file(path, data) ⇒ Integer

    Writes the data to the file at the given path.

    Parameters:

    • path (String)

      the file path to write.

    • data (String)

      the data to write.

    Returns:

    • (Integer)

      the number of bytes written.

    Raises:

Raises:



148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 148

def write_file(file_or_path, data) # rubocop:disable Metrics/MethodLength
  validate_file(file_or_path, as: 'file')

  return file_or_path.write(data) if io_stream?(file_or_path)

  path = resolve_path(file_or_path)

  unless path.start_with?(root_path)
    raise DirectoryNotFoundError,
      "unable to write file #{file_or_path} - directory not found"
  end

  mock = resolve_mock(path)

  if mock.is_a?(MockTempfile)
    mock.write(data)
    mock.rewind

    return
  end

  if mock.nil? || io_stream?(mock)
    return write_mock_file(path, data, file_or_path)
  end

  raise FileIsADirectoryError,
    "unable to write file #{file_or_path} - file is a directory"
end