mirror of
https://github.com/certbot/certbot.git
synced 2026-01-26 07:41:33 +03:00
This is part of https://github.com/certbot/certbot/issues/8782. I took it on now because the currently pinned version of `pylint` doesn't work with newer versions of `poetry` which I wanted to upgrade as part of https://github.com/certbot/certbot/issues/8787. To say a bit more about the specific changes in this PR: * Newer versions of `pylint` complain if `Popen` isn't used as a context manager. Instead of making this change, I switched to using `subprocess.run` which is simpler and [recommended in the Python docs](https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module). I also disabled this check in a few places where no longer using `Popen` would require significant refactoring. * The deleted code in `certbot/certbot/_internal/renewal.py` is cruft since https://github.com/certbot/certbot/pull/8685. * The unused argument to `enable_mod` in the Apache plugin is used in some over the override classes that subclass that class. * unpin pylint and repin dependencies * disable raise-missing-from * disable wrong-input-order * remove unused code * misc lint fixes * remove unused import * various lint fixes
142 lines
4.9 KiB
Python
142 lines
4.9 KiB
Python
"""Dual parser node assertions"""
|
|
import fnmatch
|
|
|
|
from certbot_apache._internal import interfaces
|
|
|
|
PASS = "CERTBOT_PASS_ASSERT"
|
|
|
|
|
|
def assertEqual(first, second):
|
|
""" Equality assertion """
|
|
|
|
if isinstance(first, interfaces.CommentNode):
|
|
assertEqualComment(first, second)
|
|
elif isinstance(first, interfaces.DirectiveNode):
|
|
assertEqualDirective(first, second)
|
|
|
|
# Do an extra interface implementation assertion, as the contents were
|
|
# already checked for BlockNode in the assertEqualDirective
|
|
if isinstance(first, interfaces.BlockNode):
|
|
assert isinstance(second, interfaces.BlockNode)
|
|
|
|
# Skip tests if filepath includes the pass value. This is done
|
|
# because filepath is variable of the base ParserNode interface, and
|
|
# unless the implementation is actually done, we cannot assume getting
|
|
# correct results from boolean assertion for dirty
|
|
if not isPass(first.filepath) and not isPass(second.filepath):
|
|
assert first.dirty == second.dirty
|
|
# We might want to disable this later if testing with two separate
|
|
# (but identical) directory structures.
|
|
assert first.filepath == second.filepath
|
|
|
|
def assertEqualComment(first, second): # pragma: no cover
|
|
""" Equality assertion for CommentNode """
|
|
|
|
assert isinstance(first, interfaces.CommentNode)
|
|
assert isinstance(second, interfaces.CommentNode)
|
|
|
|
if not isPass(first.comment) and not isPass(second.comment): # type: ignore
|
|
assert first.comment == second.comment # type: ignore
|
|
|
|
def _assertEqualDirectiveComponents(first, second): # pragma: no cover
|
|
""" Handles assertion for instance variables for DirectiveNode and BlockNode"""
|
|
|
|
# Enabled value cannot be asserted, because Augeas implementation
|
|
# is unable to figure that out.
|
|
# assert first.enabled == second.enabled
|
|
if not isPass(first.name) and not isPass(second.name):
|
|
assert first.name == second.name
|
|
|
|
if not isPass(first.parameters) and not isPass(second.parameters):
|
|
assert first.parameters == second.parameters
|
|
|
|
def assertEqualDirective(first, second):
|
|
""" Equality assertion for DirectiveNode """
|
|
|
|
assert isinstance(first, interfaces.DirectiveNode)
|
|
assert isinstance(second, interfaces.DirectiveNode)
|
|
_assertEqualDirectiveComponents(first, second)
|
|
|
|
def isPass(value): # pragma: no cover
|
|
"""Checks if the value is set to PASS"""
|
|
if isinstance(value, bool):
|
|
return True
|
|
return PASS in value
|
|
|
|
def isPassDirective(block):
|
|
""" Checks if BlockNode or DirectiveNode should pass the assertion """
|
|
|
|
if isPass(block.name):
|
|
return True
|
|
if isPass(block.parameters): # pragma: no cover
|
|
return True
|
|
if isPass(block.filepath): # pragma: no cover
|
|
return True
|
|
return False
|
|
|
|
def isPassComment(comment):
|
|
""" Checks if CommentNode should pass the assertion """
|
|
|
|
if isPass(comment.comment):
|
|
return True
|
|
if isPass(comment.filepath): # pragma: no cover
|
|
return True
|
|
return False
|
|
|
|
def isPassNodeList(nodelist): # pragma: no cover
|
|
""" Checks if a ParserNode in the nodelist should pass the assertion,
|
|
this function is used for results of find_* methods. Unimplemented find_*
|
|
methods should return a sequence containing a single ParserNode instance
|
|
with assertion pass string."""
|
|
|
|
try:
|
|
node = nodelist[0]
|
|
except IndexError:
|
|
node = None
|
|
|
|
if not node: # pragma: no cover
|
|
return False
|
|
|
|
if isinstance(node, interfaces.DirectiveNode):
|
|
return isPassDirective(node)
|
|
return isPassComment(node)
|
|
|
|
def assertEqualSimple(first, second):
|
|
""" Simple assertion """
|
|
if not isPass(first) and not isPass(second):
|
|
assert first == second
|
|
|
|
def isEqualVirtualHost(first, second):
|
|
"""
|
|
Checks that two VirtualHost objects are similar. There are some built
|
|
in differences with the implementations: VirtualHost created by ParserNode
|
|
implementation doesn't have "path" defined, as it was used for Augeas path
|
|
and that cannot obviously be used in the future. Similarly the legacy
|
|
version lacks "node" variable, that has a reference to the BlockNode for the
|
|
VirtualHost.
|
|
"""
|
|
return (
|
|
first.name == second.name and
|
|
first.aliases == second.aliases and
|
|
first.filep == second.filep and
|
|
first.addrs == second.addrs and
|
|
first.ssl == second.ssl and
|
|
first.enabled == second.enabled and
|
|
first.modmacro == second.modmacro and
|
|
first.ancestor == second.ancestor
|
|
)
|
|
|
|
def assertEqualPathsList(first, second): # pragma: no cover
|
|
"""
|
|
Checks that the two lists of file paths match. This assertion allows for wildcard
|
|
paths.
|
|
"""
|
|
if any(isPass(path) for path in first):
|
|
return
|
|
if any(isPass(path) for path in second):
|
|
return
|
|
for fpath in first:
|
|
assert any(fnmatch.fnmatch(fpath, spath) for spath in second)
|
|
for spath in second:
|
|
assert any(fnmatch.fnmatch(fpath, spath) for fpath in first)
|