Skip to content

Commit

Permalink
refactor: handling two require modes
Browse files Browse the repository at this point in the history
  • Loading branch information
palkan committed Oct 21, 2023
1 parent b73d227 commit 9677c82
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 35 deletions.
27 changes: 16 additions & 11 deletions Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -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]
2 changes: 1 addition & 1 deletion lib/rubocop/markdown/rubocop_ext.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/fixtures/configs/no_autodetect.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
inherit_from: "../.rubocop.yml"
inherit_from: "../../../.rubocop.yml"

Markdown:
Autodetect: false
7 changes: 7 additions & 0 deletions test/fixtures/configs/no_autodetect_with_require.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
inherit_from: "../../../.rubocop.yml"

require:
- "rubocop-md"

Markdown:
Autodetect: false
2 changes: 1 addition & 1 deletion test/fixtures/configs/no_warn_invalid.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
inherit_from: "../.rubocop.yml"
inherit_from: "../../../.rubocop.yml"

Markdown:
WarnInvalid: false
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures/configs/no_warn_invalid_with_require.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
inherit_from: "../../../.rubocop.yml"

require:
- "rubocop-md"

Markdown:
WarnInvalid: false

40 changes: 19 additions & 21 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
@@ -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"

Expand Down

0 comments on commit 9677c82

Please sign in to comment.