Skip to content

Commit e6fb9b8

Browse files
herwinweregon
authored andcommitted
Replace File.dirname(__FILE__) with __dir__
Which reads a bit easier. The only occurence left is in the specs of __dir__ itself.
1 parent 7a65447 commit e6fb9b8

File tree

20 files changed

+34
-34
lines changed

20 files changed

+34
-34
lines changed

core/dir/shared/chroot.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
before :all do
33
DirSpecs.create_mock_dirs
44

5-
@real_root = "../" * (File.dirname(__FILE__).count('/') - 1)
5+
@real_root = "../" * (__dir__.count('/') - 1)
66
@ref_dir = File.join("/", File.basename(Dir["/*"].first))
77
end
88

@@ -18,7 +18,7 @@
1818
compilations_ci = ENV["GITHUB_WORKFLOW"] == "Compilations"
1919

2020
it "can be used to change the process' root directory" do
21-
-> { Dir.send(@method, File.dirname(__FILE__)) }.should_not raise_error
21+
-> { Dir.send(@method, __dir__) }.should_not raise_error
2222
File.should.exist?("/#{File.basename(__FILE__)}")
2323
end unless compilations_ci
2424

core/dir/shared/exist.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe :dir_exist, shared: true do
22
it "returns true if the given directory exists" do
3-
Dir.send(@method, File.dirname(__FILE__)).should be_true
3+
Dir.send(@method, __dir__).should be_true
44
end
55

66
it "returns true for '.'" do
@@ -20,15 +20,15 @@
2020
end
2121

2222
it "understands relative paths" do
23-
Dir.send(@method, File.dirname(__FILE__) + '/../').should be_true
23+
Dir.send(@method, __dir__ + '/../').should be_true
2424
end
2525

2626
it "returns false if the given directory doesn't exist" do
2727
Dir.send(@method, 'y26dg27n2nwjs8a/').should be_false
2828
end
2929

3030
it "doesn't require the name to have a trailing slash" do
31-
dir = File.dirname(__FILE__)
31+
dir = __dir__
3232
dir.sub!(/\/$/,'')
3333
Dir.send(@method, dir).should be_true
3434
end
@@ -50,7 +50,7 @@
5050

5151
it "calls #to_path on non String arguments" do
5252
p = mock('path')
53-
p.should_receive(:to_path).and_return(File.dirname(__FILE__))
53+
p.should_receive(:to_path).and_return(__dir__)
5454
Dir.send(@method, p)
5555
end
5656
end

core/exception/equal_value_spec.rb

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@
2222

2323
it "returns true if both exceptions have the same class, the same message, and the same backtrace" do
2424
one = TypeError.new("message")
25-
one.set_backtrace [File.dirname(__FILE__)]
25+
one.set_backtrace [__dir__]
2626
two = TypeError.new("message")
27-
two.set_backtrace [File.dirname(__FILE__)]
27+
two.set_backtrace [__dir__]
2828
one.should == two
2929
end
3030

3131
it "returns false if the two exceptions inherit from Exception but have different classes" do
3232
one = RuntimeError.new("message")
33-
one.set_backtrace [File.dirname(__FILE__)]
33+
one.set_backtrace [__dir__]
3434
one.should be_kind_of(Exception)
3535
two = TypeError.new("message")
36-
two.set_backtrace [File.dirname(__FILE__)]
36+
two.set_backtrace [__dir__]
3737
two.should be_kind_of(Exception)
3838
one.should_not == two
3939
end
@@ -52,17 +52,17 @@
5252

5353
it "returns false if the two exceptions differ only in their backtrace" do
5454
one = RuntimeError.new("message")
55-
one.set_backtrace [File.dirname(__FILE__)]
55+
one.set_backtrace [__dir__]
5656
two = RuntimeError.new("message")
5757
two.set_backtrace nil
5858
one.should_not == two
5959
end
6060

6161
it "returns false if the two exceptions differ only in their message" do
6262
one = RuntimeError.new("message")
63-
one.set_backtrace [File.dirname(__FILE__)]
63+
one.set_backtrace [__dir__]
6464
two = RuntimeError.new("message2")
65-
two.set_backtrace [File.dirname(__FILE__)]
65+
two.set_backtrace [__dir__]
6666
one.should_not == two
6767
end
6868
end

core/file/absolute_path_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
end
8686

8787
it "accepts a second argument of a directory from which to resolve the path" do
88-
File.absolute_path(__FILE__, File.dirname(__FILE__)).should == @abs
88+
File.absolute_path(__FILE__, __dir__).should == @abs
8989
end
9090

9191
it "calls #to_path on its argument" do

core/io/eof_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
end
7777

7878
it "returns true on one-byte stream after single-byte read" do
79-
File.open(File.dirname(__FILE__) + '/fixtures/one_byte.txt') { |one_byte|
79+
File.open(__dir__ + '/fixtures/one_byte.txt') { |one_byte|
8080
one_byte.read(1)
8181
one_byte.should.eof?
8282
}

core/kernel/require_relative_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
before :each do
66
CodeLoadingSpecs.spec_setup
77
@dir = "../../fixtures/code"
8-
@abs_dir = File.realpath(@dir, File.dirname(__FILE__))
8+
@abs_dir = File.realpath(@dir, __dir__)
99
@path = "#{@dir}/load_fixture.rb"
10-
@abs_path = File.realpath(@path, File.dirname(__FILE__))
10+
@abs_path = File.realpath(@path, __dir__)
1111
end
1212

1313
after :each do
@@ -92,7 +92,7 @@
9292

9393
it "raises a LoadError that includes the missing path" do
9494
missing_path = "#{@dir}/nonexistent.rb"
95-
expanded_missing_path = File.expand_path(missing_path, File.dirname(__FILE__))
95+
expanded_missing_path = File.expand_path(missing_path, __dir__)
9696
-> { require_relative(missing_path) }.should raise_error(LoadError) { |e|
9797
e.message.should include(expanded_missing_path)
9898
e.path.should == expanded_missing_path
@@ -277,7 +277,7 @@
277277
describe "Kernel#require_relative with an absolute path" do
278278
before :each do
279279
CodeLoadingSpecs.spec_setup
280-
@dir = File.expand_path "../../fixtures/code", File.dirname(__FILE__)
280+
@dir = File.expand_path "../../fixtures/code", __dir__
281281
@abs_dir = @dir
282282
@path = File.join @dir, "load_fixture.rb"
283283
@abs_path = @path

core/kernel/test_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
describe "Kernel#test" do
55
before :all do
6-
@file = File.dirname(__FILE__) + '/fixtures/classes.rb'
7-
@dir = File.dirname(__FILE__) + '/fixtures'
6+
@file = __dir__ + '/fixtures/classes.rb'
7+
@dir = __dir__ + '/fixtures'
88
end
99

1010
it "is a private method" do

core/process/argv0_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
it "is the path given as the main script and the same as __FILE__" do
99
script = "fixtures/argv0.rb"
1010

11-
Dir.chdir(File.dirname(__FILE__)) do
11+
Dir.chdir(__dir__) do
1212
ruby_exe(script).should == "#{script}\n#{script}\nOK"
1313
end
1414
end

core/process/exec_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
end
3131

3232
it "raises Errno::EACCES when passed a directory" do
33-
-> { Process.exec File.dirname(__FILE__) }.should raise_error(Errno::EACCES)
33+
-> { Process.exec __dir__ }.should raise_error(Errno::EACCES)
3434
end
3535

3636
it "runs the specified command, replacing current process" do

core/process/spawn_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ def child_pids(pid)
714714
end
715715

716716
it "raises an Errno::EACCES or Errno::EISDIR when passed a directory" do
717-
-> { Process.spawn File.dirname(__FILE__) }.should raise_error(SystemCallError) { |e|
717+
-> { Process.spawn __dir__ }.should raise_error(SystemCallError) { |e|
718718
[Errno::EACCES, Errno::EISDIR].should include(e.class)
719719
}
720720
end

core/thread/backtrace/location/path_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
context 'when using a relative script path' do
4242
it 'returns a path relative to the working directory' do
4343
path = 'fixtures/main.rb'
44-
directory = File.dirname(__FILE__)
44+
directory = __dir__
4545
Dir.chdir(directory) {
4646
ruby_exe(path)
4747
}.should == path

language/predefined_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1037,7 +1037,7 @@ def obj.foo2; yield; end
10371037

10381038
it "is the path given as the main script and the same as __FILE__" do
10391039
script = "fixtures/dollar_zero.rb"
1040-
Dir.chdir(File.dirname(__FILE__)) do
1040+
Dir.chdir(__dir__) do
10411041
ruby_exe(script).should == "#{script}\n#{script}\nOK"
10421042
end
10431043
end

library/net/ftp/shared/getbinaryfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
describe :net_ftp_getbinaryfile, shared: true do
22
before :each do
3-
@fixture_file = File.dirname(__FILE__) + "/../fixtures/getbinaryfile"
3+
@fixture_file = __dir__ + "/../fixtures/getbinaryfile"
44
@tmp_file = tmp("getbinaryfile")
55

66
@server = NetFTPSpecs::DummyFTP.new

library/net/ftp/shared/putbinaryfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@server = NetFTPSpecs::DummyFTP.new
44
@server.serve_once
55

6-
@local_fixture_file = File.dirname(__FILE__) + "/../fixtures/putbinaryfile"
6+
@local_fixture_file = __dir__ + "/../fixtures/putbinaryfile"
77
@remote_tmp_file = tmp("binaryfile", false)
88

99
@ftp = Net::FTP.new

library/net/ftp/shared/puttextfile.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
@server = NetFTPSpecs::DummyFTP.new
44
@server.serve_once
55

6-
@local_fixture_file = File.dirname(__FILE__) + "/../fixtures/puttextfile"
6+
@local_fixture_file = __dir__ + "/../fixtures/puttextfile"
77
@remote_tmp_file = tmp("textfile", false)
88

99
@ftp = Net::FTP.new

library/net/ftp/storbinary_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@server = NetFTPSpecs::DummyFTP.new
1010
@server.serve_once
1111

12-
@local_fixture_file = File.dirname(__FILE__) + "/fixtures/putbinaryfile"
12+
@local_fixture_file = __dir__ + "/fixtures/putbinaryfile"
1313
@tmp_file = tmp("binaryfile", false)
1414

1515
@ftp = Net::FTP.new

library/net/ftp/storlines_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
@server = NetFTPSpecs::DummyFTP.new
1010
@server.serve_once
1111

12-
@local_fixture_file = File.dirname(__FILE__) + "/fixtures/puttextfile"
12+
@local_fixture_file = __dir__ + "/fixtures/puttextfile"
1313
@tmp_file = tmp("textfile", false)
1414

1515
@ftp = Net::FTP.new

library/yaml/fixtures/common.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
require 'yaml'
22

33
$test_file = tmp("yaml_test_file")
4-
$test_parse_file = File.dirname(__FILE__) + "/test_yaml.yml"
4+
$test_parse_file = __dir__ + "/test_yaml.yml"

optional/capi/object_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ def six(a, b, *c, &d); end
219219
end
220220

221221
it "requires a ruby file" do
222-
$:.unshift File.dirname(__FILE__)
222+
$:.unshift __dir__
223223
@o.rb_require()
224224
$foo.should == 7
225225
end

spec_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use_realpath = File.respond_to?(:realpath)
2-
root = File.dirname(__FILE__)
2+
root = __dir__
33
dir = "fixtures/code"
44
CODE_LOADING_DIR = use_realpath ? File.realpath(dir, root) : File.expand_path(dir, root)
55

0 commit comments

Comments
 (0)