class Serverspec::Type::Virtualenv

Public Instance Methods

pip_freeze() click to toggle source

Return a hash of all packages present in `pip freeze` output for the venv

Note that any editable packages (`-e something`) are returned as hash keys with an empty value

@example

describe virtualenv('/path/to/venv') do
  its(:pip_freeze) { should include('wsgiref' => '0.1.2') }
  its(:pip_freeze) { should include('requests') }
  its(:pip_freeze) { should include('pytest' => /^2\.6/) }
  its(:pip_freeze) { should include('-e git+git@github.com:jantman/someproject.git@1d8a380e3af9d081081d7ef685979200a7db4130#egg=someproject') }
end

@api public @return [Hash]

# File lib/serverspec_extended_types/virtualenv.rb, line 82
def pip_freeze
  @pip_freeze || get_pip_freeze
end
pip_version() click to toggle source

Return the version of pip installed in the virtualenv

@example

describe virtualenv('/path/to/venv') do
  its(:pip_version) { should match /^6\.0\.6$/ }
end

@api public @return [String]

# File lib/serverspec_extended_types/virtualenv.rb, line 50
def pip_version
  @pip_version || get_pip_version
end
python_version() click to toggle source

Return the version of python installed in the virtualenv

@example

describe virtualenv('/path/to/venv') do
  its(:python_version) { should match /^2\.7\.9$/ }
end

@api public @return [String]

# File lib/serverspec_extended_types/virtualenv.rb, line 63
def python_version
  @python_version || get_python_version
end
virtualenv?() click to toggle source

Test whether this appears to be a working venv

Tests performed:

  • venv_path/bin/pip executable by owner?

  • venv_path/bin/python executable by owner?

  • venv_path/bin/activate readable by owner?

  • 'export VIRTUAL_ENV' in venv_path/bin/activate?

@example

describe virtualenv('/path/to/venv') do
  it { should be_virtualenv }
end

@api public @return [Boolean]

# File lib/serverspec_extended_types/virtualenv.rb, line 30
def virtualenv?
  pip_path = ::File.join(@name, 'bin', 'pip')
  python_path = ::File.join(@name, 'bin', 'python')
  act_path = ::File.join(@name, 'bin', 'activate')
  cmd = "grep -q 'export VIRTUAL_ENV' #{act_path}"
  @runner.check_file_is_executable(pip_path, 'owner') and
    @runner.check_file_is_executable(python_path, 'owner') and
    @runner.check_file_is_readable(act_path, 'owner') and
    @runner.run_command(cmd).exit_status.to_i == 0
end

Private Instance Methods

get_pip_freeze() click to toggle source

Get a hash for the `pip freeze` output; set @pip_freeze

@api private @return [nil]

# File lib/serverspec_extended_types/virtualenv.rb, line 91
def get_pip_freeze()
  pip_path = ::File.join(@name, 'bin', 'pip')
  tmp = @runner.run_command("#{pip_path} freeze")
  @pip_freeze = Hash.new()
  if tmp.exit_status.to_i != 0
    return @pip_freeze
  end
  lines = tmp.stdout.split("\n")
  lines.each do |line|
    line.strip!
    if line =~ /^-e /
      @pip_freeze[line] = ''
      next
    end
    parts = line.split(/==/)
    @pip_freeze[parts[0]] = parts[1]
  end
  @pip_freeze
end
get_pip_version() click to toggle source

Get the pip version from the venv; set @pip_version

@api private @return [nil]

# File lib/serverspec_extended_types/virtualenv.rb, line 116
def get_pip_version()
  pip_path = ::File.join(@name, 'bin', 'pip')
  pip_path = ::File.join(@name, 'bin', 'pip')
  tmp = @runner.run_command("#{pip_path} --version")
  if ( tmp.stdout =~ /^pip (\d+\S+)/ )
    @pip_version = $1
  else
    @pip_version = ''
  end
end
get_python_version() click to toggle source

Get the python version from the venv; set @python_version

@api private @return [nil]

# File lib/serverspec_extended_types/virtualenv.rb, line 132
def get_python_version()
  python_path = ::File.join(@name, 'bin', 'python')
  tmp = @runner.run_command("#{python_path} --version")
  if ( tmp.stderr =~ /^[Pp]ython (\d+\S+)/ )
    @python_version = $1
  elsif ( tmp.stdout =~ /^[Pp]ython (\d+\S+)/ )
    @python_version = $1
  else
    @python_version = ''
  end
end