Skip to content

Instantly share code, notes, and snippets.

@Earlopain
Created January 5, 2025 12:18
Show Gist options
  • Save Earlopain/5883e948b440e9cf34b69852d95b53b9 to your computer and use it in GitHub Desktop.
Save Earlopain/5883e948b440e9cf34b69852d95b53b9 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
namespace :rubocop do
desc "Ensure there's a local copy of rubocop"
file "tmp/rubocop" do
sh "git clone --depth=1 https://github.com/rubocop/rubocop tmp/rubocop"
end
desc "Ensure we have a fixtures directory for the rubocop tests"
file "test/prism/fixtures/rubocop" do
mkdir_p "test/prism/fixtures/rubocop"
end
desc "Import the rubocop tests"
task import: ["tmp/rubocop", "test/prism/fixtures/rubocop"] do
cp "tmp/rubocop/LICENSE.txt", "test/prism/fixtures/rubocop/LICENSE"
require "bundler/inline"
gemfile do
source "https://rubygems.org"
eval_gemfile("tmp/rubocop/Gemfile")
end
$LOAD_PATH.unshift File.expand_path("tmp/rubocop/spec")
require_relative "../tmp/rubocop/spec/spec_helper"
# RSpec.configure do |config|
# config.before :example do |example|
# $current_description = example.metadata[:full_description]
# end
# end
module Hook
COLLECTED = []
private
def expect_offense(source, *, **replacements)
test_file = caller_locations(1..1).first.absolute_path
name = File.basename(test_file).sub("_spec.rb", "")
code = format_offense(source, **replacements)
code_no_annotations = RuboCop::RSpec::ExpectOffense::AnnotatedSource.parse(code).plain_source
COLLECTED << code_no_annotations
super
end
def expect_correction(code, ...)
test_file = caller_locations(1..1).first.absolute_path
name = File.basename(test_file).sub("_spec.rb", "")
COLLECTED << code
super
end
def expect_no_offenses(code, ...)
test_file = caller_locations(1..1).first.absolute_path
name = File.basename(test_file).sub("_spec.rb", "")
COLLECTED << code
super
end
end
RuboCop::RSpec::ExpectOffense.prepend(Hook)
RSpec::Core::Runner.run(Dir.glob('tmp/rubocop/spec/rubocop/cop/*/*'), STDERR, STDOUT)
require "digest"
Hook::COLLECTED.reject! { _1.encoding != Encoding::UTF_8 }
Hook::COLLECTED.select! { Prism.parse_success?(_1, version: "3.3") }
Hook::COLLECTED.each do |code|
# codes.reject! { _1.encoding != Encoding::UTF_8 }
# codes.select! { Prism.parse_success?(_1, version: "3.3") }
name = Digest::MD5.hexdigest(code)
filepath = "test/prism/fixtures/rubocop/#{name}.txt"
File.write(filepath, code)
end
end
desc "Clean up tmp files related to rubocop"
task :clean do
rm_rf "tmp/rubocop"
rm_rf "test/prism/fixtures/rubocop"
rm_rf "test/prism/snapshots/rubocop"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment