1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
|
# pylint: disable=missing-function-docstring,line-too-long
"""Test for diff_cover.violationsreporters.java_violations_reporter"""
from io import BytesIO
from textwrap import dedent
import pytest
from diff_cover.command_runner import CommandError
from diff_cover.violationsreporters import base
from diff_cover.violationsreporters.base import QualityReporter
from diff_cover.violationsreporters.java_violations_reporter import (
CheckstyleXmlDriver,
FindbugsXmlDriver,
PmdXmlDriver,
Violation,
checkstyle_driver,
)
@pytest.fixture(autouse=True)
def patch_so_all_files_exist(mocker):
mock = mocker.patch.object(base.os.path, "exists")
mock.returnvalue = True
@pytest.fixture
def process_patcher(mocker):
def _inner(return_value, status_code=0):
mocked_process = mocker.Mock()
mocked_process.returncode = status_code
mocked_process.communicate.return_value = return_value
mocked_subprocess = mocker.patch("diff_cover.command_runner.subprocess")
popen_mock = mocked_subprocess.Popen
popen_instance = popen_mock.return_value
popen_instance.__enter__ = mocker.Mock(return_value=mocked_process)
popen_instance.__exit__ = mocker.Mock(return_value=None)
return mocked_process
return _inner
class TestCheckstyleQualityReporterTest:
def test_no_such_file(self):
"""Expect that we get no results."""
quality = QualityReporter(checkstyle_driver)
result = quality.violations("")
assert result == []
def test_no_java_file(self):
"""Expect that we get no results because no Python files."""
quality = QualityReporter(checkstyle_driver)
file_paths = ["file1.coffee", "subdir/file2.js"]
for path in file_paths:
result = quality.violations(path)
assert result == []
def test_quality(self, process_patcher):
"""Integration test."""
# Patch the output of `checkstyle`
process_patcher(
(
dedent(
"""
[WARN] ../new_file.java:1:1: Line contains a tab character.
[WARN] ../new_file.java:13: 'if' construct must use '{}'s.
"""
)
.strip()
.encode("ascii"),
"",
)
)
expected_violations = [
Violation(1, "Line contains a tab character."),
Violation(13, "'if' construct must use '{}'s."),
]
# Parse the report
quality = QualityReporter(checkstyle_driver)
# Expect that the name is set
assert quality.name() == "checkstyle"
# Measured_lines is undefined for a
# quality reporter since all lines are measured
assert not quality.measured_lines("../new_file.java")
# Expect that we get violations for file1.java only
# We're not guaranteed that the violations are returned
# in any particular order.
actual_violations = quality.violations("../new_file.java")
assert len(actual_violations) == len(expected_violations)
for expected in expected_violations:
assert expected in actual_violations
class TestCheckstyleXmlQualityReporterTest:
@pytest.fixture(autouse=True)
def setup(self, mocker):
# Paths generated by git_path are always the given argument
_git_path_mock = mocker.patch(
"diff_cover.violationsreporters.java_violations_reporter.GitPathTool"
)
_git_path_mock.relative_path = lambda path: path
_git_path_mock.absolute_path = lambda path: path
def test_no_such_file(self):
quality = QualityReporter(CheckstyleXmlDriver())
# Expect that we get no results
result = quality.violations("")
assert result == []
def test_no_java_file(self):
quality = QualityReporter(CheckstyleXmlDriver())
file_paths = ["file1.coffee", "subdir/file2.js"]
# Expect that we get no results because no Java files
for path in file_paths:
result = quality.violations(path)
assert result == []
def test_quality(self, process_patcher):
# Patch the output of `checkstyle`
process_patcher(
(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="8.0">
<file name="file1.java">
<error line="1" severity="error" message="Missing docstring"/>
<error line="2" severity="error" message="Unused variable 'd'"/>
<error line="2" severity="warning" message="TODO: Not the real way we'll store usages!"/>
<error line="579" severity="error" message="Unable to import 'rooted_paths'"/>
<error line="113" severity="error" message="Unused argument 'cls'"/>
<error line="150" severity="error" message="error while code parsing ([Errno 2] No such file or directory)"/>
<error line="149" severity="error" message="Comma not followed by a space"/>
</file>
<file name="path/to/file2.java">
<error line="100" severity="error" message="Access to a protected member"/>
</file>
</checkstyle>
"""
)
.strip()
.encode("ascii"),
"",
)
)
expected_violations = [
Violation(1, "error: Missing docstring"),
Violation(2, "error: Unused variable 'd'"),
Violation(2, "warning: TODO: Not the real way we'll store usages!"),
Violation(579, "error: Unable to import 'rooted_paths'"),
Violation(
150,
"error: error while code parsing ([Errno 2] No such file or directory)",
),
Violation(149, "error: Comma not followed by a space"),
Violation(113, "error: Unused argument 'cls'"),
]
# Parse the report
quality = QualityReporter(CheckstyleXmlDriver())
# Expect that the name is set
assert quality.name() == "checkstyle"
# Measured_lines is undefined for a
# quality reporter since all lines are measured
assert not quality.measured_lines("file1.java")
# Expect that we get violations for file1.java only
# We're not guaranteed that the violations are returned
# in any particular order.
actual_violations = quality.violations("file1.java")
assert len(actual_violations) == len(expected_violations)
for expected in expected_violations:
assert expected in actual_violations
def test_quality_error(self, mocker, process_patcher):
# Patch the output stderr/stdout and returncode of `checkstyle`
process_patcher(
(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="8.0">
<file name="file1.java">
<error line="1" severity="error" message="Missing docstring"/>
</file>
</checkstyle>
"""
),
b"oops",
),
status_code=1,
)
# Parse the report
code = mocker.patch(
"diff_cover.violationsreporters.java_violations_reporter.run_command_for_code"
)
code.return_value = 0
quality = QualityReporter(CheckstyleXmlDriver())
with pytest.raises(CommandError):
quality.violations("file1.java")
def test_quality_pregenerated_report(self):
# When the user provides us with a pre-generated checkstyle report
# then use that instead of calling checkstyle directly.
checkstyle_reports = [
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="8.0">
<file name="path/to/file.java">
<error line="1" severity="error" message="Missing docstring"/>
<error line="57" severity="warning" message="TODO the name of this method is a little bit confusing"/>
</file>
<file name="another/file.java">
<error line="41" severity="error" message="Specify string format arguments as logging function parameters"/>
<error line="175" severity="error" message="Operator not preceded by a space"/>
<error line="259" severity="error" message="Invalid name '' for type variable (should match [a-z_][a-z0-9_]{2,30}$)"/>
</file>
</checkstyle>
"""
)
.strip()
.encode("utf-8")
),
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle version="8.0">
<file name="path/to/file.java">
<error line="183" severity="error" message="Invalid name '' for type argument (should match [a-z_][a-z0-9_]{2,30}$)"/>
</file>
<file name="another/file.java">
<error line="183" severity="error" message="Missing docstring"/>
</file>
</checkstyle>
"""
)
.strip()
.encode("utf-8")
),
]
# Generate the violation report
quality = QualityReporter(CheckstyleXmlDriver(), reports=checkstyle_reports)
# Expect that we get the right violations
expected_violations = [
Violation(1, "error: Missing docstring"),
Violation(
57, "warning: TODO the name of this method is a little bit confusing"
),
Violation(
183,
"error: Invalid name '' for type argument (should match [a-z_][a-z0-9_]{2,30}$)",
),
]
# We're not guaranteed that the violations are returned
# in any particular order.
actual_violations = quality.violations("path/to/file.java")
assert len(actual_violations) == len(expected_violations)
for expected in expected_violations:
assert expected in actual_violations
class TestFindbugsQualityReporterTest:
@pytest.fixture(autouse=True)
def setup(self, mocker):
# Paths generated by git_path are always the given argument
_git_path_mock = mocker.patch(
"diff_cover.violationsreporters.java_violations_reporter.GitPathTool"
)
_git_path_mock.relative_path = lambda path: path
_git_path_mock.absolute_path = lambda path: path
def test_no_such_file(self):
quality = QualityReporter(FindbugsXmlDriver())
# Expect that we get no results
result = quality.violations("")
assert result == []
def test_no_java_file(self):
quality = QualityReporter(FindbugsXmlDriver())
file_paths = ["file1.coffee", "subdir/file2.js"]
# Expect that we get no results because no Java files
for path in file_paths:
result = quality.violations(path)
assert result == []
def test_quality_pregenerated_report(self):
# When the user provides us with a pre-generated findbugs report
# then use that instead of calling findbugs directly.
findbugs_reports = [
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection sequence="0" release="" analysisTimestamp="1512755361404" version="3.0.1" timestamp="1512755226000">
<BugInstance instanceOccurrenceNum="0" instanceHash="1967bf8c4d25c6b964f30356014aa9fb" rank="20" abbrev="Dm" category="I18N" priority="3" type="DM_CONVERT_CASE" instanceOccurrenceMax="0">
<ShortMessage>Consider using Locale parameterized version of invoked method</ShortMessage>
<LongMessage>Use of non-localized String.toUpperCase() or String.toLowerCase() in org.opensource.sample.file$1.isMultipart(HttpServletRequest)</LongMessage>
<Class classname="org.opensource.sample.file$1" primary="true">
<SourceLine classname="org.opensource.sample.file$1" start="94" end="103" sourcepath="path/to/file.java" sourcefile="file.java">
<Message>At file.java:[lines 94-103]</Message>
</SourceLine>
<Message>In class org.opensource.sample.file$1</Message>
</Class>
<Method isStatic="false" classname="org.opensource.sample.file$1" signature="(Ljavax/servlet/http/HttpServletRequest;)Z" name="isMultipart" primary="true">
<SourceLine endBytecode="181" classname="org.opensource.sample.file$1" start="97" end="103" sourcepath="file1.java" sourcefile="file1.java" startBytecode="0" />
<Message>In method org.opensource.sample.file$1.isMultipart(HttpServletRequest)</Message>
</Method>
<SourceLine endBytecode="6" classname="org.opensource.sample.file$1" start="97" end="97" sourcepath="path/to/file.java" sourcefile="file.java" startBytecode="6" primary="true">
<Message>At file.java:[line 97]</Message>
</SourceLine>
<SourceLine role="SOURCE_LINE_ANOTHER_INSTANCE" endBytecode="55" classname="org.opensource.sample.file$1" start="103" end="104" sourcepath="another/file.java" sourcefile="file.java" startBytecode="55">
<Message>Another occurrence at file.java:[line 103, 104]</Message>
</SourceLine>
</BugInstance>
</BugCollection>
"""
)
.strip()
.encode("utf-8")
),
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection sequence="0" release="" analysisTimestamp="1512755361404" version="3.0.1" timestamp="1512755226000">
<BugInstance instanceOccurrenceNum="0" instanceHash="1967bf8c4d25c6b964f30356014aa9fb" rank="20" abbrev="Dm" category="I18N" priority="3" type="DM_CONVERT_CASE" instanceOccurrenceMax="0">
<ShortMessage>Consider using Locale parameterized version of invoked method</ShortMessage>
<LongMessage>Use of non-localized String.toUpperCase() or String.toLowerCase() in org.opensource.sample.file$1.isMultipart(HttpServletRequest)</LongMessage>
<Class classname="org.opensource.sample.file$1" primary="true">
<SourceLine classname="org.opensource.sample.file$1" start="94" end="103" sourcepath="path/to/file.java" sourcefile="file.java">
<Message>At file.java:[lines 94-103]</Message>
</SourceLine>
<Message>In class org.opensource.sample.file$1</Message>
</Class>
<Method isStatic="false" classname="org.opensource.sample.file$1" signature="(Ljavax/servlet/http/HttpServletRequest;)Z" name="isMultipart" primary="true">
<SourceLine endBytecode="181" classname="org.opensource.sample.file$1" start="97" end="103" sourcepath="file1.java" sourcefile="file1.java" startBytecode="0" />
<Message>In method org.opensource.sample.file$1.isMultipart(HttpServletRequest)</Message>
</Method>
<SourceLine endBytecode="6" classname="org.opensource.sample.file$1" start="183" end="183" sourcepath="path/to/file.java" sourcefile="file.java" startBytecode="6" primary="true">
<Message>At file.java:[line 97]</Message>
</SourceLine>
<SourceLine role="SOURCE_LINE_ANOTHER_INSTANCE" endBytecode="55" classname="org.opensource.sample.file$1" start="183" end="183" sourcepath="another/file.java" sourcefile="file.java" startBytecode="55">
<Message>Another occurrence at file.java:[line 183]</Message>
</SourceLine>
</BugInstance>
</BugCollection>
"""
)
.strip()
.encode("utf-8")
),
# this is a violation which is not bounded to a specific line. We'll skip those
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<BugCollection sequence="0" release="" analysisTimestamp="1512755361404" version="3.0.1" timestamp="1512755226000">
<BugInstance instanceOccurrenceNum="0" instanceHash="2820338ec68e2e75a81848c95d31167f" rank="19" abbrev="Se" category="BAD_PRACTICE" priority="3" type="SE_BAD_FIELD" instanceOccurrenceMax="0">
<ShortMessage>Non-transient non-serializable instance field in serializable class</ShortMessage>
<LongMessage>Class org.opensource.sample.file defines non-transient non-serializable instance field</LongMessage>
<SourceLine synthetic="true" classname="org.opensource.sample.file" sourcepath="path/to/file.java" sourcefile="file.java">
<Message>In file.java</Message>
</SourceLine>
</BugInstance>
</BugCollection>
"""
)
.strip()
.encode("utf-8")
),
]
# Generate the violation report
quality = QualityReporter(FindbugsXmlDriver(), reports=findbugs_reports)
# Expect that we get the right violations
expected_violations = [
Violation(
97,
"I18N: Consider using Locale parameterized version of invoked method",
),
Violation(
183,
"I18N: Consider using Locale parameterized version of invoked method",
),
]
# We're not guaranteed that the violations are returned
# in any particular order.
actual_violations = quality.violations("path/to/file.java")
assert len(actual_violations) == len(expected_violations)
for expected in expected_violations:
assert expected in actual_violations
class TestPmdXmlQualityReporterTest:
@pytest.fixture(autouse=True)
def setup(self, mocker):
# Paths generated by git_path are always the given argument
_git_path_mock = mocker.patch(
"diff_cover.violationsreporters.java_violations_reporter.GitPathTool"
)
_git_path_mock.relative_path = lambda path: path
_git_path_mock.absolute_path = lambda path: path
def test_no_such_file(self):
quality = QualityReporter(PmdXmlDriver())
# Expect that we get no results
result = quality.violations("")
assert result == []
def test_no_java_file(self):
quality = QualityReporter(PmdXmlDriver())
file_paths = ["file1.coffee", "subdir/file2.js"]
# Expect that we get no results because no Java files
for path in file_paths:
result = quality.violations(path)
assert result == []
def test_quality_pregenerated_report(self):
# When the user provides us with a pre-generated findbugs report
# then use that instead of calling findbugs directly.
pmd_reports = [
BytesIO(
dedent(
"""
<?xml version="1.0" encoding="UTF-8"?>
<pmd version="5.6.1" timestamp="2019-06-24T15:47:13.429">
<file name="path/to/file.java">
<violation beginline="21" endline="118" begincolumn="8" endcolumn="1" rule="ClassMustHaveAuthorRule" ruleset="AlibabaJavaComments" package="com.huifu.devops.application.component" class="LingeringInputFilter" priority="3">
must have @author comment
</violation>
</file>
<file name="path/to/file.java">
<violation beginline="10" endline="10" begincolumn="29" endcolumn="63" rule="AbstractMethodOrInterfaceMethodMustUseJavadocRule" ruleset="AlibabaJavaComments" package="com.huifu.devops.application.component" class="PipelineExecutionStepVoConverter" method="convert" priority="3">
interface method must include javadoc comment
</violation>
</file>
</pmd>
"""
)
.strip()
.encode("utf-8")
)
]
pmd_xml_driver = PmdXmlDriver()
# Generate the violation report
quality = QualityReporter(pmd_xml_driver, reports=pmd_reports)
# Expect that pmd is not installed
assert not pmd_xml_driver.installed()
# Expect that we get the right violations
expected_violations = [
Violation(21, "ClassMustHaveAuthorRule: must have @author comment"),
Violation(
10,
"AbstractMethodOrInterfaceMethodMustUseJavadocRule: interface method must include javadoc comment",
),
]
# We're not guaranteed that the violations are returned
# in any particular order.
actual_violations = quality.violations("path/to/file.java")
assert len(actual_violations) == len(expected_violations)
for expected in expected_violations:
assert expected in actual_violations
|