Class: FakeHome::Home

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

Overview

It manipulates and restores your environment variable $HOME. I recommend to use it in your test suite.

More examples are in each method. Take a look!

Examples:

include FakeHome

Home.new("/tmp/fake_home").fake_home do |new_home|
  new_home == ENV["HOME"]
end

Constant Summary collapse

DEFAULT_OPTIONS =

Default options for contructor

{prefix: "test", suffix: "home"}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Home

Creates a new Home. If no explicit path is set, it will be generated a temporary one in your ‘/tmp` (see `@prepare`).

Examples:

Home.new("/tmp/fake_home")
Home.new(prefix: "new_prefix").prefix #=> "new_prefix"
Home.new(suffix: "new_suffix").suffix #=> "new_suffix"
Home.new("/tmp/fake_home", suffix: "new_suffix")


41
42
43
44
45
46
# File 'lib/fake_home/fake_home.rb', line 41

def initialize(*args)
  @fake_home = args.first if args.first.is_a? String
  options = extract_init_options(args)
  options = DEFAULT_OPTIONS.merge(options)
  @prefix, @suffix = options[:prefix], options[:suffix]
end

Instance Attribute Details

#original_homeObject (readonly)

Stores your original HOME after preparation.



29
30
31
# File 'lib/fake_home/fake_home.rb', line 29

def original_home
  @original_home
end

#prefixObject (readonly)

Prefix are the first few characters in the generated temporary directory.



23
24
25
# File 'lib/fake_home/fake_home.rb', line 23

def prefix
  @prefix
end

#suffixObject (readonly)

Suffix are the last few characters in the generated temporary directory.



26
27
28
# File 'lib/fake_home/fake_home.rb', line 26

def suffix
  @suffix
end

Instance Method Details

#fake_homeObject

Gets your fake HOME path. If a block is set, you can work in it with your fake home. Everything outside will be your original home.

Examples:

home = Home.new("/tmp/fake_home")
ENV["HOME"] #=> "/home/username"
home.fake_home do |home|
  ENV["HOME"] #=> "/tmp/fake_home"
end
ENV["HOME"] #=> "/home/username"

Returns:

  • path of your fake home



121
122
123
124
125
126
127
128
# File 'lib/fake_home/fake_home.rb', line 121

def fake_home
  if block_given?
    prepare
    yield @fake_home
    restore
  end
  @fake_home
end

#prepareObject

Prepares your new HOME. Old HOME will be saved and can be restored.

Examples:

Home.new("/tmp/fake_home").prepare #=> "/tmp/fake_home"
Home.new.prepare #=> "/tmp/test_20130311-6400-1ku43dk_home"
home = Home.new("/tmp/fake_home")
ENV["HOME"] #=> "/home/username"
home.prepare
ENV["HOME"] #=> "/tmp/fake_home"

Returns:

  • new home path



61
62
63
64
65
# File 'lib/fake_home/fake_home.rb', line 61

def prepare
  @original_home = ENV["HOME"]
  @fake_home = mkdir
  ENV["HOME"] = @fake_home
end

#prepared?Boolean

Does your fake HOME exist?

Examples:

home = Home.new
home.prepared? #=> false
home.prepare
home.prepared? #=> true

Returns:

  • (Boolean)


74
75
76
# File 'lib/fake_home/fake_home.rb', line 74

def prepared?
  ENV["HOME"] == @fake_home
end

#restoreObject

Restores your original HOME.

Examples:

home = Home.new("/tmp/fake_home")
home.prepare
ENV["HOME"] #=> "/tmp/fake_home"
home.restore
ENV["HOME"] #=> "/home/username"

Returns:

  • original home path

Raises:

  • PreparationError if not prepared



90
91
92
93
94
95
# File 'lib/fake_home/fake_home.rb', line 90

def restore
  raise PreparationError, "You have to prepare first." unless prepared?

  FileUtils.rm_rf @fake_home
  ENV["HOME"] = @original_home
end

#restored?Boolean

Does your orginial HOME exist?

Examples:

home = Home.new
home.prepare
home.restored? #=> false
home.restore
home.restored? #=> true

Returns:

  • (Boolean)


105
106
107
# File 'lib/fake_home/fake_home.rb', line 105

def restored?
  ENV["HOME"] == @original_home
end