danger-pmd 0.1.1 → 0.2.0

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: 8d44fdbb62edb14a8de5ece8c8340f4746c300e8c54fae34a72436dd42ae5ec1
4
- data.tar.gz: 60d66c53e9495fee4e5238625504458168b3c4bf68fb08f1188fbfa26560a483
3
+ metadata.gz: ed7c20c01b799e6dbb65c79231881419974d23e159eb37eac8a06c6515f54e74
4
+ data.tar.gz: 713039e5ddc346d2d9d4ad1584f0ca0e9c12195f9d185c831286cca13d90a335
5
5
  SHA512:
6
- metadata.gz: ec9f06385c7419e7b671e941bc89cce31e909220aab39f84a1837f5a5a680d3e571f8653b86b4b43b999e6f6495be4f7740d252592baeb3efad5713d1a7dd165
7
- data.tar.gz: 7512fe083ce53ced178c0a92b23ce8e02ba7d8b411533ef18fa92165b6bf876b963ebfdd0905e7bfdb1f0d7e231c742c2fed05c2835c39101bd21cae9f7847b8
6
+ metadata.gz: ab07baefcd8d71857370394b05fda51321a163269db0c5eb7b04aa3fae69e20b4f0241aec79f66aeb8dc1eeef30e3f51e73c4fb3ceb5c4577862941bcb79456e
7
+ data.tar.gz: 3d27b055ada8d15588e9585c60b10ddf45d5cc990bbd41f380e5b2e2e69181c9c2f67f639ca3444354e17bcd7869f3b72142302674da6a4997048dd99940fd1a
data/README.md CHANGED
@@ -48,6 +48,10 @@ Defaults to "pmd".
48
48
  `skip_gradle_task` - Skip Gradle task.
49
49
  If you skip Gradle task, for example project does not manage Gradle.
50
50
 
51
+ `root_path` - Location of the project root
52
+ If your project root is different from git's top-level path.
53
+ Defaults to result of "git rev-parse --show-toplevel".
54
+
51
55
  `report_file` - Location of report file
52
56
  If your pmd task outputs to a different location, you can specify it here.
53
57
  Defaults to "app/build/reports/pmd/pmd.xml".
@@ -66,6 +70,8 @@ It fails if `report_file` or `report_files` cannot be found inside current direc
66
70
 
67
71
  `skip_gradle_task` - A getter for `skip_gradle_task`.
68
72
 
73
+ `root_path` - A getter for `root_path`.
74
+
69
75
  `report_file` - A getter for `report_file`.
70
76
 
71
77
  `report_files` - A getter for `report_files`.
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Pmd
4
- VERSION = "0.1.1"
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/pmd/plugin.rb CHANGED
@@ -15,6 +15,11 @@ module Danger
15
15
  # pmd.report_file = "module/build/reports/pmd/pmd.xml" #defalut: app/build/reports/pmd/pmd.xml
16
16
  # pmd.report
17
17
  #
18
+ # @example Running PMD with a specific root path
19
+ #
20
+ # pmd.root_path = '/Users/developer/project
21
+ # pmd.report
22
+ #
18
23
  # @example Running PMD with an array of report files (glob accepted)
19
24
  #
20
25
  # pmd.report_files = ["modules/**/build/reports/pmd/pmd.xml", "app/build/reports/pmd/pmd.xml"]
@@ -55,6 +60,18 @@ module Danger
55
60
  @skip_gradle_task ||= false
56
61
  end
57
62
 
63
+ # An absolute path to a root.
64
+ # To comment errors to VCS, this needs to know relative path of files from the root.
65
+ # Defaults to result of "git rev-parse --show-toplevel".
66
+ # @return [String] the root path of git repository by default.
67
+ attr_writer :root_path
68
+
69
+ # A getter for `root_path`, returning result of "git rev-parse --show-toplevel" if value is nil.
70
+ # @return [String]
71
+ def root_path
72
+ @root_path ||= `git rev-parse --show-toplevel`.chomp
73
+ end
74
+
58
75
  # Location of report file.
59
76
  # If your pmd task outputs to a different location, you can specify it here.
60
77
  # Defaults to "app/build/reports/pmd/pmd.xml".
@@ -84,7 +101,7 @@ module Danger
84
101
  # It fails if `report_file` cannot be found inside current directory.
85
102
  # It fails if `report_files` is empty.
86
103
  # @return [Array[PmdFile]]
87
- def report(inline_mode = true)
104
+ def report(inline_comment = true)
88
105
  unless skip_gradle_task
89
106
  return fail("Could not find `gradlew` inside current directory") unless gradlew_exists?
90
107
 
@@ -94,7 +111,7 @@ module Danger
94
111
  report_files_expanded = Dir.glob(report_files).sort
95
112
  return fail("Could not find matching PMD report files for #{report_files} inside current directory") if report_files_expanded.empty?
96
113
 
97
- report_and_send_inline_comment(report_files_expanded, inline_mode)
114
+ do_comment(report_files_expanded, inline_comment)
98
115
  end
99
116
 
100
117
  private
@@ -128,7 +145,7 @@ module Danger
128
145
  # @return [Array[PmdFile]]
129
146
  def pmd_issues(report_file)
130
147
  pmd_report(report_file).xpath("//file").map do |pmd_file|
131
- PmdFile.new(pmd_file)
148
+ PmdFile.new(root_path, pmd_file)
132
149
  end
133
150
  end
134
151
 
@@ -140,19 +157,21 @@ module Danger
140
157
 
141
158
  # Generate report and send inline comment with Danger's warn or fail method.
142
159
  # @return [Array[PmdFile]]
143
- def report_and_send_inline_comment(report_files, inline_mode = true)
160
+ def do_comment(report_files, inline_comment = true)
144
161
  pmd_issues = []
145
162
 
146
163
  report_files.each do |report_file|
147
164
  pmd_issues(report_file).each do |pmd_file|
148
- next unless target_files.include? pmd_file.absolute_path
165
+ next unless target_files.include? pmd_file.relative_path
149
166
 
150
167
  pmd_issues.push(pmd_file)
151
168
 
152
- next if inline_mode
153
-
154
169
  pmd_file.violations.each do |pmd_violation|
155
- send(pmd_violation.type, pmd_violation.description, file: pmd_file.absolute_path, line: pmd_violation.line)
170
+ if inline_comment
171
+ send(pmd_violation.type, pmd_violation.description, file: pmd_file.relative_path, line: pmd_violation.line)
172
+ else
173
+ send(pmd_violation.type, "#{pmd_file.relative_path} : #{pmd_violation.description} at #{pmd_violation.line}")
174
+ end
156
175
  end
157
176
  end
158
177
  end
data/lib/pmd/pmd_file.rb CHANGED
@@ -2,25 +2,34 @@
2
2
 
3
3
  class PmdFile
4
4
  require_relative "./pmd_violation"
5
+
5
6
  attr_accessor :file
6
7
 
7
- def initialize(file)
8
+ # An absolute path to this file
9
+ #
10
+ # @return [String]
11
+ attr_reader :absolute_path
12
+
13
+ # A relative path to this file
14
+ #
15
+ # @return [String]
16
+ attr_reader :relative_path
17
+
18
+ def initialize(prefix, file)
8
19
  @file = file
9
- end
20
+ @absolute_path = file.attribute("name").value.to_s
10
21
 
11
- def source_path
12
- @source_path ||= file.attribute("name").value.to_s
13
- end
22
+ if prefix.end_with?(file_separator)
23
+ @prefix = prefix
24
+ else
25
+ @prefix = prefix + file_separator
26
+ end
14
27
 
15
- def absolute_path
16
- dirname = File.basename(Dir.getwd)
17
- if source_path.index(dirname)
18
- index_start = source_path.index(dirname) + dirname.length + 1
28
+ if @absolute_path.start_with?(@prefix)
29
+ @relative_path = @absolute_path[@prefix.length, @absolute_path.length - @prefix.length]
19
30
  else
20
- index_start = 0
31
+ @relative_path = @absolute_path
21
32
  end
22
- index_end = source_path.length
23
- @absolute_path ||= Pathname.new(source_path[index_start, index_end]).to_s
24
33
  end
25
34
 
26
35
  def violations
@@ -28,4 +37,10 @@ class PmdFile
28
37
  PmdViolation.new(pmd_violation)
29
38
  end
30
39
  end
40
+
41
+ private
42
+
43
+ def file_separator
44
+ File::ALT_SEPARATOR || File::SEPARATOR
45
+ end
31
46
  end
data/spec/pmd_spec.rb CHANGED
@@ -14,6 +14,26 @@ module Danger
14
14
  @pmd = @dangerfile.pmd
15
15
  end
16
16
 
17
+ it "Check default Gradle task" do
18
+ expect(@pmd.gradle_task).to eq("pmd")
19
+ end
20
+
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)
25
+ end
26
+
27
+ it "Check default skip Gradle task" do
28
+ expect(@pmd.skip_gradle_task).to eq(false)
29
+ end
30
+
31
+ 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)
35
+ end
36
+
17
37
  it "Check default report file path" do
18
38
  expect(@pmd.report_file).to eq("app/build/reports/pmd/pmd.xml")
19
39
  end
@@ -34,36 +54,27 @@ module Danger
34
54
  expect(@pmd.report_files).to eq(custom_report_paths)
35
55
  end
36
56
 
37
- it "Check default Gradle task" do
38
- expect(@pmd.gradle_task).to eq("pmd")
57
+ it "Check default root path" do
58
+ expect(@pmd.root_path).to eq(Dir.pwd)
39
59
  end
40
60
 
41
- it "Set custom Gradle task" do
42
- custom_task = "pmdStagingDebug"
43
- @pmd.gradle_task = custom_task
44
- expect(@pmd.gradle_task).to eq(custom_task)
45
- end
46
-
47
- it "Check default skip Gradle task" do
48
- expect(@pmd.skip_gradle_task).to eq(false)
49
- end
50
-
51
- it "Skip Gradle task" do
52
- skip_gradle_task = true
53
- @pmd.skip_gradle_task = skip_gradle_task
54
- expect(@pmd.skip_gradle_task).to eq(skip_gradle_task)
61
+ 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)
55
65
  end
56
66
 
57
67
  it "Report with report file" do
58
68
  target_files = [
59
- "/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java",
60
- "/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java",
61
- "/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java",
62
- "/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java"
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"
63
73
  ]
64
74
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
65
75
 
66
76
  @pmd.report_file = "spec/fixtures/pmd_report.xml"
77
+ @pmd.root_path = "/Users/developer/sample/"
67
78
  @pmd.skip_gradle_task = true
68
79
 
69
80
  pmd_issues = @pmd.report
@@ -72,8 +83,8 @@ module Danger
72
83
 
73
84
  pmd_issue1 = pmd_issues[0]
74
85
  expect(pmd_issue1).not_to be_nil
75
- expect(pmd_issue1.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
76
86
  expect(pmd_issue1.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
87
+ expect(pmd_issue1.relative_path).to eq("app/src/main/java/com/android/sample/Tools.java")
77
88
  expect(pmd_issue1.violations).not_to be_nil
78
89
  expect(pmd_issue1.violations.length).to eq(1)
79
90
  expect(pmd_issue1.violations.first).not_to be_nil
@@ -82,8 +93,8 @@ module Danger
82
93
 
83
94
  pmd_issue2 = pmd_issues[1]
84
95
  expect(pmd_issue2).not_to be_nil
85
- expect(pmd_issue2.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java")
86
96
  expect(pmd_issue2.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java")
97
+ expect(pmd_issue2.relative_path).to eq("app/src/main/java/com/android/sample/MainActivity.java")
87
98
  expect(pmd_issue2.violations).not_to be_nil
88
99
  expect(pmd_issue2.violations.length).to eq(1)
89
100
  expect(pmd_issue2.violations.first).not_to be_nil
@@ -92,8 +103,8 @@ module Danger
92
103
 
93
104
  pmd_issue3 = pmd_issues[2]
94
105
  expect(pmd_issue3).not_to be_nil
95
- expect(pmd_issue3.source_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java")
96
106
  expect(pmd_issue3.absolute_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java")
107
+ expect(pmd_issue3.relative_path).to eq("app/src/test/java/com/android/sample/ExampleUnitTest.java")
97
108
  expect(pmd_issue3.violations).not_to be_nil
98
109
  expect(pmd_issue3.violations.length).to eq(1)
99
110
  expect(pmd_issue3.violations.first).not_to be_nil
@@ -102,8 +113,8 @@ module Danger
102
113
 
103
114
  pmd_issue4 = pmd_issues[3]
104
115
  expect(pmd_issue4).not_to be_nil
105
- expect(pmd_issue4.source_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
106
116
  expect(pmd_issue4.absolute_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
117
+ expect(pmd_issue4.relative_path).to eq("app/src/test/java/com/android/sample/ToolsTest.java")
107
118
  expect(pmd_issue4.violations).not_to be_nil
108
119
  expect(pmd_issue4.violations.length).to eq(2)
109
120
  expect(pmd_issue4.violations[0]).not_to be_nil
@@ -116,12 +127,13 @@ module Danger
116
127
 
117
128
  it "Report with report file not in target files" do
118
129
  target_files = [
119
- "/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java",
120
- "/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java"
130
+ "app/src/main/java/com/android/sample/Tools.java",
131
+ "app/src/test/java/com/android/sample/ToolsTest.java"
121
132
  ]
122
133
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
123
134
 
124
135
  @pmd.report_file = "spec/fixtures/pmd_report.xml"
136
+ @pmd.root_path = "/Users/developer/sample/"
125
137
  @pmd.skip_gradle_task = true
126
138
 
127
139
  pmd_issues = @pmd.report
@@ -130,8 +142,8 @@ module Danger
130
142
 
131
143
  pmd_issue1 = pmd_issues[0]
132
144
  expect(pmd_issue1).not_to be_nil
133
- expect(pmd_issue1.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
134
145
  expect(pmd_issue1.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
146
+ expect(pmd_issue1.relative_path).to eq("app/src/main/java/com/android/sample/Tools.java")
135
147
  expect(pmd_issue1.violations).not_to be_nil
136
148
  expect(pmd_issue1.violations.length).to eq(1)
137
149
  expect(pmd_issue1.violations.first).not_to be_nil
@@ -140,8 +152,8 @@ module Danger
140
152
 
141
153
  pmd_issue2 = pmd_issues[1]
142
154
  expect(pmd_issue2).not_to be_nil
143
- expect(pmd_issue2.source_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
144
155
  expect(pmd_issue2.absolute_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
156
+ expect(pmd_issue2.relative_path).to eq("app/src/test/java/com/android/sample/ToolsTest.java")
145
157
  expect(pmd_issue2.violations).not_to be_nil
146
158
  expect(pmd_issue2.violations.length).to eq(2)
147
159
  expect(pmd_issue2.violations[0]).not_to be_nil
@@ -154,16 +166,17 @@ module Danger
154
166
 
155
167
  it "Report with report files" do
156
168
  target_files = [
157
- "/Users/developer/sample/app/src/main/java/com/android/sample/Application.java",
158
- "/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java",
159
- "/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java",
160
- "/Users/developer/sample/app/src/main/java/com/android/sample/Utils.java",
161
- "/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java",
162
- "/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java"
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"
163
175
  ]
164
176
  allow_any_instance_of(Danger::DangerPmd).to receive(:target_files).and_return(target_files)
165
177
 
166
178
  @pmd.report_files = ["spec/fixtures/pmd_report.xml", "spec/fixtures/**/pmd_sub_report.xml"]
179
+ @pmd.root_path = "/Users/developer/sample"
167
180
  @pmd.skip_gradle_task = true
168
181
 
169
182
  pmd_issues = @pmd.report
@@ -172,8 +185,8 @@ module Danger
172
185
 
173
186
  pmd_issue1 = pmd_issues[0]
174
187
  expect(pmd_issue1).not_to be_nil
175
- expect(pmd_issue1.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
176
188
  expect(pmd_issue1.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Tools.java")
189
+ expect(pmd_issue1.relative_path).to eq("app/src/main/java/com/android/sample/Tools.java")
177
190
  expect(pmd_issue1.violations).not_to be_nil
178
191
  expect(pmd_issue1.violations.length).to eq(1)
179
192
  expect(pmd_issue1.violations.first).not_to be_nil
@@ -182,8 +195,8 @@ module Danger
182
195
 
183
196
  pmd_issue2 = pmd_issues[1]
184
197
  expect(pmd_issue2).not_to be_nil
185
- expect(pmd_issue2.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java")
186
198
  expect(pmd_issue2.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/MainActivity.java")
199
+ expect(pmd_issue2.relative_path).to eq("app/src/main/java/com/android/sample/MainActivity.java")
187
200
  expect(pmd_issue2.violations).not_to be_nil
188
201
  expect(pmd_issue2.violations.length).to eq(1)
189
202
  expect(pmd_issue2.violations.first).not_to be_nil
@@ -192,8 +205,8 @@ module Danger
192
205
 
193
206
  pmd_issue3 = pmd_issues[2]
194
207
  expect(pmd_issue3).not_to be_nil
195
- expect(pmd_issue3.source_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java")
196
208
  expect(pmd_issue3.absolute_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ExampleUnitTest.java")
209
+ expect(pmd_issue3.relative_path).to eq("app/src/test/java/com/android/sample/ExampleUnitTest.java")
197
210
  expect(pmd_issue3.violations).not_to be_nil
198
211
  expect(pmd_issue3.violations.length).to eq(1)
199
212
  expect(pmd_issue3.violations.first).not_to be_nil
@@ -202,8 +215,8 @@ module Danger
202
215
 
203
216
  pmd_issue4 = pmd_issues[3]
204
217
  expect(pmd_issue4).not_to be_nil
205
- expect(pmd_issue4.source_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
206
218
  expect(pmd_issue4.absolute_path).to eq("/Users/developer/sample/app/src/test/java/com/android/sample/ToolsTest.java")
219
+ expect(pmd_issue4.relative_path).to eq("app/src/test/java/com/android/sample/ToolsTest.java")
207
220
  expect(pmd_issue4.violations).not_to be_nil
208
221
  expect(pmd_issue4.violations.length).to eq(2)
209
222
  expect(pmd_issue4.violations[0]).not_to be_nil
@@ -215,8 +228,8 @@ module Danger
215
228
 
216
229
  pmd_issue5 = pmd_issues[4]
217
230
  expect(pmd_issue5).not_to be_nil
218
- expect(pmd_issue5.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Utils.java")
219
231
  expect(pmd_issue5.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Utils.java")
232
+ expect(pmd_issue5.relative_path).to eq("app/src/main/java/com/android/sample/Utils.java")
220
233
  expect(pmd_issue5.violations).not_to be_nil
221
234
  expect(pmd_issue5.violations.length).to eq(2)
222
235
  expect(pmd_issue5.violations[0]).not_to be_nil
@@ -228,8 +241,8 @@ module Danger
228
241
 
229
242
  pmd_issue6 = pmd_issues[5]
230
243
  expect(pmd_issue6).not_to be_nil
231
- expect(pmd_issue6.source_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Application.java")
232
244
  expect(pmd_issue6.absolute_path).to eq("/Users/developer/sample/app/src/main/java/com/android/sample/Application.java")
245
+ expect(pmd_issue6.relative_path).to eq("app/src/main/java/com/android/sample/Application.java")
233
246
  expect(pmd_issue6.violations).not_to be_nil
234
247
  expect(pmd_issue6.violations.length).to eq(1)
235
248
  expect(pmd_issue6.violations[0]).not_to be_nil
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: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mathieu Rul
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-12-26 00:00:00.000000000 Z
11
+ date: 2019-12-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: danger-plugin-api