class LoweredExpectations

Constants

VERSION

Public Class Methods

expect(executable, version, vopt: '--version', vpattern: '(\d+\.\d+\.\d+)') click to toggle source
# 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) click to toggle source
# File lib/lowered/expectations.rb, line 40
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) click to toggle source
# File lib/lowered/expectations.rb, line 29
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

Private Class Methods

exec!(*args) click to toggle source
# File lib/lowered/expectations.rb, line 70
def self.exec!(*args)
  cmd = Shellwords.shelljoin(args.flatten)
  logger.debug "Exec'ing: #{cmd}, in: #{Dir.pwd}"
  Kernel.exec cmd
end
pty(cmd, &block) click to toggle source
# File lib/lowered/expectations.rb, line 77
def self.pty(cmd, &block)
  PTY.spawn(cmd, &block)
  $?.exitstatus
end
run!(*args, quiet: false) click to toggle source
# File lib/lowered/expectations.rb, line 44
def self.run!(*args, quiet: false)
  cmd = Shellwords.shelljoin(args.flatten)
  status = 0
  stdout = ''
  stderr = ''
  if quiet
    # Run without streaming std* to any screen
    stdout, stderr, status = Open3.capture3(cmd)
    status = status.exitstatus
  else
    # Run but stream as well as capture stdout to the screen
    status = pty(cmd) do |r,w,pid|
      while !r.eof?
        c = r.getc
        stdout << c
        $stdout.write c.to_s
      end
      Process.wait(pid)
    end
  end
  raise CommandExecutionError.new(stderr) unless status.zero?

  stdout
end