Make ansible-test TestMessage hashable.

This commit is contained in:
Matt Clay 2019-07-19 16:34:16 -07:00
parent 2d5a0d4918
commit b378e885ed

View file

@ -6,6 +6,8 @@ import datetime
import json import json
import os import os
import lib.types as t
from lib.util import ( from lib.util import (
display, display,
make_dirs, make_dirs,
@ -246,7 +248,7 @@ class TestFailure(TestResult):
super(TestFailure, self).__init__(command, test, python_version) super(TestFailure, self).__init__(command, test, python_version)
if messages: if messages:
messages = sorted(messages, key=lambda m: m.sort_key) messages = sorted(messages)
else: else:
messages = [] messages = []
@ -430,14 +432,71 @@ class TestMessage:
:type code: str | None :type code: str | None
:type confidence: int | None :type confidence: int | None
""" """
self.path = path self.__path = path
self.line = line self.__line = line
self.column = column self.__column = column
self.level = level self.__level = level
self.code = code self.__code = code
self.message = message self.__message = message
self.confidence = confidence self.confidence = confidence
@property
def path(self): # type: () -> str
"""Return the path."""
return self.__path
@property
def line(self): # type: () -> int
"""Return the line number, or 0 if none is available."""
return self.__line
@property
def column(self): # type: () -> int
"""Return the column number, or 0 if none is available."""
return self.__column
@property
def level(self): # type: () -> str
"""Return the level."""
return self.__level
@property
def code(self): # type: () -> t.Optional[str]
"""Return the code, if any."""
return self.__code
@property
def message(self): # type: () -> str
"""Return the message."""
return self.__message
@property
def tuple(self): # type: () -> t.Tuple[str, int, int, str, t.Optional[str], str]
"""Return a tuple with all the immutable values of this test message."""
return self.__path, self.__line, self.__column, self.__level, self.__code, self.__message
def __lt__(self, other):
return self.tuple < other.tuple
def __le__(self, other):
return self.tuple <= other.tuple
def __eq__(self, other):
return self.tuple == other.tuple
def __ne__(self, other):
return self.tuple != other.tuple
def __gt__(self, other):
return self.tuple > other.tuple
def __ge__(self, other):
return self.tuple >= other.tuple
def __hash__(self):
return hash(self.tuple)
def __str__(self): def __str__(self):
return self.format() return self.format()
@ -446,19 +505,12 @@ class TestMessage:
:type show_confidence: bool :type show_confidence: bool
:rtype: str :rtype: str
""" """
if self.code: if self.__code:
msg = '%s %s' % (self.code, self.message) msg = '%s %s' % (self.__code, self.__message)
else: else:
msg = self.message msg = self.__message
if show_confidence and self.confidence is not None: if show_confidence and self.confidence is not None:
msg += ' (%d%%)' % self.confidence msg += ' (%d%%)' % self.confidence
return '%s:%s:%s: %s' % (self.path, self.line, self.column, msg) return '%s:%s:%s: %s' % (self.__path, self.__line, self.__column, msg)
@property
def sort_key(self):
"""
:rtype: str
"""
return '%s:%6d:%6d:%s:%s' % (self.path, self.line, self.column, self.code or '', self.message)