From 5ddae4dfbd6822fb3d3433481b63c8083b020160 Mon Sep 17 00:00:00 2001 From: Roberto Di Remigio Date: Tue, 17 Oct 2017 15:53:31 +0200 Subject: [PATCH] Enables use of YAPF for Python code style validation --- .mailmap | 3 +++ lib/code_style_validation/plugin.rb | 33 ++++++++++++++++++++--------- spec/code_style_validation_spec.rb | 10 +++++++++ 3 files changed, 36 insertions(+), 10 deletions(-) create mode 100644 .mailmap diff --git a/.mailmap b/.mailmap new file mode 100644 index 0000000..c9a5fa0 --- /dev/null +++ b/.mailmap @@ -0,0 +1,3 @@ +Nikolay Kasyanov +Ersen Tekin +Roberto Di Remigio diff --git a/lib/code_style_validation/plugin.rb b/lib/code_style_validation/plugin.rb index 76980e3..aac7283 100755 --- a/lib/code_style_validation/plugin.rb +++ b/lib/code_style_validation/plugin.rb @@ -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 # @@ -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\//] @@ -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]] @@ -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 @@ -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') diff --git a/spec/code_style_validation_spec.rb b/spec/code_style_validation_spec.rb index 007e338..4abf94b 100755 --- a/spec/code_style_validation_spec.rb +++ b/spec/code_style_validation_spec.rb @@ -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')