fakefs-require 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,3 @@
1
+ /.loadpath
2
+ /.project
3
+ /test.rb
data/LICENSE ADDED
@@ -0,0 +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
19
+ THE SOFTWARE.
@@ -0,0 +1,92 @@
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)
@@ -0,0 +1,29 @@
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"
29
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.1.1
@@ -0,0 +1,158 @@
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
@@ -0,0 +1,2 @@
1
+ require "fakefs/safe"
2
+ require "fakefs/require"
@@ -0,0 +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
+
175
+ end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fakefs-require
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
10
+ platform: ruby
11
+ authors:
12
+ - Lars Gierth
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-03-20 00:00:00 +01:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: fakefs
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ type: :runtime
31
+ version_requirements: *id001
32
+ description: "Faked #require and #autoload for defunkt's FakeFS"
33
+ email: lars.gierth@altefeuerwachekoeln.de
34
+ executables: []
35
+
36
+ extensions: []
37
+
38
+ extra_rdoc_files:
39
+ - LICENSE
40
+ - README.markdown
41
+ files:
42
+ - .gitignore
43
+ - LICENSE
44
+ - README.markdown
45
+ - Rakefile
46
+ - VERSION
47
+ - lib/fakefs/require.rb
48
+ - test/helper.rb
49
+ - test/require_test.rb
50
+ has_rdoc: true
51
+ homepage: http://github.com/lgierth/fakefs-require
52
+ licenses: []
53
+
54
+ post_install_message:
55
+ rdoc_options:
56
+ - --charset=UTF-8
57
+ require_paths:
58
+ - lib
59
+ required_ruby_version: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - ">="
62
+ - !ruby/object:Gem::Version
63
+ segments:
64
+ - 0
65
+ version: "0"
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ segments:
71
+ - 0
72
+ version: "0"
73
+ requirements: []
74
+
75
+ rubyforge_project:
76
+ rubygems_version: 1.3.6
77
+ signing_key:
78
+ specification_version: 3
79
+ summary: "Faked #require and #autoload for defunkt's FakeFS"
80
+ test_files:
81
+ - test/helper.rb
82
+ - test/require_test.rb