Class: LoweredExpectations

Inherits:
Object
  • Object
show all
Defined in:
lib/lowered/expectations.rb

Defined Under Namespace

Classes: CommandExecutionError, IncompatibleVersionError, MissingExecutableError, VersionPatternError

Constant Summary collapse

VERSION =
'1.0.3'.freeze

Class Method Summary collapse

Class Method Details

.expect(executable, version, vopt: '--version', vpattern: '(\d+\.\d+\.\d+)') ⇒ Object



21
22
23
24
25
26
# File 'lib/lowered/expectations.rb', line 21

def self.expect(executable, version, vopt: '--version', vpattern: '(\d+\.\d+\.\d+)')
  vstring = run! which(executable), vopt, quiet: true
  vmatch = /#{vpattern}/.match(vstring)
  raise(VersionPatternError.new("unable to match #{vpattern} in version output #{vstring} from #{executable}")) unless vmatch
  verify_version(vmatch[0], version) || raise(VersionPatternError.new("unable to match #{vpattern} in version output #{vstring} from #{executable}"))
end

.verify_version(version, pattern) ⇒ Object



39
40
41
# File 'lib/lowered/expectations.rb', line 39

def self.verify_version(version, pattern)
  Gem::Dependency.new('', pattern).match?('', version) || raise(IncompatibleVersionError.new("#{version} does not match version pattern #{pattern}"))
end

.which(cmd) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'lib/lowered/expectations.rb', line 28

def self.which(cmd)
  exts = ENV['PATHEXT'] ? ENV['PATHEXT'].split(';') : ['']
  ENV['PATH'].split(File::PATH_SEPARATOR).each do |path|
    exts.each do |ext|
      exe = File.join(path, "#{cmd}#{ext}")
      return exe if File.executable?(exe) && !File.directory?(exe)
    end
  end
  raise MissingExecutableError.new("#{cmd} not found in #{ENV['PATH']}")
end