wireshark/test/matchers.py
Gerald Combs 30c392f166 Tools+test: Call python3 explicitly.
PEP 394[1] says,

"In cases where the script is expected to be executed outside virtual
 environments, developers will need to be aware of the following
 discrepancies across platforms and installation methods:

  * Older Linux distributions will provide a python command that refers
    to Python 2, and will likely not provide a python2 command.

  * Some newer Linux distributions will provide a python command that
    refers to Python 3.

  * Some Linux distributions will not provide a python command at all by
    default, but will provide a python3 command by default."

Debian has forced the issue by choosing the third option[2]:

"NOTE: Debian testing (bullseye) has removed the "python" package and
 the '/usr/bin/python' symlink due to the deprecation of Python 2."

Switch our shebang from "#!/usr/bin/env python" to "#!/usr/bin/env
python3" in some places. Remove some 2/3 version checks if we know we're
running under Python 3. Remove the "coding: utf-8" in a bunch of places
since that's the default in Python 3.

[1]https://www.python.org/dev/peps/pep-0394/#for-python-script-publishers
[2]https://wiki.debian.org/Python
2020-11-05 06:46:35 +00:00

67 lines
1.7 KiB
Python

#
# Wireshark tests
#
# Copyright (c) 2018 Peter Wu <peter@lekensteyn.nl>
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
'''Helpers for matching test results.'''
import re
class MatchAny(object):
'''Matches any other value.'''
def __init__(self, type=None):
self.type = type
def __eq__(self, other):
return self.type is None or self.type == type(other)
def __repr__(self):
return '<MatchAny type=%s>' % (self.type.__name__,)
class MatchObject(object):
'''Matches all expected fields of an object, ignoring excess others.'''
def __init__(self, fields):
self.fields = fields
def __eq__(self, other):
return all(other.get(k) == v for k, v in self.fields.items())
def __repr__(self):
return '<MatchObject fields=%r>' % (self.fields,)
class MatchList(object):
'''Matches elements of a list. Optionally checks list length.'''
def __init__(self, item, n=None, match_element=all):
self.item = item
self.n = n
self.match_element = match_element
def __eq__(self, other):
if self.n is not None and len(other) != self.n:
return False
return self.match_element(self.item == elm for elm in other)
def __repr__(self):
return '<MatchList item=%r n=%r match_element=%s>' % \
(self.item, self.n, self.match_element.__name__)
class MatchRegExp(object):
'''Matches a string against a regular expression.'''
def __init__(self, pattern):
self.pattern = pattern
def __eq__(self, other):
return type(other) == str and re.match(self.pattern, other)
def __repr__(self):
return '<MatchRegExp pattern=%r>' % (self.pattern)