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.
Files changed (42) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +3 -0
  3. data/LICENSE +289 -0
  4. data/README.md +128 -0
  5. data/lib/fs/atomic_write.rb +121 -0
  6. data/lib/fs/concerns/argument_validation.rb +140 -0
  7. data/lib/fs/concerns/file_dsl_methods.rb +471 -0
  8. data/lib/fs/concerns/required_arguments.rb +141 -0
  9. data/lib/fs/directory/accessibility_check.rb +89 -0
  10. data/lib/fs/directory.rb +497 -0
  11. data/lib/fs/errors.rb +491 -0
  12. data/lib/fs/file.rb +1524 -0
  13. data/lib/fs/file_variant_builder.rb +269 -0
  14. data/lib/fs/file_variant_proxy.rb +253 -0
  15. data/lib/fs/files/csv.rb +26 -0
  16. data/lib/fs/files/gif.rb +10 -0
  17. data/lib/fs/files/image.rb +390 -0
  18. data/lib/fs/files/jpg.rb +36 -0
  19. data/lib/fs/files/json.rb +24 -0
  20. data/lib/fs/files/pdf.rb +231 -0
  21. data/lib/fs/files/pdf_collection.rb +64 -0
  22. data/lib/fs/files/png.rb +10 -0
  23. data/lib/fs/files/text.rb +13 -0
  24. data/lib/fs/files/tiff.rb +24 -0
  25. data/lib/fs/files/zip.rb +123 -0
  26. data/lib/fs/lockfile_sweeper.rb +70 -0
  27. data/lib/fs/tempfile_sweeper.rb +79 -0
  28. data/lib/fs/testing/mock/fs/configurator.rb +223 -0
  29. data/lib/fs/testing/mock/fs/dirs/directory.rb +41 -0
  30. data/lib/fs/testing/mock/fs/dsl.rb +81 -0
  31. data/lib/fs/testing/mock/fs/file.rb +60 -0
  32. data/lib/fs/testing/mock/fs/file_klass_mock.rb +350 -0
  33. data/lib/fs/testing/mock/fs/files/jpg.rb +14 -0
  34. data/lib/fs/testing/mock/fs/files/pdf.rb +19 -0
  35. data/lib/fs/testing/mock/fs/files/png.rb +12 -0
  36. data/lib/fs/testing/mock/fs/files/tiff.rb +12 -0
  37. data/lib/fs/testing/mock/fs/files/zip.rb +23 -0
  38. data/lib/fs/testing.rb +17 -0
  39. data/lib/fs/version.rb +3 -0
  40. data/lib/fs.rb +26 -0
  41. data/lib/gubbins/fs.rb +4 -0
  42. metadata +124 -0
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Fs
5
+ # The DSL macro methods available inside `Mock::Fs::FileKlassMock`
6
+ # subclasses
7
+ module Dsl
8
+ # DSL macro method for quickly mocking an `Fs::File` instance method
9
+ #
10
+ # @example
11
+ # We have a `Fs::Files::JPG` class we wish to make mockable. The class has
12
+ # two instance methods inherited from `Fs::Files::Image`, `#identify` and `#ocr`.
13
+ #
14
+ # ```ruby
15
+ #
16
+ # module Mock
17
+ # module Fs
18
+ # module Files
19
+ # class JPG < Mock::Fs::FileKlassMock
20
+ # mock_method :identify, default_value: "image/jpeg"
21
+ # mock_method :ocr, default_value: "Lorem ipsum dolor"
22
+ # end
23
+ # end
24
+ # end
25
+ # end
26
+ # ```
27
+ #
28
+ # `mock_method` is a shorthand for defining an instance method that calls
29
+ # `#install_mock_on`, so the above is equivalent to
30
+ #
31
+ # ```ruby
32
+ # module Mock
33
+ # module Fs
34
+ # module Files
35
+ # class JPG < Mock::Fs::FileKlassMock
36
+ # def identify(**kargs, &block)
37
+ # install_mock_on(:identify, configure_with: &block, **kargs)
38
+ # end
39
+ #
40
+ # def ocr(**kargs, &block)
41
+ # install_mock_on(:ocr, configure_with: &block, **kargs)
42
+ # end
43
+ # end
44
+ # end
45
+ # end
46
+ # end
47
+ # ```
48
+ #
49
+ # In either way, all instance methods are executed when you call `.install`, ie.
50
+ # `Mock::Fs::Files::JPG.install` - except for those you configure manually in a
51
+ # block given to `.install`.
52
+ #
53
+ # @param method_name [Symbol] the name of the `Shell::Command` class method to mock
54
+ # @param default_value [Object] the value this mock will return if no other value
55
+ # is specified when the mock is installed.
56
+ # @param default_yield [Object] the value this mock will yield if no other value
57
+ # is specified when the mock is installed.
58
+ # @return [void]
59
+ def mock_method(method_name, default_value: nil, default_yield: nil)
60
+ define_method(method_name) do |&block|
61
+ install_mock_on(
62
+ method_name,
63
+ configure_with: block,
64
+ default_value: default_value,
65
+ default_yield: default_yield
66
+ )
67
+ end
68
+ end
69
+
70
+ def mock_variant(variant_name, with:, successful: true, existing_file: true, &block)
71
+ @variants ||= {}
72
+ @variants[variant_name] = {
73
+ klass: with,
74
+ configure_block: block,
75
+ successful: successful,
76
+ existing_file: existing_file
77
+ }
78
+ end
79
+ end
80
+ end
81
+ end
@@ -0,0 +1,60 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/enumerable"
5
+
6
+ # Rails.root
7
+ # .join("app/lib/fs/files")
8
+ # .glob("**/*.rb")
9
+ # .each { |file| next if file.directory? ; require_dependency file }
10
+
11
+ module Mock
12
+ module Fs
13
+ class File
14
+ include RSpec::Mocks::ExampleMethods
15
+
16
+ attr_reader :klasses
17
+
18
+ def self.on(*klasses)
19
+ new(*klasses)
20
+ end
21
+
22
+ def self.on_all
23
+ on(*klasses_in(Files, descendant_of: Mock::Fs::FileKlassMock))
24
+ end
25
+
26
+ def self.klasses_in(namespace, descendant_of: nil)
27
+ result = []
28
+
29
+ namespace.constants
30
+ .map do |constant_name|
31
+
32
+ const = namespace.const_get(constant_name)
33
+
34
+ next if descendant_of && const.ancestors.exclude?(descendant_of)
35
+
36
+ case const
37
+ when Class
38
+ result << const
39
+ when Module
40
+ result.concat klasses_in(const)
41
+ end
42
+ end
43
+
44
+ result
45
+ end
46
+
47
+ def initialize(*klasses)
48
+ @klasses = klasses
49
+
50
+ yield self if block_given?
51
+ end
52
+
53
+ def mock_success
54
+ klasses.each do |klass|
55
+ klass.new.mock
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,350 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "active_support"
4
+ require "active_support/core_ext/string/inflections"
5
+ require_relative "dsl"
6
+
7
+ module Mock
8
+ module Fs
9
+ class FileKlassMock
10
+ extend Mock::Fs::Dsl
11
+
12
+ class InstanceConfigurator
13
+ attr_reader :mock_manager
14
+ attr_reader :mocked_methods
15
+ attr_reader :variants
16
+
17
+ def initialize(mock_manager)
18
+ @mock_manager = mock_manager
19
+ @mocked_methods = []
20
+ @variants = Hash.new({})
21
+ end
22
+
23
+ def on(method_name, &block)
24
+ mocked_methods << method_name
25
+
26
+ configurator =
27
+ Mock::Fs::Configurator.new(target: mock_manager.instance_mock, method_name: method_name)
28
+
29
+ if block.arity == 0
30
+ configurator.instance_eval(&block)
31
+ else
32
+ block.call(configurator)
33
+ end
34
+ end
35
+
36
+ def variant(name, with: nil, successful: nil, existing_file: nil, &block)
37
+ @variants[name] = {
38
+ klass: with,
39
+ configure_block: block,
40
+ successful: successful,
41
+ existing_file: existing_file
42
+ }
43
+
44
+ # We want parameters to be overrideable selectively,
45
+ # so we're removing everything that should remain default
46
+ @variants[name].delete_if { |_key, value| value.nil? }
47
+ end
48
+ end
49
+
50
+ include RSpec::Mocks::ExampleMethods
51
+
52
+ attr_reader :target_klass
53
+ attr_reader :instance_mock
54
+
55
+ KLASS_METHODS_RETURNING_INSTANCES = [
56
+ :new, :load, :create, :load_or_create, :copy_from, :move_from
57
+ ].freeze
58
+
59
+ def self.variants
60
+ @variants
61
+ end
62
+
63
+ def self.inherited(subklass)
64
+ subklass.instance_variable_set("@variants", variants.dup)
65
+ end
66
+
67
+ def self.install(successful: true, existing_file: true, &block)
68
+ mock_manager = new
69
+
70
+ configurator =
71
+ InstanceConfigurator.new(mock_manager)
72
+
73
+ if block
74
+ if block.arity == 0
75
+ configurator.instance_eval(&block)
76
+ else
77
+ block.call(configurator)
78
+ end
79
+ end
80
+
81
+ mock_manager.mock_klass_methods(successful: successful, existing_file: existing_file)
82
+ .mock_instance_methods_with_defaults(except: configurator.mocked_methods)
83
+
84
+ mock_manager.install_variants(configurator: configurator)
85
+
86
+ mock_manager
87
+ end
88
+
89
+ def self.instance_mock(&block)
90
+ mock_manager = new
91
+
92
+ configurator =
93
+ InstanceConfigurator.new(mock_manager)
94
+
95
+ if block
96
+ if block.arity == 0
97
+ configurator.instance_eval(&block)
98
+ else
99
+ block.call(configurator)
100
+ end
101
+ end
102
+
103
+ mock_manager.mock_instance_methods_with_defaults(except: configurator.mocked_methods)
104
+
105
+ mock_manager.instance_mock
106
+ end
107
+
108
+ def self.multiple_instance_mocks(&block)
109
+ raise ArgumentError, "block expected" unless block
110
+ raise ArgumentError, "block must take at least one argument" if block.arity == 0
111
+
112
+ configurators =
113
+ block.arity.times.map do
114
+ mock_manager = new
115
+ InstanceConfigurator.new(mock_manager)
116
+ end
117
+
118
+ block.call(*configurators)
119
+
120
+ configurators.map do |configurator|
121
+ configurator.mock_manager
122
+ .mock_instance_methods_with_defaults(except: configurator.mocked_methods)
123
+
124
+ configurator.mock_manager.instance_mock
125
+ end
126
+ end
127
+
128
+ def initialize(target_klass = nil)
129
+ @target_klass =
130
+ target_klass || self.class.name.gsub(/^Mock::/, "").constantize
131
+
132
+ @instance_mock = instance_double(@target_klass)
133
+ end
134
+
135
+ def copy_to
136
+ install_mock_on(
137
+ :copy_to,
138
+ configure_with: nil,
139
+ default_value: instance_double(@target_klass)
140
+ )
141
+ end
142
+
143
+ def move_to
144
+ install_mock_on(
145
+ :copy_to,
146
+ configure_with: nil,
147
+ default_value: instance_double(@target_klass)
148
+ )
149
+ end
150
+
151
+ def exist?
152
+ install_mock_on(
153
+ :exist?,
154
+ configure_with: nil,
155
+ default_value: true
156
+ )
157
+ end
158
+
159
+ def path
160
+ install_mock_on(
161
+ :path,
162
+ configure_with: nil,
163
+ default_value: ""
164
+ )
165
+ end
166
+
167
+ def mime_type
168
+ install_mock_on(
169
+ :mime_type,
170
+ configure_with: nil,
171
+ default_value: target_klass.mime_type.first
172
+ )
173
+ end
174
+
175
+ def mock_klass_methods(successful: true, existing_file: true)
176
+ allow(target_klass).to receive(:exists?).and_return(existing_file)
177
+ allow(target_klass).to receive(:accepts?).and_call_original
178
+ allow(target_klass).to receive(:extensions).and_call_original
179
+
180
+ if successful
181
+ KLASS_METHODS_RETURNING_INSTANCES.each do |klass_method|
182
+ allow(target_klass).to receive(klass_method).and_return(instance_mock)
183
+ end
184
+ else
185
+ KLASS_METHODS_RETURNING_INSTANCES.each do |klass_method|
186
+ allow(target_klass).to receive(klass_method).and_raise(::Error::Fs::ReadWrite)
187
+ end
188
+ end
189
+
190
+ self
191
+ end
192
+
193
+ def mock_instance_methods_with_defaults(except: [])
194
+ (
195
+ self.class
196
+ .ancestors
197
+ .take_while { |klass| klass <= FileKlassMock }
198
+ .map { |klass| klass.instance_methods(false) }
199
+ .flatten - [:mock_klass_methods, :mock_instance_methods_with_defaults, :install_variants, *except]
200
+ ).each { |method_name| send(method_name) }
201
+
202
+ self
203
+ end
204
+
205
+ # Mocks the `variant(:name)` method on the instance mock
206
+ #
207
+ # @param configurator [InstanceConfigurator]
208
+ # @return [void]
209
+ def install_variants(configurator:)
210
+ return unless self.class.variants && self.class.variants.any?
211
+
212
+ self.class.variants.each do |variant_name, config|
213
+ config =
214
+ config.merge(configurator.variants[variant_name])
215
+
216
+ variant_klass = config[:klass]
217
+ configure_block = config[:configure_block]
218
+ successful = config[:successful]
219
+ existing_file = config[:existing_file]
220
+
221
+ mock_proxy =
222
+ instance_double(::Fs::FileVariantProxy)
223
+
224
+ if configure_block && configure_block.arity > 1
225
+ variant_instances =
226
+ variant_klass.multiple_instance_mocks(&configure_block)
227
+
228
+ response = -> {
229
+ raise ::Fs::ReadWriteError unless successful
230
+ variant_instances.shift
231
+ }
232
+
233
+ if successful
234
+ allow(mock_proxy).to receive(:create_temp) { |&block| block.call(variant_instances.shift) }
235
+ else
236
+ allow(mock_proxy).to receive(:create_temp).and_raise(::Fs::ReadWriteError)
237
+ end
238
+
239
+ else
240
+ variant_instance =
241
+ variant_klass.instance_mock(&configure_block)
242
+
243
+ response = -> {
244
+ raise ::Fs::ReadWriteError unless successful
245
+ variant_instance
246
+ }
247
+
248
+ if successful
249
+ allow(mock_proxy).to receive(:create_temp).and_yield(variant_instance)
250
+ else
251
+ allow(mock_proxy).to receive(:create_temp).and_raise(::Fs::ReadWriteError)
252
+ end
253
+ end
254
+
255
+ allow(mock_proxy).to receive(:exists?) { existing_file }
256
+ allow(mock_proxy).to receive(:load, &response)
257
+ allow(mock_proxy).to receive(:create, &response)
258
+ allow(mock_proxy).to receive(:load_or_create, &response)
259
+
260
+ allow(instance_mock).to receive(:variant).with(variant_name).and_return(mock_proxy)
261
+ end
262
+ end
263
+
264
+ private
265
+
266
+ # A private method for setting up a standard instance method mock
267
+ #
268
+ # @example
269
+ # ```ruby
270
+ # module Mock
271
+ # module Fs
272
+ # module Files
273
+ # class Jpg < Mock::Fs::FileKlassMock
274
+ # def identify(&block)
275
+ # install_mock_on(:identify, configure_with: block, default_value: "image/jpeg")
276
+ # end
277
+ # end
278
+ # end
279
+ # end
280
+ # end
281
+ # ```
282
+ #
283
+ # @param method_name [Symbol] the name of the `Shell::Command` class method to mock
284
+ # @param configure_with [Proc] a block that is passed along from the outer method;
285
+ # this block will be either passed a `Mock::Fs::Configurator` instance as an argument,
286
+ # or executed in its scope if it does not take a parameter.
287
+ # @param default_value [Object] the value this mock will return if no other value
288
+ # is specified when the mock is installed. Procs are supported.
289
+ # @param default_yield [Object] the value this mock will yield if no other value
290
+ # is specified when the mock is installed. Procs are supported.
291
+ # @return [void]
292
+ def install_mock_on(method_name, configure_with:, default_value: nil, default_yield: nil)
293
+ configurator =
294
+ Mock::Fs::Configurator.new(target: instance_mock, method_name: method_name)
295
+
296
+ if block_given?
297
+ yield configurator
298
+ end
299
+
300
+ if configure_with
301
+ extract_value_from_parameter(configure_with, scope: configurator)
302
+ else
303
+ if default_value
304
+ value =
305
+ extract_value_from_parameter(default_value, scope: configurator)
306
+
307
+ configurator.return_value(value)
308
+ end
309
+
310
+ if default_yield
311
+ value =
312
+ extract_value_from_parameter(default_yield, scope: configurator)
313
+
314
+ configurator.yield_once(value)
315
+ end
316
+ end
317
+ end
318
+
319
+ # Extracts a value from a parameter that might be a callable
320
+ #
321
+ # If it is a callable, it can choose through its arity whether
322
+ # it wants to be given `scope` as a block argument, or executed
323
+ # in the binding of `scope`.
324
+ #
325
+ # @example
326
+ # ```ruby
327
+ # # Callable receives `scope` as a block argument
328
+ # some_method(some_parameter: ->(scope) { })
329
+ #
330
+ # # Callable accepts no block argument and is thus executed
331
+ # in the binding of `scope`
332
+ # some_method(some_parameter: -> { } )
333
+ #
334
+ # @param parameter [Object]
335
+ # @param scope [Object]
336
+ # @return [Object]
337
+ def extract_value_from_parameter(parameter, scope: self)
338
+ if parameter.respond_to?(:call)
339
+ if parameter.arity == 0
340
+ scope.instance_exec(&parameter)
341
+ else
342
+ parameter.call(scope)
343
+ end
344
+ else
345
+ parameter
346
+ end
347
+ end
348
+ end
349
+ end
350
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../file_klass_mock.rb"
4
+
5
+ module Mock
6
+ module Fs
7
+ module Files
8
+ class JPG < Mock::Fs::FileKlassMock
9
+ mock_method :identify, default_value: "image/jpeg"
10
+ mock_method :ocr, default_value: "Lorem ipsum dolor"
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../file_klass_mock.rb"
4
+
5
+ module Mock
6
+ module Fs
7
+ module Files
8
+ class PDF < Mock::Fs::FileKlassMock
9
+ mock_method :text, default_value: "Lorem ipsum dolor"
10
+ mock_method :text_layer?, default_value: true
11
+ mock_method :reader,
12
+ default_value: -> { instance_double("PDF::Reader") }
13
+
14
+ mock_variant :subset,
15
+ with: self
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Fs
5
+ module Files
6
+ class PNG < Mock::Fs::FileKlassMock
7
+ mock_method :identify, default_value: "image/png"
8
+ mock_method :ocr, default_value: "Lorem ipsum dolor"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Mock
4
+ module Fs
5
+ module Files
6
+ class TIFF < Mock::Fs::FileKlassMock
7
+ mock_method :identify, default_value: "image/tiff"
8
+ mock_method :ocr, default_value: "Lorem ipsum dolor"
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "shell"
4
+ require_relative "../file_klass_mock.rb"
5
+
6
+ module Mock
7
+ module Fs
8
+ module Files
9
+ class Zip < Mock::Fs::FileKlassMock
10
+ mock_method :unzip_to_temp,
11
+ default_yield: -> { Dir.tmpdir }
12
+ mock_method :each_unzipped_temp_file,
13
+ default_yield: -> { Mock::Fs::Files::Jpg.instance_mock }
14
+ mock_method :unzip_to,
15
+ default_value: -> { Mock::Shell::Command::Result.build }
16
+ mock_method :list,
17
+ default_value: -> { [] }
18
+ mock_method :uncompressed_size_below?,
19
+ default_value: true
20
+ end
21
+ end
22
+ end
23
+ end
data/lib/fs/testing.rb ADDED
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Core mock infrastructure
4
+ require_relative "testing/mock/fs/configurator"
5
+ require_relative "testing/mock/fs/dsl"
6
+ require_relative "testing/mock/fs/file_klass_mock"
7
+ require_relative "testing/mock/fs/file"
8
+
9
+ # Directory mocks
10
+ require_relative "testing/mock/fs/dirs/directory"
11
+
12
+ # File type mocks
13
+ require_relative "testing/mock/fs/files/jpg"
14
+ require_relative "testing/mock/fs/files/pdf"
15
+ require_relative "testing/mock/fs/files/png"
16
+ require_relative "testing/mock/fs/files/tiff"
17
+ require_relative "testing/mock/fs/files/zip"
data/lib/fs/version.rb ADDED
@@ -0,0 +1,3 @@
1
+ module Fs
2
+ VERSION = "0.1.0"
3
+ end
data/lib/fs.rb ADDED
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "fs/version"
4
+ require_relative "fs/errors"
5
+
6
+ require_relative "fs/directory"
7
+ require_relative "fs/file"
8
+ require_relative "fs/atomic_write"
9
+ require_relative "fs/lockfile_sweeper"
10
+ require_relative "fs/tempfile_sweeper"
11
+ require_relative "fs/files/csv"
12
+
13
+ require_relative "fs/files/image"
14
+ require_relative "fs/files/gif"
15
+ require_relative "fs/files/jpg"
16
+ require_relative "fs/files/png"
17
+ require_relative "fs/files/tiff"
18
+
19
+ require_relative "fs/files/json"
20
+ require_relative "fs/files/text"
21
+ require_relative "fs/files/pdf"
22
+ require_relative "fs/files/pdf_collection"
23
+ require_relative "fs/files/zip"
24
+
25
+ module Fs
26
+ end
data/lib/gubbins/fs.rb ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ # What Bundler requires for `gem "gubbins-fs"`
4
+ require_relative "../fs"