lib/cliver/detector.rb in cliver-0.1.5 vs lib/cliver/detector.rb in cliver-0.2.0

- old
+ new

@@ -3,36 +3,46 @@ module Cliver # Default implementation of the detector needed by Cliver::Assertion, # which will take anything that #respond_to?(:to_proc) class Detector < Struct.new(:command_arg, :version_pattern) + # @param detector_argument [#call, Object] + # If detector_argument responds to #call, return it; otherwise attempt + # to create an instance of self. + def self.generate(detector_argument) + return detector_argument if detector_argument.respond_to?(:call) + new(*Array(detector_argument)) + end # Default pattern to use when searching {#version_command} output - DEFAULT_VERSION_PATTERN = /version [0-9][.0-9a-z]+/i.freeze + DEFAULT_VERSION_PATTERN = /(version ?)?[0-9][.0-9a-z]+/i.freeze # Default command argument to use against the executable to get # version output DEFAULT_COMMAND_ARG = '--version'.freeze # Forgiving input, allows either argument if only one supplied. # - # @overload initialize(command_arg) + # @overload initialize(*command_args) + # @param command_args [Array<String>] # @overload initialize(version_pattern) - # @overload initialize(command_arg, version_pattern) - # @param command_arg [String] - # @param version_pattern [Regexp] + # @param version_pattern [Regexp] + # @overload initialize(*command_args, version_pattern) + # @param command_args [Array<String>] + # @param version_pattern [Regexp] def initialize(*args) - command_arg = args.shift if args.first.kind_of?(String) - version_pattern = args.shift - super(command_arg, version_pattern) + version_pattern = args.pop if args.last.kind_of?(Regexp) + command_args = args unless args.empty? + + super(command_args, version_pattern) end # @param executable_path [String] - the path to the executable to test # @return [String] - should be contain {Gem::Version}-parsable # version number. def detect_version(executable_path) - output = `#{version_command(executable_path).shelljoin} 2>&1` + output = shell_out_and_capture version_command(executable_path).shelljoin output[version_pattern] end # This is the interface that any detector must have. # If not overridden, returns a proc that wraps #detect_version @@ -51,17 +61,25 @@ super || DEFAULT_VERSION_PATTERN end # The argument to pass to the executable to get current version # Defaults to {DEFAULT_COMMAND_ARG} - # @return [String] + # @return [String, Array<String>] def command_arg super || DEFAULT_COMMAND_ARG end # @param executable_path [String] the executable to test # @return [Array<String>] def version_command(executable_path) - [executable_path, command_arg] + [executable_path, *Array(command_arg)] + end + + private + + # @api private + # A boundary that is useful for testing. + def shell_out_and_capture(command) + `#{command} 2>&1` end end end