Skip to content

Commit

Permalink
Testing of qa_checkers.
Browse files Browse the repository at this point in the history
  • Loading branch information
netsettler committed Jul 28, 2023
1 parent 8d52b69 commit 95b6e37
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
12 changes: 9 additions & 3 deletions dcicutils/contribution_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def json_for_actor(cls, actor: git.Actor) -> Dict:
def json_for_commit(cls, commit: git.Commit) -> Dict:
return {
'commit': commit.hexsha,
'date': commit.authored_datetime,
'date': commit.committed_datetime.isoformat(),
'author': cls.json_for_actor(commit.author),
'coauthors': [cls.json_for_actor(co_author) for co_author in commit.co_authors],
'message': commit.message,
Expand Down Expand Up @@ -227,7 +227,11 @@ def contributors_json_file(self):
"""
Returns the name of the CONTRIBUTORS.json file for the repo associated with this class.
"""
return os.path.join(PROJECT_HOME, self.repo, self.CONTRIBUTORS_CACHE_FILE)
return self.contributors_json_file_for_repo(self.repo)

@classmethod
def contributors_json_file_for_repo(cls, repo):
return os.path.join(PROJECT_HOME, repo, cls.CONTRIBUTORS_CACHE_FILE)

def existing_contributors_json_file(self):
"""
Expand Down Expand Up @@ -441,7 +445,9 @@ def as_dict(self):
def load_from_dict(self, data: Dict):
forked_at: Optional[str] = data.get('forked_at')
excluded_fork = data.get('excluded_fork')
self.forked_at = None if forked_at is None else datetime.datetime.fromisoformat(forked_at)
self.forked_at: Optional[datetime.datetime] = (None
if forked_at is None
else datetime.datetime.fromisoformat(forked_at))
self.exclude_fork = excluded_fork

fork_contributors_by_name_json = data.get('pre_fork_contributors_by_name') or {}
Expand Down
19 changes: 11 additions & 8 deletions test/test_contribution_utils.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import datetime
import git
import os
import pytest
Expand All @@ -18,10 +19,10 @@ def __init__(self, name: Optional[str], email: str):


class MockGitCommit:
def __init__(self, hexsha: str, authored_datetime: str, author: dict,
def __init__(self, hexsha: str, committed_datetime: str, author: dict,
message: str, co_authors: Optional[List[dict]] = None):
self.hexsha = hexsha
self.authored_datetime = authored_datetime
self.committed_datetime = datetime.datetime.fromisoformat(committed_datetime)
self.author = MockGitActor(**author)
self.co_authors = [MockGitActor(**co_author) for co_author in (co_authors or [])]
self.message = message
Expand Down Expand Up @@ -50,6 +51,8 @@ def Repo(self, path):
repo_name = os.path.basename(path)
return MockGitRepo(name=repo_name, path=path, mocked_commits=self._mocked_commits.get(repo_name, []))

Actor = MockGitActor


SAMPLE_USER_HOME = "/home/jdoe"
SAMPLE_PROJECT_HOME = f"{SAMPLE_USER_HOME}/repos"
Expand Down Expand Up @@ -78,13 +81,13 @@ def test_git_analysis_git_commits():
with git_context(mocked_commits={"foo": [
{
"hexsha": "aaaa",
"authored_datetime": "2020-01-01 01:23:45",
"committed_datetime": "2020-01-01 01:23:45",
"author": {"name": "Jdoe", "email": "jdoe@foo"},
"message": "something"
},
{
"hexsha": "bbbb",
"authored_datetime": "2020-01-02 12:34:56",
"committed_datetime": "2020-01-02 12:34:56",
"author": {"name": "Sally", "email": "ssmith@foo"},
"message": "something else"
}
Expand All @@ -106,13 +109,13 @@ def test_git_analysis_iter_commits_scenario(): # Tests .iter_commits, .json_for
with git_context(mocked_commits={"foo": [
{
"hexsha": "aaaa",
"authored_datetime": "2020-01-01 01:23:45",
"committed_datetime": "2020-01-01T01:23:45-05:00",
"author": {"name": "Jdoe", "email": "jdoe@foo"},
"message": "something"
},
{
"hexsha": "bbbb",
"authored_datetime": "2020-01-02 12:34:56",
"committed_datetime": "2020-01-02T12:34:56-05:00",
"author": {"name": "Sally", "email": "ssmith@foo"},
"co_authors": [{"name": "William Simmons", "email": "bill@someplace"}],
"message": "something else"
Expand All @@ -125,14 +128,14 @@ def test_git_analysis_iter_commits_scenario(): # Tests .iter_commits, .json_for
'author': {'email': 'jdoe@foo', 'name': 'Jdoe'},
'coauthors': [],
'commit': 'aaaa',
'date': '2020-01-01 01:23:45',
'date': '2020-01-01T01:23:45-05:00',
'message': 'something'
},
{
'author': {'email': 'ssmith@foo', 'name': 'Sally'},
'coauthors': [{'email': 'bill@someplace', 'name': 'William Simmons'}],
'commit': 'bbbb',
'date': '2020-01-02 12:34:56',
'date': '2020-01-02T12:34:56-05:00',
'message': 'something else'
}
]
Expand Down
65 changes: 63 additions & 2 deletions test/test_qa_checkers.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import io
import json
import os
import pytest

from dcicutils import contribution_utils as contribution_utils_module
from dcicutils.contribution_utils import Contributions
from dcicutils.misc_utils import lines_printed_to, remove_prefix
from dcicutils.qa_checkers import (
DebuggingArtifactChecker, DocsChecker, ChangeLogChecker, VersionChecker, confirm_no_uses, find_uses,
ContributionsChecker
)
from dcicutils.qa_utils import MockFileSystem, printed_output
from unittest import mock
from .test_contribution_utils import git_context, SAMPLE_PROJECT_HOME
from .conftest_settings import TEST_DIR


Expand Down Expand Up @@ -252,7 +258,7 @@ def test_debugging_artifact_checker():
assert isinstance(exc.value, AssertionError)
assert str(exc.value) == "1 problem detected:\n In foo/bar.py, 1 call to print."

assert printed.lines == [] # We might at some point print the actual problems, but we don't now.
assert printed.lines == [] # We might at some point print the actual problems, but we don't know.

with lines_printed_to("foo/bar.py") as out:
out('x = 1')
Expand All @@ -262,4 +268,59 @@ def test_debugging_artifact_checker():
assert isinstance(exc.value, AssertionError)
assert str(exc.value) == "1 problem detected:\n In foo/bar.py, 1 active use of pdb.set_trace."

assert printed.lines == [] # We might at some point print the actual problems, but we don't now.
assert printed.lines == [] # We might at some point print the actual problems, but we don't know.


@mock.patch.object(contribution_utils_module, "PROJECT_HOME", SAMPLE_PROJECT_HOME)
def test_contribution_checker():

print() # start on a fresh line
some_repo_name = 'foo'
mfs = MockFileSystem()
with mfs.mock_exists_open_remove_abspath_getcwd_chdir():
contributions_cache_file = Contributions.contributors_json_file_for_repo(some_repo_name)
print(f"contributions_cache_file={contributions_cache_file}")
os.chdir(os.path.join(SAMPLE_PROJECT_HOME, some_repo_name))
print(f"working dir={os.getcwd()}")
with io.open(contributions_cache_file, 'w') as fp:
cache_data = {
"forked_at": "2015-01-01T12:34:56-05:00",
"excluded_fork": None,
"pre_fork_contributors_by_name": None,
"contributors_by_name": {
"John Smith": {
"emails": ["jsmith@somewhere"],
"names": ["John Smith"],
}
}
}
json.dump(cache_data, fp=fp)
mocked_commits = {
some_repo_name: [
{
"hexsha": "aaaa",
"committed_datetime": "2016-01-01T01:23:45-05:00",
"author": {"name": "John Smith", "email": "jsmith@somewhere"},
"message": "something"
},
{
"hexsha": "bbbb",
"committed_datetime": "2017-01-02T12:34:56-05:00",
"author": {"name": "Sally", "email": "ssmith@elsewhere"},
"message": "something else"
}
]
}
with git_context(mocked_commits=mocked_commits):
with printed_output() as printed:
with pytest.raises(AssertionError) as exc:
ContributionsChecker.validate()
assert str(exc.value) == "There are contributor cache discrepancies."
assert printed.lines == [
"John Smith (jsmith@somewhere)",
"Sally (ssmith@elsewhere)",
"===== THERE ARE CONTRIBUTOR CACHE DISCREPANCIES =====",
"To Add:",
" * contributors.Sally.emails.ssmith@elsewhere",
" * contributors.Sally.names.Sally",
]

0 comments on commit 95b6e37

Please sign in to comment.