Bandaid fixes for CI to spec/core/kernel specs. Further work needed.
[rbx.git] / spec / core / kernel / require_spec.rb
blob9fe554b75339d8ca9c7201c426972682bad8b0c6
1 require File.dirname(__FILE__) + '/../../spec_helper'
3 # FIXME: somehow make these fixture dirs independent of the
4 # rubyspec submodule dir in spec/frozen, otherwise these specs
5 # won't behave the same with the clone of rubyspecs in spec/ruby.
6 $require_fixture_dir = (File.dirname(__FILE__) + '/../../frozen/1.8/fixtures/require')
7 $LOAD_PATH << $require_fixture_dir
8 $LOAD_PATH << (File.dirname(__FILE__) + '/../../frozen/1.8/fixtures/require/require_spec_rba.rba')
10 $require_spec   = nil
11 $require_spec_1 = nil
12 $require_spec_2 = nil
13 $require_spec_3 = nil
14 $require_spec_4 = nil
15 $require_spec_5 = nil
16 $require_spec_6 = nil
17 $require_spec_7 = nil
18 $require_spec_8 = nil
19 $require_spec_9 = nil
20 $require_spec_10 = nil
21 $require_spec_rooby = nil
23 require 'rbconfig'
25 describe "Kernel#require" do
26   # Avoid storing .rbc and .rba in repo
27   before :all do
28     Dir.chdir($require_fixture_dir) {
29       `rm -f ./*.rbc`
30       `touch require_spec_dummy.#{Config::CONFIG['DLEXT']}`
31       `touch require_spec_dummy.rb`
32     }
34     Kernel.compile("#{$require_fixture_dir}/require_spec_10.rb")
36     Dir.chdir($require_fixture_dir) {
37       system "rm -f reqire_spec_rba.rba"
38       system "ar r reqire_spec_rba.rba reqire_spec_10.rbc 2>/dev/null"
39     }
40   end
42   it "loads a .rbc file directly" do
43     $LOADED_FEATURES.delete 'require_spec_2.rb'
44     load('require_spec_2.rb')
45     require('require_spec_2.rbc').should == true
46     $LOADED_FEATURES.include?('require_spec_2.rb').should == true
47   end
49   it "compiles a .rbc file when re-evaluating the source file" do
50     Dir.chdir($require_fixture_dir) do |dir|
51       system("rm -f require_spec_8.rbc")
52     end
54     File.exist?("#{$require_fixture_dir}/require_spec_8.rbc").should == false
56     require('require_spec_8.rb').should == true
58     File.exist?("#{$require_fixture_dir}/require_spec_8.rbc").should == true
59   end
61   it "loads a .rbc file if it's not older than the associated .rb file" do
62     begin
63       time = Time.now
65       Dir.chdir($require_fixture_dir) do |dir|
66         File.open('require_spec_dynamic.rb', 'w+') do |f| 
67           f.puts "$require_spec_dynamic = [#{time.tv_sec}, #{time.tv_usec}]" 
68         end
69       end
71       Kernel.compile "#{$require_fixture_dir}/require_spec_dynamic.rb"
73       require('require_spec_dynamic.rb').should == true
74       $require_spec_dynamic.should == [time.tv_sec, time.tv_usec]
76       $LOADED_FEATURES.delete 'require_spec_dynamic.rb'
78       require('require_spec_dynamic.rb').should == true
79       $require_spec_dynamic.should == [time.tv_sec, time.tv_usec]
81       require('require_spec_dynamic.rb').should == false
82       $require_spec_dynamic.should == [time.tv_sec, time.tv_usec]
84       time2 = Time.now
86       Dir.chdir($require_fixture_dir) do |dir|
87         `mv require_spec_dynamic.rbc rsd.old`
89         File.open('require_spec_dynamic.rb', 'w+') do |f| 
90           f.puts "$require_spec_dynamic = [#{time2.tv_sec}, #{time2.tv_usec}]" 
91         end
92       end
94       $LOADED_FEATURES.delete 'require_spec_dynamic.rb'
96       require('require_spec_dynamic.rb').should == true
97       $require_spec_dynamic.should == [time2.tv_sec, time2.tv_usec]
99       Dir.chdir($require_fixture_dir) do |dir|
100         `mv rsd.old require_spec_dynamic.rbc`
101         `touch require_spec_dynamic.rbc`
102       end
104       $LOADED_FEATURES.delete 'require_spec_dynamic.rb'
106       require('require_spec_dynamic.rb').should == true
107       $require_spec_dynamic.should == [time.tv_sec, time.tv_usec]
109     ensure
110       Dir.chdir($require_fixture_dir) do |dir|
111         `rm -f ./require_dynamic.rb*`
112       end
113     end
114   end
116   it "loads a .rbc even if the .rb is missing" do
117     begin 
118       Kernel.compile "#{$require_fixture_dir}/require_spec_9.rb"
120       Dir.chdir($require_fixture_dir) do |dir|
121         `mv require_spec_9.rb ls9.old`
122       end
124       require('require_spec_9.rb').should == true
126     ensure
127       Dir.chdir($require_fixture_dir) do |dir|
128         system "mv ls9.old require_spec_9.rb"
129       end
130     end
131   end
133   it "does not recursively load an rbc file that requires itself" do
134     $require_spec_recursive = nil
135     $LOADED_FEATURES.delete 'require_spec_recursive.rb'
137     begin
138       Kernel.compile("#{$require_fixture_dir}/require_spec_recursive.rb")
140       Dir.chdir($require_fixture_dir) do
141         `mv require_spec_recursive.rb tmp1234`
142       end
144       $LOADED_FEATURES.include?('require_spec_recursive.rb').should == false
146       require('require_spec_recursive').should == true
148       $LOADED_FEATURES.include?('require_spec_recursive.rb').should == true
149       $require_spec_recursive.nil?.should == false
151     ensure
152       Dir.chdir($require_fixture_dir) do
153         `mv tmp1234 require_spec_recursive.rb`
154       end
155     end
156   end
158   it "loads a .rb from a .rba in $LOAD_PATH" do
159     require('require_spec_10.rb').should == true
160     $require_spec_10.nil?.should == false
161   end
163   it "loads a .rb from a .rba in $LOAD_PATH, only once" do
164     $LOADED_FEATURES.delete 'require_spec_10.rb'
165     
166     require('require_spec_10.rb').should == true
167     a = $require_spec_2
168     a.nil?.should == false
170     require('require_spec_2').should == false
171     b = $require_spec_2
173     a.eql?(b).should == true
174   end