Skip to content

Commit

Permalink
Replace File.dirname(__FILE__) with __dir__
Browse files Browse the repository at this point in the history
Which reads a bit easier. The only occurence left is in the specs of
__dir__ itself.
  • Loading branch information
herwinw authored and eregon committed Sep 4, 2023
1 parent 7a65447 commit e6fb9b8
Show file tree
Hide file tree
Showing 20 changed files with 34 additions and 34 deletions.
4 changes: 2 additions & 2 deletions core/dir/shared/chroot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down
8 changes: 4 additions & 4 deletions core/dir/shared/exist.rb
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -20,15 +20,15 @@
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
Dir.send(@method, 'y26dg27n2nwjs8a/').should be_false
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
Expand All @@ -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
14 changes: 7 additions & 7 deletions core/exception/equal_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -52,17 +52,17 @@

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
end

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
2 changes: 1 addition & 1 deletion core/file/absolute_path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/io/eof_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
}
Expand Down
8 changes: 4 additions & 4 deletions core/kernel/require_relative_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions core/kernel/test_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/process/argv0_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/process/exec_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/process/spawn_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion core/thread/backtrace/location/path_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion language/predefined_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/net/ftp/shared/getbinaryfile.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/net/ftp/shared/putbinaryfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/net/ftp/shared/puttextfile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/net/ftp/storbinary_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/net/ftp/storlines_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion library/yaml/fixtures/common.rb
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion optional/capi/object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion spec_helper.rb
Original file line number Diff line number Diff line change
@@ -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)

Expand Down

0 comments on commit e6fb9b8

Please sign in to comment.