Skip to content

Commit

Permalink
Simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
mathroule committed Mar 23, 2021
1 parent e6d16c3 commit e3b744c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
7 changes: 3 additions & 4 deletions lib/pmd/entity/pmd_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 6 additions & 2 deletions lib/pmd/plugin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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|
Expand Down Expand Up @@ -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)
Expand Down
32 changes: 16 additions & 16 deletions spec/pmd_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e3b744c

Please sign in to comment.