Skip to content

Commit 3756e99

Browse files
robertodrnikolaykasyanov
authored andcommitted
Enables use of YAPF for Python code style validation
1 parent c52c367 commit 3756e99

File tree

3 files changed

+36
-10
lines changed

3 files changed

+36
-10
lines changed

.mailmap

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Nikolay Kasyanov <nikolay.kasyanov@flixbus.com> <nikolaykasyanov@users.noreply.github.com>
2+
Ersen Tekin <ersen.tekin@flixbus.com>
3+
Roberto Di Remigio <roberto.diremigio@gmail.com> <robertodr@users.noreply.github.com>

lib/code_style_validation/plugin.rb

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
module Danger
2-
# This plugin uses 'clang-format' to look for code style violations in added
3-
# lines on the current MR / PR, and offers inline patches.
4-
# By default only Objective-C files, with extensions ".h", ".m", and ".mm"
5-
# are checked.
2+
# This plugin uses code style checker (validator in the following) to look
3+
# for code style violations in added lines on the current MR / PR, and offers
4+
# inline patches.
5+
# The default validator is 'clang-format'. Only Objective-C files, with
6+
# extensions ".h", ".m", and ".mm" are checked.
7+
# It is possible to use other validators for other languages, e.g. 'yapf' for Python.
68
#
79
# @example Ensure that changes do not violate code style in Objective-C files
810
#
@@ -12,6 +14,11 @@ module Danger
1214
#
1315
# code_style_validation.check file_extensions: ['.hpp', '.cpp']
1416
#
17+
# @example Ensure that changes do not violate code style in Python files with YAPF
18+
#
19+
# code_style_validation.check validator: 'yapf',
20+
# file_extensions: ['.py']
21+
#
1522
# @example Ensure that changes do not violate code style, ignoring Pods directory
1623
#
1724
# code_style_validation.check ignore_file_patterns: [/^Pods\//]
@@ -22,12 +29,12 @@ module Danger
2229
class DangerCodeStyleValidation < Plugin
2330
VIOLATION_ERROR_MESSAGE = 'Code style violations detected.'.freeze
2431

25-
# Validates the code style of changed & added files using clang-format.
32+
# Validates the code style of changed & added files using a validator program.
2633
# Generates Markdown message with respective patches.
2734
#
2835
# @return [void]
2936
def check(config = {})
30-
defaults = {validator: ['clang-format'], file_extensions: ['.h', '.m', '.mm'], ignore_file_patterns: []}
37+
defaults = {validator: 'clang-format', file_extensions: ['.h', '.m', '.mm'], ignore_file_patterns: []}
3138
config = defaults.merge(config)
3239
validator = *config[:validator]
3340
file_extensions = [*config[:file_extensions]]
@@ -55,7 +62,7 @@ def check(config = {})
5562
message += '* `' + file_name + "`\n\n"
5663
end
5764
message += 'Execute one of the following actions and commit again:' + "\n"
58-
message += '1. Run `clang-format` on the offending files' + "\n"
65+
message += '1. Run `%s` on the offending files' % validator + "\n"
5966
message += '2. Apply the suggested patches with `git apply patch`.' + "\n\n"
6067
message += patches.join("\n")
6168
end
@@ -151,18 +158,24 @@ def resolve_changes(validator, changes)
151158

152159
offending_files = []
153160
patches = []
154-
# patches.each do |patch|
161+
if validator.include? "clang-format"
162+
# clang-format
163+
changed_lines_option = "-lines=%s:%s"
164+
else
165+
# YAPF
166+
changed_lines_option = "--lines=%s-%s"
167+
end
155168
changes.each do |file_name, changed_lines|
156169
changed_lines_command_array = []
157170

158171
changed_lines.each do |line_number|
159-
changed_lines_command_array.push('-lines=' + line_number.to_s + ':' + line_number.to_s)
172+
changed_lines_command_array.push(changed_lines_option % [line_number.to_s, line_number.to_s])
160173
end
161174

162175
changed_lines_command = changed_lines_command_array.join(' ')
163176
format_command_array = [validator, changed_lines_command, file_name]
164177

165-
# clang-format command for formatting JUST changed lines
178+
# validator command for formatting JUST changed lines
166179
formatted = `#{format_command_array.join(' ')}`
167180

168181
formatted_temp_file = Tempfile.new('temp-formatted')

spec/code_style_validation_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,16 @@ module Danger
3333
expect(@dangerfile.status_report[:errors]).to eq([])
3434
end
3535

36+
it 'Accepts a validator other than clang-format' do
37+
diff = File.read('spec/fixtures/violated_diff.diff')
38+
39+
allow(@my_plugin.github).to receive(:pr_diff).and_return diff
40+
@my_plugin.check validator: 'yapf',
41+
file_extensions: ['.py']
42+
43+
expect(@dangerfile.status_report[:errors]).to eq([])
44+
end
45+
3646
it 'Does not report error when code not violated' do
3747
diff = File.read('spec/fixtures/innocent_diff.diff')
3848

0 commit comments

Comments
 (0)