def test_phabricator_coverity_build_error( mock_phabricator, phab, mock_try_task, mock_task ): """ Test Phabricator Lint for a CoverityIssue """ with mock_phabricator as api: revision = Revision.from_try(mock_try_task, api) revision.lines = { # Add dummy lines diff "test.cpp": [41, 42, 43] } revision.build_target_phid = "PHID-HMBD-deadbeef12456" revision.repository = "https://hg.mozilla.org/try" revision.repository_try_name = "try" revision.mercurial_revision = "deadbeef1234" reporter = PhabricatorReporter({}, api=api) issue_dict = { "line": 41, "reliability": "medium", "message": 'Dereferencing a pointer that might be "nullptr" "env" when calling "lookupImport".', "flag": "NULL_RETURNS", "build_error": True, "extra": { "category": "Null pointer dereferences", "stateOnServer": { "ownerLdapServerName": "local", "stream": "Firefox", "cid": 95687, "cached": False, "retrievalDateTime": "2019-05-13T10:20:22+00:00", "firstDetectedDateTime": "2019-04-08T12:57:07+00:00", "presentInReferenceSnapshot": False, "components": ["js"], "customTriage": {}, "triage": { "fixTarget": "Untargeted", "severity": "Unspecified", "classification": "Unclassified", "owner": "try", "legacy": "False", "action": "Undecided", "externalReference": "", }, }, }, } issue = CoverityIssue( mock_task(CoverityTask, "mock-coverity"), revision, issue_dict, "test.cpp" ) assert issue.is_publishable() issues, patches = reporter.publish([issue], revision, []) assert len(issues) == 1 assert len(patches) == 0 # Check the callback has been used assert phab.build_messages["PHID-HMBD-deadbeef12456"] == [ { "buildTargetPHID": "PHID-HMBD-deadbeef12456", "lint": [ { "code": "NULL_RETURNS", "description": '(IMPORTANT) ERROR: Dereferencing a pointer that might be "nullptr" ' '"env" when calling "lookupImport".', "line": 41, "name": "Build Error", "path": "test.cpp", "severity": "error", } ], "type": "work", "unit": [], } ] assert phab.comments[51] == [VALID_COVERITY_BUILD_ERROR_MESSAGE]
def test_phabricator_unitresult(mock_phabricator, mock_try_task): """ Test Phabricator UnitResult for a CoverityIssue """ from code_review_bot.tasks.coverity import CoverityIssue from code_review_bot.report.phabricator import PhabricatorReporter from code_review_bot.revisions import Revision def _check_message(request): # Check the Phabricator main comment is well formed payload = urllib.parse.parse_qs(request.body) assert payload["output"] == ["json"] assert len(payload["params"]) == 1 details = json.loads(payload["params"][0]) assert details == { "buildTargetPHID": "PHID-HMBD-deadbeef12456", "lint": [], "unit": [ { "details": 'Code review bot found a **build error**: \nDereferencing a pointer that might be "nullptr" "env" when calling "lookupImport".', "format": "remarkup", "name": "general", "namespace": "code-review", "result": "fail", } ], "type": "work", "__conduit__": {"token": "deadbeef"}, } # Outputs dummy empty response resp = {"error_code": None, "result": None} return ( 201, {"Content-Type": "application/json", "unittest": "coverity"}, json.dumps(resp), ) # Catch the publication of a UnitResult failure responses.add_callback( responses.POST, "http://phabricator.test/api/harbormaster.sendmessage", callback=_check_message, ) with mock_phabricator as api: revision = Revision.from_try(mock_try_task, api) revision.lines = { # Add dummy lines diff "test.cpp": [41, 42, 43] } revision.build_target_phid = "PHID-HMBD-deadbeef12456" reporter = PhabricatorReporter( {"analyzers": ["coverity"], "publish_build_errors": True}, api=api ) issue_dict = { "line": 41, "reliability": "medium", "message": 'Dereferencing a pointer that might be "nullptr" "env" when calling "lookupImport".', "flag": "NULL_RETURNS", "build_error": True, "extra": { "category": "Null pointer dereferences", "stateOnServer": { "ownerLdapServerName": "local", "stream": "Firefox", "cid": 95687, "cached": False, "retrievalDateTime": "2019-05-13T10:20:22+00:00", "firstDetectedDateTime": "2019-04-08T12:57:07+00:00", "presentInReferenceSnapshot": False, "components": ["js"], "customTriage": {}, "triage": { "fixTarget": "Untargeted", "severity": "Unspecified", "classification": "Unclassified", "owner": "try", "legacy": "False", "action": "Undecided", "externalReference": "", }, }, }, } issue = CoverityIssue("mock-coverity", revision, issue_dict, "test.cpp") assert issue.is_publishable() issues, patches = reporter.publish([issue], revision, []) assert len(issues) == 1 assert len(patches) == 0 # Check the callback has been used assert len(responses.calls) > 0 # Only check the last call print(responses.calls[-1].request.url) assert responses.calls[-1].response.headers.get("unittest") == "coverity"