Class: Cuprum::Cli::Dependencies::FileSystem::Mock
- Inherits:
-
Cuprum::Cli::Dependencies::FileSystem
- Object
- Cuprum::Cli::Dependencies::FileSystem
- Cuprum::Cli::Dependencies::FileSystem::Mock
- 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
-
#files ⇒ Hash{String => Hash, IO}
readonly
The mocked directories and files.
-
#tempfiles ⇒ Array<String>
readonly
The contents of each generated tempfile.
Attributes inherited from Cuprum::Cli::Dependencies::FileSystem
Instance Method Summary collapse
-
#create_directory(path, recursive: false) ⇒ String
(also: #make_directory)
Creates a directory at the requested path.
-
#directory?(path) ⇒ true, false
(also: #directory_exists?)
Checks if the requested directory exists.
- #each_file(pattern) ⇒ Object
-
#file?(path) ⇒ true, false
(also: #file_exists?)
Checks if the requested directory exists.
-
#initialize(files: {}, root_path: Dir.pwd) ⇒ Mock
constructor
A new instance of Mock.
- #read_file(file_or_path) ⇒ Object (also: #read)
-
#with_tempfile {|the| ... } ⇒ Object
Creates a tempfile and passes it to the block.
- #write_file(file_or_path, data) ⇒ Object (also: #write)
Constructor Details
#initialize(files: {}, root_path: Dir.pwd) ⇒ Mock
Returns a new instance of Mock.
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
#files ⇒ Hash{String => Hash, IO} (readonly)
Returns the mocked directories and files.
40 41 42 |
# File 'lib/cuprum/cli/dependencies/file_system/mock.rb', line 40 def files @files end |
#tempfiles ⇒ Array<String> (readonly)
Returns 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.
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.
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_file ⇒ Enumerator<String> #each_file({ |file| }) {|the| ... } ⇒ 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.
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
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.
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
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 |