-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathRakefile
135 lines (117 loc) · 3.72 KB
/
Rakefile
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# frozen_string_literal: true
require "bundler/gem_tasks"
require "rake/testtask"
require "rubocop/rake_task"
RuboCop::RakeTask.new
task default: ['smoke:test', :check_default_yml]
task :check_default_yml do
require 'yaml'
YAML.safe_load_file('config/default.yml')
end
namespace :smoke do
COP_FILE_MAP = {
'YARD/TagTypeSyntax' => [
{ name: "tag_type_syntax" }
],
'YARD/MeaninglessTag' => [
{ name: "meaningless_tag" },
{ name: "meaningless_tag_n_plus_one_query" },
],
'YARD/MismatchName' => [
{ name: "mismatch_name", correct: true }
],
'YARD/CollectionStyle' => [
{ name: "collection_style", style: "long", correct: true },
{ name: "collection_style", style: "short", correct: true },
],
'YARD/CollectionType' => [
{ name: "collection_type", style: "long", correct: true },
{ name: "collection_type", style: "short", correct: true },
],
'YARD/TagTypePosition' => [
{ name: "tag_type_position" }
]
}
task :start_server do
sh "bundle exec rubocop --restart-server"
end
desc "Run testing for smoke files"
task test: [:start_server] do
require 'json'
errors = []
each_config do |cop, content, with_style_name, rb_path, json_path, cmd|
puts "Running #{rb_path} and #{json_path}"
actual = `#{cmd} #{rb_path}`
actual_out = JSON.parse(actual).except("metadata")
expect = File.read(json_path)
expect_out = JSON.parse(expect).except("metadata")
unless actual_out == expect_out
puts '---actual---'
pp actual_out
puts '---expect---'
pp expect_out
errors << "Change output `rubocop #{rb_path}` with #{json_path}"
end
if content[:correct]
corrected_path = "smoke/generated/#{with_style_name}_correct.rb"
puts "Running #{corrected_path}"
system("#{cmd} --autocorrect #{corrected_path}")
sh("git diff --exit-code #{corrected_path}")
end
end
unless errors.empty?
errors.each do |error|
$stderr.puts error
end
raise
end
end
desc "Regenerate smoke files"
task regenerate: [:start_server] do
require 'tempfile'
each_config do |cop, content, with_style_name, rb_path, json_path, cmd|
rm json_path rescue nil
puts "Generate #{json_path}"
if content[:correct]
correct_path = "smoke/generated/#{with_style_name}_correct.rb"
IO.copy_stream(rb_path, correct_path)
system("#{cmd} --autocorrect #{correct_path}")
end
sh("#{cmd} #{rb_path} | jq > #{json_path}")
end
end
def each_config
COP_FILE_MAP.each do |cop, contents|
contents.each do |content|
with_style = if content[:style]
"_#{content[:style]}"
else
""
end
with_style_name = "#{content[:name]}#{with_style}"
rb_path = "smoke/#{with_style_name}.rb"
json_path = "smoke/generated/#{with_style_name}.json"
cmds = ["bundle", "exec", "rubocop", "--only", cop, "--format", "json"]
if content[:style]
cmds << '--config' << "smoke/#{with_style_name}.yml"
else
cmds << '--config' << ".rubocop.yml"
end
yield [cop, content, with_style_name, rb_path, json_path, cmds.join(' ')]
end
end
end
end
desc 'Generate a new cop with a template'
task :new_cop, [:cop] do |_task, args|
require 'rubocop'
cop_name = args.fetch(:cop) do
warn 'usage: bundle exec rake new_cop[Department/Name]'
exit!
end
generator = RuboCop::Cop::Generator.new(cop_name)
generator.write_source
generator.inject_require(root_file_path: 'lib/rubocop/cop/yard_cops.rb')
generator.inject_config(config_file_path: 'config/default.yml')
puts generator.todo
end