Skip to content

Commit

Permalink
Define path with __dir__
Browse files Browse the repository at this point in the history
".. with __dir__ we can restore order in the Universe." - by @fxn

Related to 5b8738c
  • Loading branch information
bogdanvlviv committed May 22, 2017
1 parent d414881 commit 40bdbce
Show file tree
Hide file tree
Showing 117 changed files with 197 additions and 204 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ group :cable do
end

# Add your own local bundler stuff.
local_gemfile = File.dirname(__FILE__) + "/.Gemfile"
local_gemfile = File.expand_path(".Gemfile", __dir__)
instance_eval File.read local_gemfile if File.exist? local_gemfile

group :test do
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "net/http"

$:.unshift File.expand_path("..", __FILE__)
$:.unshift __dir__
require "tasks/release"
require "railties/lib/rails/api/task"

Expand Down
2 changes: 1 addition & 1 deletion actioncable/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,7 @@ application. The recommended basic setup is as follows:

```ruby
# cable/config.ru
require ::File.expand_path('../../config/environment', __FILE__)
require ::File.expand_path('../config/environment', __dir__)
Rails.application.eager_load!

run ActionCable.server
Expand Down
10 changes: 4 additions & 6 deletions actioncable/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,13 @@ require "pathname"
require "open3"
require "action_cable"

dir = File.dirname(__FILE__)

task default: :test

task package: %w( assets:compile assets:verify )

Rake::TestTask.new do |t|
t.libs << "test"
t.test_files = Dir.glob("#{dir}/test/**/*_test.rb")
t.test_files = Dir.glob("#{__dir__}/test/**/*_test.rb")
t.warning = true
t.verbose = true
t.ruby_opts = ["--dev"] if defined?(JRUBY_VERSION)
Expand Down Expand Up @@ -46,7 +44,7 @@ namespace :assets do
desc "Verify compiled Action Cable assets"
task :verify do
file = "lib/assets/compiled/action_cable.js"
pathname = Pathname.new("#{dir}/#{file}")
pathname = Pathname.new("#{__dir__}/#{file}")

print "[verify] #{file} exists "
if pathname.exist?
Expand All @@ -64,8 +62,8 @@ namespace :assets do
fail
end

print "[verify] #{dir} can be required as a module "
_, stderr, status = Open3.capture3("node", "--print", "window = {}; require('#{dir}');")
print "[verify] #{__dir__} can be required as a module "
_, stderr, status = Open3.capture3("node", "--print", "window = {}; require('#{__dir__}');")
if status.success?
puts "[OK]"
else
Expand Down
2 changes: 1 addition & 1 deletion actioncable/actioncable.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip
version = File.read(File.expand_path("../RAILS_VERSION", __dir__)).strip

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Rails
module Generators
class ChannelGenerator < NamedBase
source_root File.expand_path("../templates", __FILE__)
source_root File.expand_path("templates", __dir__)

argument :actions, type: :array, default: [], banner: "method method"

Expand Down
2 changes: 1 addition & 1 deletion actioncable/test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
end

# Require all the stubs and models
Dir[File.dirname(__FILE__) + "/stubs/*.rb"].each { |file| require file }
Dir[File.expand_path("stubs/*.rb", __dir__)].each { |file| require file }

class ActionCable::TestCase < ActiveSupport::TestCase
def wait_for_async
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/actionmailer.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip
version = File.read(File.expand_path("../RAILS_VERSION", __dir__)).strip

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Rails
module Generators
class MailerGenerator < NamedBase
source_root File.expand_path("../templates", __FILE__)
source_root File.expand_path("templates", __dir__)

argument :actions, type: :array, default: [], banner: "method method"

Expand Down
4 changes: 2 additions & 2 deletions actionmailer/test/abstract_unit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

module Rails
def self.root
File.expand_path("../", File.dirname(__FILE__))
File.expand_path("..", __dir__)
end
end

Expand All @@ -28,7 +28,7 @@ def self.root
# Disable available locale checks to avoid warnings running the test suite.
I18n.enforce_available_locales = false

FIXTURE_LOAD_PATH = File.expand_path("fixtures", File.dirname(__FILE__))
FIXTURE_LOAD_PATH = File.expand_path("fixtures", __dir__)
ActionMailer::Base.view_paths = FIXTURE_LOAD_PATH

class ActiveSupport::TestCase
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/test/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

CACHE_DIR = "test_cache"
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
FILE_STORE_PATH = File.join(File.dirname(__FILE__), "/../temp/", CACHE_DIR)
FILE_STORE_PATH = File.join(__dir__, "/../temp/", CACHE_DIR)

class FragmentCachingMailer < ActionMailer::Base
abstract!
Expand Down
2 changes: 1 addition & 1 deletion actionmailer/test/log_subscriber_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def test_deliver_is_notified
end

def test_receive_is_notified
fixture = File.read(File.dirname(__FILE__) + "/fixtures/raw_email")
fixture = File.read(File.expand_path("fixtures/raw_email", __dir__))
TestMailer.receive(fixture)
wait
assert_equal(1, @logger.logged(:info).size)
Expand Down
2 changes: 1 addition & 1 deletion actionpack/Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ namespace :test do
end

task :lines do
load File.expand_path("..", File.dirname(__FILE__)) + "/tools/line_statistics"
load File.expand_path("..", __dir__) + "/tools/line_statistics"
files = FileList["lib/**/*.rb"]
CodeTools::LineStatistics.new(files).print_loc
end
Expand Down
2 changes: 1 addition & 1 deletion actionpack/actionpack.gemspec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version = File.read(File.expand_path("../../RAILS_VERSION", __FILE__)).strip
version = File.read(File.expand_path("../RAILS_VERSION", __dir__)).strip

Gem::Specification.new do |s|
s.platform = Gem::Platform::RUBY
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ def to_svg
end

def visualizer(paths, title = "FSM")
viz_dir = File.join File.dirname(__FILE__), "..", "visualizer"
viz_dir = File.join __dir__, "..", "visualizer"
fsm_js = File.read File.join(viz_dir, "fsm.js")
fsm_css = File.read File.join(viz_dir, "fsm.css")
erb = File.read File.join(viz_dir, "index.html.erb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module ActionDispatch
# This middleware is responsible for logging exceptions and
# showing a debugging page in case the request is local.
class DebugExceptions
RESCUES_TEMPLATE_PATH = File.expand_path("../templates", __FILE__)
RESCUES_TEMPLATE_PATH = File.expand_path("templates", __dir__)

class DebugView < ActionView::Base
def debug_params(params)
Expand Down
10 changes: 5 additions & 5 deletions actionpack/test/abstract_unit.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
$:.unshift(File.dirname(__FILE__) + "/lib")
$:.unshift(File.dirname(__FILE__) + "/fixtures/helpers")
$:.unshift(File.dirname(__FILE__) + "/fixtures/alternate_helpers")
$:.unshift File.expand_path("lib", __dir__)
$:.unshift File.expand_path("fixtures/helpers", __dir__)
$:.unshift File.expand_path("fixtures/alternate_helpers", __dir__)

require "active_support/core_ext/kernel/reporting"

Expand Down Expand Up @@ -56,7 +56,7 @@ def root; end;
# Disable available locale checks to avoid warnings running the test suite.
I18n.enforce_available_locales = false

FIXTURE_LOAD_PATH = File.join(File.dirname(__FILE__), "fixtures")
FIXTURE_LOAD_PATH = File.join(__dir__, "fixtures")

SharedTestRoutes = ActionDispatch::Routing::RouteSet.new

Expand Down Expand Up @@ -156,7 +156,7 @@ def with_routing(&block)
end

def with_autoload_path(path)
path = File.join(File.dirname(__FILE__), "fixtures", path)
path = File.join(__dir__, "fixtures", path)
if ActiveSupport::Dependencies.autoload_paths.include?(path)
yield
else
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/action_pack_assertions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def raise_exception_on_post
end

def render_file_absolute_path
render file: File.expand_path("../../../README.rdoc", __FILE__)
render file: File.expand_path("../../README.rdoc", __dir__)
end

def render_file_relative_path
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/api/data_streaming_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "abstract_unit"

module TestApiFileUtils
def file_path() File.expand_path(__FILE__) end
def file_path() __FILE__ end
def file_data() @data ||= File.open(file_path, "rb") { |f| f.read } end
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/caching_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

CACHE_DIR = "test_cache"
# Don't change '/../temp/' cavalierly or you might hose something you don't want hosed
FILE_STORE_PATH = File.join(File.dirname(__FILE__), "/../temp/", CACHE_DIR)
FILE_STORE_PATH = File.join(__dir__, "../temp/", CACHE_DIR)

class FragmentCachingMetalTestController < ActionController::Metal
abstract!
Expand Down
8 changes: 4 additions & 4 deletions actionpack/test/controller/helper_test.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
require "abstract_unit"

ActionController::Base.helpers_path = File.expand_path("../../fixtures/helpers", __FILE__)
ActionController::Base.helpers_path = File.expand_path("../fixtures/helpers", __dir__)

module Fun
class GamesController < ActionController::Base
Expand Down Expand Up @@ -48,7 +48,7 @@ class MeTooController < JustMeController

class HelpersPathsController < ActionController::Base
paths = ["helpers2_pack", "helpers1_pack"].map do |path|
File.join(File.expand_path("../../fixtures", __FILE__), path)
File.join(File.expand_path("../fixtures", __dir__), path)
end
$:.unshift(*paths)

Expand All @@ -61,7 +61,7 @@ def index
end

class HelpersTypoController < ActionController::Base
path = File.expand_path("../../fixtures/helpers_typo", __FILE__)
path = File.expand_path("../fixtures/helpers_typo", __dir__)
$:.unshift(path)
self.helpers_path = path
end
Expand Down Expand Up @@ -178,7 +178,7 @@ def test_all_helpers
end

def test_all_helpers_with_alternate_helper_dir
@controller_class.helpers_path = File.expand_path("../../fixtures/alternate_helpers", __FILE__)
@controller_class.helpers_path = File.expand_path("../fixtures/alternate_helpers", __dir__)

# Reload helpers
@controller_class._helpers = Module.new
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1091,7 +1091,7 @@ def app
end

def self.fixture_path
File.dirname(__FILE__) + "/../fixtures/multipart"
File.expand_path("../fixtures/multipart", __dir__)
end

routes.draw do
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/live_stream_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def blocking_stream
end

def write_sleep_autoload
path = File.join(File.dirname(__FILE__), "../fixtures")
path = File.expand_path("../fixtures", __dir__)
ActiveSupport::Dependencies.autoload_paths << path

response.headers["Content-Type"] = "text/event-stream"
Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/mime/accept_format_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_javascript_with_no_format_only_star_star
end

class AbstractPostController < ActionController::Base
self.view_paths = File.dirname(__FILE__) + "/../../fixtures/post_test/"
self.view_paths = File.expand_path("../../fixtures/post_test", __dir__)
end

# For testing layouts which are set automatically
Expand Down
10 changes: 5 additions & 5 deletions actionpack/test/controller/new_base/render_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

module RenderFile
class BasicController < ActionController::Base
self.view_paths = File.dirname(__FILE__)
self.view_paths = __dir__

def index
render file: File.join(File.dirname(__FILE__), *%w[.. .. fixtures test hello_world])
render file: File.expand_path("../../fixtures/test/hello_world", __dir__)
end

def with_instance_variables
@secret = "in the sauce"
render file: File.join(File.dirname(__FILE__), "../../fixtures/test/render_file_with_ivar")
render file: File.expand_path("../../fixtures/test/render_file_with_ivar", __dir__)
end

def relative_path
Expand All @@ -25,11 +25,11 @@ def relative_path_with_dot

def pathname
@secret = "in the sauce"
render file: Pathname.new(File.dirname(__FILE__)).join(*%w[.. .. fixtures test dot.directory render_file_with_ivar])
render file: Pathname.new(__dir__).join(*%w[.. .. fixtures test dot.directory render_file_with_ivar])
end

def with_locals
path = File.join(File.dirname(__FILE__), "../../fixtures/test/render_file_with_locals")
path = File.expand_path("../../fixtures/test/render_file_with_locals", __dir__)
render file: path, locals: { secret: "in the sauce" }
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class SimpleController < ::ApplicationController
"render_implicit_action/simple/hello_world.html.erb" => "Hello world!",
"render_implicit_action/simple/hyphen-ated.html.erb" => "Hello hyphen-ated!",
"render_implicit_action/simple/not_implemented.html.erb" => "Not Implemented"
), ActionView::FileSystemResolver.new(File.expand_path("../../../controller", __FILE__))]
), ActionView::FileSystemResolver.new(File.expand_path("../../controller", __dir__))]

def hello_world() end
end
Expand Down
12 changes: 6 additions & 6 deletions actionpack/test/controller/render_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def determine_layout
module TemplateModificationHelper
private
def modify_template(name)
path = File.expand_path("../../fixtures/#{name}.erb", __FILE__)
path = File.expand_path("../fixtures/#{name}.erb", __dir__)
original = File.read(path)
File.write(path, "#{original} Modified!")
ActionView::LookupContext::DetailsKey.clear
Expand Down Expand Up @@ -287,9 +287,9 @@ def setup

def test_dynamic_render_with_file
# This is extremely bad, but should be possible to do.
assert File.exist?(File.join(File.dirname(__FILE__), "../../test/abstract_unit.rb"))
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
response = get :dynamic_render_with_file, params: { id: '../\\../test/abstract_unit.rb' }
assert_equal File.read(File.join(File.dirname(__FILE__), "../../test/abstract_unit.rb")),
assert_equal File.read(File.expand_path("../../test/abstract_unit.rb", __dir__)),
response.body
end

Expand All @@ -306,16 +306,16 @@ def test_dynamic_render_with_absolute_path
end

def test_dynamic_render
assert File.exist?(File.join(File.dirname(__FILE__), "../../test/abstract_unit.rb"))
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
assert_raises ActionView::MissingTemplate do
get :dynamic_render, params: { id: '../\\../test/abstract_unit.rb' }
end
end

def test_permitted_dynamic_render_file_hash
assert File.exist?(File.join(File.dirname(__FILE__), "../../test/abstract_unit.rb"))
assert File.exist?(File.expand_path("../../test/abstract_unit.rb", __dir__))
response = get :dynamic_render_permit, params: { id: { file: '../\\../test/abstract_unit.rb' } }
assert_equal File.read(File.join(File.dirname(__FILE__), "../../test/abstract_unit.rb")),
assert_equal File.read(File.expand_path("../../test/abstract_unit.rb", __dir__)),
response.body
end

Expand Down
2 changes: 1 addition & 1 deletion actionpack/test/controller/send_file_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module TestFileUtils
def file_name() File.basename(__FILE__) end
def file_path() File.expand_path(__FILE__) end
def file_path() __FILE__ end
def file_data() @data ||= File.open(file_path, "rb") { |f| f.read } end
end

Expand Down
Loading

0 comments on commit 40bdbce

Please sign in to comment.