fake_home 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a003250068366fcfa4f37a0bf370e8e0c2e6516
4
+ data.tar.gz: 68b801ad71065df8429f6201e0cb770dd78e049f
5
+ SHA512:
6
+ metadata.gz: 2b30c0320f890e955a4c110e7ddcb42ed93ea3ce99a4b41c428467145b0cdec2e11c9231fdd72f0cfab4204a1e69326f9ac27a70ec9cf7e6d56fe596eee745df
7
+ data.tar.gz: 7ae37d20668b22002b574a3cedfa6676c400db98c903a8564c2981102ba8a9d2afcac6985ebdf0640071140a63eb14a570973bd9f8bbd7bf4ddba7f45b2b8866
data/.gitignore ADDED
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in fake_home.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 DSIW
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # FakeHome
2
+
3
+ It manipulates and restores your environment variable $HOME. I recommend to use it in your test suite.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'fake_home'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install fake_home
18
+
19
+ ## Usage
20
+
21
+ ``` ruby
22
+ include FakeHome
23
+
24
+ Home.new("/tmp/fake_home").fake_home do |new_home|
25
+ new_home == ENV["HOME"]
26
+ end
27
+ ```
28
+
29
+ ## Contributing
30
+
31
+ 1. Fork it
32
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
33
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
34
+ 4. Push to the branch (`git push origin my-new-feature`)
35
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ require "bundler/gem_tasks"
2
+
3
+ require 'rake/clean'
4
+ require 'rake/version_task'
5
+
6
+ require 'rspec/core/rake_task'
7
+
8
+ RSpec::Core::RakeTask.new do |t|
9
+ # Put spec opts in a file named .rspec in root
10
+ end
11
+
12
+ Rake::VersionTask.new
13
+
14
+ task :doc => [ 'doc:clean', 'doc:api' ]
15
+
16
+ CLOBBER << "doc"
17
+ CLEAN << "doc"
18
+ namespace :doc do
19
+ require 'yard'
20
+ YARD::Rake::YardocTask.new(:api) do |t|
21
+ t.files = ['lib/**/*.rb']
22
+ t.options = [
23
+ '--output-dir', 'doc/api',
24
+ '--markup', 'markdown'
25
+ ]
26
+ end
27
+
28
+ desc 'Remove YARD Documentation'
29
+ task :clean do
30
+ system("rm -rf #{File.dirname(__FILE__)}/doc/api")
31
+ end
32
+ end
33
+
34
+ task :default => [:spec]
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/fake_home.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'fake_home/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "fake_home"
8
+ spec.version = FakeHome::VERSION
9
+ spec.authors = ["DSIW"]
10
+ spec.email = ["dsiw@dsiw-it.de"]
11
+ spec.description = %q{Manipulates your HOME environment variable.}
12
+ spec.summary = %q{Manipulates and restores your HOME environment variable for test suites.}
13
+ spec.homepage = "https://github.com/DSIW/fake_home"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency('version')
24
+ spec.add_development_dependency('pry')
25
+ spec.add_development_dependency('rspec')
26
+ spec.add_development_dependency('yard')
27
+ end
data/lib/fake_home.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "fake_home/version"
2
+ require "fake_home/fake_home"
@@ -0,0 +1,149 @@
1
+ module FakeHome
2
+ require 'tempfile'
3
+ require 'tmpdir'
4
+
5
+ # Will be thrown if your HOME isn't prepared for restoring.
6
+ class PreparationError < StandardError; end
7
+
8
+ # It manipulates and restores your environment variable $HOME. I recommend to use it in your test suite.
9
+ #
10
+ # More examples are in each method. Take a look!
11
+ #
12
+ # @example
13
+ # include FakeHome
14
+ #
15
+ # Home.new("/tmp/fake_home").fake_home do |new_home|
16
+ # new_home == ENV["HOME"]
17
+ # end
18
+ class Home
19
+ # Default options for contructor
20
+ DEFAULT_OPTIONS = {prefix: "test", suffix: "home"}
21
+
22
+ # Prefix are the first few characters in the generated temporary directory.
23
+ attr_reader :prefix
24
+
25
+ # Suffix are the last few characters in the generated temporary directory.
26
+ attr_reader :suffix
27
+
28
+ # Stores your original HOME after preparation.
29
+ attr_reader :original_home
30
+
31
+ # Creates a new Home. If no explicit path is set, it will be generated a temporary one in your `/tmp` (see `@prepare`).
32
+ #
33
+ # @example
34
+ # Home.new("/tmp/fake_home")
35
+ # @example
36
+ # Home.new(prefix: "new_prefix").prefix #=> "new_prefix"
37
+ # @example
38
+ # Home.new(suffix: "new_suffix").suffix #=> "new_suffix"
39
+ # @example
40
+ # Home.new("/tmp/fake_home", suffix: "new_suffix")
41
+ def initialize(*args)
42
+ @fake_home = args.first if args.first.is_a? String
43
+ options = extract_init_options(args)
44
+ options = DEFAULT_OPTIONS.merge(options)
45
+ @prefix, @suffix = options[:prefix], options[:suffix]
46
+ end
47
+
48
+ # Prepares your new HOME. Old HOME will be saved and can be restored.
49
+ #
50
+ # @example
51
+ # Home.new("/tmp/fake_home").prepare #=> "/tmp/fake_home"
52
+ # Home.new.prepare #=> "/tmp/test_20130311-6400-1ku43dk_home"
53
+ #
54
+ # @example
55
+ # home = Home.new("/tmp/fake_home")
56
+ # ENV["HOME"] #=> "/home/username"
57
+ # home.prepare
58
+ # ENV["HOME"] #=> "/tmp/fake_home"
59
+ #
60
+ # @return new home path
61
+ def prepare
62
+ @original_home = ENV["HOME"]
63
+ @fake_home = mkdir
64
+ ENV["HOME"] = @fake_home
65
+ end
66
+
67
+ # Does your fake HOME exist?
68
+ #
69
+ # @example
70
+ # home = Home.new
71
+ # home.prepared? #=> false
72
+ # home.prepare
73
+ # home.prepared? #=> true
74
+ def prepared?
75
+ ENV["HOME"] == @fake_home
76
+ end
77
+
78
+ # Restores your original HOME.
79
+ #
80
+ # @raise PreparationError if not prepared
81
+ #
82
+ # @example
83
+ # home = Home.new("/tmp/fake_home")
84
+ # home.prepare
85
+ # ENV["HOME"] #=> "/tmp/fake_home"
86
+ # home.restore
87
+ # ENV["HOME"] #=> "/home/username"
88
+ #
89
+ # @return original home path
90
+ def restore
91
+ raise PreparationError, "You have to prepare first." unless prepared?
92
+
93
+ FileUtils.rm_rf @fake_home
94
+ ENV["HOME"] = @original_home
95
+ end
96
+
97
+ # Does your orginial HOME exist?
98
+ #
99
+ # @example
100
+ # home = Home.new
101
+ # home.prepare
102
+ # home.restored? #=> false
103
+ # home.restore
104
+ # home.restored? #=> true
105
+ def restored?
106
+ ENV["HOME"] == @original_home
107
+ end
108
+
109
+ # Gets your fake HOME path. If a block is set, you can work in it with your fake home. Everything outside will be
110
+ # your original home.
111
+ #
112
+ # @example
113
+ # home = Home.new("/tmp/fake_home")
114
+ # ENV["HOME"] #=> "/home/username"
115
+ # home.fake_home do |home|
116
+ # ENV["HOME"] #=> "/tmp/fake_home"
117
+ # end
118
+ # ENV["HOME"] #=> "/home/username"
119
+ #
120
+ # @return path of your fake home
121
+ def fake_home
122
+ if block_given?
123
+ prepare
124
+ yield @fake_home
125
+ restore
126
+ end
127
+ @fake_home
128
+ end
129
+
130
+ private
131
+
132
+ def extract_init_options(args)
133
+ last = args.last
134
+ if args && last && last.is_a?(Hash)
135
+ options = last
136
+ end
137
+ options ||= {}
138
+ end
139
+
140
+ def mkdir
141
+ if @fake_home
142
+ FileUtils.mkdir_p(@fake_home)
143
+ @fake_home
144
+ else
145
+ Dir.mktmpdir([@prefix+"_", "_"+@suffix], Dir.tmpdir)
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,3 @@
1
+ module FakeHome
2
+ VERSION = File.read(File.absolute_path("../../../VERSION", __FILE__))
3
+ end
@@ -0,0 +1,149 @@
1
+ require "spec_helper"
2
+
3
+ describe Home do
4
+ let!(:old_home) { ENV["HOME"] }
5
+ let(:explicit_path) { "/tmp/fake_home" }
6
+ let(:tmp_path) { %r{/tmp/test_.+_home} }
7
+
8
+ subject { Home.new(*args) }
9
+ let(:args) { nil }
10
+
11
+ describe "#init" do
12
+ context "with set prefix and default suffix" do
13
+ let(:args) { [{prefix: "new_prefix"}] }
14
+
15
+ its(:prefix) { should == "new_prefix" }
16
+ its(:suffix) { should == "home" }
17
+ end
18
+
19
+ context "with default prefix and set suffix" do
20
+ let(:args) { [{suffix: "new_suffix"}] }
21
+
22
+ its(:prefix) { should == "test" }
23
+ its(:suffix) { should == "new_suffix" }
24
+ end
25
+
26
+ context "with explicit home path" do
27
+ let(:args) { explicit_path }
28
+
29
+ its(:fake_home) { should == explicit_path}
30
+ end
31
+
32
+ context "with explicit path and prefix" do
33
+ let(:args) { [explicit_path, prefix: "new_prefix"] }
34
+ its(:fake_home) { should == explicit_path}
35
+ its(:prefix) { should == "new_prefix" }
36
+ end
37
+ end
38
+
39
+ describe "#original_home" do
40
+ its(:original_home) { should be_nil }
41
+
42
+ context "after prepare" do
43
+ before { subject.prepare }
44
+ it "should be the old $HOME" do
45
+ subject.original_home.should == old_home
46
+ end
47
+ end
48
+ end
49
+
50
+ describe "#prepare" do
51
+ let(:args) { explicit_path }
52
+ before { subject.prepare }
53
+ it "should set new home in ENV" do
54
+ ENV["HOME"].should == explicit_path
55
+ end
56
+ end
57
+
58
+ describe "#prepared?" do
59
+ it "should not be prepared" do
60
+ subject.should_not be_prepared
61
+ end
62
+
63
+ context "after prepare" do
64
+ before { subject.prepare }
65
+ it "should be prepared" do
66
+ subject.should be_prepared
67
+ end
68
+ end
69
+ end
70
+
71
+ describe "#restore" do
72
+ it "should raise error if not prepared" do
73
+ expect{ subject.restore }.to raise_error PreparationError
74
+ end
75
+
76
+ context "when prepared and restored" do
77
+ before :each do
78
+ subject.prepare
79
+ subject.restore
80
+ end
81
+
82
+ it "should restore old home path in ENV" do
83
+ ENV["HOME"].should == old_home
84
+ end
85
+
86
+ it "should remove recursive fake_home directory" do
87
+ File.exist?(subject.fake_home).should be_false
88
+ subject.fake_home.should =~ tmp_path
89
+ end
90
+ end
91
+ end
92
+
93
+ describe "#restored?" do
94
+ it "should be restored" do
95
+ subject.should_not be_restored
96
+ end
97
+
98
+ context "after prepare" do
99
+ before { subject.prepare }
100
+ it "should not be restored" do
101
+ subject.should_not be_restored
102
+ end
103
+
104
+ context "after restore" do
105
+ before { subject.restore }
106
+ it "should be restored" do
107
+ subject.should be_restored
108
+ end
109
+ end
110
+ end
111
+ end
112
+
113
+ describe "#fake_home" do
114
+ let(:args) { explicit_path }
115
+
116
+ its(:fake_home) { should == explicit_path }
117
+
118
+ context "when block is given then" do
119
+ it "should not be prepared before fake_home call" do
120
+ subject.should_not be_prepared
121
+ end
122
+ it "should yield fake_home directory" do
123
+ expect { |b| subject.fake_home(&b) }.to yield_with_args(explicit_path)
124
+ end
125
+ it "should be restored after fake_home" do
126
+ subject.fake_home { |fake_home| }
127
+ subject.should be_restored
128
+ end
129
+ end
130
+ end
131
+
132
+ describe "Private methods" do
133
+ describe "#mkdir" do
134
+ its(:mkdir) { should =~ tmp_path }
135
+ it "should create the directory in filesystem" do
136
+ File.exist?(subject.send(:mkdir)).should be_true
137
+ end
138
+
139
+ context "when explicit path is set then" do
140
+ let(:args) { explicit_path }
141
+ before { subject.send(:mkdir) }
142
+ its(:mkdir) { should == explicit_path }
143
+ it "should create the directory in filesystem" do
144
+ File.exist?(subject.send(:mkdir)).should be_true
145
+ end
146
+ end
147
+ end
148
+ end
149
+ end
@@ -0,0 +1,12 @@
1
+ require 'rspec'
2
+ require 'fake_home'
3
+
4
+ RSpec.configure do |config|
5
+ include FakeHome
6
+
7
+ config.mock_with :rspec
8
+
9
+ config.treat_symbols_as_metadata_keys_with_true_values = true
10
+ config.filter_run :focus => true
11
+ config.run_all_when_everything_filtered = true
12
+ end
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fake_home
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - DSIW
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: version
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: yard
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - '>='
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - '>='
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Manipulates your HOME environment variable.
98
+ email:
99
+ - dsiw@dsiw-it.de
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - .gitignore
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - VERSION
110
+ - fake_home.gemspec
111
+ - lib/fake_home.rb
112
+ - lib/fake_home/fake_home.rb
113
+ - lib/fake_home/version.rb
114
+ - spec/fake_home_spec.rb
115
+ - spec/spec_helper.rb
116
+ homepage: https://github.com/DSIW/fake_home
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - '>='
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - '>='
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.0.0
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Manipulates and restores your HOME environment variable for test suites.
140
+ test_files:
141
+ - spec/fake_home_spec.rb
142
+ - spec/spec_helper.rb
143
+ has_rdoc: