Sha256: 15a8b1b62a1d77d8f241f89fa654aa60b181ab771e3480826ca52a49968d0df4

Contents?: true

Size: 999 Bytes

Versions: 5

Compression:

Stored size: 999 Bytes

Contents

require 'mspec/guards/guard'

class Object
  def env
    env = ""
    if PlatformGuard.windows?
      env = Hash[*`cmd.exe /C set`.split("\n").map { |e| e.split("=", 2) }.flatten]
    else
      env = Hash[*`env`.split("\n").map { |e| e.split("=", 2) }.flatten]
    end
    env
  end

  def windows_env_echo(var)
    `cmd.exe /C ECHO %#{var}%`.strip
  end

  def username
    user = ""
    if PlatformGuard.windows?
      user = windows_env_echo('USERNAME')
    else
      user = `whoami`.strip
    end
    user
  end

  def home_directory
    return ENV['HOME'] unless PlatformGuard.windows?
    windows_env_echo('HOMEDRIVE') + windows_env_echo('HOMEPATH')
  end

  def dev_null
    if PlatformGuard.windows?
      "NUL"
    else
      "/dev/null"
    end
  end

  def hostname
    commands = ['hostname', 'uname -n']
    commands.each do |command|
      name = `#{command}`
      return name.strip if $?.success?
    end
    raise Exception, "hostname: unable to find a working command"
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mspec-1.6.0 lib/mspec/helpers/environment.rb
mspec-1.5.21 lib/mspec/helpers/environment.rb
mspec-1.5.20 lib/mspec/helpers/environment.rb
mspec-1.5.19 lib/mspec/helpers/environment.rb
mspec-1.5.18 lib/mspec/helpers/environment.rb