From 91c89a7071e0198547e20028a25d63a2edb016a9 Mon Sep 17 00:00:00 2001 From: prog-supdex Date: Wed, 18 Oct 2023 13:32:33 +0200 Subject: [PATCH 1/2] Fixing the problem when RuboCop::Markdown.config_store was not filled. This problem appeared when "rubocop-md" loaded via .rubocop.yml and used "--formater" Fixes #22 --- lib/rubocop/markdown/rubocop_ext.rb | 5 +++-- test/fixtures/.rubocop.yml | 5 +++++ test/fixtures/configs/no_autodetect.yml | 2 +- test/fixtures/configs/no_warn_invalid.yml | 3 ++- test/integration_test.rb | 12 ++++++++++-- 5 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 test/fixtures/.rubocop.yml diff --git a/lib/rubocop/markdown/rubocop_ext.rb b/lib/rubocop/markdown/rubocop_ext.rb index 62fd945..1bfbc56 100644 --- a/lib/rubocop/markdown/rubocop_ext.rb +++ b/lib/rubocop/markdown/rubocop_ext.rb @@ -42,9 +42,10 @@ def markdown_file?(file) RuboCop::Runner.prepend(Module.new do # Set config store for Markdown - def initialize(*args) - super + def get_processed_source(*args) RuboCop::Markdown.config_store = @config_store + + super end # Do not cache markdown files, 'cause cache doesn't know about processing. diff --git a/test/fixtures/.rubocop.yml b/test/fixtures/.rubocop.yml new file mode 100644 index 0000000..a5638d5 --- /dev/null +++ b/test/fixtures/.rubocop.yml @@ -0,0 +1,5 @@ +inherit_from: + - "../../.rubocop.yml" + +require: + - "rubocop-md" diff --git a/test/fixtures/configs/no_autodetect.yml b/test/fixtures/configs/no_autodetect.yml index a6e1f35..82a6a9e 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_warn_invalid.yml b/test/fixtures/configs/no_warn_invalid.yml index 3edd292..f6c7f71 100644 --- a/test/fixtures/configs/no_warn_invalid.yml +++ b/test/fixtures/configs/no_warn_invalid.yml @@ -1,4 +1,5 @@ -inherit_from: "../../../.rubocop.yml" +inherit_from: "../.rubocop.yml" Markdown: WarnInvalid: false + diff --git a/test/integration_test.rb b/test/integration_test.rb index eb19490..366c365 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -6,11 +6,11 @@ module RuboCopRunner def run_rubocop(path, options: "") - md_path = File.expand_path("../lib/rubocop-md.rb", __dir__) output, _status = Open3.capture2( - "bundle exec rubocop -r #{md_path} #{options} #{path}", + "bundle exec rubocop #{options} #{path}", chdir: File.join(__dir__, "fixtures") ) + output end end @@ -26,6 +26,14 @@ def test_single_snippet_file assert_match %r{Style/StringLiterals}, res end + def test_file_with_format_options + res = run_rubocop("single_snippet.md", options: "--format progress") + + assert_match %r{Inspecting 1 file}, res + assert_match %r{1 offense detected}, res + assert_match %r{Style/StringLiterals}, res + end + def test_multiple_snippets_file res = run_rubocop("multiple_snippets.markdown") From 2dc6053f8f67631cc05c41de8dc962540f94f497 Mon Sep 17 00:00:00 2001 From: prog-supdex Date: Thu, 19 Oct 2023 16:54:27 +0200 Subject: [PATCH 2/2] run Raketask :test twice with different modes --- Rakefile | 13 ++++++++++++- test/integration_test.rb | 19 ++++++++++++++++++- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/Rakefile b/Rakefile index e92e2be..87233b3 100644 --- a/Rakefile +++ b/Rakefile @@ -2,7 +2,7 @@ require "bundler/gem_tasks" require "rake/testtask" require "rubocop/rake_task" -Rake::TestTask.new(:test) do |t| +Rake::TestTask.new(:rubocop_md_tests) do |t| t.libs << "test" t.libs << "lib" t.warning = false @@ -11,4 +11,15 @@ 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 + + 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 +end + task default: [:rubocop, :test] diff --git a/test/integration_test.rb b/test/integration_test.rb index 366c365..b04fb23 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -5,14 +5,31 @@ require "fileutils" module RuboCopRunner + MD_LOAD_INLINE_MODE = "inline" + def run_rubocop(path, options: "") output, _status = Open3.capture2( - "bundle exec rubocop #{options} #{path}", + cmd_command_by_env(path, options), 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