DEV: Use rubocop-discourse gem to add custom chdir cop

DEV: Use rubocop-discourse gem to add custom chdir cop

Followup to b27e009655a866973f04e16f6694b8ab853124d3

diff --git a/.rubocop.yml b/.rubocop.yml
index 15096124c0..dc0fd5cceb 100644
--- a/.rubocop.yml
+++ b/.rubocop.yml
@@ -1,3 +1,6 @@
+require:
+  - rubocop-discourse
+
 AllCops:
   TargetRubyVersion: 2.4
   DisabledByDefault: true
@@ -128,3 +131,8 @@ Style/Semicolon:
 
 Style/RedundantReturn:
   Enabled: true
+
+DiscourseCops/NoChdir:	
+  Enabled: true	
+  Exclude:	
+    - 'spec/**/*' # Specs are run sequentially, so chdir can be used	
diff --git a/Gemfile b/Gemfile
index 6d4182e56c..c4ce6cb479 100644
--- a/Gemfile
+++ b/Gemfile
@@ -143,6 +143,7 @@ group :test, :development do
   gem 'pry-nav'
   gem 'byebug', require: ENV['RM_INFO'].nil?
   gem 'rubocop', require: false
+  gem "rubocop-discourse", require: false
   gem 'parallel_tests'
 end
 
diff --git a/Gemfile.lock b/Gemfile.lock
index 6abc17fb50..43eac52d79 100644
--- a/Gemfile.lock
+++ b/Gemfile.lock
@@ -348,6 +348,8 @@ GEM
       rainbow (>= 2.2.2, < 4.0)
       ruby-progressbar (~> 1.7)
       unicode-display_width (>= 1.4.0, < 1.7)
+    rubocop-discourse (1.0.1)
+      rubocop (>= 0.69.0)
     ruby-openid (2.7.0)
     ruby-prof (0.17.0)
     ruby-progressbar (1.10.0)
@@ -524,6 +526,7 @@ DEPENDENCIES
   rspec-rails (= 4.0.0.beta2)
   rtlit
   rubocop
+  rubocop-discourse
   ruby-prof
   ruby-readability
   rubyzip
diff --git a/lib/rubocop/cop/discourse_cops.rb b/lib/rubocop/cop/discourse_cops.rb
deleted file mode 100644
index 3acc3d7127..0000000000
--- a/lib/rubocop/cop/discourse_cops.rb
+++ /dev/null
@@ -1,34 +0,0 @@
-# frozen_string_literal: true
-
-module RuboCop
-  module Cop
-    module DiscourseCops
-      # Avoid using chdir - it is not thread safe.
-      #
-      # Instead, you may be able to use:
-      # Discourse::Utils.execute_command(chdir: 'test') do |runner|
-      #   runner.exec('pwd')
-      # end
-      #
-      # @example
-      #   # bad
-      #   Dir.chdir('test')
-      class NoChdir < Cop
-        MSG = 'Chdir is not thread safe.'
-
-        def_node_matcher :using_dir_chdir?, <<-MATCHER
-          (send (const nil? :Dir) :chdir ...)
-        MATCHER
-
-        def_node_matcher :using_fileutils_cd?, <<-MATCHER
-          (send (const nil? :FileUtils) :cd ...)
-        MATCHER
-
-        def on_send(node)
-          return if !(using_dir_chdir?(node) || using_fileutils_cd?(node))
-          add_offense(node, message: MSG)
-        end
-      end
-    end
-  end
-end

GitHub sha: eaf60968

3 Likes

Looks like for the discourse_api gem travis is erroring out on rubocop because of this?

Running RuboCop...

cannot load such file -- rubocop-discourse

I was just able to get it to fail locally by running

gem uninstall rubocop-discourse

I don’t really like that the solution to fix this is to add a development dependency to the discourse_api.gemspec, but maybe that is what we have to do?

Yeah, I think adding a dev dependency is what we will need to do

1 Like