Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code style validation for Python using YAPF #12

Merged
merged 1 commit into from
Oct 18, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .mailmap
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Nikolay Kasyanov <[email protected]> <[email protected]>
Ersen Tekin <[email protected]>
Roberto Di Remigio <[email protected]> <[email protected]>
33 changes: 23 additions & 10 deletions lib/code_style_validation/plugin.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module Danger
# This plugin uses 'clang-format' to look for code style violations in added
# lines on the current MR / PR, and offers inline patches.
# By default only Objective-C files, with extensions ".h", ".m", and ".mm"
# are checked.
# This plugin uses code style checker (validator in the following) to look
# for code style violations in added lines on the current MR / PR, and offers
# inline patches.
# The default validator is 'clang-format'. Only Objective-C files, with
# extensions ".h", ".m", and ".mm" are checked.
# It is possible to use other validators for other languages, e.g. 'yapf' for Python.
#
# @example Ensure that changes do not violate code style in Objective-C files
#
Expand All @@ -12,6 +14,11 @@ module Danger
#
# code_style_validation.check file_extensions: ['.hpp', '.cpp']
#
# @example Ensure that changes do not violate code style in Python files with YAPF
#
# code_style_validation.check validator: 'yapf',
# file_extensions: ['.py']
#
# @example Ensure that changes do not violate code style, ignoring Pods directory
#
# code_style_validation.check ignore_file_patterns: [/^Pods\//]
Expand All @@ -22,12 +29,12 @@ module Danger
class DangerCodeStyleValidation < Plugin
VIOLATION_ERROR_MESSAGE = 'Code style violations detected.'.freeze

# Validates the code style of changed & added files using clang-format.
# Validates the code style of changed & added files using a validator program.
# Generates Markdown message with respective patches.
#
# @return [void]
def check(config = {})
defaults = {validator: ['clang-format'], file_extensions: ['.h', '.m', '.mm'], ignore_file_patterns: []}
defaults = {validator: 'clang-format', file_extensions: ['.h', '.m', '.mm'], ignore_file_patterns: []}
config = defaults.merge(config)
validator = *config[:validator]
file_extensions = [*config[:file_extensions]]
Expand Down Expand Up @@ -55,7 +62,7 @@ def check(config = {})
message += '* `' + file_name + "`\n\n"
end
message += 'Execute one of the following actions and commit again:' + "\n"
message += '1. Run `clang-format` on the offending files' + "\n"
message += '1. Run `%s` on the offending files' % validator + "\n"
message += '2. Apply the suggested patches with `git apply patch`.' + "\n\n"
message += patches.join("\n")
end
Expand Down Expand Up @@ -151,18 +158,24 @@ def resolve_changes(validator, changes)

offending_files = []
patches = []
# patches.each do |patch|
if validator.include? "clang-format"
# clang-format
changed_lines_option = "-lines=%s:%s"
else
# YAPF
changed_lines_option = "--lines=%s-%s"
end
changes.each do |file_name, changed_lines|
changed_lines_command_array = []

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

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

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

formatted_temp_file = Tempfile.new('temp-formatted')
Expand Down
10 changes: 10 additions & 0 deletions spec/code_style_validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,16 @@ module Danger
expect(@dangerfile.status_report[:errors]).to eq([])
end

it 'Accepts a validator other than clang-format' do
diff = File.read('spec/fixtures/violated_diff.diff')

allow(@my_plugin.github).to receive(:pr_diff).and_return diff
@my_plugin.check validator: 'yapf',
file_extensions: ['.py']

expect(@dangerfile.status_report[:errors]).to eq([])
end

it 'Does not report error when code not violated' do
diff = File.read('spec/fixtures/innocent_diff.diff')

Expand Down