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')
20 $require_spec_10 = nil
21 $require_spec_rooby = nil
25 describe "Kernel#require" do
26 # Avoid storing .rbc and .rba in repo
28 Dir.chdir($require_fixture_dir) {
30 `touch require_spec_dummy.#{Config::CONFIG['DLEXT']}`
31 `touch require_spec_dummy.rb`
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"
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
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")
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
61 it "loads a .rbc file if it's not older than the associated .rb file" do
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}]"
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]
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}]"
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`
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]
110 Dir.chdir($require_fixture_dir) do |dir|
111 `rm -f ./require_dynamic.rb*`
116 it "loads a .rbc even if the .rb is missing" do
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`
124 require('require_spec_9.rb').should == true
127 Dir.chdir($require_fixture_dir) do |dir|
128 system "mv ls9.old require_spec_9.rb"
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'
138 Kernel.compile("#{$require_fixture_dir}/require_spec_recursive.rb")
140 Dir.chdir($require_fixture_dir) do
141 `mv require_spec_recursive.rb tmp1234`
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
152 Dir.chdir($require_fixture_dir) do
153 `mv tmp1234 require_spec_recursive.rb`
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
163 it "loads a .rb from a .rba in $LOAD_PATH, only once" do
164 $LOADED_FEATURES.delete 'require_spec_10.rb'
166 require('require_spec_10.rb').should == true
168 a.nil?.should == false
170 require('require_spec_2').should == false
173 a.eql?(b).should == true