From e3b744cfe1890820866e9a97058a332717cb75f5 Mon Sep 17 00:00:00 2001 From: Mathieu Rul Date: Tue, 23 Mar 2021 23:40:20 +0100 Subject: [PATCH] Simplify code --- lib/pmd/entity/pmd_file.rb | 7 +++---- lib/pmd/plugin.rb | 8 ++++++-- spec/pmd_spec.rb | 32 ++++++++++++++++---------------- 3 files changed, 25 insertions(+), 22 deletions(-) diff --git a/lib/pmd/entity/pmd_file.rb b/lib/pmd/entity/pmd_file.rb index 96c4268..83731ec 100644 --- a/lib/pmd/entity/pmd_file.rb +++ b/lib/pmd/entity/pmd_file.rb @@ -20,10 +20,9 @@ def initialize(prefix, file) @file = file @absolute_path = file.attribute('name').value.to_s - @prefix = prefix + (prefix.end_with?(file_separator) ? '' : file_separator) - - @relative_path = if @absolute_path.start_with?(@prefix) - @absolute_path[@prefix.length, @absolute_path.length - @prefix.length] + prefix += (prefix.end_with?(file_separator) ? '' : file_separator) + @relative_path = if @absolute_path.start_with?(prefix) + @absolute_path[prefix.length, @absolute_path.length - prefix.length] else @absolute_path end diff --git a/lib/pmd/plugin.rb b/lib/pmd/plugin.rb index 8c2e2d1..1e4cf6e 100644 --- a/lib/pmd/plugin.rb +++ b/lib/pmd/plugin.rb @@ -148,6 +148,8 @@ def exec_gradle_task # A getter for `pmd_report`, returning PMD report. # + # @param [String] report_file The report file. + # # @return [Oga::XML::Document] def pmd_report(report_file) require 'oga' @@ -156,6 +158,8 @@ def pmd_report(report_file) # A getter for PMD issues, returning PMD issues. # + # @param [String] report_file The report file. + # # @return [Array[PmdFile]] def pmd_issues(report_file) pmd_report(report_file).xpath('//file').map do |pmd_file| @@ -184,14 +188,14 @@ def do_comment(report_files, inline_mode) pmd_issues.push(pmd_file) - parse_file(pmd_file, inline_mode) + send_comment(pmd_file, inline_mode) end end pmd_issues end - def parse_file(pmd_file, inline_mode) + def send_comment(pmd_file, inline_mode) pmd_file.violations.each do |pmd_violation| if inline_mode send(pmd_violation.type, pmd_violation.description, file: pmd_file.relative_path, line: pmd_violation.line) diff --git a/spec/pmd_spec.rb b/spec/pmd_spec.rb index d08bf8c..5f6c1fc 100644 --- a/spec/pmd_spec.rb +++ b/spec/pmd_spec.rb @@ -71,10 +71,10 @@ module Danger it 'Report with report file' do # noinspection RubyLiteralArrayInspection target_files = [ - "app/src/main/java/com/android/sample/MainActivity.java", - "app/src/main/java/com/android/sample/Tools.java", - "app/src/test/java/com/android/sample/ExampleUnitTest.java", - "app/src/test/java/com/android/sample/ToolsTest.java" + 'app/src/main/java/com/android/sample/MainActivity.java', + 'app/src/main/java/com/android/sample/Tools.java', + 'app/src/test/java/com/android/sample/ExampleUnitTest.java', + 'app/src/test/java/com/android/sample/ToolsTest.java' ] allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files) @@ -130,16 +130,16 @@ module Danger expect(pmd_issue4.violations[1].description).to eq("The JUnit 4 test method name 'getLabel_2' doesn't match '[a-z][a-zA-Z0-9]*'") end - it "Report with report file not in target files" do + it 'Report with report file not in target files' do # noinspection RubyLiteralArrayInspection target_files = [ - "app/src/main/java/com/android/sample/Tools.java", - "app/src/test/java/com/android/sample/ToolsTest.java" + 'app/src/main/java/com/android/sample/Tools.java', + 'app/src/test/java/com/android/sample/ToolsTest.java' ] allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files) - @pmd.report_file = "spec/fixtures/pmd_report.xml" - @pmd.root_path = "/Users/developer/sample/" + @pmd.report_file = 'spec/fixtures/pmd_report.xml' + @pmd.root_path = '/Users/developer/sample/' @pmd.skip_gradle_task = true pmd_issues = @pmd.report @@ -170,15 +170,15 @@ module Danger expect(pmd_issue2.violations[1].description).to eq("The JUnit 4 test method name 'getLabel_2' doesn't match '[a-z][a-zA-Z0-9]*'") end - it "Report with report files" do + it 'Report with report files' do # noinspection RubyLiteralArrayInspection target_files = [ - "app/src/main/java/com/android/sample/Application.java", - "app/src/main/java/com/android/sample/MainActivity.java", - "app/src/main/java/com/android/sample/Tools.java", - "app/src/main/java/com/android/sample/Utils.java", - "app/src/test/java/com/android/sample/ExampleUnitTest.java", - "app/src/test/java/com/android/sample/ToolsTest.java" + 'app/src/main/java/com/android/sample/Application.java', + 'app/src/main/java/com/android/sample/MainActivity.java', + 'app/src/main/java/com/android/sample/Tools.java', + 'app/src/main/java/com/android/sample/Utils.java', + 'app/src/test/java/com/android/sample/ExampleUnitTest.java', + 'app/src/test/java/com/android/sample/ToolsTest.java' ] allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)