From ce8509e1a7d652581be63a0c77e451fafc3aba8b Mon Sep 17 00:00:00 2001 From: Vladimir Dementyev Date: Fri, 20 Oct 2023 16:59:05 -0700 Subject: [PATCH] refactor: handling two require modes --- Rakefile | 27 ++++++++----- lib/rubocop/markdown/rubocop_ext.rb | 2 +- test/fixtures/configs/no_autodetect.yml | 2 +- .../configs/no_autodetect_with_require.yml | 7 ++++ test/fixtures/configs/no_warn_invalid.yml | 2 +- .../configs/no_warn_invalid_with_require.yml | 8 ++++ test/integration_test.rb | 40 +++++++++---------- test/test_helper.rb | 7 ++++ 8 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 test/fixtures/configs/no_autodetect_with_require.yml create mode 100644 test/fixtures/configs/no_warn_invalid_with_require.yml diff --git a/Rakefile b/Rakefile index 87233b3..e842bf9 100644 --- a/Rakefile +++ b/Rakefile @@ -2,24 +2,29 @@ require "bundler/gem_tasks" require "rake/testtask" require "rubocop/rake_task" -Rake::TestTask.new(:rubocop_md_tests) do |t| +Rake::TestTask.new("test:default") do |t| t.libs << "test" t.libs << "lib" t.warning = false t.test_files = FileList["test/**/*_test.rb"] end -RuboCop::RakeTask.new - -task :test do - ENV["MD_LOAD_MODE"] = "inline" - $stdout.puts "⚙️ Runs rubocop with '-r rubocop_md' options" - Rake::Task[:rubocop_md_tests].invoke +namespace :test do + task :options do + sh <<~COMMAND + MD_LOAD_MODE=options rake test:default + COMMAND + end - ENV["MD_LOAD_MODE"] = "config" - $stdout.puts "⚙️ Runs rubocop with 'required rubocop_md' section in .rubocop.yml" - Rake::Task[:rubocop_md_tests].reenable - Rake::Task[:rubocop_md_tests].invoke + task :config do + sh <<~COMMAND + MD_LOAD_MODE=config rake test:default + COMMAND + end end +RuboCop::RakeTask.new + +task test: ["test:options", "test:config"] + task default: [:rubocop, :test] diff --git a/lib/rubocop/markdown/rubocop_ext.rb b/lib/rubocop/markdown/rubocop_ext.rb index 1bfbc56..8a387ce 100644 --- a/lib/rubocop/markdown/rubocop_ext.rb +++ b/lib/rubocop/markdown/rubocop_ext.rb @@ -43,7 +43,7 @@ def markdown_file?(file) RuboCop::Runner.prepend(Module.new do # Set config store for Markdown def get_processed_source(*args) - RuboCop::Markdown.config_store = @config_store + RuboCop::Markdown.config_store = @config_store unless RuboCop::Markdown.config_store super end diff --git a/test/fixtures/configs/no_autodetect.yml b/test/fixtures/configs/no_autodetect.yml index 82a6a9e..a6e1f35 100644 --- a/test/fixtures/configs/no_autodetect.yml +++ b/test/fixtures/configs/no_autodetect.yml @@ -1,4 +1,4 @@ -inherit_from: "../.rubocop.yml" +inherit_from: "../../../.rubocop.yml" Markdown: Autodetect: false diff --git a/test/fixtures/configs/no_autodetect_with_require.yml b/test/fixtures/configs/no_autodetect_with_require.yml new file mode 100644 index 0000000..8f0587f --- /dev/null +++ b/test/fixtures/configs/no_autodetect_with_require.yml @@ -0,0 +1,7 @@ +inherit_from: "../../../.rubocop.yml" + +require: + - "rubocop-md" + +Markdown: + Autodetect: false diff --git a/test/fixtures/configs/no_warn_invalid.yml b/test/fixtures/configs/no_warn_invalid.yml index f6c7f71..b736833 100644 --- a/test/fixtures/configs/no_warn_invalid.yml +++ b/test/fixtures/configs/no_warn_invalid.yml @@ -1,4 +1,4 @@ -inherit_from: "../.rubocop.yml" +inherit_from: "../../../.rubocop.yml" Markdown: WarnInvalid: false diff --git a/test/fixtures/configs/no_warn_invalid_with_require.yml b/test/fixtures/configs/no_warn_invalid_with_require.yml new file mode 100644 index 0000000..f569fba --- /dev/null +++ b/test/fixtures/configs/no_warn_invalid_with_require.yml @@ -0,0 +1,8 @@ +inherit_from: "../../../.rubocop.yml" + +require: + - "rubocop-md" + +Markdown: + WarnInvalid: false + diff --git a/test/integration_test.rb b/test/integration_test.rb index b04fb23..bfc5655 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -5,31 +5,29 @@ require "fileutils" module RuboCopRunner - MD_LOAD_INLINE_MODE = "inline" + def run_rubocop(path, options: "", config: nil) + md_path = File.expand_path("../lib/rubocop-md.rb", __dir__) + md_config_path = File.expand_path("./fixtures/.rubocop.yml", __dir__) + + options = "#{options} -r #{md_path}" if ENV["MD_LOAD_MODE"] == "options" + + if ENV["MD_LOAD_MODE"] == "config" + # Add "_with_require" suffix + config = if config + config.sub(/\.yml$/, "_with_require.yml") + else + md_config_path + end + end + + options = "#{options} -c #{config}" if config - def run_rubocop(path, options: "") output, _status = Open3.capture2( - cmd_command_by_env(path, options), + "bundle exec rubocop #{options} #{path}", chdir: File.join(__dir__, "fixtures") ) - output end - - private - - def cmd_command_by_env(path, options) - cmd_command = "bundle exec rubocop" - load_mode = ENV.fetch("MD_LOAD_MODE", MD_LOAD_INLINE_MODE) - - if load_mode == MD_LOAD_INLINE_MODE - md_path = File.expand_path("../lib/rubocop-md.rb", __dir__) - - cmd_command = "#{cmd_command} -r #{md_path}" - end - - "#{cmd_command} #{options} #{path}" - end end class RuboCop::Markdown::AnalyzeTest < Minitest::Test @@ -70,7 +68,7 @@ def test_multiple_invalid_snippets_file def test_multiple_invalid_snippets_file_no_warn res = run_rubocop( "multiple_invalid_snippets.md", - options: "-c configs/no_warn_invalid.yml" + config: "configs/no_warn_invalid.yml" ) assert_match %r{Inspecting 1 file}, res @@ -80,7 +78,7 @@ def test_multiple_invalid_snippets_file_no_warn def test_multiple_invalid_snippets_file_no_autodetect res = run_rubocop( "multiple_invalid_snippets_unknown.md", - options: "-c configs/no_autodetect.yml" + config: "configs/no_autodetect.yml" ) assert_match %r{Inspecting 1 file}, res diff --git a/test/test_helper.rb b/test/test_helper.rb index 859fb41..925df7d 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,6 +1,13 @@ # frozen_string_literal: true $LOAD_PATH.unshift File.expand_path("../lib", __dir__) + +if ENV["MD_LOAD_MODE"] == "options" + $stdout.puts "⚙️ Run rubocop with '-r rubocop-md' options" +elsif ENV["MD_LOAD_MODE"] == "config" + $stdout.puts "⚙️ Run rubocop with 'require: - rubocop-md' in .rubocop.yml" +end + require "rubocop" require "rubocop-md"