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/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..12e416d 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -5,30 +5,32 @@ 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__) - def run_rubocop(path, options: "") - output, _status = Open3.capture2( - 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 ENV["MD_LOAD_MODE"] == "options" + options = "#{options} -r #{md_path}" + end - if load_mode == MD_LOAD_INLINE_MODE - md_path = File.expand_path("../lib/rubocop-md.rb", __dir__) + if ENV["MD_LOAD_MODE"] == "config" + # Add "_with_require" suffix + if config + config = config.sub(/\.yml$/, "_with_require.yml") + else + config = md_config_path + end + end - cmd_command = "#{cmd_command} -r #{md_path}" + if config + options = "#{options} -c #{config}" end - "#{cmd_command} #{options} #{path}" + output, _status = Open3.capture2( + "bundle exec rubocop #{options} #{path}", + chdir: File.join(__dir__, "fixtures") + ) + output end end @@ -70,7 +72,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 +82,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"