forked from fengguang/lkp-tests
-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathRakefile
More file actions
239 lines (193 loc) · 6.69 KB
/
Copy pathRakefile
File metadata and controls
239 lines (193 loc) · 6.69 KB
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
require 'rubygems'
require 'bundler/setup'
require 'rake/file_utils'
require 'rspec/core/rake_task'
require 'fileutils'
require 'rubocop/rake_task'
require 'English'
require 'term/ansicolor'
class String
include Term::ANSIColor
end
def tool_available?(cmd)
system("which #{cmd} >/dev/null 2>&1")
end
GIT_SCANNABLE_FILES = 'git ls-files -z --cached --others --exclude-standard -- . ":!workspace_tmp"'.freeze
# Same file set, narrowed to executables under 100k, matching the `find
# -type f -executable ! -size +100k` filter every lint task used to repeat.
def scannable_executables_cmd
"#{GIT_SCANNABLE_FILES} | xargs -0 -I{} find \"{}\" -maxdepth 0 -type f -executable ! -size +100k 2>/dev/null"
end
desc 'Show help'
task :help do
puts <<~EOF
## SPEC
usage
rake spec [spec=result_path]
example
rake spec # check all unit tests status
rake spec spec=job # check spec/job_spec.rb status
## RUBOCOP
usage
rake rubocop [file=pattern]
example
rake rubocop file="lib/**/*.rb" # check all lib files
EOF
end
RSpec::Core::RakeTask.new do |t|
ENV['LKP_SRC'] ||= File.expand_path File.dirname(__FILE__).to_s
puts "PWD = #{Dir.pwd}"
puts "ENV['LKP_SRC'] = #{ENV.fetch('LKP_SRC', nil)}"
spec = ENV['spec'] || '*'
spec = spec.sub(/_spec$/, '')
t.pattern = "spec/**{,/*/**}/#{spec}_spec.rb"
t.rspec_opts = "--example '#{ENV['example']}'" if ENV['example']
end
if ENV['GENERATE_REPORTS'] == 'true'
require 'ci/reporter/rake/rspec'
task spec: 'ci:setup:rspec'
end
RuboCop::RakeTask.new(:rubocop) do |t|
ruby_version = `ruby --version | grep -oE "[0-9]+\\.[0-9]+"`.chomp
rubocop_config_file = ".rubocop.#{ruby_version}.yml"
rubocop_config_file = '.rubocop.yml' unless File.size?(rubocop_config_file)
t.options = ['-D', "-c#{rubocop_config_file}"]
t.options.unshift('-a') if ENV['fix'] == '1'
t.patterns = [ENV['file']] if ENV['file']
version = `rubocop --version 2>&1`.strip
puts "rubocop (#{version}) start...".yellow
puts "PWD = #{Dir.pwd}"
puts "rubocop.patterns = #{t.patterns}"
puts "rubocop.options = #{t.options}"
end
desc 'Run syntax check'
task :syntax do
puts 'syntax start...'.yellow
executables = `#{scannable_executables_cmd}`.split("\n").join(' ')
sh "grep -s -l '^#!/.*ruby$' #{executables} | xargs -P$(nproc) -n1 ruby -c >/dev/null", verbose: false do |ok, res|
exit res.exitstatus unless ok
end
sh "grep -s -l '^#!/.*bash$' #{executables} | xargs -P$(nproc) -n1 bash -n", verbose: false do |ok, res|
exit res.exitstatus unless ok
end
sh "grep -s -l '^#!/bin/sh$' #{executables} | xargs -P$(nproc) -n1 dash -n", verbose: false do |ok, res|
exit res.exitstatus unless ok
end
puts 'syntax OK'.green
end
desc 'Run shfmt'
task :shfmt do
unless tool_available?('shfmt')
puts 'shfmt not installed, skipping'.yellow
next
end
executables = if ENV['file']
ENV['file']
else
dir = ENV['dir'] || '.'
find_opts = '-type f -executable ' \
'! -path "*/kbuild/kbuild.sh" ' \
'! -path "*/node_modules/*" ' \
'! -path "*/sbin/makepkg" ' \
'! -path "*/sbin/pacman-LKP" ' \
'! -path "*/workspace_tmp/*" ' \
'! -path "*/.claude/worktrees/*" ' \
'! -path "./.git*" ' \
'! -path "./vendor*" ' \
'! -size +100k'
`find #{dir} #{find_opts} | xargs -P$(nproc) grep -s -l -e '^#!/.*bash$' -e '^#!/bin/sh$'`.split("\n").join(' ')
end
version = `shfmt --version 2>&1`.strip.delete_prefix('v')
puts "shfmt (#{version}) start...".yellow
mode_flag = ENV['fix'] == '1' ? '-w' : '-d'
sh "shfmt #{mode_flag} -ln bash -i 0 -fn #{executables}", verbose: false do |ok, res|
if ok
puts "shfmt (#{version}) OK".green
else
exit res.exitstatus
end
end
end
desc 'Run shellcode (shellcheck and shfmt)'
task shellcode: %i[shellcheck shfmt]
desc 'Run shellcheck'
task :shellcheck do
unless tool_available?('shellcheck')
puts 'shellcheck not installed, skipping'.yellow
next
end
executables = ENV['file'] || `#{scannable_executables_cmd} | xargs -P$(nproc) grep -s -l -e '^#!/.*bash$' -e '^#!/bin/sh$'`.split("\n").join(' ')
format = ENV['format'] || 'tty'
base_cmd = "shellcheck -S warning -f #{format}"
base_cmd += " -i #{ENV['code']}" if ENV['code']
version = `shellcheck --version 2>&1`.match(/^version: (\S+)/)&.captures&.first || '?'
puts "shellcheck (#{version}) start...".yellow
sh "#{base_cmd} #{executables}", verbose: false do |ok, res|
if ok
puts "shellcheck (#{version}) OK".green
else
exit res.exitstatus
end
end
end
desc 'Run yamllint'
task :yamllint do
unless tool_available?('yamllint')
puts 'yamllint not installed, skipping'.yellow
next
end
version = `yamllint --version 2>&1`.strip.split.last
puts "yamllint (#{version}) start...".yellow
sh 'yamllint', '--strict', '--format=auto', '.', verbose: false do |ok, res|
if ok
puts "yamllint (#{version}) OK".green
else
exit res.exitstatus
end
end
end
desc 'Run ruff'
task :ruff do
unless tool_available?('ruff')
puts 'ruff not installed, skipping'.yellow
next
end
ENV['RUFF_CACHE_DIR'] ||= '/tmp/.ruff_cache'
version = `python3 -m ruff --version 2>&1`.strip.split.last
puts "ruff (#{version}) start...".yellow
fix_flag = ENV['fix'] == '1' ? '--fix' : ''
format_flag = ENV['fix'] == '1' ? '' : '--check'
sh "python3 -m ruff check #{fix_flag} #{ENV['file'] || '.'} && python3 -m ruff format #{format_flag} #{ENV['file'] || '.'}", verbose: false do |ok, res|
if ok
puts "ruff (#{version}) OK".green
else
exit res.exitstatus
end
end
end
desc 'Run pylint'
task :pylint do
unless tool_available?('pylint')
puts 'pylint not installed, skipping'.yellow
next
end
version = `pylint --version 2>&1`.split[1]
puts "pylint (#{version}) start...".yellow
sh "pylint --recursive y #{ENV['file'] || '.'}", verbose: false do |ok, res|
if ok
puts "pylint (#{version}) OK".green
else
exit res.exitstatus
end
end
end
desc 'Run code check'
task code: %i[syntax yamllint shellcode rubocop ruff pylint]
namespace :docker do
desc 'Build docker image'
task :build do
# image is in the form of debian/buster
raise "ENV['image'] can't be #{ENV['image'].inspect}" unless ENV['image']
sh "docker build . -f docker/#{ENV['image'].split('/').first}/Dockerfile -t lkp-tests/#{ENV.fetch('image', nil)} --build-arg base_image=#{ENV['image'].sub('/', ':')}"
end
end