gubbins-fs 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/CHANGELOG.md +3 -0
- data/LICENSE +289 -0
- data/README.md +128 -0
- data/lib/fs/atomic_write.rb +121 -0
- data/lib/fs/concerns/argument_validation.rb +140 -0
- data/lib/fs/concerns/file_dsl_methods.rb +471 -0
- data/lib/fs/concerns/required_arguments.rb +141 -0
- data/lib/fs/directory/accessibility_check.rb +89 -0
- data/lib/fs/directory.rb +497 -0
- data/lib/fs/errors.rb +491 -0
- data/lib/fs/file.rb +1524 -0
- data/lib/fs/file_variant_builder.rb +269 -0
- data/lib/fs/file_variant_proxy.rb +253 -0
- data/lib/fs/files/csv.rb +26 -0
- data/lib/fs/files/gif.rb +10 -0
- data/lib/fs/files/image.rb +390 -0
- data/lib/fs/files/jpg.rb +36 -0
- data/lib/fs/files/json.rb +24 -0
- data/lib/fs/files/pdf.rb +231 -0
- data/lib/fs/files/pdf_collection.rb +64 -0
- data/lib/fs/files/png.rb +10 -0
- data/lib/fs/files/text.rb +13 -0
- data/lib/fs/files/tiff.rb +24 -0
- data/lib/fs/files/zip.rb +123 -0
- data/lib/fs/lockfile_sweeper.rb +70 -0
- data/lib/fs/tempfile_sweeper.rb +79 -0
- data/lib/fs/testing/mock/fs/configurator.rb +223 -0
- data/lib/fs/testing/mock/fs/dirs/directory.rb +41 -0
- data/lib/fs/testing/mock/fs/dsl.rb +81 -0
- data/lib/fs/testing/mock/fs/file.rb +60 -0
- data/lib/fs/testing/mock/fs/file_klass_mock.rb +350 -0
- data/lib/fs/testing/mock/fs/files/jpg.rb +14 -0
- data/lib/fs/testing/mock/fs/files/pdf.rb +19 -0
- data/lib/fs/testing/mock/fs/files/png.rb +12 -0
- data/lib/fs/testing/mock/fs/files/tiff.rb +12 -0
- data/lib/fs/testing/mock/fs/files/zip.rb +23 -0
- data/lib/fs/testing.rb +17 -0
- data/lib/fs/version.rb +3 -0
- data/lib/fs.rb +26 -0
- data/lib/gubbins/fs.rb +4 -0
- metadata +124 -0
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shell/pdf_detach"
|
|
4
|
+
|
|
5
|
+
module Fs
|
|
6
|
+
module Files
|
|
7
|
+
# Represents a PDF collection (aka "PDF portfolio"), an Adobe-proprietary
|
|
8
|
+
# format in which a .pdf file acts as a container for other embedded files.
|
|
9
|
+
class PDFCollection < Fs::Files::PDF
|
|
10
|
+
# @see Shell::PDFDetach.to_temp
|
|
11
|
+
# @yieldparam temp_dir [Fs::Directory]
|
|
12
|
+
# @return [Shell::Command::Result]
|
|
13
|
+
def extract_to_temp(&block)
|
|
14
|
+
Shell::PDFDetach.to_temp(self, &block)
|
|
15
|
+
.raise_failures
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# @param path [Pathname]
|
|
19
|
+
# @see Shell::PDFDetach.to
|
|
20
|
+
# @return [Shell::Command::Result]
|
|
21
|
+
def extract_to(path)
|
|
22
|
+
Shell::PDFDetach.to(input: self, output: path)
|
|
23
|
+
.raise_failures
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @see Shell::PDFDetach.list
|
|
27
|
+
# @return [Array<Pathname>]
|
|
28
|
+
def list
|
|
29
|
+
Shell::PDFDetach.list(self)
|
|
30
|
+
.raise_failures
|
|
31
|
+
.value
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Extracts embedded files to a temporary directory and yields each one
|
|
35
|
+
#
|
|
36
|
+
# The temporary directory is removed automatically after the block returns.
|
|
37
|
+
#
|
|
38
|
+
# Pass `wrap: true` to receive {Fs::File} objects instead of {Pathname}s.
|
|
39
|
+
#
|
|
40
|
+
# @param wrap [Boolean] yield {Fs::File} objects instead of {Pathname}
|
|
41
|
+
# @yieldparam [Pathname, Fs::File] once per extracted file
|
|
42
|
+
# @return [Shell::Command::Result]
|
|
43
|
+
def each_extracted_temp_file(wrap: false, &block)
|
|
44
|
+
Shell::PDFDetach.to_temp(self) do |temp_dir|
|
|
45
|
+
temp_dir.glob("*")
|
|
46
|
+
.reject(&:directory?)
|
|
47
|
+
.each { |file_path| yield wrap ? Fs::File.new(path: file_path) : file_path }
|
|
48
|
+
end.raise_failures
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
private
|
|
52
|
+
|
|
53
|
+
# Reverse of {Fs::Files::PDF#custom_validations} — raises if the file is
|
|
54
|
+
# NOT a collection. Does not call super, as the parent raises for collections.
|
|
55
|
+
#
|
|
56
|
+
# @raise [Errors::Fs::Validation::FileUnreadable]
|
|
57
|
+
# @return [void]
|
|
58
|
+
def custom_validations
|
|
59
|
+
raise Errors::Fs::Validation::FileUnreadable,
|
|
60
|
+
"#{path} is not a PDF collection" unless collection?
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
end
|
|
64
|
+
end
|
data/lib/fs/files/png.rb
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shell/tiff_2_pdf"
|
|
4
|
+
|
|
5
|
+
module Fs
|
|
6
|
+
module Files
|
|
7
|
+
class TIFF < Image
|
|
8
|
+
extension :tif, :tiff
|
|
9
|
+
mime_type "image/tiff"
|
|
10
|
+
|
|
11
|
+
variant(:pdf, "Fs::Files::PDF") do
|
|
12
|
+
get_path_from do |klass:|
|
|
13
|
+
path.sub_ext("." + klass.extensions.first)
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
create_with do |klass:, path:, content: nil, create_block: nil, **kargs|
|
|
17
|
+
Shell::TIFF2PDF.convert(input: self.path, output: path, **kargs).raise_failures
|
|
18
|
+
|
|
19
|
+
klass.load(path: path)
|
|
20
|
+
end
|
|
21
|
+
end
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
end
|
data/lib/fs/files/zip.rb
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "shell/seven_zip"
|
|
4
|
+
|
|
5
|
+
module Fs
|
|
6
|
+
module Files
|
|
7
|
+
class Zip < Fs::File
|
|
8
|
+
extension :zip
|
|
9
|
+
mime_type "application/zip"
|
|
10
|
+
encoding "binary"
|
|
11
|
+
|
|
12
|
+
# Unzips the archive into a temporary directory and yields
|
|
13
|
+
# the directories {Pathname} to the given block
|
|
14
|
+
#
|
|
15
|
+
# The temporary directory is removed automatically after the
|
|
16
|
+
# block returns.
|
|
17
|
+
#
|
|
18
|
+
# @raise [Error::Fs::ReadWrite]
|
|
19
|
+
# @yield [path_name] the {Pathname} for the temp dir
|
|
20
|
+
# @return [Shell::Command::Result]
|
|
21
|
+
# `Result#value` [Pathname] only when called without a block
|
|
22
|
+
def unzip_to_temp(&block)
|
|
23
|
+
Shell::SevenZip.to_temp(self, &block)
|
|
24
|
+
.raise_failures
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# Unzips the archive into a temporary directory and yields
|
|
28
|
+
# each temp file to the given block
|
|
29
|
+
#
|
|
30
|
+
# The temporary directory is removed automatically after the
|
|
31
|
+
# last block returns.
|
|
32
|
+
#
|
|
33
|
+
# Pass `wrap: true` to receive {Fs:File} objects instead of
|
|
34
|
+
# {Pathname}s.
|
|
35
|
+
#
|
|
36
|
+
# @param wrap [Boolean] yield {Fs::File} objects instead of
|
|
37
|
+
# {Pathname}
|
|
38
|
+
# @yieldparam [Pathname,Fs::File] pathname once per unzipped file,
|
|
39
|
+
# depending on `wrap`
|
|
40
|
+
# @raise [Error::Fs::ReadWrite]
|
|
41
|
+
# @return [Shell::Command::Result]
|
|
42
|
+
def each_unzipped_temp_file(wrap: false, &block)
|
|
43
|
+
Shell::SevenZip.to_temp(self) do |temp_dir_path|
|
|
44
|
+
entries =
|
|
45
|
+
begin
|
|
46
|
+
temp_dir_path.glob("**/*")
|
|
47
|
+
|
|
48
|
+
rescue ArgumentError => ex
|
|
49
|
+
# In production on Debian, we have encountered issues with filenames
|
|
50
|
+
# from certain Windows-sourced Zip archives.
|
|
51
|
+
#
|
|
52
|
+
# This is a hacky workaround. I cannot reproduce the issue
|
|
53
|
+
# locally, so this might be due to our locale setting being
|
|
54
|
+
# fucky in staging/prod.
|
|
55
|
+
#
|
|
56
|
+
# It might be worth trying to either a) fix the locale properly,
|
|
57
|
+
# or b) set LC_ALL=en_US.UTF-8 while executing 7z (Hash as first
|
|
58
|
+
# parameter to Open3.capture3).
|
|
59
|
+
if ex.message == "invalid byte sequence in UTF-8"
|
|
60
|
+
temp_dir_path.glob("**/*".encode("ISO-8859-15"))
|
|
61
|
+
|
|
62
|
+
else
|
|
63
|
+
raise
|
|
64
|
+
end
|
|
65
|
+
end
|
|
66
|
+
|
|
67
|
+
entries.reject(&:directory?)
|
|
68
|
+
.each { |file_path| yield wrap ? Fs::File.new(path: file_path) : file_path }
|
|
69
|
+
end.raise_failures
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
# Unzips the archive to `path`
|
|
73
|
+
#
|
|
74
|
+
# TODO check and document whether the target directory must
|
|
75
|
+
# exist, both with unzip and 7z
|
|
76
|
+
#
|
|
77
|
+
# @param path [Pathname] path to the unzip target directory
|
|
78
|
+
# @return [Shell::Command::Result]
|
|
79
|
+
def unzip_to(path)
|
|
80
|
+
Shell::SevenZip.to(input: self, output: path)
|
|
81
|
+
.raise_failures
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# @see {Shell::SevenZip.list}
|
|
85
|
+
def list
|
|
86
|
+
Shell::SevenZip.list(self)
|
|
87
|
+
.raise_failures
|
|
88
|
+
.value
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# @see {Shell::SevenZip.uncompressed_size_below?}
|
|
92
|
+
def uncompressed_size_below?(megabytes:)
|
|
93
|
+
Shell::SevenZip.uncompressed_size_below?(self, megabytes: megabytes)
|
|
94
|
+
.raise_failures
|
|
95
|
+
.value
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
# @raise [Errors::Fs::Validation::FileUnreadable]
|
|
99
|
+
# @return [void]
|
|
100
|
+
def custom_validations
|
|
101
|
+
super
|
|
102
|
+
|
|
103
|
+
unless valid_zip?
|
|
104
|
+
raise Errors::Fs::Validation::FileUnreadable
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
# Are we a valid archive?
|
|
109
|
+
#
|
|
110
|
+
# @return [Boolean]
|
|
111
|
+
def valid_zip?(**kargs)
|
|
112
|
+
Shell::SevenZip.valid?(self)
|
|
113
|
+
end
|
|
114
|
+
|
|
115
|
+
# Are we a password-protected archive?
|
|
116
|
+
#
|
|
117
|
+
# @return [Boolean]
|
|
118
|
+
def encrypted?(**kargs)
|
|
119
|
+
Shell::SevenZip.encrypted?(self)
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
end
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fs
|
|
4
|
+
# Removes lockfiles created by {Fs::File.with_creation_lock}.
|
|
5
|
+
#
|
|
6
|
+
# Those lockfiles are deliberately never cleaned up by {Fs::File} itself -
|
|
7
|
+
# see the docs on {Fs::File.with_creation_lock} for an explanation.
|
|
8
|
+
#
|
|
9
|
+
# The caller needs to ensure that this is called at a time when all locks
|
|
10
|
+
# are guaranteed to be stale.
|
|
11
|
+
#
|
|
12
|
+
# @example sweep a single directory's lockfiles, ignoring subdirectories
|
|
13
|
+
# Fs::LockfileSweeper.sweep("/foo/bar")
|
|
14
|
+
#
|
|
15
|
+
# @example sweep a whole directory tree
|
|
16
|
+
# Fs::LockfileSweeper.sweep("/foo/bar", recursive: true)
|
|
17
|
+
class LockfileSweeper
|
|
18
|
+
# @param path [Pathname, String, Fs::Directory, Fs::File] the
|
|
19
|
+
# directory to sweep
|
|
20
|
+
# @param recursive [Boolean] also sweep subdirectories
|
|
21
|
+
# @return [Integer] the number of lockfiles removed
|
|
22
|
+
def self.sweep(path, recursive: false)
|
|
23
|
+
new(path, recursive: recursive).sweep
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
# @param path [Pathname, String, Fs::Directory, Fs::File]
|
|
27
|
+
# @param recursive [Boolean]
|
|
28
|
+
def initialize(path, recursive: false)
|
|
29
|
+
@path = Fs::File.normalize_path_input(path)
|
|
30
|
+
@recursive = recursive
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# @return [Integer] the number of lockfiles removed
|
|
34
|
+
def sweep
|
|
35
|
+
lockfiles.count { |lockfile| delete(lockfile) }
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
private
|
|
39
|
+
|
|
40
|
+
# A lockfile which is gone by the time we get to it (a concurrent sweep, or
|
|
41
|
+
# the removal of its directory) is what we are after anyway. It does not
|
|
42
|
+
# count as removed by us, though.
|
|
43
|
+
#
|
|
44
|
+
# @param lockfile [Pathname]
|
|
45
|
+
# @return [Boolean] whether we removed it
|
|
46
|
+
def delete(lockfile)
|
|
47
|
+
::File.delete(lockfile)
|
|
48
|
+
true
|
|
49
|
+
|
|
50
|
+
rescue Errno::ENOENT
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# @return [Array<Pathname>]
|
|
55
|
+
def lockfiles
|
|
56
|
+
@path.glob(glob_pattern)
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# @return [String]
|
|
60
|
+
def glob_pattern
|
|
61
|
+
pattern = "*.#{Fs::File::CREATION_LOCK_EXTENSION}"
|
|
62
|
+
recursive? ? "**/#{pattern}" : pattern
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# @return [Boolean]
|
|
66
|
+
def recursive?
|
|
67
|
+
@recursive
|
|
68
|
+
end
|
|
69
|
+
end
|
|
70
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Fs
|
|
4
|
+
# Removes the temporary files {Fs::AtomicWrite} leaves behind when its process is killed
|
|
5
|
+
# mid-write.
|
|
6
|
+
#
|
|
7
|
+
# Unlike lockfiles (see {Fs::LockfileSweeper}), these are real files and may belong to a
|
|
8
|
+
# write which is still going on, so only those older than a given age are removed.
|
|
9
|
+
#
|
|
10
|
+
# @example sweep the configured temporary directory
|
|
11
|
+
# Fs::TempfileSweeper.sweep(Fs::Dirs::Tmp.new, older_than: 1.day)
|
|
12
|
+
#
|
|
13
|
+
# @example sweep below a directory, for temporary files created next to their targets
|
|
14
|
+
# Fs::TempfileSweeper.sweep("/foo/bar", older_than: 1.day, recursive: true)
|
|
15
|
+
class TempfileSweeper
|
|
16
|
+
# @param path [Pathname, String, Fs::Directory, Fs::File] the
|
|
17
|
+
# directory to sweep
|
|
18
|
+
# @param older_than [Numeric] age in seconds, eg. `1.day`
|
|
19
|
+
# @param recursive [Boolean] also sweep subdirectories
|
|
20
|
+
# @return [Integer] the number of temporary files removed
|
|
21
|
+
def self.sweep(path, older_than:, recursive: false)
|
|
22
|
+
new(path, older_than: older_than, recursive: recursive).sweep
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @param path [Pathname, String, Fs::Directory, Fs::File]
|
|
26
|
+
# @param older_than [Numeric]
|
|
27
|
+
# @param recursive [Boolean]
|
|
28
|
+
def initialize(path, older_than:, recursive: false)
|
|
29
|
+
@path = Fs::File.normalize_path_input(path)
|
|
30
|
+
@older_than = older_than
|
|
31
|
+
@recursive = recursive
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# @return [Integer] the number of temporary files removed
|
|
35
|
+
def sweep
|
|
36
|
+
leftovers.count { |leftover| delete(leftover) }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
private
|
|
40
|
+
|
|
41
|
+
# A file which is gone by the time we get to it is what we are after anyway. It does
|
|
42
|
+
# not count as removed by us, though.
|
|
43
|
+
#
|
|
44
|
+
# @param leftover [Pathname]
|
|
45
|
+
# @return [Boolean] whether we removed it
|
|
46
|
+
def delete(leftover)
|
|
47
|
+
::File.delete(leftover)
|
|
48
|
+
true
|
|
49
|
+
|
|
50
|
+
rescue Errno::ENOENT
|
|
51
|
+
false
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
# @return [Array<Pathname>]
|
|
55
|
+
def leftovers
|
|
56
|
+
@path.glob(glob_pattern)
|
|
57
|
+
.select { |candidate| Fs::AtomicWrite.temp_file?(candidate) && abandoned?(candidate) }
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
# @param candidate [Pathname]
|
|
61
|
+
# @return [Boolean]
|
|
62
|
+
def abandoned?(candidate)
|
|
63
|
+
candidate.file? && candidate.mtime < Time.now - @older_than
|
|
64
|
+
|
|
65
|
+
rescue Errno::ENOENT
|
|
66
|
+
false
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
# @return [String]
|
|
70
|
+
def glob_pattern
|
|
71
|
+
recursive? ? "**/.*" : ".*"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
# @return [Boolean]
|
|
75
|
+
def recursive?
|
|
76
|
+
@recursive
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
# A copy of the `Mock::Configurator` of gubbins-rails, so that this gem does not depend on it
|
|
4
|
+
module Mock
|
|
5
|
+
module Fs
|
|
6
|
+
# `Mock::Fs::Configurator` is an interface object for configuring mocks. It
|
|
7
|
+
# is supposed to be yielded by methods which install mocks:
|
|
8
|
+
#
|
|
9
|
+
# ```ruby
|
|
10
|
+
# # We want to create a helper for mocking `SomeClass.some_class_method`:
|
|
11
|
+
# module Mock
|
|
12
|
+
# class SomeClass
|
|
13
|
+
# def self.some_class_method
|
|
14
|
+
# configurator =
|
|
15
|
+
# Mock::Fs::Configurator.new(target: SomeClass, method_name: :some_class_method)
|
|
16
|
+
#
|
|
17
|
+
# yield configurator
|
|
18
|
+
# end
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# # We can now configure the mock:
|
|
23
|
+
# Mock::SomeClass.some_class_method do |config|
|
|
24
|
+
# # `config` is our `Mock::Fs::Configurator` instance
|
|
25
|
+
# config.return_value true
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# # Please not that, different from the example above, `Mock::Shell::` and `Mock::Fs::`
|
|
29
|
+
# # classes also allow you to skip the block argument when configuring a mock:
|
|
30
|
+
# Mock::Shell::File.on(:mime_type) do
|
|
31
|
+
# # Since our block does not take any arguments, the block scope
|
|
32
|
+
# # is set to our `Mock::Fs::Configurator` instance
|
|
33
|
+
# return_value true
|
|
34
|
+
# end
|
|
35
|
+
# ```
|
|
36
|
+
#
|
|
37
|
+
# The instance methods `#return_value`, `#for_arg` and `fail_with` are used
|
|
38
|
+
# to configure the behaviour of the mock method to be installed.
|
|
39
|
+
class Configurator
|
|
40
|
+
include RSpec::Mocks::ExampleMethods
|
|
41
|
+
|
|
42
|
+
attr_reader :target
|
|
43
|
+
attr_reader :method_name
|
|
44
|
+
attr_reader :value_transformer
|
|
45
|
+
|
|
46
|
+
# @param target [Class] the class on which to install the mock
|
|
47
|
+
# @param method_name [Symbol] the method to mock
|
|
48
|
+
def initialize(target:, method_name:)
|
|
49
|
+
@target = target
|
|
50
|
+
@method_name = method_name
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Configure a value transformer block
|
|
54
|
+
#
|
|
55
|
+
# The given block will be called for each value the mock is supposed
|
|
56
|
+
# to return, and the block return value will be returned from the mock instead.
|
|
57
|
+
#
|
|
58
|
+
# @example
|
|
59
|
+
# ```ruby
|
|
60
|
+
# # We want to create a helper for mocking `SomeClass.some_class_method`.
|
|
61
|
+
# # `.some_class_method` returns an object with a `.value` method. To make it easier
|
|
62
|
+
# # to configure the mock, we take care of that with `.transform_values`:
|
|
63
|
+
# module Mock
|
|
64
|
+
# class SomeClass
|
|
65
|
+
# def self.some_class_method(&block)
|
|
66
|
+
# configurator =
|
|
67
|
+
# Mock::Fs::Configurator.new(target: SomeClass, method_name: :some_class_method)
|
|
68
|
+
# .transform_values { |value| Struct.new(:value).new(value) }
|
|
69
|
+
# yield configurator
|
|
70
|
+
# end
|
|
71
|
+
# end
|
|
72
|
+
# end
|
|
73
|
+
#
|
|
74
|
+
# Mock::SomeClass.some_class_method { |config| config.return_value "yay", "nay" }
|
|
75
|
+
#
|
|
76
|
+
# SomeClass.some_class_method
|
|
77
|
+
# # => #<struct value="yay">
|
|
78
|
+
# SomeClass.some_class_method
|
|
79
|
+
# # => #<struct value="nay">
|
|
80
|
+
# ```
|
|
81
|
+
# @param block [Proc]
|
|
82
|
+
# @yieldparam value [Object] the value this mock is configured to return
|
|
83
|
+
# @yieldreturn [Object] the value your block has determined should be returned instead
|
|
84
|
+
# @return [self]
|
|
85
|
+
def transform_values(&block)
|
|
86
|
+
@value_transformer = block
|
|
87
|
+
|
|
88
|
+
self
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Configure one or more values your mock should return
|
|
92
|
+
#
|
|
93
|
+
# When providing multiple values, each consecutive call to the mock
|
|
94
|
+
# will return the next value. When providing a block, the block
|
|
95
|
+
# return value will be returned instead; the block form is especially useful if you
|
|
96
|
+
# need the parameters that were passed to the mocked method, as they are passed in to
|
|
97
|
+
# the block.
|
|
98
|
+
#
|
|
99
|
+
# See the documentation for `RSpec::Mocks::ExampleMethods#and_return` for the first
|
|
100
|
+
# form, and the docs for `RSpec::Mocks::ExampleMethods#receive` for the block form.
|
|
101
|
+
#
|
|
102
|
+
# @param values [Object] the value or values to be returned by the mock
|
|
103
|
+
# @param block [Proc] alternatively, a block whose return value will be returned from
|
|
104
|
+
# the mock
|
|
105
|
+
# @yield [Object, Object ...] any parameter the mocked method was called with
|
|
106
|
+
# @yieldreturn [Object] the value the mock should return
|
|
107
|
+
# @return [void]
|
|
108
|
+
def return_value(*values, &block)
|
|
109
|
+
if block_given?
|
|
110
|
+
allow(target).to receive(method_name, &block)
|
|
111
|
+
else
|
|
112
|
+
values =
|
|
113
|
+
values.map(&value_transformer) if value_transformer
|
|
114
|
+
|
|
115
|
+
allow(target).to receive(method_name).and_return(*values)
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
|
|
119
|
+
# Configure multiple values your mock should pass by yielding multiple times
|
|
120
|
+
#
|
|
121
|
+
# The mock will yield once with a single argument for every value.
|
|
122
|
+
#
|
|
123
|
+
# See the documentation for `RSpec::Mocks::ExampleMethods#and_yield`.
|
|
124
|
+
#
|
|
125
|
+
# @param values [Object] the value or values to be yielded by the mock
|
|
126
|
+
# @return [void]
|
|
127
|
+
def yield_several(*values)
|
|
128
|
+
values =
|
|
129
|
+
values.map(&value_transformer) if value_transformer
|
|
130
|
+
|
|
131
|
+
allow(target).to receive(method_name).tap { |proxy|
|
|
132
|
+
values.each do |value|
|
|
133
|
+
proxy.and_yield(value)
|
|
134
|
+
end
|
|
135
|
+
}
|
|
136
|
+
end
|
|
137
|
+
|
|
138
|
+
# Configure one or more values your mock should yield once
|
|
139
|
+
#
|
|
140
|
+
# The mock will yield a single time with one or more arguments.
|
|
141
|
+
#
|
|
142
|
+
# See the documentation for `RSpec::Mocks::ExampleMethods#and_yield`.
|
|
143
|
+
#
|
|
144
|
+
# @param values [Object] the value or values to be yielded by the mock
|
|
145
|
+
# @return [void]
|
|
146
|
+
def yield_once(*values)
|
|
147
|
+
values =
|
|
148
|
+
values.map(&value_transformer) if value_transformer
|
|
149
|
+
|
|
150
|
+
allow(target).to receive(method_name).and_yield(*values)
|
|
151
|
+
end
|
|
152
|
+
|
|
153
|
+
# Configure a value your mock should respond with if the mock is called with a
|
|
154
|
+
# given parameter
|
|
155
|
+
#
|
|
156
|
+
# When providing a block, the block return value will be returned instead; the
|
|
157
|
+
# block form is especially useful if you need the parameters that were passed
|
|
158
|
+
# to the mocked method, as they are passed in to the block.
|
|
159
|
+
#
|
|
160
|
+
# See the documentation for `RSpec::Mocks::ExampleMethods#and_return` for the first
|
|
161
|
+
# form, and the docs for `RSpec::Mocks::ExampleMethods#receive` for the block form.
|
|
162
|
+
#
|
|
163
|
+
# @example
|
|
164
|
+
# ```ruby
|
|
165
|
+
# Mock::SomeClass.some_class_method do |config|
|
|
166
|
+
# config.for_arg "foo", return_value: true
|
|
167
|
+
# config.for_arg "bar", return_value: false
|
|
168
|
+
# # You also have all of RSPecs argument matchers at your disposal
|
|
169
|
+
# config.for_arg array_including(5), return_value: 10
|
|
170
|
+
# end
|
|
171
|
+
#
|
|
172
|
+
# SomeClass.some_class_method("foo")
|
|
173
|
+
# => true
|
|
174
|
+
# SomeClass.some_class_method("bar")
|
|
175
|
+
# => false
|
|
176
|
+
# SomeClass.some_class_method([1,5,6])
|
|
177
|
+
# => 10
|
|
178
|
+
# ```
|
|
179
|
+
#
|
|
180
|
+
# @param arg [Object] the argument to be passed to the mock for which this
|
|
181
|
+
# value should be returned
|
|
182
|
+
# @param return_value [Object] the value that should be returned by the mock
|
|
183
|
+
# @param yield_value [Object] the value that should be yielded by the mock
|
|
184
|
+
# @param fail_with [StandardError, nil] an exception to be raised when the mock is
|
|
185
|
+
# called
|
|
186
|
+
# @param block [Proc] alternatively, a block whose return value will be returned from
|
|
187
|
+
# the mock
|
|
188
|
+
# @yield [Object, Object ...] any parameter the mocked method was called with
|
|
189
|
+
# @yieldreturn [Object] the value the mock should return
|
|
190
|
+
# @return [void]
|
|
191
|
+
def for_arg(arg, return_value: nil, yield_value: nil, fail_with: false, &block)
|
|
192
|
+
if block_given?
|
|
193
|
+
allow(target).to receive(method_name, &block).with(arg)
|
|
194
|
+
return
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
if fail_with
|
|
198
|
+
allow(target).to receive(method_name).with(arg).and_raise(fail_with)
|
|
199
|
+
|
|
200
|
+
elsif return_value
|
|
201
|
+
return_value =
|
|
202
|
+
value_transformer.call(return_value) if value_transformer
|
|
203
|
+
|
|
204
|
+
allow(target).to receive(method_name).with(arg).and_return(return_value)
|
|
205
|
+
|
|
206
|
+
elsif yield_value
|
|
207
|
+
yield_value =
|
|
208
|
+
value_transformer.call(yield_value) if value_transformer
|
|
209
|
+
|
|
210
|
+
allow(target).to receive(method_name).with(arg).and_yield(yield_value)
|
|
211
|
+
end
|
|
212
|
+
end
|
|
213
|
+
|
|
214
|
+
# Raise an exception `exception_klass` if the mock is called
|
|
215
|
+
#
|
|
216
|
+
# @param exception_klass [Class]
|
|
217
|
+
# @return [void]
|
|
218
|
+
def fail_with(exception_klass)
|
|
219
|
+
allow(target).to receive(method_name).and_raise(fail_with)
|
|
220
|
+
end
|
|
221
|
+
end
|
|
222
|
+
end
|
|
223
|
+
end
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require "active_support"
|
|
4
|
+
require "active_support/core_ext/string/inflections"
|
|
5
|
+
|
|
6
|
+
module Mock
|
|
7
|
+
module Fs
|
|
8
|
+
module Dirs
|
|
9
|
+
class Directory
|
|
10
|
+
class << self
|
|
11
|
+
def default_path(value = nil)
|
|
12
|
+
if value
|
|
13
|
+
@default_path = value
|
|
14
|
+
else
|
|
15
|
+
@default_path
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
end
|
|
19
|
+
def self.install
|
|
20
|
+
new.tap(&:mock_default)
|
|
21
|
+
.tap(&:install_default)
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
def initialize
|
|
25
|
+
@target_klass =
|
|
26
|
+
target_klass || self.class.name.gsub(/^Mock::/, "").constantize
|
|
27
|
+
|
|
28
|
+
@instance_mock = instance_double(@target_klass)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def mock_default
|
|
32
|
+
allow(@instance_mock).to receive(:path).and_return(self.class.default_path)
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
def install_default
|
|
36
|
+
allow(@target_klass).to receive(:new).and_return(@instance_mock)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
end
|