class ExecutableMock
Constants
- Error
- TEMPLATE
- TEMPLATE_PATH
- VERSION
Attributes
file_path[R]
path_setup[R]
Public Class Methods
finalize_all()
click to toggle source
# File lib/executable_mock.rb, line 23 def finalize_all registry.each(&:finalize) end
generate(name, mappings, ruby_bin: RbConfig.ruby, directory: Dir.mktmpdir) { |instance| ... }
click to toggle source
# File lib/executable_mock.rb, line 15 def generate(name, mappings, ruby_bin: RbConfig.ruby, directory: Dir.mktmpdir) instance = new(name, mappings, ruby_bin: ruby_bin, directory: directory) yield(instance).tap do |result| instance.finalize(result) end end
new(name, mappings, ruby_bin: RbConfig.ruby, directory: Dir.mktmpdir)
click to toggle source
# File lib/executable_mock.rb, line 28 def initialize(name, mappings, ruby_bin: RbConfig.ruby, directory: Dir.mktmpdir) @mappings = mappings @ruby_bin = ruby_bin @name = name @file_path = File.join(directory, name) @path_setup = %(PATH="#{directory}:$PATH") call_error_log_file = Tempfile.new call_error_log_file.close @call_error_log_file_path = call_error_log_file.path write_executable register_self end
Public Instance Methods
check_call_error_log_file()
click to toggle source
# File lib/executable_mock.rb, line 55 def check_call_error_log_file return unless File.size?(@call_error_log_file_path) raise(Error, File.read(@call_error_log_file_path)) end
check_mismatched_argvs_calls(argvs_map)
click to toggle source
# File lib/executable_mock.rb, line 70 def check_mismatched_argvs_calls(argvs_map) mismatched_argvs_calls = @mappings.select do |argv, outputs| outputs.is_a?(Array) && outputs.size != argvs_map[argv] end raise(Error, <<~MESSAGE) if mismatched_argvs_calls.any? The following argvs were not called the correct number of times: #{mismatched_argvs_calls.inspect} MESSAGE end
check_uncalled_argvs(argvs_map)
click to toggle source
# File lib/executable_mock.rb, line 61 def check_uncalled_argvs(argvs_map) uncalled_argvs = argvs_map.select { |_, v| v.zero? } raise(Error, <<~MESSAGE) if uncalled_argvs.any? The following argvs were not called: #{uncalled_argvs.keys.join("\n")} MESSAGE end
counter_cache_path()
click to toggle source
# File lib/executable_mock.rb, line 81 def counter_cache_path @counter_cache_path ||= begin data = Marshal.dump(@mappings.transform_values { 0 }) Tempfile.new.tap do |file| file.write(data) file.close end.path end end
finalize(command_result = nil)
click to toggle source
# File lib/executable_mock.rb, line 42 def finalize(command_result = nil) called_argvs_map = Marshal.load(File.read(counter_cache_path)) # rubocop:disable Security/MarshalLoad check_call_error_log_file check_uncalled_argvs(called_argvs_map) check_mismatched_argvs_calls(called_argvs_map) rescue Error puts command_result if command_result raise ensure FileUtils.rm_f([@file_path, @call_error_log_file_path, counter_cache_path]) deregister_self end
write_executable()
click to toggle source
# File lib/executable_mock.rb, line 91 def write_executable bindings = { ruby_bin: @ruby_bin, mappings: @mappings, name: @name, call_error_log_file_path: @call_error_log_file_path, counter_cache_path: counter_cache_path } executable_contents = TEMPLATE.result_with_hash(bindings) File.open(@file_path, "w") do |file| file.write(executable_contents) end File.chmod(0o755, @file_path) end