Class: FakeHome::Home
- Inherits:
-
Object
- Object
- FakeHome::Home
- 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!
Constant Summary collapse
- DEFAULT_OPTIONS =
Default options for contructor
{prefix: "test", suffix: "home"}
Instance Attribute Summary collapse
-
#original_home ⇒ Object
readonly
Stores your original HOME after preparation.
-
#prefix ⇒ Object
readonly
Prefix are the first few characters in the generated temporary directory.
-
#suffix ⇒ Object
readonly
Suffix are the last few characters in the generated temporary directory.
Instance Method Summary collapse
-
#fake_home ⇒ Object
Gets your fake HOME path.
-
#initialize(*args) ⇒ Home
constructor
Creates a new Home.
-
#prepare ⇒ Object
Prepares your new HOME.
-
#prepared? ⇒ Boolean
Does your fake HOME exist?.
-
#restore ⇒ Object
Restores your original HOME.
-
#restored? ⇒ Boolean
Does your orginial HOME exist?.
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`).
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 = (args) = DEFAULT_OPTIONS.merge() @prefix, @suffix = [:prefix], [:suffix] end |
Instance Attribute Details
#original_home ⇒ Object (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 |
#prefix ⇒ Object (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 |
#suffix ⇒ Object (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_home ⇒ Object
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.
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 |
#prepare ⇒ Object
Prepares your new HOME. Old HOME will be saved and can be restored.
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?
74 75 76 |
# File 'lib/fake_home/fake_home.rb', line 74 def prepared? ENV["HOME"] == @fake_home end |
#restore ⇒ Object
Restores your original HOME.
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?
105 106 107 |
# File 'lib/fake_home/fake_home.rb', line 105 def restored? ENV["HOME"] == @original_home end |