fakefs-require 0.1.1 → 0.1.2

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.
Files changed (7) hide show
  1. data/LICENSE +18 -18
  2. data/README.markdown +95 -92
  3. data/Rakefile +27 -28
  4. data/VERSION +1 -1
  5. data/lib/fakefs/require.rb +160 -158
  6. data/test/require_test.rb +174 -174
  7. metadata +10 -19
data/LICENSE CHANGED
@@ -1,19 +1,19 @@
1
- Copyright (c) 2010 Lars Gierth
2
-
3
- Permission is hereby granted, free of charge, to any person obtaining a copy
4
- of this software and associated documentation files (the "Software"), to deal
5
- in the Software without restriction, including without limitation the rights
6
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
- copies of the Software, and to permit persons to whom the Software is
8
- furnished to do so, subject to the following conditions:
9
-
10
- The above copyright notice and this permission notice shall be included in
11
- all copies or substantial portions of the Software.
12
-
13
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
1
+ Copyright (c) 2010 Lars Gierth
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ of this software and associated documentation files (the "Software"), to deal
5
+ in the Software without restriction, including without limitation the rights
6
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7
+ copies of the Software, and to permit persons to whom the Software is
8
+ furnished to do so, subject to the following conditions:
9
+
10
+ The above copyright notice and this permission notice shall be included in
11
+ all copies or substantial portions of the Software.
12
+
13
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19
19
  THE SOFTWARE.
@@ -1,92 +1,95 @@
1
- Faked #require and #autoload
2
- ============================
3
-
4
- Testing a library that loads ruby source files by configuration, I noticed that
5
- defunkt's great FakeFS was lacking support for faking #require, #autoload, etc.
6
-
7
-
8
- Installation
9
- ------------
10
-
11
- $ gem install fakefs-require
12
-
13
-
14
- Usage
15
- -----
16
-
17
- require "fakefs/require"
18
- FakeFS::Require.activate!
19
-
20
- File.open "foo.rb", "w" {|f| f.write "puts 'hello world!'" }
21
- require "foo"
22
-
23
-
24
- Gems, Standard Lib and Load Path
25
- --------------------------------
26
-
27
- FakeFS::Require provides a fallback for tests which use code (most likely gems)
28
- that #require source files or gems at runtime (e.g. Usher). This will fail
29
- because the required files don't exist in FakeFS. You can workaround this issue
30
- by activating like this:
31
-
32
- FakeFS::Require.activate! :fallback => true
33
-
34
- This will make the faked #require call the original #require if loading the
35
- passed file in FakeFS fails.
36
-
37
- FakeFS::Require uses $LOAD_PATH to find out in which paths to search (just like
38
- the original...).
39
-
40
-
41
- Autoloading
42
- -----------
43
-
44
- The autoloading mechanism ignores a monkey patched #require (see
45
- http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-core/20190?20046-21072+split-mode-vertical),
46
- so we will have to fake #autoload itself to get this working. If you use gems or
47
- other code that utilizes #autoload (e.g. Rack), pass the :autoload flag to
48
- ::activate!.
49
-
50
- FakeFS::Require.activate! :autoload => true
51
-
52
- Note: This will only work for autoload calls made _after_ ::activate! has been
53
- called.
54
-
55
-
56
- Limitations
57
- -----------
58
-
59
- There are two known limitations to FakeFS::Require (besides ones to FakeFS).
60
-
61
- 1. David Masover points this out:
62
-
63
- irb(main):001:0> autoload :CSV, 'csv'
64
- => nil
65
- irb(main):002:0> module CSV
66
- irb(main):003:1> end
67
- TypeError: CSV is not a module
68
- from (irb):2
69
-
70
- See http://www.ruby-forum.com/topic/205612#898211 for more information on
71
- this.
72
-
73
- 2. If a class/module that calls #autoload defines #const_missing the faked
74
- autoload won't work. Maybe this is a WONTFIX, I don't know... any ideas?
75
-
76
-
77
- Contributing
78
- ------------
79
-
80
- Once you've made your great commits:
81
-
82
- 1. Fork fakefs-require
83
- 2. Create a topic branch - `git checkout -b my_branch`
84
- 3. Push to your branch - `git push origin my_branch`
85
- 4. Create an issue with a link to your branch
86
- 5. That's it!
87
-
88
-
89
- License
90
- -------
91
-
92
- fakefs-require is subject to the MIT License (see LICENSE file)
1
+ Faked #require and #autoload
2
+ ============================
3
+
4
+ Testing a library that loads ruby source files by configuration, I noticed that
5
+ defunkt's great FakeFS was lacking support for faking #require, #autoload, etc.
6
+
7
+
8
+ Installation
9
+ ------------
10
+
11
+ $ gem install fakefs-require
12
+
13
+
14
+ Usage
15
+ -----
16
+
17
+ require "fakefs/safe"
18
+ require "fakefs/require"
19
+
20
+ FakeFS.activate!
21
+ FakeFS::Require.activate!
22
+
23
+ File.open("foo.rb", "w") {|f| f.write "puts 'hello world!'" }
24
+ require "foo"
25
+
26
+
27
+ Gems, Standard Lib and Load Path
28
+ --------------------------------
29
+
30
+ FakeFS::Require provides a fallback for tests which use code (most likely gems)
31
+ that #require source files or gems at runtime (e.g. Usher). This will fail
32
+ because the required files don't exist in FakeFS. You can workaround this issue
33
+ by activating like this:
34
+
35
+ FakeFS::Require.activate! :fallback => true
36
+
37
+ This will make the faked #require call the original #require if loading the
38
+ passed file in FakeFS fails.
39
+
40
+ FakeFS::Require uses $LOAD_PATH to find out in which paths to search (just like
41
+ the original...).
42
+
43
+
44
+ Autoloading
45
+ -----------
46
+
47
+ The autoloading mechanism ignores a monkey patched #require (see
48
+ http://blade.nagaokaut.ac.jp/cgi-bin/vframe.rb/ruby/ruby-core/20190?20046-21072+split-mode-vertical),
49
+ so we will have to fake #autoload itself to get this working. If you use gems or
50
+ other code that utilizes #autoload (e.g. Rack), pass the :autoload flag to
51
+ ::activate!.
52
+
53
+ FakeFS::Require.activate! :autoload => true
54
+
55
+ Note: This will only work for autoload calls made _after_ ::activate! has been
56
+ called.
57
+
58
+
59
+ Limitations
60
+ -----------
61
+
62
+ There are two known limitations to FakeFS::Require (besides ones to FakeFS).
63
+
64
+ 1. David Masover points this out:
65
+
66
+ irb(main):001:0> autoload :CSV, 'csv'
67
+ => nil
68
+ irb(main):002:0> module CSV
69
+ irb(main):003:1> end
70
+ TypeError: CSV is not a module
71
+ from (irb):2
72
+
73
+ See http://www.ruby-forum.com/topic/205612#898211 for more information on
74
+ this.
75
+
76
+ 2. If a class/module that calls #autoload defines #const_missing the faked
77
+ autoload won't work. Maybe this is a WONTFIX, I don't know... any ideas?
78
+
79
+
80
+ Contributing
81
+ ------------
82
+
83
+ Once you've made your great commits:
84
+
85
+ 1. Fork fakefs-require
86
+ 2. Create a topic branch - `git checkout -b my_branch`
87
+ 3. Push to your branch - `git push origin my_branch`
88
+ 4. Create an issue with a link to your branch
89
+ 5. That's it!
90
+
91
+
92
+ License
93
+ -------
94
+
95
+ fakefs-require is subject to the MIT License (see LICENSE file)
data/Rakefile CHANGED
@@ -1,29 +1,28 @@
1
- require "rake/testtask"
2
-
3
- task :default => [:test]
4
-
5
- Rake::TestTask.new :test do |t|
6
- t.libs = ["lib"]
7
- t.test_files = FileList["test/*_test.rb"]
8
- t.ruby_opts << "-rubygems"
9
- t.ruby_opts << "-rtest/unit"
10
- t.ruby_opts << "-rtest/helper"
11
- end
12
-
13
- begin
14
- require 'jeweler'
15
-
16
- Jeweler::Tasks.new do |gemspec|
17
- gemspec.name = "fakefs-require"
18
- gemspec.summary = "Faked #require and #autoload for defunkt's FakeFS"
19
- gemspec.email = "lars.gierth@altefeuerwachekoeln.de"
20
- gemspec.homepage = "http://github.com/lgierth/fakefs-require"
21
- gemspec.description = "Faked #require and #autoload for defunkt's FakeFS"
22
- gemspec.authors = ["Lars Gierth"]
23
- gemspec.has_rdoc = false
24
- gemspec.add_dependency "fakefs"
25
- end
26
- Jeweler::GemcutterTasks.new
27
- rescue LoadError
28
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
1
+ require "rake/testtask"
2
+
3
+ task :default => [:test]
4
+
5
+ Rake::TestTask.new :test do |t|
6
+ t.libs = ["lib"]
7
+ t.test_files = FileList["test/*_test.rb"]
8
+ t.ruby_opts << "-rubygems"
9
+ t.ruby_opts << "-rtest/unit"
10
+ t.ruby_opts << "-rtest/helper"
11
+ end
12
+
13
+ begin
14
+ require 'jeweler'
15
+
16
+ Jeweler::Tasks.new do |gemspec|
17
+ gemspec.name = "fakefs-require"
18
+ gemspec.summary = "Faked #require and #autoload for defunkt's FakeFS"
19
+ gemspec.email = "lars.gierth@altefeuerwachekoeln.de"
20
+ gemspec.homepage = "http://github.com/lgierth/fakefs-require"
21
+ gemspec.authors = ["Lars Gierth"]
22
+ gemspec.has_rdoc = false
23
+ gemspec.add_dependency "fakefs"
24
+ end
25
+ Jeweler::GemcutterTasks.new
26
+ rescue LoadError
27
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
29
28
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.1.2
@@ -1,158 +1,160 @@
1
- module FakeFS
2
-
3
- # TODO write documentation
4
- module Require
5
- @active = false
6
-
7
- def self.activate! opts = {}
8
- unless @active
9
- Kernel.class_eval do
10
- alias_method :fakefs_original_require, :require
11
- alias_method :require, :fakefs_require
12
- end
13
- @active = true
14
-
15
- @opts = {
16
- :fallback => false,
17
- :autoload => false,
18
- }
19
- @opts.merge!(opts)
20
- @identity_map = []
21
-
22
- Module.class_eval do
23
- alias_method :fakefs_original_autoload, :autoload
24
- alias_method :autoload, :fakefs_autoload
25
-
26
- alias_method :fakefs_original_autoload?, :autoload?
27
- alias_method :autoload?, :fakefs_autoload?
28
-
29
- alias_method :fakefs_original_const_missing, :const_missing
30
- alias_method :const_missing, :fakefs_const_missing
31
- end if @opts[:autoload]
32
- end
33
- end
34
-
35
- def self.deactivate!
36
- if @active
37
- Kernel.class_eval do
38
- alias_method :require, :fakefs_original_require
39
- end
40
- @active = false
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]
47
- end
48
- end
49
-
50
- def self.active?
51
- @active
52
- end
53
-
54
- def self.clear
55
- @identity_map = []
56
- @autoloadable = {}
57
- @autoloaded = {}
58
- end
59
-
60
- def self.opts
61
- @opts
62
- end
63
-
64
- def self.identity_map
65
- @identity_map
66
- end
67
-
68
- def self.autoloadable
69
- @autoloadable ||= {}
70
- end
71
-
72
- def self.autoloaded
73
- @autoloaded ||= {}
74
- end
75
-
76
- # TODO fix identity map behaviour
77
- def fakefs_require fn
78
- begin
79
- orig_fn = fn.dup
80
- fn = fn + ".rb" unless fn[-3..-1] == ".rb"
81
-
82
- path = nil
83
- 2.times do
84
- if fn.match "^/"
85
- path = fn if File.exist? fn
86
- else
87
- $LOAD_PATH.each do |p|
88
- if File.exist? p + "/" + fn
89
- path = p + "/" + fn
90
- break
91
- end
92
- end
93
- end
94
-
95
- break if path or orig_fn[-3..-1] == ".rb" or fn[-3..-1] != ".rb"
96
- fn = fn[0..-3]
97
- end
98
-
99
- raise LoadError, "no such file to load -- " + orig_fn unless path
100
- rescue LoadError => load_error
101
- if FakeFS::Require.opts[:fallback]
102
- opts = FakeFS::Require.opts
103
- begin
104
- FakeFS.deactivate!
105
- FakeFS::Require.deactivate!
106
- return fakefs_original_require orig_fn
107
- ensure
108
- FakeFS::Require.activate! opts
109
- FakeFS.activate!
110
- end
111
- end
112
- raise load_error
113
- end
114
-
115
- return false if FakeFS::Require.identity_map.include? path
116
-
117
- FakeFS::Require.identity_map << path
118
- File.open path, "r" do |f|
119
- Object.class_eval f.read, fn, 1
120
- end
121
- return true
122
- end
123
-
124
- module Autoload
125
- def fakefs_autoload const, file
126
- Require.autoloadable[self] ||= {}
127
- Require.autoloadable[self][const] = file
128
- end
129
-
130
- def fakefs_autoload? const
131
- hsh = Require.autoloadable[self]
132
- return hsh[const] if hsh
133
- end
134
-
135
- def fakefs_const_missing name
136
- Require.autoloaded[self] ||= {}
137
- file = autoload? name
138
- if file and !Require.autoloaded[self][name]
139
- Require.autoloaded[self][name] = true
140
- require file
141
- return const_get name if const_defined? name
142
- end
143
- parent = (self == Object) ? "" : self.to_s + "::"
144
- raise NameError, "uninitialized constant #{parent}" + name.to_s, caller
145
- end
146
- end
147
-
148
- end
149
-
150
- end
151
-
152
- module Kernel
153
- include FakeFS::Require
154
- end
155
-
156
- class Module
157
- include FakeFS::Require::Autoload
158
- end
1
+ module FakeFS
2
+
3
+ # TODO write documentation
4
+ module Require
5
+ @active = false
6
+
7
+ def self.activate! opts = {}
8
+ unless @active
9
+ Kernel.class_eval do
10
+ alias_method :fakefs_original_require, :require
11
+ alias_method :require, :fakefs_require
12
+ end
13
+ @active = true
14
+
15
+ @opts = {
16
+ :fallback => false,
17
+ :autoload => false,
18
+ }
19
+ @opts.merge!(opts)
20
+ @identity_map = []
21
+
22
+ Module.class_eval do
23
+ alias_method :fakefs_original_autoload, :autoload
24
+ alias_method :autoload, :fakefs_autoload
25
+
26
+ alias_method :fakefs_original_autoload?, :autoload?
27
+ alias_method :autoload?, :fakefs_autoload?
28
+
29
+ alias_method :fakefs_original_const_missing, :const_missing
30
+ alias_method :const_missing, :fakefs_const_missing
31
+ end if @opts[:autoload]
32
+ end
33
+ end
34
+
35
+ def self.deactivate!
36
+ if @active
37
+ Kernel.class_eval do
38
+ alias_method :require, :fakefs_original_require
39
+ end
40
+ @active = false
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]
47
+ end
48
+ end
49
+
50
+ def self.active?
51
+ @active
52
+ end
53
+
54
+ def self.clear
55
+ @identity_map = []
56
+ @autoloadable = {}
57
+ @autoloaded = {}
58
+ end
59
+
60
+ def self.opts
61
+ @opts
62
+ end
63
+
64
+ def self.identity_map
65
+ @identity_map
66
+ end
67
+
68
+ def self.autoloadable
69
+ @autoloadable ||= {}
70
+ end
71
+
72
+ def self.autoloaded
73
+ @autoloaded ||= {}
74
+ end
75
+
76
+ # TODO fix identity map behaviour
77
+ def fakefs_require fn
78
+ fn = fn.to_s unless fn.is_a? String
79
+
80
+ begin
81
+ orig_fn = fn.dup
82
+ fn = fn + ".rb" unless fn[-3..-1] == ".rb"
83
+
84
+ path = nil
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
100
+
101
+ raise LoadError, "no such file to load -- " + orig_fn unless path
102
+ rescue LoadError => load_error
103
+ if FakeFS::Require.opts[:fallback]
104
+ opts = FakeFS::Require.opts
105
+ begin
106
+ FakeFS.deactivate!
107
+ FakeFS::Require.deactivate!
108
+ return fakefs_original_require orig_fn
109
+ ensure
110
+ FakeFS::Require.activate! opts
111
+ FakeFS.activate!
112
+ end
113
+ end
114
+ raise load_error
115
+ end
116
+
117
+ return false if FakeFS::Require.identity_map.include? path
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
124
+ end
125
+
126
+ module Autoload
127
+ def fakefs_autoload const, file
128
+ Require.autoloadable[self] ||= {}
129
+ Require.autoloadable[self][const] = file
130
+ end
131
+
132
+ def fakefs_autoload? const
133
+ hsh = Require.autoloadable[self]
134
+ return hsh[const] if hsh
135
+ end
136
+
137
+ def fakefs_const_missing name
138
+ Require.autoloaded[self] ||= {}
139
+ file = autoload? name
140
+ if file and !Require.autoloaded[self][name]
141
+ Require.autoloaded[self][name] = true
142
+ require file
143
+ return const_get name if const_defined? name
144
+ end
145
+ parent = (self == Object) ? "" : self.to_s + "::"
146
+ raise NameError, "uninitialized constant #{parent}" + name.to_s, caller
147
+ end
148
+ end
149
+
150
+ end
151
+
152
+ end
153
+
154
+ module Kernel
155
+ include FakeFS::Require
156
+ end
157
+
158
+ class Module
159
+ include FakeFS::Require::Autoload
160
+ end
@@ -1,175 +1,175 @@
1
- class RequireTest < Test::Unit::TestCase
2
-
3
- def setup
4
- FakeFS.activate!
5
- end
6
-
7
- def teardown
8
- FakeFS::FileSystem.clear
9
- FakeFS.deactivate!
10
- end
11
-
12
- def test_fakes_require
13
- FakeFS::Require.activate!
14
-
15
- # require a file
16
- code = <<-EOS
17
- module FakeFSTestRequire1
18
- end
19
- EOS
20
- File.open "fake_fs_test_require1.rb", "w" do |f|
21
- f.write code
22
- end
23
- require "fake_fs_test_require1.rb"
24
- assert ::FakeFSTestRequire1
25
-
26
- # require a file that doesn't exist
27
- assert_raise LoadError do
28
- require "foo"
29
- end
30
-
31
- # automatically append .rb
32
- code = <<-EOS
33
- module FakeFSTestRequire2
34
- end
35
- EOS
36
- File.open "fake_fs_test_require2.rb", "w" do |f|
37
- f.write code
38
- end
39
- require "fake_fs_test_require2"
40
- assert ::FakeFSTestRequire2
41
-
42
- # foo.rb has higher priority than foo
43
- code = <<-EOS
44
- module FakeFSTestRequire3_WithDotRb
45
- end
46
- EOS
47
- File.open "fake_fs_test_require3.rb", "w" do |f|
48
- f.write code
49
- end
50
- code = <<-EOS
51
- module FakeFSTestRequire3_WithoutDotRb
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
59
-
60
- # don't require files twice
61
- code = <<-EOS
62
- module FakeFSTestRequire4
63
- end
64
- EOS
65
- File.open "fake_fs_test_require4.rb", "w" do |f|
66
- f.write code
67
- end
68
- assert require("fake_fs_test_require4")
69
- assert !require("fake_fs_test_require4")
70
- FakeFS::Require.clear
71
- assert require("fake_fs_test_require4")
72
-
73
- # properly deactivate
74
- FakeFS::Require.deactivate!
75
- assert_raise LoadError do
76
- require "bar"
77
- end
78
-
79
- FakeFS::Require.clear
80
- end
81
-
82
- def test_fakes_require_with_fallback
83
- FakeFS.deactivate!
84
- require "tmpdir"
85
- FakeFS.activate!
86
-
87
- FakeFS::Require.activate! :fallback => true
88
-
89
- # load a file that's in the real (= non-faked) load path
90
- begin
91
- dir = RealDir.tmpdir + "/" + rand.to_s[2..-1]
92
- RealDir.mkdir dir
93
-
94
- $LOAD_PATH.unshift dir
95
-
96
- code = <<-EOS
97
- module FakeFSTestRequireWithFallback
98
- end
99
- EOS
100
- RealFile.open dir + "/fake_fs_test_require_with_fallback.rb", "w" do |f|
101
- f.write code
102
- end
103
-
104
- require "fake_fs_test_require_with_fallback.rb"
105
- assert FakeFSTestRequireWithFallback
106
- ensure
107
- RealFile.delete dir + "/fake_fs_test_require_with_fallback.rb"
108
- RealDir.delete dir
109
- $LOAD_PATH.delete dir
110
- end
111
-
112
- # load a file that exists neither in fakefs nor in the real load path
113
- assert_raise LoadError do
114
- require "fake_fs_test_require_with_fooback.rb"
115
- end
116
-
117
- # load a file from a gem
118
- require "rack/static.rb"
119
- assert ::Rack::Static
120
- assert_raise LoadError do
121
- require "rack/is_great"
122
- end
123
-
124
- # turned off fallback
125
- FakeFS::Require.opts[:fallback] = false
126
- assert_raise LoadError do
127
- require "rack/mime"
128
- end
129
-
130
- FakeFS::Require.deactivate!
131
- FakeFS::Require.clear
132
- end
133
-
134
- def test_fakes_autoload
135
- FakeFS::Require.activate! :autoload => true
136
-
137
- code = <<-EOS
138
- module FakeFSTestAutoload
139
- autoload :Foo, "fake_fs_test_autoload/foo"
140
- autoload :Bar, "fake_fs_test_autoload/bar"
141
- end
142
- EOS
143
- File.open "fake_fs_test_autoload.rb", "w" do |f|
144
- f.write code
145
- end
146
- code = <<-EOS
147
- module FakeFSTestAutoload
148
- module Foo
149
- end
150
- end
151
- EOS
152
- File.open "fake_fs_test_autoload/foo.rb", "w" do |f|
153
- f.write code
154
- end
155
-
156
- require "fake_fs_test_autoload"
157
-
158
- # autoload
159
- assert FakeFSTestAutoload::Foo
160
-
161
- # autoload with non-existing path
162
- assert_raise LoadError do
163
- FakeFSTestAutoload::Bar
164
- end
165
-
166
- # no autoload
167
- assert_raise NameError do
168
- FakeFSTestAutoload::Baz
169
- end
170
-
171
- FakeFS::Require.deactivate!
172
- FakeFS::Require.clear
173
- end
174
-
1
+ class RequireTest < Test::Unit::TestCase
2
+
3
+ def setup
4
+ FakeFS.activate!
5
+ end
6
+
7
+ def teardown
8
+ FakeFS::FileSystem.clear
9
+ FakeFS.deactivate!
10
+ end
11
+
12
+ def test_fakes_require
13
+ FakeFS::Require.activate!
14
+
15
+ # require a file
16
+ code = <<-EOS
17
+ module FakeFSTestRequire1
18
+ end
19
+ EOS
20
+ File.open "fake_fs_test_require1.rb", "w" do |f|
21
+ f.write code
22
+ end
23
+ require "fake_fs_test_require1.rb"
24
+ assert ::FakeFSTestRequire1
25
+
26
+ # require a file that doesn't exist
27
+ assert_raise LoadError do
28
+ require "foo"
29
+ end
30
+
31
+ # automatically append .rb
32
+ code = <<-EOS
33
+ module FakeFSTestRequire2
34
+ end
35
+ EOS
36
+ File.open "fake_fs_test_require2.rb", "w" do |f|
37
+ f.write code
38
+ end
39
+ require "fake_fs_test_require2"
40
+ assert ::FakeFSTestRequire2
41
+
42
+ # foo.rb has higher priority than foo
43
+ code = <<-EOS
44
+ module FakeFSTestRequire3_WithDotRb
45
+ end
46
+ EOS
47
+ File.open "fake_fs_test_require3.rb", "w" do |f|
48
+ f.write code
49
+ end
50
+ code = <<-EOS
51
+ module FakeFSTestRequire3_WithoutDotRb
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
59
+
60
+ # don't require files twice
61
+ code = <<-EOS
62
+ module FakeFSTestRequire4
63
+ end
64
+ EOS
65
+ File.open "fake_fs_test_require4.rb", "w" do |f|
66
+ f.write code
67
+ end
68
+ assert require("fake_fs_test_require4")
69
+ assert !require("fake_fs_test_require4")
70
+ FakeFS::Require.clear
71
+ assert require("fake_fs_test_require4")
72
+
73
+ # properly deactivate
74
+ FakeFS::Require.deactivate!
75
+ assert_raise LoadError do
76
+ require "bar"
77
+ end
78
+
79
+ FakeFS::Require.clear
80
+ end
81
+
82
+ def test_fakes_require_with_fallback
83
+ FakeFS.deactivate!
84
+ require "tmpdir"
85
+ FakeFS.activate!
86
+
87
+ FakeFS::Require.activate! :fallback => true
88
+
89
+ # load a file that's in the real (= non-faked) load path
90
+ begin
91
+ dir = RealDir.tmpdir + "/" + rand.to_s[2..-1]
92
+ RealDir.mkdir dir
93
+
94
+ $LOAD_PATH.unshift dir
95
+
96
+ code = <<-EOS
97
+ module FakeFSTestRequireWithFallback
98
+ end
99
+ EOS
100
+ RealFile.open dir + "/fake_fs_test_require_with_fallback.rb", "w" do |f|
101
+ f.write code
102
+ end
103
+
104
+ require "fake_fs_test_require_with_fallback.rb"
105
+ assert FakeFSTestRequireWithFallback
106
+ ensure
107
+ RealFile.delete dir + "/fake_fs_test_require_with_fallback.rb"
108
+ RealDir.delete dir
109
+ $LOAD_PATH.delete dir
110
+ end
111
+
112
+ # load a file that exists neither in fakefs nor in the real load path
113
+ assert_raise LoadError do
114
+ require "fake_fs_test_require_with_fooback.rb"
115
+ end
116
+
117
+ # load a file from a gem
118
+ require "rack/static.rb"
119
+ assert ::Rack::Static
120
+ assert_raise LoadError do
121
+ require "rack/is_great"
122
+ end
123
+
124
+ # turned off fallback
125
+ FakeFS::Require.opts[:fallback] = false
126
+ assert_raise LoadError do
127
+ require "rack/mime"
128
+ end
129
+
130
+ FakeFS::Require.deactivate!
131
+ FakeFS::Require.clear
132
+ end
133
+
134
+ def test_fakes_autoload
135
+ FakeFS::Require.activate! :autoload => true
136
+
137
+ code = <<-EOS
138
+ module FakeFSTestAutoload
139
+ autoload :Foo, "fake_fs_test_autoload/foo"
140
+ autoload :Bar, "fake_fs_test_autoload/bar"
141
+ end
142
+ EOS
143
+ File.open "fake_fs_test_autoload.rb", "w" do |f|
144
+ f.write code
145
+ end
146
+ code = <<-EOS
147
+ module FakeFSTestAutoload
148
+ module Foo
149
+ end
150
+ end
151
+ EOS
152
+ File.open "fake_fs_test_autoload/foo.rb", "w" do |f|
153
+ f.write code
154
+ end
155
+
156
+ require "fake_fs_test_autoload"
157
+
158
+ # autoload
159
+ assert FakeFSTestAutoload::Foo
160
+
161
+ # autoload with non-existing path
162
+ assert_raise LoadError do
163
+ FakeFSTestAutoload::Bar
164
+ end
165
+
166
+ # no autoload
167
+ assert_raise NameError do
168
+ FakeFSTestAutoload::Baz
169
+ end
170
+
171
+ FakeFS::Require.deactivate!
172
+ FakeFS::Require.clear
173
+ end
174
+
175
175
  end
metadata CHANGED
@@ -1,12 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fakefs-require
3
3
  version: !ruby/object:Gem::Version
4
- prerelease: false
5
- segments:
6
- - 0
7
- - 1
8
- - 1
9
- version: 0.1.1
4
+ version: 0.1.2
10
5
  platform: ruby
11
6
  authors:
12
7
  - Lars Gierth
@@ -14,22 +9,20 @@ autorequire:
14
9
  bindir: bin
15
10
  cert_chain: []
16
11
 
17
- date: 2010-03-20 00:00:00 +01:00
12
+ date: 2010-03-30 00:00:00 +02:00
18
13
  default_executable:
19
14
  dependencies:
20
15
  - !ruby/object:Gem::Dependency
21
16
  name: fakefs
22
- prerelease: false
23
- requirement: &id001 !ruby/object:Gem::Requirement
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
24
20
  requirements:
25
21
  - - ">="
26
22
  - !ruby/object:Gem::Version
27
- segments:
28
- - 0
29
23
  version: "0"
30
- type: :runtime
31
- version_requirements: *id001
32
- description: "Faked #require and #autoload for defunkt's FakeFS"
24
+ version:
25
+ description:
33
26
  email: lars.gierth@altefeuerwachekoeln.de
34
27
  executables: []
35
28
 
@@ -60,20 +53,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
60
53
  requirements:
61
54
  - - ">="
62
55
  - !ruby/object:Gem::Version
63
- segments:
64
- - 0
65
56
  version: "0"
57
+ version:
66
58
  required_rubygems_version: !ruby/object:Gem::Requirement
67
59
  requirements:
68
60
  - - ">="
69
61
  - !ruby/object:Gem::Version
70
- segments:
71
- - 0
72
62
  version: "0"
63
+ version:
73
64
  requirements: []
74
65
 
75
66
  rubyforge_project:
76
- rubygems_version: 1.3.6
67
+ rubygems_version: 1.3.5
77
68
  signing_key:
78
69
  specification_version: 3
79
70
  summary: "Faked #require and #autoload for defunkt's FakeFS"