A platform for community discussion. Free, open, simple.
DEV: Avoid `$` globals (#15453)
Jarek Radosz5a50f18c0c6e
diff --git a/app/controllers/forums_controller.rb b/app/controllers/forums_controller.rbindex 361e75cff9a..88d2879e9b1 100644--- a/app/controllers/forums_controller.rb+++ b/app/controllers/forums_controller.rb@@ -17,11 +17,6 @@ class ForumsController < ActionController::Baseendend- if $shutdown # rubocop:disable Style/GlobalVars- render plain: "shutting down", status: (params[:shutdown_ok] ? 200 : 500)- else- render plain: "ok"- end+ render plain: "ok"end-enddiff --git a/app/services/destroy_task.rb b/app/services/destroy_task.rbindex 4c5036eca66..d06a4ad7556 100644--- a/app/services/destroy_task.rb+++ b/app/services/destroy_task.rb@@ -1,8 +1,7 @@# frozen_string_literal: trueclass DestroyTask-- def initialize(io = $stdout)+ def initialize(io = STDOUT)@io = ioenddiff --git a/bin/unicorn b/bin/unicornindex 9735e3eec0b..6f6e169313b 100755--- a/bin/unicorn+++ b/bin/unicorn@@ -80,14 +80,14 @@ if ARGV.include?("--help")exitend-# this dev_mode hackery enables, the following to be used to restart unicorn:+# this dev_mode hackery enables the following to be used to restart unicorn:## pkill -USR2 -f 'ruby bin/unicorn'## This is handy if you want to bind a key to restarting unicorn in devif dev_mode- $unicorn_dev_supervisor_pid = Process.pid # rubocop:disable Style/GlobalVars+ UNICORN_DEV_SUPERVISOR_PID = Process.pidrestart = truewhile restartdiff --git a/config/application.rb b/config/application.rbindex ce62faa6fef..a6ecb270d0c 100644--- a/config/application.rb+++ b/config/application.rb@@ -291,9 +291,12 @@ module Discourserequire 'logster/redis_store'# Use redis for our cacheconfig.cache_store = DiscourseRedis.new_redis_store- $redis = DiscourseRedis.new # rubocop:disable Style/GlobalVars+ Discourse.redis = DiscourseRedis.newLogster.store = Logster::RedisStore.new(DiscourseRedis.new)+ # Deprecated+ $redis = Discourse.redis # rubocop:disable Style/GlobalVars+# we configure rack cache on demand in an initializer# our setup does not use rack cache and instead defers to nginxconfig.action_dispatch.rack_cache = nildiff --git a/config/initializers/000-development_reload_warnings.rb b/config/initializers/000-development_reload_warnings.rbindex 374bb7415f6..ce0260a8ee5 100644--- a/config/initializers/000-development_reload_warnings.rb+++ b/config/initializers/000-development_reload_warnings.rb@@ -12,7 +12,7 @@ if Rails.env.development? && !Rails.configuration.cache_classes && Discourse.run]Listen.to(*paths, only: /\.rb$/) do |modified, added, removed|- supervisor_pid = $unicorn_dev_supervisor_pid # rubocop:disable Style/GlobalVars+ supervisor_pid = UNICORN_DEV_SUPERVISOR_PIDauto_restart = supervisor_pid && ENV["AUTO_RESTART"] != "0"files = modified + added + removeddiff --git a/config/initializers/100-watch_for_restart.rb b/config/initializers/100-watch_for_restart.rbdeleted file mode 100644index 0998cf01b19..00000000000--- a/config/initializers/100-watch_for_restart.rb+++ /dev/null@@ -1,44 +0,0 @@-# frozen_string_literal: true--# this is a trivial graceful restart on touch of tmp/restart.-#-# It simply drains all the requests (waits up to 4 seconds) and issues a HUP-# if you need a more sophisticated cycling restart for multiple thins it will need to be written-#-# This works fine for Discourse.org cause we host our app across multiple machines, if you hosting-# on a single machine you have a trickier problem at hand as you need to cycle the processes in order-#--# VIM users rejoice, if you add this to your .vimrc CTRL-a will restart puma:-# nmap <C-a> <Esc>:!touch tmp/restart<CR><CR>--Thread.new do- file = "#{Rails.root}/tmp/restart"- old_time = File.ctime(file).to_i if File.exist? file- wait_seconds = 4-- if Rails.env.development? && $PROGRAM_NAME =~ /puma/- require 'listen'-- time = nil-- begin- listener = Listen.to("#{Rails.root}/tmp", only: /restart/) do-- time = File.ctime(file).to_i if File.exist? file-- if old_time != time- Rails.logger.info "attempting to reload #{$$} #{$PROGRAM_NAME} in #{wait_seconds} seconds"- $shutdown = true # rubocop:disable Style/GlobalVars- sleep wait_seconds- Rails.logger.info "restarting #{$$}"- Process.kill("USR2", $$)- end- end- listener.start- sleep- rescue => e- puts "Failed to watch for restart, this probably means the old postgres directory is in tmp, remove it #{e}"- end- end-enddiff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rbindex 50b0a622974..e69979adfe1 100644--- a/config/unicorn.conf.rb+++ b/config/unicorn.conf.rb@@ -26,7 +26,7 @@ endpid (ENV["UNICORN_PID_PATH"] || "#{discourse_path}/tmp/pids/unicorn.pid")if ENV["RAILS_ENV"] != "production"- logger Logger.new($stdout)+ logger Logger.new(STDOUT)# we want a longer timeout in dev cause first request can be really slowtimeout (ENV["UNICORN_TIMEOUT"] && ENV["UNICORN_TIMEOUT"].to_i || 60)elsediff --git a/lib/discourse.rb b/lib/discourse.rbindex f1b10278f12..22b182dfc76 100644--- a/lib/discourse.rb+++ b/lib/discourse.rb@@ -1,5 +1,4 @@# frozen_string_literal: true-# rubocop:disable Style/GlobalVarsrequire 'cache'require 'open3'@@ -653,36 +652,32 @@ module Discourseenddef self.git_version- $git_version ||=- begin- git_cmd = 'git rev-parse HEAD'- self.try_git(git_cmd, Discourse::VERSION::STRING)- end # rubocop:disable Style/GlobalVars+ @git_version ||= begin+ git_cmd = 'git rev-parse HEAD'+ self.try_git(git_cmd, Discourse::VERSION::STRING)+ endenddef self.git_branch- $git_branch ||=- begin- git_cmd = 'git rev-parse --abbrev-ref HEAD'- self.try_git(git_cmd, 'unknown')- end+ @git_branch ||= begin+ git_cmd = 'git rev-parse --abbrev-ref HEAD'+ self.try_git(git_cmd, 'unknown')+ endenddef self.full_version- $full_version ||=- begin- git_cmd = 'git describe --dirty --match "v[0-9]*"'- self.try_git(git_cmd, 'unknown')- end+ @full_version ||= begin+ git_cmd = 'git describe --dirty --match "v[0-9]*"'+ self.try_git(git_cmd, 'unknown')+ endenddef self.last_commit_date- $last_commit_date ||=- begin- git_cmd = 'git log -1 --format="%ct"'- seconds = self.try_git(git_cmd, nil)- seconds.nil? ? nil : DateTime.strptime(seconds, '%s')- end+ @last_commit_date ||= begin+ git_cmd = 'git log -1 --format="%ct"'+ seconds = self.try_git(git_cmd, nil)+ seconds.nil? ? nil : DateTime.strptime(seconds, '%s')+ endenddef self.try_git(git_cmd, default_value)@@ -1001,9 +996,7 @@ module Discourse@preloaded_rails = trueend- def self.redis- $redis- end+ mattr_accessor :redisdef self.is_parallel_test?ENV['RAILS_ENV'] == "test" && ENV['TEST_ENV_NUMBER']@@ -1031,5 +1024,3 @@ module DiscourseRails.env.development? || ENV["ALLOW_DEV_POPULATE"] == "1"endend--# rubocop:enable Style/GlobalVarsdiff --git a/lib/tasks/themes.rake b/lib/tasks/themes.rakeindex d8ef7ad9b00..88b85080afa 100644--- a/lib/tasks/themes.rake+++ b/lib/tasks/themes.rake@@ -145,7 +145,7 @@ task "themes:isolated_test" => :environment do |t, args|redis = TemporaryRedis.newredis.start- $redis = redis.instance # rubocop:disable Style/GlobalVars+ Discourse.redis = redis.instancedb = TemporaryDb.newdb.startdb.migratediff --git a/spec/models/user_option_spec.rb b/spec/models/user_option_spec.rbindex cc4db584751..51de45ea48a 100644--- a/spec/models/user_option_spec.rb+++ b/spec/models/user_option_spec.rb@@ -125,7 +125,7 @@ describe UserOption doendafter do- $redis.flushdb+ Discourse.redis.flushdbendit "should have a reason for the first visit" dodiff --git a/spec/rails_helper.rb b/spec/rails_helper.rbindex 579a0c61f23..ef6f87dc6e3 100644--- a/spec/rails_helper.rb+++ b/spec/rails_helper.rb@@ -14,21 +14,15 @@ endrequire 'rubygems'require 'rbtrace'-require 'pry'require 'pry-byebug'require 'pry-rails'--# Loading more in this block will cause your tests to run faster. However,-# if you change any configuration or code from libraries loaded here, you'll-# need to restart spork for it take effect.require 'fabrication'require 'mocha/api'require 'certified'require 'webmock/rspec'class RspecErrorTracker-def self.last_exception=(ex)@ex = exenddiff --git a/spec/requests/forums_controller_spec.rb b/spec/requests/forums_controller_spec.rbindex 19b1c6420bb..c57384009a1 100644--- a/spec/requests/forums_controller_spec.rb+++ b/spec/requests/forums_controller_spec.rb@@ -19,24 +19,6 @@ RSpec.describe ForumsController doendend- describe "during shutdown" do- before(:each) do- $shutdown = true- end- after(:each) do- $shutdown = nil- end-- it "returns a 500 response" do- get "/srv/status"- expect(response.status).to eq(500)- end- it "returns a 200 response when given shutdown_ok" do- get "/srv/status?shutdown_ok=1"- expect(response.status).to eq(200)- end- end-describe "cluster parameter" doit "returns a 500 response if the cluster is not configured" doget "/srv/status?cluster=abc"diff --git a/spec/requests/users_controller_spec.rb b/spec/requests/users_controller_spec.rbindex 2309239b779..24126c533b8 100644--- a/spec/requests/users_controller_spec.rb+++ b/spec/requests/users_controller_spec.rb@@ -1610,13 +1610,13 @@ describe UsersController docontext 'is too long' dobefore do- get "/u/check_username.json", params: { username: generate_username(User.username_length.last + 1) }+ get "/u/check_username.json", params: { username: SecureRandom.alphanumeric(SiteSetting.max_username_length.to_i + 1) }endinclude_examples 'checking an invalid username'it 'should return the "too long" message' doexpect(response.status).to eq(200)- expect(response.parsed_body['errors']).to include(I18n.t(:'user.username.long', max: User.username_length.end))+ expect(response.parsed_body['errors']).to include(I18n.t(:'user.username.long', max: SiteSetting.max_username_length))endenddiff --git a/spec/support/helpers.rb b/spec/support/helpers.rbindex f0f02932c0f..69aa330e788 100644--- a/spec/support/helpers.rb+++ b/spec/support/helpers.rb@@ -58,11 +58,6 @@ module Helperspostend- def generate_username(length = 10)- range = [*'a'..'z']- Array.new(length) { range.sample }.join- end-def stub_guardian(user)guardian = Guardian.new(user)yield(guardian) if block_given?@@ -82,14 +77,6 @@ module Helpersexpect(result).to eq(true)end- def fill_email(mail, from, to, body = nil, subject = nil, cc = nil)- result = mail.gsub("FROM", from).gsub("TO", to)- result.gsub!(/Hey.*/m, body) if body- result.sub!(/We .*/, subject) if subject- result.sub!("CC", cc.presence || "")- result- end-def email(email_name)fixture_file("emails/#{email_name}.eml")end