Skip to content

Commit

Permalink
Don't output messy nulls to saved contributors files.
Browse files Browse the repository at this point in the history
  • Loading branch information
netsettler committed Jul 30, 2023
1 parent 6788905 commit bdd76a6
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
27 changes: 17 additions & 10 deletions dcicutils/contribution_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,23 +213,30 @@ def contributor_values_as_objects(cls, contributor_index: Optional[Dict]) -> Opt
if contributor_index is None:
return None
else:
items = contributor_index.items()
key: str
result = {
return {
key: Contributor.from_dict(value)
for key, value in items
for key, value in contributor_index.items()
}
return result

def checkpoint_state(self):
return self.as_dict()

def as_dict(self):
data = {
"forked_at": self.forked_at.isoformat() if self.forked_at else None,
"pre_fork_contributors_by_name": self.contributor_values_as_dicts(self.pre_fork_contributors_by_name),
"contributors_by_name": self.contributor_values_as_dicts(self.contributors_by_name),
}

data = {}

forked_at = self.forked_at.isoformat() if self.forked_at else None
if forked_at is not None:
data["forked_at"] = forked_at

pre_fork_contributors_by_name = self.contributor_values_as_dicts(self.pre_fork_contributors_by_name)
if pre_fork_contributors_by_name is not None:
data["pre_fork_contributors_by_name"] = pre_fork_contributors_by_name

contributors_by_name = self.contributor_values_as_dicts(self.contributors_by_name)
if contributors_by_name is not None:
data["contributors_by_name"] = contributors_by_name

return data

def save_contributor_data(self, filename: Optional[str] = None) -> str:
Expand Down
32 changes: 18 additions & 14 deletions test/test_contribution_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -578,24 +578,28 @@ def test_save_contributor_data():

contributions = BasicContributions()
cache_file = contributions.save_contributor_data()
assert json.loads(file_contents(cache_file)) == {}

assert file_contents(cache_file) == (
'{\n'
' "forked_at": null,\n'
' "pre_fork_contributors_by_name": null,\n'
' "contributors_by_name": null\n'
'}\n'
)
contributions = BasicContributions()
contributions.forked_at = datetime.datetime(2020, 1, 1, 12, 34, 56)
contributions.pre_fork_contributors_by_name = {}
contributions.contributors_by_name = contributions.contributor_values_as_objects({
"Joe": {"names": ["Joe"], "emails": ["joe@foo"]}
})
cache_file = contributions.save_contributor_data()
assert json.loads(file_contents(cache_file)) == {
"forked_at": "2020-01-01T12:34:56",
"pre_fork_contributors_by_name": {},
"contributors_by_name": {"Joe": {"names": ["Joe"], "emails": ["joe@foo"]}}
}

cache_file_2 = contributions.save_contributor_data('some.file')
assert cache_file_2 == 'some.file'
assert file_contents(cache_file_2) == (
'{\n'
' "forked_at": null,\n'
' "pre_fork_contributors_by_name": null,\n'
' "contributors_by_name": null\n'
'}\n'
)
assert json.loads(file_contents(cache_file_2)) == {
"forked_at": "2020-01-01T12:34:56",
"pre_fork_contributors_by_name": {},
"contributors_by_name": {"Joe": {"names": ["Joe"], "emails": ["joe@foo"]}}
}


def test_repo_contributor_names():
Expand Down

0 comments on commit bdd76a6

Please sign in to comment.