-
-
Notifications
You must be signed in to change notification settings - Fork 386
/
require_spec.rb
282 lines (221 loc) · 8.91 KB
/
require_spec.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
require File.dirname(__FILE__) + '/../../spec_helper'
$require_fixture_dir = (File.dirname(__FILE__) + '/../../fixtures/require')
$LOAD_PATH << $require_fixture_dir
$require_spec = nil
$require_spec_1 = nil
$require_spec_2 = nil
$require_spec_3 = nil
$require_spec_4 = nil
$require_spec_5 = nil
$require_spec_6 = nil
$require_spec_7 = nil
$require_spec_8 = nil
$require_spec_9 = nil
$require_spec_10 = nil
$require_spec_rooby = nil
$require_spec_recursive = nil
require 'rbconfig'
describe "Kernel#require" do
runner_is_not :rspec do
# RSpec requires rubygems, which redefines #require without setting
# it's visibility back to module_function or private
it "is a private method" do
Kernel.private_instance_methods.should include("require")
end
end
# Avoid storing .rbc in repo
before :all do
Dir.chdir($require_fixture_dir) {
`rm -f ./*.rbc`
`touch require_spec_dummy.#{Config::CONFIG['DLEXT']}`
`touch require_spec_dummy.rb`
}
end
# The files used below just contain code that assigns
# Time.now to the respective global variable so that
# reloads can easily be verified.
# CAUTION: Some of these work because a different path is used to
# load the same file. Be careful if you change the order
# or add items.
it "loads a .rb from an absolute path and returns true" do
path = File.expand_path(File.dirname(__FILE__) + '/../../fixtures/require/require_spec_1.rb')
require(path).should == true
$require_spec_1.nil?.should == false
end
it "loads a .rb from a relative path and returns true" do
Dir.chdir($require_fixture_dir) do |dir|
$require_spec_1 = nil
require('../../fixtures/require/require_spec_1.rb').should == true
$require_spec_1.nil?.should == false
$require_spec_1 = nil
require('./../require/require_spec_1.rb').should == true
$require_spec_1.nil?.should == false
end
end
it "loads an unqualified .rb by looking in $LOAD_PATH and returns true" do
require('require_spec_2.rb').should == true
$require_spec_2.nil?.should == false
end
it "loads extension files" do
# TODO: Not sure how to spec this yet since it needs an extfile
end
it "does not expand/resolve qualified files against $LOAD_PATH" do
num_features = $LOADED_FEATURES.size
Dir.chdir($require_fixture_dir + '/../') do |dir|
# This would be a valid path if expanded against the fixture dir
lambda { require '../require/require_spec_2.rb' }.should raise_error LoadError
end
$LOADED_FEATURES.size.should == num_features
end
it "allows unqualified files to contain path information (just not in the beginning)" do
name = (File.dirname(__FILE__) + '/../../fixtures')
$LOAD_PATH << name
$require_spec2 = nil
require('require/../require/require_spec_2.rb').should == true
$require_spec_2.nil?.should == false
$LOAD_PATH.delete name
end
it "appends a file with no extension with .rb/.<ext> in that order to locate file" do
load('require_spec')
$require_spec.should == :noext
load('require_spec.rb')
$require_spec.should == :rb
$require_spec = nil
require('require_spec')
$require_spec.should == :rb
end
it "prefers to use .rb over .<ext> if given non-extensioned file and both exist" do
require('require_spec_dummy').should == true
$LOADED_FEATURES.include?('require_spec_dummy.rb').should == true
$LOADED_FEATURES.include?("require_spec_dummy.#{Config::CONFIG['DLEXT']}").should == false
end
runner_is_not :rspec do
it "will not add a bad load to LOADED_FEATURES" do
lambda { require('require_spec_raises') }.should raise_error(RuntimeError)
$LOADED_FEATURES.include?('require_spec_raises.rb').should == false
end
end
it "will load file.rb when given 'file' if it exists even if file.<ext> is loaded" do
$LOADED_FEATURES << "require_spec_3.#{Config::CONFIG['DLEXT']}"
require('require_spec_3.rb').should == true
$LOADED_FEATURES.include?('require_spec_3.rb').should == true
end
it "will not load file.<ext> when given 'file' if file.rb already loaded" do
unless $LOADED_FEATURES.include?('require_spec_dummy.rb')
require('require_spec_dummy.rb')
end
require('require_spec_dummy').should == false
end
it "will load explicit file.<ext> even if file.rb already loaded and vice versa" do
# Not sure how to spec this yet because it needs an extfile.
end
it "appends any non-ruby extensioned file with .rb/.<ext> in that order to locate file" do
load('require_spec.rooby')
$require_spec_rooby.should == :rooby
load('require_spec.rooby.rb')
$require_spec_rooby.should == :rb
$require_spec_rooby = nil
require('require_spec.rooby')
$require_spec_rooby.should == :rb
end
it "produces __FILE__ as the given filename and __LINE__ as the source line number" do
Dir.chdir($require_fixture_dir) do |dir|
require('require_spec_4').should == true
$require_spec_4.should == [['./require_spec_4.rb', 1], ['./require_spec_4.rb', 10]]
extended_on :rubinius do
`rm require_spec_4.rbc`
end
end
$require_spec_4 = nil
require("#{$require_fixture_dir}/require_spec_4").should == true
$require_spec_4[0][0].should =~ %r[^.*/fixtures/require/require_spec_4.rb]
$require_spec_4[0][1].should == 1
$require_spec_4[1][0].should =~ %r[^.*/fixtures/require/require_spec_4.rb]
$require_spec_4[1][1].should == 10
end
it "stores the loaded file in $LOADED_FEATURES" do
$LOADED_FEATURES.include?('require_spec_6.rb').should == false
require('require_spec_6.rb').should == true
$LOADED_FEATURES.include?('require_spec_6.rb').should == true
end
it "stores a non-extensioned file with its located suffix" do
$LOADED_FEATURES.delete 'require_spec_6.rb'
require('require_spec_6').should == true
$LOADED_FEATURES.include?('require_spec_6.rb').should == true
end
it "bases the filename in $LOADED_FEATURES on the path given, not just basename" do
$LOADED_FEATURES.include?('require_spec_6.rb').should == true
Dir.chdir($require_fixture_dir) do |dir|
require('../../fixtures/require/require_spec_6.rb').should == true
end
$LOADED_FEATURES.include?('require_spec_6.rb').should == true
$LOADED_FEATURES.include?('../../fixtures/require/require_spec_6.rb').should == true
end
it "will not load the same file twice, returns false instead" do
$LOADED_FEATURES.include?('require_spec_7.rb').should == false
require('require_spec_7.rb').should == true
a = $require_spec_7
a.nil?.should == false
$LOADED_FEATURES.include?('require_spec_7.rb').should == true
require('require_spec_7.rb').should == false
b = $require_spec_7
b.nil?.should == false
# Timestamps should not differ
a.eql?(b).should == true
$LOADED_FEATURES.include?('require_spec_7.rb').should == true
end
it "checks $LOADED_FEATURES to see whether file is already loaded" do
$LOADED_FEATURES.include?('require_spec_7.rb').should == true
require('require_spec_7.rb').should == false
$LOADED_FEATURES.delete 'require_spec_7.rb'
require('require_spec_7.rb').should == true
require('require_spec_7.rb').should == false
$LOADED_FEATURES.include?('require_spec_7.rb').should == true
end
it "raises a LoadError if the file can't be found" do
lambda { require "nonesuch#{$$}#{Time.now.to_f}" }.should raise_error LoadError
end
it "only accepts strings" do
lambda { require(nil) }.should raise_error(TypeError)
lambda { require(42) }.should raise_error(TypeError)
lambda { require([]) }.should raise_error(TypeError)
end
it "does not infinite loop on an rb file that requires itself" do
$require_spec_recursive = nil
$LOADED_FEATURES.delete 'require_spec_recursive.rb'
$LOADED_FEATURES.include?('require_spec_recursive.rb').should == false
require('require_spec_recursive').should == true
$LOADED_FEATURES.include?('require_spec_recursive.rb').should == true
$require_spec_recursive.nil?.should == false
end
end
describe "Shell expansion in Kernel#require" do
before :all do
@rs_home = ENV["HOME"]
ENV["HOME"] = $require_fixture_dir
@rs_short = "~/require_spec_1.rb"
@rs_long = "#{$require_fixture_dir}/require_spec_1.rb"
end
after :all do
ENV["HOME"] = @rs_home
end
before :each do
$LOADED_FEATURES.delete @rs_long
$LOADED_FEATURES.delete @rs_short
end
it "expands a preceding ~/ to the user's home directory for building the path to search" do
$require_spec_1 = nil
require(@rs_short).should == true
$require_spec_1.nil?.should == false
end
it "adds the path to $LOADED_FEATURES" do
$require_spec_1 = nil
require(@rs_short).should == true
$require_spec_1.nil?.should == false
$LOADED_FEATURES.find {|f| f == @rs_short || f == @rs_long }.nil?.should == false
end
end
describe "Kernel.require" do
it "needs to be reviewed for spec completeness" do
end
end