fakefs-require 0.1.2 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/LICENSE +1 -1
- data/README.markdown +14 -0
- data/Rakefile +6 -2
- data/TODO +3 -0
- data/VERSION +1 -1
- data/lib/fakefs/require.rb +130 -93
- data/test/helper.rb +3 -1
- data/test/require_test.rb +56 -37
- metadata +22 -11
data/LICENSE
CHANGED
@@ -16,4 +16,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
16
16
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
17
17
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
18
18
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
19
|
-
THE SOFTWARE.
|
19
|
+
THE SOFTWARE.
|
data/README.markdown
CHANGED
@@ -22,6 +22,8 @@ Usage
|
|
22
22
|
|
23
23
|
File.open("foo.rb", "w") {|f| f.write "puts 'hello world!'" }
|
24
24
|
require "foo"
|
25
|
+
|
26
|
+
Files loaded by #require will be appended to $".
|
25
27
|
|
26
28
|
|
27
29
|
Gems, Standard Lib and Load Path
|
@@ -56,6 +58,15 @@ Note: This will only work for autoload calls made _after_ ::activate! has been
|
|
56
58
|
called.
|
57
59
|
|
58
60
|
|
61
|
+
Kernel#load
|
62
|
+
-----------
|
63
|
+
|
64
|
+
There is also support for Kernel#load, including loading the file within an
|
65
|
+
anonymous module.
|
66
|
+
|
67
|
+
FakeFS::Require.activate! :load => true
|
68
|
+
|
69
|
+
|
59
70
|
Limitations
|
60
71
|
-----------
|
61
72
|
|
@@ -76,6 +87,9 @@ There are two known limitations to FakeFS::Require (besides ones to FakeFS).
|
|
76
87
|
2. If a class/module that calls #autoload defines #const_missing the faked
|
77
88
|
autoload won't work. Maybe this is a WONTFIX, I don't know... any ideas?
|
78
89
|
|
90
|
+
3. #require does only work with .rb files. Binary files like .so and .dll will
|
91
|
+
not be loaded.
|
92
|
+
|
79
93
|
|
80
94
|
Contributing
|
81
95
|
------------
|
data/Rakefile
CHANGED
@@ -19,10 +19,14 @@ begin
|
|
19
19
|
gemspec.email = "lars.gierth@altefeuerwachekoeln.de"
|
20
20
|
gemspec.homepage = "http://github.com/lgierth/fakefs-require"
|
21
21
|
gemspec.authors = ["Lars Gierth"]
|
22
|
-
|
22
|
+
|
23
|
+
gemspec.has_rdoc = true
|
24
|
+
gemspec.extra_rdoc_files = ["README.markdown"]
|
25
|
+
gemspec.rdoc_options << "--main" << "README.markdown"
|
26
|
+
|
23
27
|
gemspec.add_dependency "fakefs"
|
24
28
|
end
|
25
29
|
Jeweler::GemcutterTasks.new
|
26
30
|
rescue LoadError
|
27
31
|
puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
|
28
|
-
end
|
32
|
+
end
|
data/TODO
ADDED
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.2.0
|
data/lib/fakefs/require.rb
CHANGED
@@ -1,149 +1,185 @@
|
|
1
|
+
require "stringio"
|
2
|
+
|
1
3
|
module FakeFS
|
2
4
|
|
3
|
-
# TODO write documentation
|
4
5
|
module Require
|
5
6
|
@active = false
|
6
7
|
|
8
|
+
# Activates faked #require
|
9
|
+
#
|
10
|
+
# = Options
|
11
|
+
#
|
12
|
+
# * :fallback => true # activates the fallback to Kernel#require
|
13
|
+
# * :autoload => true # activates faked #autoload, #autoload? and #const_missing
|
14
|
+
# * :load => true # activates faked #load
|
7
15
|
def self.activate! opts = {}
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
16
|
+
return if active?
|
17
|
+
|
18
|
+
@active = true
|
19
|
+
|
20
|
+
@opts = {
|
21
|
+
:fallback => false,
|
22
|
+
:autoload => false,
|
23
|
+
:load => false,
|
24
|
+
}.merge opts
|
25
|
+
|
26
|
+
Kernel.class_eval do
|
27
|
+
alias_method :fakefs_original_require, :require
|
28
|
+
alias_method :require, :fakefs_require
|
29
|
+
end
|
30
|
+
|
31
|
+
Module.class_eval do
|
32
|
+
alias_method :fakefs_original_autoload, :autoload
|
33
|
+
alias_method :autoload, :fakefs_autoload
|
14
34
|
|
15
|
-
|
16
|
-
|
17
|
-
:autoload => false,
|
18
|
-
}
|
19
|
-
@opts.merge!(opts)
|
20
|
-
@identity_map = []
|
35
|
+
alias_method :fakefs_original_autoload?, :autoload?
|
36
|
+
alias_method :autoload?, :fakefs_autoload?
|
21
37
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
alias_method :const_missing, :fakefs_const_missing
|
31
|
-
end if @opts[:autoload]
|
32
|
-
end
|
38
|
+
alias_method :fakefs_original_const_missing, :const_missing
|
39
|
+
alias_method :const_missing, :fakefs_const_missing
|
40
|
+
end if @opts[:autoload]
|
41
|
+
|
42
|
+
Kernel.class_eval do
|
43
|
+
alias_method :fakefs_original_load, :load
|
44
|
+
alias_method :load, :fakefs_load
|
45
|
+
end if @opts[:load]
|
33
46
|
end
|
34
47
|
|
48
|
+
# Deactivates the faked methods
|
35
49
|
def self.deactivate!
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
Module.class_eval do
|
43
|
-
alias_method :autoload, :fakefs_original_autoload
|
44
|
-
alias_method :autoload?, :fakefs_original_autoload?
|
45
|
-
alias_method :const_missing, :fakefs_original_const_missing
|
46
|
-
end if @opts[:autoload]
|
50
|
+
return unless active?
|
51
|
+
|
52
|
+
@active = false
|
53
|
+
|
54
|
+
Kernel.class_eval do
|
55
|
+
alias_method :require, :fakefs_original_require
|
47
56
|
end
|
57
|
+
|
58
|
+
Module.class_eval do
|
59
|
+
alias_method :autoload, :fakefs_original_autoload
|
60
|
+
alias_method :autoload?, :fakefs_original_autoload?
|
61
|
+
alias_method :const_missing, :fakefs_original_const_missing
|
62
|
+
end if @opts[:autoload]
|
63
|
+
|
64
|
+
Kernel.class_eval do
|
65
|
+
alias_method :load, :fakefs_original_load
|
66
|
+
end if @opts[:load]
|
67
|
+
|
68
|
+
@opts = nil
|
48
69
|
end
|
49
70
|
|
71
|
+
# Returns whether FakeFS::Require is active
|
50
72
|
def self.active?
|
51
73
|
@active
|
52
74
|
end
|
53
75
|
|
54
|
-
|
55
|
-
@identity_map = []
|
56
|
-
@autoloadable = {}
|
57
|
-
@autoloaded = {}
|
58
|
-
end
|
59
|
-
|
76
|
+
# Returns the options passed to ::activate!
|
60
77
|
def self.opts
|
61
78
|
@opts
|
62
79
|
end
|
63
80
|
|
64
|
-
|
65
|
-
@identity_map
|
66
|
-
end
|
67
|
-
|
81
|
+
# Returns a hash containing autoload data
|
68
82
|
def self.autoloadable
|
69
83
|
@autoloadable ||= {}
|
70
84
|
end
|
71
85
|
|
72
|
-
|
73
|
-
|
86
|
+
# Clears the autoload data hash
|
87
|
+
def self.clear
|
88
|
+
@autoloadable = {}
|
74
89
|
end
|
75
90
|
|
76
|
-
#
|
91
|
+
# Resolves the passed filename to a path
|
92
|
+
def self.resolve fn
|
93
|
+
found = nil
|
94
|
+
if fn[0...1] == "/"
|
95
|
+
found = fn if File.exist? fn
|
96
|
+
else
|
97
|
+
$LOAD_PATH.each do |p|
|
98
|
+
path = p + "/" + fn
|
99
|
+
found = path if File.exist? path
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
return found
|
104
|
+
end
|
105
|
+
|
106
|
+
# Faked #require (see Kernel#require)
|
77
107
|
def fakefs_require fn
|
78
|
-
fn = fn.to_s
|
108
|
+
fn = fn.to_s
|
109
|
+
orig_fn = fn.dup
|
79
110
|
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
2.times do
|
86
|
-
if fn.match "^/"
|
87
|
-
path = fn if File.exist? fn
|
88
|
-
else
|
89
|
-
$LOAD_PATH.each do |p|
|
90
|
-
if File.exist? p + "/" + fn
|
91
|
-
path = p + "/" + fn
|
92
|
-
break
|
93
|
-
end
|
94
|
-
end
|
95
|
-
end
|
96
|
-
|
97
|
-
break if path or orig_fn[-3..-1] == ".rb" or fn[-3..-1] != ".rb"
|
98
|
-
fn = fn[0..-3]
|
99
|
-
end
|
111
|
+
fn = fn + ".rb" unless fn[-3..-1] == ".rb"
|
112
|
+
path = FakeFS::Require.resolve fn
|
113
|
+
|
114
|
+
if path
|
115
|
+
return false if $".include? fn
|
100
116
|
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
117
|
+
$" << fn
|
118
|
+
File.open(path, "r") {|f| Object.class_eval f.read, fn, 1 }
|
119
|
+
return true
|
120
|
+
elsif FakeFS::Require.opts[:fallback]
|
121
|
+
opts = FakeFS::Require.opts
|
122
|
+
begin
|
123
|
+
FakeFS.deactivate!
|
124
|
+
FakeFS::Require.deactivate!
|
125
|
+
return fakefs_original_require orig_fn
|
126
|
+
ensure
|
127
|
+
FakeFS::Require.activate! opts
|
128
|
+
FakeFS.activate!
|
113
129
|
end
|
114
|
-
raise load_error
|
115
130
|
end
|
116
131
|
|
117
|
-
|
118
|
-
|
119
|
-
FakeFS::Require.identity_map << path
|
120
|
-
File.open path, "r" do |f|
|
121
|
-
Object.class_eval f.read, fn, 1
|
122
|
-
end
|
123
|
-
return true
|
132
|
+
raise LoadError, "no such file to load -- " + orig_fn
|
124
133
|
end
|
125
134
|
|
126
135
|
module Autoload
|
136
|
+
# Faked #autoload (see Module#autoload)
|
127
137
|
def fakefs_autoload const, file
|
128
138
|
Require.autoloadable[self] ||= {}
|
129
139
|
Require.autoloadable[self][const] = file
|
130
140
|
end
|
131
141
|
|
142
|
+
# Faked #autoload? (see Module#autoload?)
|
132
143
|
def fakefs_autoload? const
|
133
144
|
hsh = Require.autoloadable[self]
|
134
145
|
return hsh[const] if hsh
|
135
146
|
end
|
136
147
|
|
148
|
+
# Implementation of #const_missing, catches autoload cases
|
137
149
|
def fakefs_const_missing name
|
138
|
-
Require.autoloaded[self] ||= {}
|
139
150
|
file = autoload? name
|
140
|
-
if file
|
141
|
-
Require.autoloaded[self][name] = true
|
151
|
+
if file
|
142
152
|
require file
|
143
153
|
return const_get name if const_defined? name
|
144
154
|
end
|
145
155
|
parent = (self == Object) ? "" : self.to_s + "::"
|
146
|
-
raise NameError, "uninitialized constant #{parent
|
156
|
+
raise NameError, "uninitialized constant #{parent + name.to_s}", caller
|
157
|
+
end
|
158
|
+
end
|
159
|
+
|
160
|
+
module Load
|
161
|
+
# Faked #load (see Kernel#load)
|
162
|
+
def fakefs_load fn, wrap = false
|
163
|
+
fn = fn.to_s
|
164
|
+
orig_fn = fn.dup
|
165
|
+
|
166
|
+
fn = fn + ".rb" unless fn[-3..-1] == ".rb"
|
167
|
+
path = FakeFS::Require.resolve fn
|
168
|
+
|
169
|
+
if path
|
170
|
+
File.open path, "r" do |f|
|
171
|
+
if wrap
|
172
|
+
Module.new.module_eval f.read, fn, 1
|
173
|
+
else
|
174
|
+
Object.class_eval f.read, fn, 1
|
175
|
+
end
|
176
|
+
end
|
177
|
+
return true
|
178
|
+
elsif FakeFS::Require.opts[:fallback]
|
179
|
+
return fakefs_original_load orig_fn, wrap
|
180
|
+
end
|
181
|
+
|
182
|
+
raise LoadError, "no such file to load -- " + fn
|
147
183
|
end
|
148
184
|
end
|
149
185
|
|
@@ -153,6 +189,7 @@ end
|
|
153
189
|
|
154
190
|
module Kernel
|
155
191
|
include FakeFS::Require
|
192
|
+
include FakeFS::Require::Load
|
156
193
|
end
|
157
194
|
|
158
195
|
class Module
|
data/test/helper.rb
CHANGED
data/test/require_test.rb
CHANGED
@@ -5,6 +5,9 @@ class RequireTest < Test::Unit::TestCase
|
|
5
5
|
end
|
6
6
|
|
7
7
|
def teardown
|
8
|
+
FakeFS::Require.deactivate!
|
9
|
+
FakeFS::Require.clear
|
10
|
+
|
8
11
|
FakeFS::FileSystem.clear
|
9
12
|
FakeFS.deactivate!
|
10
13
|
end
|
@@ -28,62 +31,44 @@ class RequireTest < Test::Unit::TestCase
|
|
28
31
|
require "foo"
|
29
32
|
end
|
30
33
|
|
31
|
-
#
|
34
|
+
# always append .rb if the filename doesn't end with it
|
32
35
|
code = <<-EOS
|
33
|
-
module
|
36
|
+
module FakeFSTestRequire2_WithDotRb
|
34
37
|
end
|
35
38
|
EOS
|
36
39
|
File.open "fake_fs_test_require2.rb", "w" do |f|
|
37
40
|
f.write code
|
38
41
|
end
|
39
|
-
require "fake_fs_test_require2"
|
40
|
-
assert ::FakeFSTestRequire2
|
41
|
-
|
42
|
-
# foo.rb has higher priority than foo
|
43
42
|
code = <<-EOS
|
44
|
-
module
|
43
|
+
module FakeFSTestRequire2_WithoutDotRb
|
45
44
|
end
|
46
45
|
EOS
|
47
|
-
File.open "
|
46
|
+
File.open "fake_fs_test_require2", "w" do |f|
|
48
47
|
f.write code
|
49
48
|
end
|
50
|
-
|
51
|
-
|
52
|
-
end
|
53
|
-
EOS
|
54
|
-
File.open "fake_fs_test_require3", "w" do |f|
|
55
|
-
f.write code
|
56
|
-
end
|
57
|
-
require "fake_fs_test_require3"
|
58
|
-
assert ::FakeFSTestRequire3_WithDotRb
|
49
|
+
require "fake_fs_test_require2"
|
50
|
+
assert ::FakeFSTestRequire2_WithDotRb
|
59
51
|
|
60
|
-
#
|
52
|
+
# remember which files have been loaded
|
61
53
|
code = <<-EOS
|
62
|
-
module
|
54
|
+
module FakeFSTestRequire3
|
63
55
|
end
|
64
56
|
EOS
|
65
|
-
File.open "
|
57
|
+
File.open "fake_fs_test_require3.rb", "w" do |f|
|
66
58
|
f.write code
|
67
59
|
end
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
assert require("fake_fs_test_require4")
|
60
|
+
require "fake_fs_test_require3"
|
61
|
+
assert_equal "fake_fs_test_require3.rb", $".last
|
62
|
+
assert !require("fake_fs_test_require3")
|
72
63
|
|
73
64
|
# properly deactivate
|
74
65
|
FakeFS::Require.deactivate!
|
75
66
|
assert_raise LoadError do
|
76
67
|
require "bar"
|
77
68
|
end
|
78
|
-
|
79
|
-
FakeFS::Require.clear
|
80
69
|
end
|
81
70
|
|
82
71
|
def test_fakes_require_with_fallback
|
83
|
-
FakeFS.deactivate!
|
84
|
-
require "tmpdir"
|
85
|
-
FakeFS.activate!
|
86
|
-
|
87
72
|
FakeFS::Require.activate! :fallback => true
|
88
73
|
|
89
74
|
# load a file that's in the real (= non-faked) load path
|
@@ -126,9 +111,6 @@ class RequireTest < Test::Unit::TestCase
|
|
126
111
|
assert_raise LoadError do
|
127
112
|
require "rack/mime"
|
128
113
|
end
|
129
|
-
|
130
|
-
FakeFS::Require.deactivate!
|
131
|
-
FakeFS::Require.clear
|
132
114
|
end
|
133
115
|
|
134
116
|
def test_fakes_autoload
|
@@ -167,9 +149,46 @@ class RequireTest < Test::Unit::TestCase
|
|
167
149
|
assert_raise NameError do
|
168
150
|
FakeFSTestAutoload::Baz
|
169
151
|
end
|
170
|
-
|
171
|
-
|
172
|
-
|
152
|
+
end
|
153
|
+
|
154
|
+
def test_fakes_load
|
155
|
+
FakeFS::Require.activate! :load => true
|
156
|
+
|
157
|
+
# loads a file
|
158
|
+
File.open "fake_fs_test_load.rb", "w" do |f|
|
159
|
+
f.write <<-CODE
|
160
|
+
module FakeFSTestLoad
|
161
|
+
@count ||= 0
|
162
|
+
@count += 1
|
163
|
+
def self.count; return @count; end
|
164
|
+
end
|
165
|
+
CODE
|
166
|
+
end
|
167
|
+
load "fake_fs_test_load.rb"
|
168
|
+
assert_equal 1, FakeFSTestLoad.count
|
169
|
+
|
170
|
+
# loads the file twice
|
171
|
+
load "fake_fs_test_load.rb"
|
172
|
+
assert_equal 2, FakeFSTestLoad.count
|
173
|
+
|
174
|
+
# doesn't append .rb
|
175
|
+
assert_raise(LoadError) { load "fake_fs_test_load/asd.rb" }
|
176
|
+
|
177
|
+
# falls back to the original #load
|
178
|
+
fn = "fake_fs_test_load2.rb"
|
179
|
+
FakeFS::Require.opts[:fallback] = true
|
180
|
+
self.expects(:fakefs_original_load).with(fn, false).returns(true)
|
181
|
+
load fn
|
182
|
+
|
183
|
+
# executes the file within an anonymous module
|
184
|
+
File.open "fake_fs_test_load3.rb", "w" do |f|
|
185
|
+
f.write <<-CODE
|
186
|
+
module FakeFSTestLoad3
|
187
|
+
end
|
188
|
+
CODE
|
189
|
+
end
|
190
|
+
load "fake_fs_test_load3.rb", true
|
191
|
+
assert_raise(NameError) { FakeFSTestLoad3 }
|
173
192
|
end
|
174
193
|
|
175
|
-
end
|
194
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,12 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: fakefs-require
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: false
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 2
|
8
|
+
- 0
|
9
|
+
version: 0.2.0
|
5
10
|
platform: ruby
|
6
11
|
authors:
|
7
12
|
- Lars Gierth
|
@@ -9,19 +14,21 @@ autorequire:
|
|
9
14
|
bindir: bin
|
10
15
|
cert_chain: []
|
11
16
|
|
12
|
-
date: 2010-
|
17
|
+
date: 2010-04-13 00:00:00 +02:00
|
13
18
|
default_executable:
|
14
19
|
dependencies:
|
15
20
|
- !ruby/object:Gem::Dependency
|
16
21
|
name: fakefs
|
17
|
-
|
18
|
-
|
19
|
-
version_requirements: !ruby/object:Gem::Requirement
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
20
24
|
requirements:
|
21
25
|
- - ">="
|
22
26
|
- !ruby/object:Gem::Version
|
27
|
+
segments:
|
28
|
+
- 0
|
23
29
|
version: "0"
|
24
|
-
|
30
|
+
type: :runtime
|
31
|
+
version_requirements: *id001
|
25
32
|
description:
|
26
33
|
email: lars.gierth@altefeuerwachekoeln.de
|
27
34
|
executables: []
|
@@ -29,13 +36,13 @@ executables: []
|
|
29
36
|
extensions: []
|
30
37
|
|
31
38
|
extra_rdoc_files:
|
32
|
-
- LICENSE
|
33
39
|
- README.markdown
|
34
40
|
files:
|
35
41
|
- .gitignore
|
36
42
|
- LICENSE
|
37
43
|
- README.markdown
|
38
44
|
- Rakefile
|
45
|
+
- TODO
|
39
46
|
- VERSION
|
40
47
|
- lib/fakefs/require.rb
|
41
48
|
- test/helper.rb
|
@@ -47,27 +54,31 @@ licenses: []
|
|
47
54
|
post_install_message:
|
48
55
|
rdoc_options:
|
49
56
|
- --charset=UTF-8
|
57
|
+
- --main
|
58
|
+
- README.markdown
|
50
59
|
require_paths:
|
51
60
|
- lib
|
52
61
|
required_ruby_version: !ruby/object:Gem::Requirement
|
53
62
|
requirements:
|
54
63
|
- - ">="
|
55
64
|
- !ruby/object:Gem::Version
|
65
|
+
segments:
|
66
|
+
- 0
|
56
67
|
version: "0"
|
57
|
-
version:
|
58
68
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
59
69
|
requirements:
|
60
70
|
- - ">="
|
61
71
|
- !ruby/object:Gem::Version
|
72
|
+
segments:
|
73
|
+
- 0
|
62
74
|
version: "0"
|
63
|
-
version:
|
64
75
|
requirements: []
|
65
76
|
|
66
77
|
rubyforge_project:
|
67
|
-
rubygems_version: 1.3.
|
78
|
+
rubygems_version: 1.3.6
|
68
79
|
signing_key:
|
69
80
|
specification_version: 3
|
70
81
|
summary: "Faked #require and #autoload for defunkt's FakeFS"
|
71
82
|
test_files:
|
72
|
-
- test/helper.rb
|
73
83
|
- test/require_test.rb
|
84
|
+
- test/helper.rb
|