Class: CC::CLI::Test
Constant Summary
Constants inherited
from Command
Command::CODECLIMATE_YAML
Instance Method Summary
collapse
Methods inherited from Command
command_name, #execute, #fatal, #initialize, #require_codeclimate_yml, #say, #success, #warn
Instance Method Details
#announce_fail(marker, actual_issues) ⇒ Object
154
155
156
157
158
159
160
161
|
# File 'lib/cc/cli/test.rb', line 154
def announce_fail(marker, actual_issues)
say colorize(format("FAIL %3d: %s", marker.line, marker.line_text), :red)
say colorize("Expected:", :yellow)
say colorize(JSON.pretty_generate(marker.issue), :yellow)
say "\n"
say colorize("Actual:", :yellow)
say colorize(JSON.pretty_generate(actual_issues), :yellow)
end
|
#announce_pass(marker) ⇒ Object
150
151
152
|
# File 'lib/cc/cli/test.rb', line 150
def announce_pass(marker)
say format("PASS %3d: %s", marker.line, marker.line_text)
end
|
#capture_stdout ⇒ Object
Stolen from ActiveSupport (where it was deprecated)
254
255
256
257
258
259
260
261
262
263
264
265
266
267
|
# File 'lib/cc/cli/test.rb', line 254
def capture_stdout
captured_stream = Tempfile.new("stdout")
origin_stream = $stdout.dup
$stdout.reopen(captured_stream)
yield
$stdout.rewind
return captured_stream.read
ensure
captured_stream.close
captured_stream.unlink
$stdout.reopen(origin_stream)
end
|
#codeclimate_analyze(relative_path) ⇒ Object
187
188
189
190
191
192
193
194
195
196
197
198
199
|
# File 'lib/cc/cli/test.rb', line 187
def codeclimate_analyze(relative_path)
codeclimate_path = File.expand_path(File.join(File.dirname(__FILE__), "../../../bin/codeclimate"))
system([
"unset CODE_PATH &&",
"unset FILESYSTEM_DIR &&",
Shellwords.escape(codeclimate_path),
"analyze",
"--engine", Shellwords.escape(@engine_name),
"-f", "json",
Shellwords.escape(relative_path)
].join(" "))
end
|
#compare_issues(actual_issues, markers) ⇒ Object
122
123
124
125
126
127
128
|
# File 'lib/cc/cli/test.rb', line 122
def compare_issues(actual_issues, markers)
markers.each do |marker|
validate_issue(marker, actual_issues)
end
validate_unexpected_issues(actual_issues)
end
|
#create_tmpdir ⇒ Object
221
222
223
224
225
|
# File 'lib/cc/cli/test.rb', line 221
def create_tmpdir
tmpdir = File.join("/tmp/cc", SecureRandom.uuid)
FileUtils.mkdir_p(tmpdir)
tmpdir
end
|
#engine_image ⇒ Object
249
250
251
|
# File 'lib/cc/cli/test.rb', line 249
def engine_image
engine_registry[@engine_name]["image"]
end
|
#engine_spec ⇒ Object
245
246
247
|
# File 'lib/cc/cli/test.rb', line 245
def engine_spec
@engine_spec ||= JSON.parse(`docker run --rm #{engine_image} cat /engine.json`)
end
|
#fuzzy_match(expected, actual) ⇒ Object
173
174
175
176
177
|
# File 'lib/cc/cli/test.rb', line 173
def fuzzy_match(expected, actual)
expected.all? do |key, value|
actual[key] == value
end
end
|
#issues_in(test_file) ⇒ Object
179
180
181
182
183
184
185
|
# File 'lib/cc/cli/test.rb', line 179
def issues_in(test_file)
issue_docs = capture_stdout do
codeclimate_analyze(test_file)
end
JSON.parse(issue_docs)
end
|
#locate_match(actual_issues, marker) ⇒ Object
140
141
142
143
144
145
146
147
148
|
# File 'lib/cc/cli/test.rb', line 140
def locate_match(actual_issues, marker)
actual_issues.each_with_index do |actual, index|
if fuzzy_match(marker.issue, actual)
return index
end
end
nil
end
|
#markers_in(test_file) ⇒ Object
209
210
211
212
213
214
215
216
217
218
219
|
# File 'lib/cc/cli/test.rb', line 209
def markers_in(test_file)
lines = File.readlines(test_file)
[].tap do |markers|
lines.each_with_index do |line, index|
if line =~ /\[issue\].*/
markers << Marker.from_text(@engine_name, test_file, index + 1, line)
end
end
end
end
|
#null_container_id ⇒ Object
231
232
233
234
235
|
# File 'lib/cc/cli/test.rb', line 231
def null_container_id
@null_container_id = `docker run -d #{engine_image} false`.chomp
end
|
#prepare_working_dir ⇒ Object
201
202
203
204
205
206
207
|
# File 'lib/cc/cli/test.rb', line 201
def prepare_working_dir
`git init`
File.open(".codeclimate.yml", "w") do |config|
config.write("engines:\n #{@engine_name}:\n enabled: true")
end
end
|
#process_file(test_file) ⇒ Object
115
116
117
118
119
120
|
# File 'lib/cc/cli/test.rb', line 115
def process_file(test_file)
markers = markers_in(test_file)
actual_issues = issues_in(test_file)
compare_issues(actual_issues, markers)
end
|
#remove_null_container ⇒ Object
237
238
239
|
# File 'lib/cc/cli/test.rb', line 237
def remove_null_container
`docker rm -f #{null_container_id}` if null_container_id
end
|
#run ⇒ Object
72
73
74
75
76
77
78
79
80
|
# File 'lib/cc/cli/test.rb', line 72
def run
@engine_name = @args.first
if @engine_name.blank?
fatal "Usage: codeclimate test #{rainbow.wrap('engine_name').underline}"
end
test_engine
end
|
#run_tests ⇒ Object
108
109
110
111
112
113
|
# File 'lib/cc/cli/test.rb', line 108
def run_tests
Dir["**/*"].each do |test_file|
next unless File.file?(test_file)
process_file(test_file)
end
end
|
#test_engine ⇒ Object
82
83
84
85
86
87
88
89
90
|
# File 'lib/cc/cli/test.rb', line 82
def test_engine
within_tempdir do
prepare_working_dir
unpack_tests
run_tests
end
ensure
remove_null_container
end
|
#test_paths ⇒ Object
241
242
243
|
# File 'lib/cc/cli/test.rb', line 241
def test_paths
Array.wrap(engine_spec["test_paths"])
end
|
#unpack(path) ⇒ Object
227
228
229
|
# File 'lib/cc/cli/test.rb', line 227
def unpack(path)
system("docker cp #{null_container_id}:#{path} .")
end
|
#unpack_tests ⇒ Object
102
103
104
105
106
|
# File 'lib/cc/cli/test.rb', line 102
def unpack_tests
test_paths.each do |test_path|
unpack(test_path)
end
end
|
#validate_issue(marker, actual_issues) ⇒ Object
130
131
132
133
134
135
136
137
138
|
# File 'lib/cc/cli/test.rb', line 130
def validate_issue(marker, actual_issues)
if (index = locate_match(actual_issues, marker))
announce_pass(marker)
actual_issues.delete_at(index)
else
announce_fail(marker, actual_issues)
fatal "Expected issue not found."
end
end
|
#validate_unexpected_issues(actual_issues) ⇒ Object
163
164
165
166
167
168
169
170
171
|
# File 'lib/cc/cli/test.rb', line 163
def validate_unexpected_issues(actual_issues)
if actual_issues.any?
say colorize("Actuals not empty after matching.", :red)
say "\n"
say colorize("#{actual_issues.size} remaining:", :yellow)
say colorize(JSON.pretty_generate(actual_issues), :yellow)
fatal "Unexpected issue found."
end
end
|
#within_tempdir ⇒ Object
92
93
94
95
96
97
98
99
100
|
# File 'lib/cc/cli/test.rb', line 92
def within_tempdir
tmpdir = create_tmpdir
Dir.chdir(tmpdir) do
yield
end
ensure
FileUtils.rm_rf(tmpdir)
end
|