diff --git a/core/dir/shared/chroot.rb b/core/dir/shared/chroot.rb index 8c0599fe3f..a8f7c10a19 100644 --- a/core/dir/shared/chroot.rb +++ b/core/dir/shared/chroot.rb @@ -2,7 +2,7 @@ before :all do DirSpecs.create_mock_dirs - @real_root = "../" * (File.dirname(__FILE__).count('/') - 1) + @real_root = "../" * (__dir__.count('/') - 1) @ref_dir = File.join("/", File.basename(Dir["/*"].first)) end @@ -18,7 +18,7 @@ compilations_ci = ENV["GITHUB_WORKFLOW"] == "Compilations" it "can be used to change the process' root directory" do - -> { Dir.send(@method, File.dirname(__FILE__)) }.should_not raise_error + -> { Dir.send(@method, __dir__) }.should_not raise_error File.should.exist?("/#{File.basename(__FILE__)}") end unless compilations_ci diff --git a/core/dir/shared/exist.rb b/core/dir/shared/exist.rb index 765d1b656c..2ea4f88a80 100644 --- a/core/dir/shared/exist.rb +++ b/core/dir/shared/exist.rb @@ -1,6 +1,6 @@ describe :dir_exist, shared: true do it "returns true if the given directory exists" do - Dir.send(@method, File.dirname(__FILE__)).should be_true + Dir.send(@method, __dir__).should be_true end it "returns true for '.'" do @@ -20,7 +20,7 @@ end it "understands relative paths" do - Dir.send(@method, File.dirname(__FILE__) + '/../').should be_true + Dir.send(@method, __dir__ + '/../').should be_true end it "returns false if the given directory doesn't exist" do @@ -28,7 +28,7 @@ end it "doesn't require the name to have a trailing slash" do - dir = File.dirname(__FILE__) + dir = __dir__ dir.sub!(/\/$/,'') Dir.send(@method, dir).should be_true end @@ -50,7 +50,7 @@ it "calls #to_path on non String arguments" do p = mock('path') - p.should_receive(:to_path).and_return(File.dirname(__FILE__)) + p.should_receive(:to_path).and_return(__dir__) Dir.send(@method, p) end end diff --git a/core/exception/equal_value_spec.rb b/core/exception/equal_value_spec.rb index 7f2065511a..e8f3ce0f8d 100644 --- a/core/exception/equal_value_spec.rb +++ b/core/exception/equal_value_spec.rb @@ -22,18 +22,18 @@ it "returns true if both exceptions have the same class, the same message, and the same backtrace" do one = TypeError.new("message") - one.set_backtrace [File.dirname(__FILE__)] + one.set_backtrace [__dir__] two = TypeError.new("message") - two.set_backtrace [File.dirname(__FILE__)] + two.set_backtrace [__dir__] one.should == two end it "returns false if the two exceptions inherit from Exception but have different classes" do one = RuntimeError.new("message") - one.set_backtrace [File.dirname(__FILE__)] + one.set_backtrace [__dir__] one.should be_kind_of(Exception) two = TypeError.new("message") - two.set_backtrace [File.dirname(__FILE__)] + two.set_backtrace [__dir__] two.should be_kind_of(Exception) one.should_not == two end @@ -52,7 +52,7 @@ it "returns false if the two exceptions differ only in their backtrace" do one = RuntimeError.new("message") - one.set_backtrace [File.dirname(__FILE__)] + one.set_backtrace [__dir__] two = RuntimeError.new("message") two.set_backtrace nil one.should_not == two @@ -60,9 +60,9 @@ it "returns false if the two exceptions differ only in their message" do one = RuntimeError.new("message") - one.set_backtrace [File.dirname(__FILE__)] + one.set_backtrace [__dir__] two = RuntimeError.new("message2") - two.set_backtrace [File.dirname(__FILE__)] + two.set_backtrace [__dir__] one.should_not == two end end diff --git a/core/file/absolute_path_spec.rb b/core/file/absolute_path_spec.rb index e35c80ec3c..315eead34f 100644 --- a/core/file/absolute_path_spec.rb +++ b/core/file/absolute_path_spec.rb @@ -85,7 +85,7 @@ end it "accepts a second argument of a directory from which to resolve the path" do - File.absolute_path(__FILE__, File.dirname(__FILE__)).should == @abs + File.absolute_path(__FILE__, __dir__).should == @abs end it "calls #to_path on its argument" do diff --git a/core/io/eof_spec.rb b/core/io/eof_spec.rb index 315345d942..b4850df437 100644 --- a/core/io/eof_spec.rb +++ b/core/io/eof_spec.rb @@ -76,7 +76,7 @@ end it "returns true on one-byte stream after single-byte read" do - File.open(File.dirname(__FILE__) + '/fixtures/one_byte.txt') { |one_byte| + File.open(__dir__ + '/fixtures/one_byte.txt') { |one_byte| one_byte.read(1) one_byte.should.eof? } diff --git a/core/kernel/require_relative_spec.rb b/core/kernel/require_relative_spec.rb index 5999855de6..6188d13a4e 100644 --- a/core/kernel/require_relative_spec.rb +++ b/core/kernel/require_relative_spec.rb @@ -5,9 +5,9 @@ before :each do CodeLoadingSpecs.spec_setup @dir = "../../fixtures/code" - @abs_dir = File.realpath(@dir, File.dirname(__FILE__)) + @abs_dir = File.realpath(@dir, __dir__) @path = "#{@dir}/load_fixture.rb" - @abs_path = File.realpath(@path, File.dirname(__FILE__)) + @abs_path = File.realpath(@path, __dir__) end after :each do @@ -92,7 +92,7 @@ it "raises a LoadError that includes the missing path" do missing_path = "#{@dir}/nonexistent.rb" - expanded_missing_path = File.expand_path(missing_path, File.dirname(__FILE__)) + expanded_missing_path = File.expand_path(missing_path, __dir__) -> { require_relative(missing_path) }.should raise_error(LoadError) { |e| e.message.should include(expanded_missing_path) e.path.should == expanded_missing_path @@ -277,7 +277,7 @@ describe "Kernel#require_relative with an absolute path" do before :each do CodeLoadingSpecs.spec_setup - @dir = File.expand_path "../../fixtures/code", File.dirname(__FILE__) + @dir = File.expand_path "../../fixtures/code", __dir__ @abs_dir = @dir @path = File.join @dir, "load_fixture.rb" @abs_path = @path diff --git a/core/kernel/test_spec.rb b/core/kernel/test_spec.rb index abb365aed2..d26dc06361 100644 --- a/core/kernel/test_spec.rb +++ b/core/kernel/test_spec.rb @@ -3,8 +3,8 @@ describe "Kernel#test" do before :all do - @file = File.dirname(__FILE__) + '/fixtures/classes.rb' - @dir = File.dirname(__FILE__) + '/fixtures' + @file = __dir__ + '/fixtures/classes.rb' + @dir = __dir__ + '/fixtures' end it "is a private method" do diff --git a/core/process/argv0_spec.rb b/core/process/argv0_spec.rb index 7c2342f959..f5aba719e9 100644 --- a/core/process/argv0_spec.rb +++ b/core/process/argv0_spec.rb @@ -8,7 +8,7 @@ it "is the path given as the main script and the same as __FILE__" do script = "fixtures/argv0.rb" - Dir.chdir(File.dirname(__FILE__)) do + Dir.chdir(__dir__) do ruby_exe(script).should == "#{script}\n#{script}\nOK" end end diff --git a/core/process/exec_spec.rb b/core/process/exec_spec.rb index deb8913b6b..2fa8b08975 100644 --- a/core/process/exec_spec.rb +++ b/core/process/exec_spec.rb @@ -30,7 +30,7 @@ end it "raises Errno::EACCES when passed a directory" do - -> { Process.exec File.dirname(__FILE__) }.should raise_error(Errno::EACCES) + -> { Process.exec __dir__ }.should raise_error(Errno::EACCES) end it "runs the specified command, replacing current process" do diff --git a/core/process/spawn_spec.rb b/core/process/spawn_spec.rb index c8a58c4d04..283a7f033d 100644 --- a/core/process/spawn_spec.rb +++ b/core/process/spawn_spec.rb @@ -714,7 +714,7 @@ def child_pids(pid) end it "raises an Errno::EACCES or Errno::EISDIR when passed a directory" do - -> { Process.spawn File.dirname(__FILE__) }.should raise_error(SystemCallError) { |e| + -> { Process.spawn __dir__ }.should raise_error(SystemCallError) { |e| [Errno::EACCES, Errno::EISDIR].should include(e.class) } end diff --git a/core/thread/backtrace/location/path_spec.rb b/core/thread/backtrace/location/path_spec.rb index 7863c055d3..75f76833a9 100644 --- a/core/thread/backtrace/location/path_spec.rb +++ b/core/thread/backtrace/location/path_spec.rb @@ -41,7 +41,7 @@ context 'when using a relative script path' do it 'returns a path relative to the working directory' do path = 'fixtures/main.rb' - directory = File.dirname(__FILE__) + directory = __dir__ Dir.chdir(directory) { ruby_exe(path) }.should == path diff --git a/language/predefined_spec.rb b/language/predefined_spec.rb index 6f779b55f5..fe865cc325 100644 --- a/language/predefined_spec.rb +++ b/language/predefined_spec.rb @@ -1037,7 +1037,7 @@ def obj.foo2; yield; end it "is the path given as the main script and the same as __FILE__" do script = "fixtures/dollar_zero.rb" - Dir.chdir(File.dirname(__FILE__)) do + Dir.chdir(__dir__) do ruby_exe(script).should == "#{script}\n#{script}\nOK" end end diff --git a/library/net/ftp/shared/getbinaryfile.rb b/library/net/ftp/shared/getbinaryfile.rb index 71f226089d..ceec8e7cd5 100644 --- a/library/net/ftp/shared/getbinaryfile.rb +++ b/library/net/ftp/shared/getbinaryfile.rb @@ -1,6 +1,6 @@ describe :net_ftp_getbinaryfile, shared: true do before :each do - @fixture_file = File.dirname(__FILE__) + "/../fixtures/getbinaryfile" + @fixture_file = __dir__ + "/../fixtures/getbinaryfile" @tmp_file = tmp("getbinaryfile") @server = NetFTPSpecs::DummyFTP.new diff --git a/library/net/ftp/shared/putbinaryfile.rb b/library/net/ftp/shared/putbinaryfile.rb index 7dddcbc26b..45f53adc2a 100644 --- a/library/net/ftp/shared/putbinaryfile.rb +++ b/library/net/ftp/shared/putbinaryfile.rb @@ -3,7 +3,7 @@ @server = NetFTPSpecs::DummyFTP.new @server.serve_once - @local_fixture_file = File.dirname(__FILE__) + "/../fixtures/putbinaryfile" + @local_fixture_file = __dir__ + "/../fixtures/putbinaryfile" @remote_tmp_file = tmp("binaryfile", false) @ftp = Net::FTP.new diff --git a/library/net/ftp/shared/puttextfile.rb b/library/net/ftp/shared/puttextfile.rb index 50e8de28e6..3836e954b8 100644 --- a/library/net/ftp/shared/puttextfile.rb +++ b/library/net/ftp/shared/puttextfile.rb @@ -3,7 +3,7 @@ @server = NetFTPSpecs::DummyFTP.new @server.serve_once - @local_fixture_file = File.dirname(__FILE__) + "/../fixtures/puttextfile" + @local_fixture_file = __dir__ + "/../fixtures/puttextfile" @remote_tmp_file = tmp("textfile", false) @ftp = Net::FTP.new diff --git a/library/net/ftp/storbinary_spec.rb b/library/net/ftp/storbinary_spec.rb index 64c9090760..6f73344612 100644 --- a/library/net/ftp/storbinary_spec.rb +++ b/library/net/ftp/storbinary_spec.rb @@ -9,7 +9,7 @@ @server = NetFTPSpecs::DummyFTP.new @server.serve_once - @local_fixture_file = File.dirname(__FILE__) + "/fixtures/putbinaryfile" + @local_fixture_file = __dir__ + "/fixtures/putbinaryfile" @tmp_file = tmp("binaryfile", false) @ftp = Net::FTP.new diff --git a/library/net/ftp/storlines_spec.rb b/library/net/ftp/storlines_spec.rb index a4bb7af799..32b9448732 100644 --- a/library/net/ftp/storlines_spec.rb +++ b/library/net/ftp/storlines_spec.rb @@ -9,7 +9,7 @@ @server = NetFTPSpecs::DummyFTP.new @server.serve_once - @local_fixture_file = File.dirname(__FILE__) + "/fixtures/puttextfile" + @local_fixture_file = __dir__ + "/fixtures/puttextfile" @tmp_file = tmp("textfile", false) @ftp = Net::FTP.new diff --git a/library/yaml/fixtures/common.rb b/library/yaml/fixtures/common.rb index f7fb4037e7..895213b844 100644 --- a/library/yaml/fixtures/common.rb +++ b/library/yaml/fixtures/common.rb @@ -1,4 +1,4 @@ require 'yaml' $test_file = tmp("yaml_test_file") -$test_parse_file = File.dirname(__FILE__) + "/test_yaml.yml" +$test_parse_file = __dir__ + "/test_yaml.yml" diff --git a/optional/capi/object_spec.rb b/optional/capi/object_spec.rb index 9efc892202..6ee6d65680 100644 --- a/optional/capi/object_spec.rb +++ b/optional/capi/object_spec.rb @@ -219,7 +219,7 @@ def six(a, b, *c, &d); end end it "requires a ruby file" do - $:.unshift File.dirname(__FILE__) + $:.unshift __dir__ @o.rb_require() $foo.should == 7 end diff --git a/spec_helper.rb b/spec_helper.rb index 3404521c03..af1c385878 100644 --- a/spec_helper.rb +++ b/spec_helper.rb @@ -1,5 +1,5 @@ use_realpath = File.respond_to?(:realpath) -root = File.dirname(__FILE__) +root = __dir__ dir = "fixtures/code" CODE_LOADING_DIR = use_realpath ? File.realpath(dir, root) : File.expand_path(dir, root)