Skip to content

Commit c77349a

Browse files
committed
Merge pull request #117 from adangel:issue-116-relativize-paths
Use relative paths without leading slash #117
2 parents 7a1bfff + 77f6c2c commit c77349a

File tree

6 files changed

+71
-8
lines changed

6 files changed

+71
-8
lines changed

History.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
## Fixed Issues
88

99
* [#115](https://github.com/pmd/pmd-regression-tester/pull/115): Fix manual integration tests
10+
* [#116](https://github.com/pmd/pmd-regression-tester/issues/116): Relative paths in pmd report result in missing code snippets
1011

1112
## External Contributions
1213

lib/pmdtester/parsers/pmd_report_document.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ def start_element(name, attrs = [])
4141
end
4242

4343
def characters(string)
44-
@cur_text << remove_work_dir!(string)
44+
@cur_text << string
4545
end
4646

4747
def cdata_block(string)
48-
@cur_text << remove_work_dir!(string)
48+
@cur_text << string
4949
end
5050

5151
def end_element(name)
@@ -76,11 +76,12 @@ def end_element(name)
7676
# Modifies the string in place and returns it
7777
# (this is what sub! does, except it returns nil if no replacement occurred)
7878
def remove_work_dir!(str)
79-
str.sub!(/#{@working_dir}/, '')
79+
str.sub!(%r{#{@working_dir}/}, '')
8080
str
8181
end
8282

8383
def finish_text!
84+
remove_work_dir!(@cur_text)
8485
res = @cur_text.strip!.dup.freeze
8586
@cur_text.clear
8687
res

lib/pmdtester/project.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,19 @@ def default_webview_url
6060
# Change the file path from 'LOCAL_DIR/SOURCE_CODE_PATH' to
6161
# 'WEB_VIEW_URL/SOURCE_CODE_PATH'
6262
def get_webview_url(file_path)
63-
file_path.gsub(%r{/#{clone_root_path}}, @webview_url)
63+
file_path.gsub(%r{^#{clone_root_path}/}, "#{@webview_url}/")
6464
end
6565

6666
# Change the file path from 'LOCAL_DIR/SOURCE_CODE_PATH' to
6767
# 'PROJECT_NAME/SOURCE_CODE_PATH'
6868
def get_path_inside_project(file_path)
69-
file_path.gsub(%r{/#{clone_root_path}}, @name)
69+
file_path.gsub(%r{^#{clone_root_path}/}, "#{@name}/")
7070
end
7171

72+
# Change the file path from 'LOCAL_DIR/SOURCE_CODE_PATH' to
73+
# 'SOURCE_CODE_PATH'
7274
def get_local_path(file_path)
73-
file_path.sub(%r{/#{clone_root_path}/}, '')
75+
file_path.sub(%r{^#{clone_root_path}/}, '')
7476
end
7577

7678
def get_pmd_report_path(branch_name)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<pmd xmlns="http://pmd.sourceforge.net/report/2.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://pmd.sourceforge.net/report/2.0.0 http://pmd.sourceforge.net/report_2_0_0.xsd"
5+
version="6.3.0-SNAPSHOT" timestamp="2018-04-16T22:41:45.065">
6+
<file name="/home/runner/work/pmd/target/repositories/spring-framework/gradle/jdiff/Null.java">
7+
<violation beginline="7" endline="8" begincolumn="12" endcolumn="5" rule="UncommentedEmptyConstructor" ruleset="Documentation" class="Null" method="Null" externalInfoUrl="http://pmd.sourceforge.net/snapshot/pmd_rules_java_documentation.html#uncommentedemptyconstructor" priority="3">
8+
Document &apos;empty&apos; constructor
9+
</violation>
10+
</file>
11+
<!-- the same file, but already relativized -->
12+
<file name="target/repositories/spring-framework/gradle/jdiff/Null.java">
13+
<violation beginline="8" endline="9" begincolumn="12" endcolumn="5" rule="UncommentedEmptyConstructor" ruleset="Documentation" class="Null" method="Null" externalInfoUrl="http://pmd.sourceforge.net/snapshot/pmd_rules_java_documentation.html#uncommentedemptyconstructor" priority="3">
14+
Document &apos;empty&apos; constructor
15+
</violation>
16+
</file>
17+
</pmd>

test/test_pmd_report_document.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
class TestPmdReportDocument < Test::Unit::TestCase
66
include PmdTester
77

8-
FIRST_FILE = '/target/repositories/spring-framework/gradle/jdiff/Null.java'
8+
FIRST_FILE = 'target/repositories/spring-framework/gradle/jdiff/Null.java'
99

1010
def test_document
1111
doc = PmdReportDocument.new('base', 'SHOULD_BE_REPLACED')
@@ -36,7 +36,7 @@ def test_filter_set_single_rule
3636
parser.parse(File.open('test/resources/pmd_report_document/test_document.xml'))
3737
assert_equal(4, doc.violations.total_size, 'wrong number of violations')
3838
assert_equal(3, doc.violations.num_files, 'wrong number of files')
39-
first_file = '/target/repositories/spring-framework/spring-aop/src/main/java/'\
39+
first_file = 'target/repositories/spring-framework/spring-aop/src/main/java/'\
4040
'org/springframework/aop/ClassFilter.java'
4141
assert_equal(44, doc.violations[first_file][0].line)
4242
end
@@ -50,4 +50,16 @@ def test_error_filename_without_path
5050
assert_equal(1, filenames.length)
5151
assert_equal('InputXpathQueryGeneratorTabWidth.java', filenames[0])
5252
end
53+
54+
def test_relativized_filenames
55+
doc = PmdReportDocument.new('base', '/home/runner/work/pmd')
56+
parser = Nokogiri::XML::SAX::Parser.new(doc)
57+
parser.parse(File.open('test/resources/pmd_report_document/sample_report_relativized_paths.xml'))
58+
59+
assert_equal(2, doc.violations.total_size, 'wrong number of violations')
60+
assert_equal(1, doc.violations.num_files, 'wrong number of files')
61+
first_file = 'target/repositories/spring-framework/gradle/jdiff/Null.java'
62+
assert_equal(7, doc.violations[first_file][0].line)
63+
assert_equal(8, doc.violations[first_file][1].line)
64+
end
5365
end

test/test_project.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
# Unit test for PmdTester::Project
6+
class TestProject < Test::Unit::TestCase
7+
FILE_PATH = 'src/java.base/aix/classes/sun/nio/ch/AixAsynchronousChannelProvider.java'
8+
9+
def setup
10+
@projects = PmdTester::ProjectsParser.new.parse('test/resources/projects_parser/project-list.xml')
11+
assert_equal(3, @projects.size)
12+
@project = @projects[1]
13+
assert_equal('openjdk-11', @project.name)
14+
assert_equal('target/repositories/openjdk-11', @project.clone_root_path)
15+
end
16+
17+
def test_get_webview_url
18+
assert_equal("https://github.com/openjdk/jdk/tree/jdk-11+28/#{FILE_PATH}",
19+
@project.get_webview_url("target/repositories/openjdk-11/#{FILE_PATH}"))
20+
end
21+
22+
def test_get_path_inside_project
23+
assert_equal("openjdk-11/#{FILE_PATH}",
24+
@project.get_path_inside_project("target/repositories/openjdk-11/#{FILE_PATH}"))
25+
end
26+
27+
def test_get_local_path
28+
assert_equal(FILE_PATH, @project.get_local_path("target/repositories/openjdk-11/#{FILE_PATH}"))
29+
end
30+
end

0 commit comments

Comments
 (0)