danger-pmd 1.0.2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd4d93b63b4d3ea4eae56af9b26c904bcb3775556ba1ac76c0f3e946e4d9a020
4
- data.tar.gz: cab647a193c407d35280addde594b52fa3f2c8c07051de104acc4456d55274a7
3
+ metadata.gz: a536da3a3b5d479ac3f2ecc733a3751a58a09c15c06716f1c6d712c1d22f53c9
4
+ data.tar.gz: fed8e05378b9e6cc981055fbe63d7806452ed53d64d7b85dbade5dfd6b99d129
5
5
  SHA512:
6
- metadata.gz: 0df54a5be420d1dc391079f98c67d381897429434f2ddc21118b3557671f9b1b4cbc2f3f4078e8cbfe43ebc355d12842394a833e4f2f182ec1b3eaaf36fd3f1c
7
- data.tar.gz: 78145da8cabb58fce118efac7930ac5831f58aa83cc15fbe824775b163d952b7019fcc94043472b0ddb29f7c99454cf07da15f1ab3e742e3989232a57be02aff
6
+ metadata.gz: 12b0b73c71cad94acdd1d75e49addb227067ae1fe92950c54670bd49915717ddb16e53a218cca352f6a0cb690e777c9f7d112e06a59cd1ef48c1e066243f7b47
7
+ data.tar.gz: '068f0e26176ef43f1a3d07f6fccccc1b5d5eefec9b3d6a3d82726988920132e3e0661da4be7e236a365ceeb7d821a3b00d724b1fc0c5c0f293503973f9717f28'
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- danger-pmd (1.0.2)
4
+ danger-pmd (1.0.3)
5
5
  danger-plugin-api (~> 1.0)
6
6
  oga (~> 2.15)
7
7
 
data/README.md CHANGED
@@ -23,15 +23,15 @@ pmd.report
23
23
 
24
24
  <blockquote>Running PMD with a specific Gradle task or report file (glob accepted)
25
25
  <pre>
26
- pmd.gradle_task = 'app:pmd' # default: pmd
27
- pmd.report_file = 'module/build/reports/pmd/pmd.xml' # default: app/build/reports/pmd/pmd.xml
26
+ pmd.gradle_task = 'module:pmd' # default: 'pmd'
27
+ pmd.report_file = 'module/build/reports/pmd/pmd.xml' # default: 'app/build/reports/pmd/pmd.xml'
28
28
  pmd.report
29
29
  </pre>
30
30
  </blockquote>
31
31
 
32
32
  <blockquote>Running PMD with a specific root path
33
33
  <pre>
34
- pmd.root_path = '/Users/developer/project'
34
+ pmd.root_path = '/Users/developer/project' # default: result of `git rev-parse --show-toplevel`
35
35
  pmd.report
36
36
  </pre>
37
37
  </blockquote>
@@ -45,11 +45,17 @@ pmd.report
45
45
 
46
46
  <blockquote>Running PMD without running a Gradle task
47
47
  <pre>
48
- pmd.skip_gradle_task = true
48
+ pmd.skip_gradle_task = true # default: false
49
49
  pmd.report
50
50
  </pre>
51
51
  </blockquote>
52
52
 
53
+ <blockquote>Running PMD without inline comment
54
+ <pre>
55
+ pmd.report(inline_mode: false) # default: true
56
+ </pre>
57
+ </blockquote>
58
+
53
59
  #### Attributes
54
60
 
55
61
  `gradle_task` - Custom Gradle task to run.
@@ -20,10 +20,9 @@ class PmdFile
20
20
  @file = file
21
21
  @absolute_path = file.attribute('name').value.to_s
22
22
 
23
- @prefix = prefix + (prefix.end_with?(file_separator) ? '' : file_separator)
24
-
25
- @relative_path = if @absolute_path.start_with?(@prefix)
26
- @absolute_path[@prefix.length, @absolute_path.length - @prefix.length]
23
+ prefix += (prefix.end_with?(file_separator) ? '' : file_separator)
24
+ @relative_path = if @absolute_path.start_with?(prefix)
25
+ @absolute_path[prefix.length, @absolute_path.length - prefix.length]
27
26
  else
28
27
  @absolute_path
29
28
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pmd
4
- VERSION = '1.0.2'
4
+ VERSION = '1.0.3'
5
5
  end
data/lib/pmd/plugin.rb CHANGED
@@ -11,13 +11,13 @@ module Danger
11
11
  #
12
12
  # @example Running PMD with a specific Gradle task or report file (glob accepted)
13
13
  #
14
- # pmd.gradle_task = 'app:pmd' # default: pmd
15
- # pmd.report_file = 'module/build/reports/pmd/pmd.xml' # default: app/build/reports/pmd/pmd.xml
14
+ # pmd.gradle_task = 'module:pmd' # default: 'pmd'
15
+ # pmd.report_file = 'module/build/reports/pmd/pmd.xml' # default: 'app/build/reports/pmd/pmd.xml'
16
16
  # pmd.report
17
17
  #
18
18
  # @example Running PMD with a specific root path
19
19
  #
20
- # pmd.root_path = '/Users/developer/project'
20
+ # pmd.root_path = '/Users/developer/project' # default: result of `git rev-parse --show-toplevel`
21
21
  # pmd.report
22
22
  #
23
23
  # @example Running PMD with an array of report files (glob accepted)
@@ -27,9 +27,13 @@ module Danger
27
27
  #
28
28
  # @example Running PMD without running a Gradle task
29
29
  #
30
- # pmd.skip_gradle_task = true
30
+ # pmd.skip_gradle_task = true # default: false
31
31
  # pmd.report
32
32
  #
33
+ # @example Running PMD without inline comment
34
+ #
35
+ # pmd.report(inline_mode: false) # default: true
36
+ #
33
37
  # @see mathroule/danger-pmd
34
38
  # @tags java, android, pmd
35
39
  class DangerPmd < Plugin
@@ -38,10 +42,12 @@ module Danger
38
42
  # Custom Gradle task to run.
39
43
  # This is useful when your project has different flavors.
40
44
  # Defaults to 'pmd'.
45
+ #
41
46
  # @return [String]
42
47
  attr_writer :gradle_task
43
48
 
44
49
  # A getter for `gradle_task`, returning 'pmd' if value is nil.
50
+ #
45
51
  # @return [String]
46
52
  def gradle_task
47
53
  @gradle_task ||= 'pmd'
@@ -50,10 +56,12 @@ module Danger
50
56
  # Skip Gradle task.
51
57
  # If you skip Gradle task, for example project does not manage Gradle.
52
58
  # Defaults to `false`.
53
- # @return [Bool]
59
+ #
60
+ # @return [Boolean]
54
61
  attr_writer :skip_gradle_task
55
62
 
56
63
  # A getter for `skip_gradle_task`, returning false if value is nil.
64
+ #
57
65
  # @return [Boolean]
58
66
  def skip_gradle_task
59
67
  @skip_gradle_task ||= false
@@ -62,34 +70,40 @@ module Danger
62
70
  # An absolute path to a root.
63
71
  # To comment errors to VCS, this needs to know relative path of files from the root.
64
72
  # Defaults to result of 'git rev-parse --show-toplevel'.
65
- # @return [String] the root path of git repository by default.
73
+ #
74
+ # @return [String]
66
75
  attr_writer :root_path
67
76
 
68
- # A getter for `root_path`, returning result of 'git rev-parse --show-toplevel' if value is nil.
77
+ # A getter for `root_path`, returning result of `git rev-parse --show-toplevel` if value is nil.
78
+ #
69
79
  # @return [String]
70
80
  def root_path
71
81
  @root_path ||= `git rev-parse --show-toplevel`.chomp
72
82
  end
73
83
 
74
84
  # Location of report file.
75
- # If your pmd task outputs to a different location, you can specify it here.
85
+ # If your PMD task task outputs to a different location, you can specify it here.
76
86
  # Defaults to 'app/build/reports/pmd/pmd.xml'.
87
+ #
77
88
  # @return [String]
78
89
  attr_writer :report_file
79
90
 
80
91
  # A getter for `report_file`, returning 'app/build/reports/pmd/pmd.xml' if value is nil.
92
+ #
81
93
  # @return [String]
82
94
  def report_file
83
95
  @report_file ||= 'app/build/reports/pmd/pmd.xml'
84
96
  end
85
97
 
86
98
  # Location of report files.
87
- # If your pmd task outputs to a different location, you can specify it here.
99
+ # If your PMD task outputs to a different location, you can specify it here.
88
100
  # Defaults to ['app/build/reports/pmd/pmd.xml'].
101
+ #
89
102
  # @return [Array[String]]
90
103
  attr_writer :report_files
91
104
 
92
105
  # A getter for `report_files`, returning ['app/build/reports/pmd/pmd.xml'] if value is nil.
106
+ #
93
107
  # @return [Array[String]]
94
108
  def report_files
95
109
  @report_files ||= [report_file]
@@ -99,6 +113,9 @@ module Danger
99
113
  # It fails if `gradlew` cannot be found inside current directory.
100
114
  # It fails if `report_file` cannot be found inside current directory.
101
115
  # It fails if `report_files` is empty.
116
+ #
117
+ # @param [Boolean] inline_mode Report as inline comment, defaults to [true].
118
+ #
102
119
  # @return [Array[PmdFile]]
103
120
  def report(inline_mode: true)
104
121
  unless skip_gradle_task
@@ -116,24 +133,23 @@ module Danger
116
133
  private
117
134
 
118
135
  # Check gradlew file exists in current directory.
119
- # @return [Bool]
136
+ #
137
+ # @return [Boolean]
120
138
  def gradlew_exists?
121
139
  !`ls gradlew`.strip.empty?
122
140
  end
123
141
 
124
142
  # Run Gradle task.
143
+ #
125
144
  # @return [void]
126
145
  def exec_gradle_task
127
146
  system "./gradlew #{gradle_task}"
128
147
  end
129
148
 
130
- # Check report_file exists in current directory.
131
- # @return [Bool]
132
- def report_file_exist?(report_file)
133
- File.exist?(report_file)
134
- end
135
-
136
149
  # A getter for `pmd_report`, returning PMD report.
150
+ #
151
+ # @param [String] report_file The report file.
152
+ #
137
153
  # @return [Oga::XML::Document]
138
154
  def pmd_report(report_file)
139
155
  require 'oga'
@@ -141,6 +157,9 @@ module Danger
141
157
  end
142
158
 
143
159
  # A getter for PMD issues, returning PMD issues.
160
+ #
161
+ # @param [String] report_file The report file.
162
+ #
144
163
  # @return [Array[PmdFile]]
145
164
  def pmd_issues(report_file)
146
165
  pmd_report(report_file).xpath('//file').map do |pmd_file|
@@ -149,12 +168,16 @@ module Danger
149
168
  end
150
169
 
151
170
  # A getter for current updated files.
171
+ #
152
172
  # @return [Array[String]]
153
173
  def target_files
154
174
  @target_files ||= (git.modified_files - git.deleted_files) + git.added_files
155
175
  end
156
176
 
157
177
  # Generate report and send inline comment with Danger's warn or fail method.
178
+ #
179
+ # @param [Boolean] inline_mode Report as inline comment, defaults to [true].
180
+ #
158
181
  # @return [Array[PmdFile]]
159
182
  def do_comment(report_files, inline_mode)
160
183
  pmd_issues = []
@@ -163,16 +186,16 @@ module Danger
163
186
  pmd_issues(report_file).each do |pmd_file|
164
187
  next unless target_files.include? pmd_file.relative_path
165
188
 
166
- parse_file(pmd_file, pmd_issues, inline_mode)
189
+ pmd_issues.push(pmd_file)
190
+
191
+ send_comment(pmd_file, inline_mode)
167
192
  end
168
193
  end
169
194
 
170
195
  pmd_issues
171
196
  end
172
197
 
173
- def parse_file(pmd_file, pmd_issues, inline_mode)
174
- pmd_issues.push(pmd_file)
175
-
198
+ def send_comment(pmd_file, inline_mode)
176
199
  pmd_file.violations.each do |pmd_violation|
177
200
  if inline_mode
178
201
  send(pmd_violation.type, pmd_violation.description, file: pmd_file.relative_path, line: pmd_violation.line)
data/spec/pmd_spec.rb CHANGED
@@ -19,19 +19,26 @@ module Danger
19
19
  end
20
20
 
21
21
  it 'Set custom Gradle task' do
22
- custom_task = 'pmdStagingDebug'
23
- @pmd.gradle_task = custom_task
24
- expect(@pmd.gradle_task).to eq(custom_task)
22
+ @pmd.gradle_task = 'pmdStagingDebug'
23
+ expect(@pmd.gradle_task).to eq('pmdStagingDebug')
25
24
  end
26
25
 
27
26
  it 'Check default skip Gradle task' do
28
- expect(@pmd.skip_gradle_task).to eq(false)
27
+ expect(@pmd.skip_gradle_task).to be_falsey
29
28
  end
30
29
 
31
30
  it 'Set custom skip Gradle task' do
32
- skip_gradle_task = true
33
- @pmd.skip_gradle_task = skip_gradle_task
34
- expect(@pmd.skip_gradle_task).to eq(skip_gradle_task)
31
+ @pmd.skip_gradle_task = true
32
+ expect(@pmd.skip_gradle_task).to be_truthy
33
+ end
34
+
35
+ it 'Check default root path' do
36
+ expect(@pmd.root_path).to eq(Dir.pwd)
37
+ end
38
+
39
+ it 'Set custom root path' do
40
+ @pmd.root_path = '/Users/developer/project'
41
+ expect(@pmd.root_path).to eq('/Users/developer/project')
35
42
  end
36
43
 
37
44
  it 'Check default report file path' do
@@ -39,19 +46,17 @@ module Danger
39
46
  end
40
47
 
41
48
  it 'Set custom report file path' do
42
- custom_report_path = 'custom-path/pmd_sub_report.xml'
43
- @pmd.report_file = custom_report_path
44
- expect(@pmd.report_file).to eq(custom_report_path)
49
+ @pmd.report_file = 'custom-path/pmd_sub_report.xml'
50
+ expect(@pmd.report_file).to eq('custom-path/pmd_sub_report.xml')
45
51
  end
46
52
 
47
53
  it 'Check default report files paths' do
48
- expect(@pmd.report_files).to eq(['app/build/reports/pmd/pmd.xml'])
54
+ expect(@pmd.report_files).to contain_exactly('app/build/reports/pmd/pmd.xml')
49
55
  end
50
56
 
51
57
  it 'Set custom report files paths' do
52
- custom_report_paths = %w[custom-path/pmd_report_1.xml custom-path/pmd_report_2.xml]
53
- @pmd.report_files = custom_report_paths
54
- expect(@pmd.report_files).to eq(custom_report_paths)
58
+ @pmd.report_files = %w[custom-path/pmd_report_1.xml custom-path/pmd_report_2.xml]
59
+ expect(@pmd.report_files).to contain_exactly('custom-path/pmd_report_1.xml', 'custom-path/pmd_report_2.xml')
55
60
  end
56
61
 
57
62
  it 'Check default root path' do
@@ -59,17 +64,17 @@ module Danger
59
64
  end
60
65
 
61
66
  it 'Set custom root path' do
62
- root_path = '/Users/developer/sample/'
63
- @pmd.root_path = root_path
64
- expect(@pmd.root_path).to eq(root_path)
67
+ @pmd.root_path = '/Users/developer/sample/'
68
+ expect(@pmd.root_path).to eq('/Users/developer/sample/')
65
69
  end
66
70
 
67
71
  it 'Report with report file' do
72
+ # noinspection RubyLiteralArrayInspection
68
73
  target_files = [
69
- "app/src/main/java/com/android/sample/MainActivity.java",
70
- "app/src/main/java/com/android/sample/Tools.java",
71
- "app/src/test/java/com/android/sample/ExampleUnitTest.java",
72
- "app/src/test/java/com/android/sample/ToolsTest.java"
74
+ 'app/src/main/java/com/android/sample/MainActivity.java',
75
+ 'app/src/main/java/com/android/sample/Tools.java',
76
+ 'app/src/test/java/com/android/sample/ExampleUnitTest.java',
77
+ 'app/src/test/java/com/android/sample/ToolsTest.java'
73
78
  ]
74
79
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
75
80
 
@@ -125,15 +130,16 @@ module Danger
125
130
  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]*'")
126
131
  end
127
132
 
128
- it "Report with report file not in target files" do
133
+ it 'Report with report file not in target files' do
134
+ # noinspection RubyLiteralArrayInspection
129
135
  target_files = [
130
- "app/src/main/java/com/android/sample/Tools.java",
131
- "app/src/test/java/com/android/sample/ToolsTest.java"
136
+ 'app/src/main/java/com/android/sample/Tools.java',
137
+ 'app/src/test/java/com/android/sample/ToolsTest.java'
132
138
  ]
133
139
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
134
140
 
135
- @pmd.report_file = "spec/fixtures/pmd_report.xml"
136
- @pmd.root_path = "/Users/developer/sample/"
141
+ @pmd.report_file = 'spec/fixtures/pmd_report.xml'
142
+ @pmd.root_path = '/Users/developer/sample/'
137
143
  @pmd.skip_gradle_task = true
138
144
 
139
145
  pmd_issues = @pmd.report
@@ -164,14 +170,15 @@ module Danger
164
170
  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]*'")
165
171
  end
166
172
 
167
- it "Report with report files" do
173
+ it 'Report with report files' do
174
+ # noinspection RubyLiteralArrayInspection
168
175
  target_files = [
169
- "app/src/main/java/com/android/sample/Application.java",
170
- "app/src/main/java/com/android/sample/MainActivity.java",
171
- "app/src/main/java/com/android/sample/Tools.java",
172
- "app/src/main/java/com/android/sample/Utils.java",
173
- "app/src/test/java/com/android/sample/ExampleUnitTest.java",
174
- "app/src/test/java/com/android/sample/ToolsTest.java"
176
+ 'app/src/main/java/com/android/sample/Application.java',
177
+ 'app/src/main/java/com/android/sample/MainActivity.java',
178
+ 'app/src/main/java/com/android/sample/Tools.java',
179
+ 'app/src/main/java/com/android/sample/Utils.java',
180
+ 'app/src/test/java/com/android/sample/ExampleUnitTest.java',
181
+ 'app/src/test/java/com/android/sample/ToolsTest.java'
175
182
  ]
176
183
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
177
184
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger-pmd
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 1.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathieu Rul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-18 00:00:00.000000000 Z
11
+ date: 2021-03-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api