Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change method of reporting target data #4593

Merged
merged 11 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/python/pants/goal/run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -429,14 +429,16 @@ def _create_dict_with_nested_keys_and_val(cls, keys, value):
:param primitive value: The value of the information being stored.
:return: dict of nested keys leading to the value.
"""
current_index = len(keys) - 1

if len(keys) > 1:
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks really succinct and neat now. Sometimes Python has its moments...

new_val = {keys[current_index]: value}
new_keys = keys[:current_index]
new_keys = keys[:-1]
new_val = {keys[-1]: value}
return cls._create_dict_with_nested_keys_and_val(new_keys, new_val)
elif len(keys) == 1:
return {keys[current_index]: value}
return {keys[0]: value}
else:
raise ValueError('Keys must contain at least one key.')


Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a bit unorthodox to have a function (especially a recursive function) that returns a value but that doesn't handle every possible branch. In this case, len(keys) == 0 is not handled. Effictively it's like returning None, which might cause an unintuitive error down the line. So it's better to throw an explicit error as soon as this case is encountered.

@classmethod
def _merge_list_of_keys_into_dict(cls, data, keys, value, index=0):
Expand Down
6 changes: 3 additions & 3 deletions src/python/pants/task/testrunner_task_mixin.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def report_all_info_for_single_test(self, scope, target, test_name, test_info):

Given the dict of test information
{'time': 0.124, 'result_code': 'success', 'classname': 'some.test.class'}
iterate through each item and report the single item with report_test_info.
iterate through each item and report the single item with _report_test_info.

:param string scope: The scope for which we are reporting the information.
:param Target target: The target that we want to store the test information under.
Expand All @@ -80,9 +80,9 @@ def report_all_info_for_single_test(self, scope, target, test_name, test_info):
"""
for test_info_key, test_info_val in test_info.items():
key_list = [test_name, test_info_key]
self.report_test_info(scope, target, key_list, test_info_val)
self._report_test_info(scope, target, key_list, test_info_val)

def report_test_info(self, scope, target, keys, test_info):
def _report_test_info(self, scope, target, keys, test_info):
"""Add test information to target information.

:param string scope: The scope for which we are reporting information.
Expand Down
6 changes: 2 additions & 4 deletions tests/python/pants_test/goal/test_run_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,8 @@ def test_write_stats_to_json_file(self):
def test_create_dict_with_nested_keys_and_val(self):
Copy link
Sponsor Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the comprehensive tests!

keys = []

self.assertEquals(
RunTracker._create_dict_with_nested_keys_and_val(keys, 'something'),
None
)
with self.assertRaises(ValueError):
RunTracker._create_dict_with_nested_keys_and_val(keys, 'something')

keys += ['one']
self.assertEquals(
Expand Down