2017-03-15 20:17:42 +01:00
|
|
|
"""Test metadata for passing data to delegated tests."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-03-15 20:17:42 +01:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from . import types as t
|
2019-07-11 22:03:49 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .util import (
|
2017-03-15 20:17:42 +01:00
|
|
|
display,
|
2017-07-12 23:16:46 +02:00
|
|
|
is_shippable,
|
2017-03-15 20:17:42 +01:00
|
|
|
)
|
|
|
|
|
2020-02-04 20:21:53 +01:00
|
|
|
from .io import (
|
2019-08-28 08:40:06 +02:00
|
|
|
write_json_file,
|
2020-02-04 20:21:53 +01:00
|
|
|
read_json_file,
|
2019-08-28 08:40:06 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .diff import (
|
2017-03-15 20:17:42 +01:00
|
|
|
parse_diff,
|
2017-08-23 20:09:50 +02:00
|
|
|
FileDiff,
|
2017-03-15 20:17:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class Metadata:
|
2017-03-15 20:17:42 +01:00
|
|
|
"""Metadata object for passing data to delegated tests."""
|
|
|
|
def __init__(self):
|
|
|
|
"""Initialize metadata."""
|
2019-07-11 22:03:49 +02:00
|
|
|
self.changes = {} # type: t.Dict[str, t.Tuple[t.Tuple[int, int]]]
|
|
|
|
self.cloud_config = None # type: t.Optional[t.Dict[str, str]]
|
|
|
|
self.instance_config = None # type: t.Optional[t.List[t.Dict[str, str]]]
|
|
|
|
self.change_description = None # type: t.Optional[ChangeDescription]
|
2017-03-15 20:17:42 +01:00
|
|
|
|
2017-07-12 23:16:46 +02:00
|
|
|
if is_shippable():
|
|
|
|
self.ci_provider = 'shippable'
|
|
|
|
else:
|
|
|
|
self.ci_provider = ''
|
|
|
|
|
2017-03-15 20:17:42 +01:00
|
|
|
def populate_changes(self, diff):
|
|
|
|
"""
|
|
|
|
:type diff: list[str] | None
|
|
|
|
"""
|
|
|
|
patches = parse_diff(diff)
|
2019-07-11 22:03:49 +02:00
|
|
|
patches = sorted(patches, key=lambda k: k.new.path) # type: t.List[FileDiff]
|
2017-03-15 20:17:42 +01:00
|
|
|
|
|
|
|
self.changes = dict((patch.new.path, tuple(patch.new.ranges)) for patch in patches)
|
|
|
|
|
|
|
|
renames = [patch.old.path for patch in patches if patch.old.path != patch.new.path and patch.old.exists and patch.new.exists]
|
|
|
|
deletes = [patch.old.path for patch in patches if not patch.new.exists]
|
|
|
|
|
|
|
|
# make sure old paths which were renamed or deleted are registered in changes
|
|
|
|
for path in renames + deletes:
|
|
|
|
if path in self.changes:
|
|
|
|
# old path was replaced with another file
|
|
|
|
continue
|
|
|
|
|
|
|
|
# failed tests involving deleted files should be using line 0 since there is no content remaining
|
|
|
|
self.changes[path] = ((0, 0),)
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
"""
|
|
|
|
:rtype: dict[str, any]
|
|
|
|
"""
|
|
|
|
return dict(
|
|
|
|
changes=self.changes,
|
2017-05-05 10:23:00 +02:00
|
|
|
cloud_config=self.cloud_config,
|
2017-10-26 09:21:46 +02:00
|
|
|
instance_config=self.instance_config,
|
2017-07-12 23:16:46 +02:00
|
|
|
ci_provider=self.ci_provider,
|
2018-04-13 01:15:28 +02:00
|
|
|
change_description=self.change_description.to_dict(),
|
2017-03-15 20:17:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
def to_file(self, path):
|
|
|
|
"""
|
|
|
|
:type path: path
|
|
|
|
"""
|
|
|
|
data = self.to_dict()
|
|
|
|
|
|
|
|
display.info('>>> Metadata: %s\n%s' % (path, data), verbosity=3)
|
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
write_json_file(path, data)
|
2017-03-15 20:17:42 +01:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_file(path):
|
|
|
|
"""
|
|
|
|
:type path: str
|
|
|
|
:rtype: Metadata
|
|
|
|
"""
|
2020-02-04 20:21:53 +01:00
|
|
|
data = read_json_file(path)
|
2017-03-15 20:17:42 +01:00
|
|
|
return Metadata.from_dict(data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_dict(data):
|
|
|
|
"""
|
|
|
|
:type data: dict[str, any]
|
|
|
|
:rtype: Metadata
|
|
|
|
"""
|
|
|
|
metadata = Metadata()
|
|
|
|
metadata.changes = data['changes']
|
2017-05-05 10:23:00 +02:00
|
|
|
metadata.cloud_config = data['cloud_config']
|
2017-10-26 09:21:46 +02:00
|
|
|
metadata.instance_config = data['instance_config']
|
2017-07-12 23:16:46 +02:00
|
|
|
metadata.ci_provider = data['ci_provider']
|
2018-04-13 01:15:28 +02:00
|
|
|
metadata.change_description = ChangeDescription.from_dict(data['change_description'])
|
2017-03-15 20:17:42 +01:00
|
|
|
|
|
|
|
return metadata
|
2018-04-13 01:15:28 +02:00
|
|
|
|
|
|
|
|
2019-07-12 22:17:20 +02:00
|
|
|
class ChangeDescription:
|
2018-04-13 01:15:28 +02:00
|
|
|
"""Description of changes."""
|
|
|
|
def __init__(self):
|
|
|
|
self.command = '' # type: str
|
2019-07-11 22:03:49 +02:00
|
|
|
self.changed_paths = [] # type: t.List[str]
|
|
|
|
self.deleted_paths = [] # type: t.List[str]
|
|
|
|
self.regular_command_targets = {} # type: t.Dict[str, t.List[str]]
|
|
|
|
self.focused_command_targets = {} # type: t.Dict[str, t.List[str]]
|
|
|
|
self.no_integration_paths = [] # type: t.List[str]
|
2018-04-13 01:15:28 +02:00
|
|
|
|
|
|
|
@property
|
|
|
|
def targets(self):
|
|
|
|
"""
|
|
|
|
:rtype: list[str] | None
|
|
|
|
"""
|
|
|
|
return self.regular_command_targets.get(self.command)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def focused_targets(self):
|
|
|
|
"""
|
|
|
|
:rtype: list[str] | None
|
|
|
|
"""
|
|
|
|
return self.focused_command_targets.get(self.command)
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
"""
|
|
|
|
:rtype: dict[str, any]
|
|
|
|
"""
|
|
|
|
return dict(
|
|
|
|
command=self.command,
|
|
|
|
changed_paths=self.changed_paths,
|
|
|
|
deleted_paths=self.deleted_paths,
|
|
|
|
regular_command_targets=self.regular_command_targets,
|
|
|
|
focused_command_targets=self.focused_command_targets,
|
|
|
|
no_integration_paths=self.no_integration_paths,
|
|
|
|
)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def from_dict(data):
|
|
|
|
"""
|
|
|
|
:param data: dict[str, any]
|
|
|
|
:rtype: ChangeDescription
|
|
|
|
"""
|
|
|
|
changes = ChangeDescription()
|
|
|
|
changes.command = data['command']
|
|
|
|
changes.changed_paths = data['changed_paths']
|
|
|
|
changes.deleted_paths = data['deleted_paths']
|
|
|
|
changes.regular_command_targets = data['regular_command_targets']
|
|
|
|
changes.focused_command_targets = data['focused_command_targets']
|
|
|
|
changes.no_integration_paths = data['no_integration_paths']
|
|
|
|
|
|
|
|
return changes
|