Class: Cuprum::Cli::Dependencies::FileSystem::Mock
- Parent Namespace
- Cuprum::Cli::Dependencies::FileSystem
- Inherited Classes
- Cuprum::Cli::Dependencies::FileSystem
>
Object
- Defined In
- lib/cuprum/cli/dependencies/file_system/mock.rb
Table Of Contents
Overview
Mock implementation of FileSystem for testing purposes.
Back To Top
Defined Under Namespace
- Classes
- InvalidPathError,
MockTempfile
Back To Top
Constructor
#initialize(files: {}, root_path: Dir.pwd) => Mock
Parameters
- files (Hash{String => Hash, IO}) — 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) — the path to the root directory. Defaults to the
value of `Dir.pwd`.
Returns
- (Mock) — a new instance of Mock
Back To Top
Instance Attributes
#files => Hash{String => Hash, IO} (readonly)
Returns
- (Hash{String => Hash, IO}) — the mocked directories and files.
#root_path => String (readonly)
- Inherited From
- Cuprum::Cli::Dependencies::FileSystem
Returns
- (String) — the path to the root directory.
#tempfiles => Array<String> (readonly)
Returns
- (Array<String>) — the contents of each generated tempfile.
Back To Top
Instance Methods
#copy_file(source_path, destination_path, force: false) => Integer
#copy_file(source_path, destination_path, force: false) { |source| } => Integer
- Inherited From
- Cuprum::Cli::Dependencies::FileSystem
Overloads
#copy_file(source_path, destination_path, force: false) => Integer
Copies the contents of the source file to the destination path.
Parameters
- source_path (String) — the path of the file to copy.
- target_path (String) — the path to write the file to.
- force (true, false) — if true, overwrites an existing file (not a
directory) at the destination path. Defaults to false.
Returns
- (Integer) — the number of bytes written.
Raises
- (FileNotFoundError) — if the file to copy does not exist.
- (FileIsADirectoryError) — if the file to copy is a directory, or if
there is a directory at the destination path.
- (FileAlreadyExistsError) — if there is a file at the destination
path and the force: option is false.
#copy_file(source_path, destination_path, force: false) { |source| } => Integer
Reads the source file and generates a new file with modified contents.
Parameters
- source_path (String) — the path of the file to copy.
- target_path (String) — the path to write the file to.
- force (true, false) — if true, overwrites an existing file (not a
directory) at the destination path. Defaults to false.
Yields
- the transformation function used to modify the contents of the
file.
Yield Parameters
- source (String) — the contents of the source file.
Yield Returns
- (String) — the contents of the destination file.
Returns
- (Integer) — the number of bytes written.
Raises
- (FileNotFoundError) — if the file to copy does not exist.
- (FileIsADirectoryError) — if the file to copy is a directory, or if
there is a directory at the destination path.
- (FileAlreadyExistsError) — if there is a file at the destination
path and the force: option is false.
#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) — 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.
#delete_directory(path, force: false, recursive: false) => String
Also known as:
remove_directory
Removes the directory at the requested path.
Fails if the directory contains any files or child directories. The
:recursive option enables removing empty child or descendent directories,
while the :force option enables removing both files and directories.
Parameters
- path (String) — the path to the directory.
- force (true, false) — if true, removes nested files and directories.
- recursive (true, false) — if true, removes nested empty directories.
Returns
- (String) — the path to the removed directory.
Raises
#delete_file(path) => String
Also known as:
remove_file
Removes the file at the requested path.
Parameters
- path (String) — the path to the file.
Returns
- (String) — the path to the removed file.
#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.
#each_file => Enumerator<String>
#each_file { |file| } => nil
Overloads
#each_file => Enumerator<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| } => 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
#expanded_dirs => Array<String>
Returns
- (Array<String>) — a list of the defined directory paths in the mock
file system.
#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.
#flattened_files => Array<String>
Returns
- (Array<String>) — a list of the defined file paths in the mock file
system.
#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
- (FileIsADirectoryError) — if the requested file is a directory.
- (FileNotFoundError) — if the requested file is not found.
Raises
#with_tempfile(&block) => Object
Creates a tempfile and passes it to the block.
Yield Parameters
- the (File) — generated tempfile.
Returns
- (Object) — the value returned by the block.
#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
- (DirectoryIsAFileError) — if the path to the requested file
includes an existing file.
- (DirectoryNotFoundError) — if the directory for the requested file
is not found.
- (FileIsADirectoryError) — if the requested file is a directory.
Raises
Back To Top
Back to
Documentation |
Reference
|
Cuprum
|
Cuprum::Cli
|
Cuprum::Cli::Dependencies
|
Cuprum::Cli::Dependencies::FileSystem