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 20, 2023
1 parent 2dc6053 commit 74dff95
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 34 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 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

44 changes: 23 additions & 21 deletions test/integration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
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 74dff95

Please sign in to comment.