Skip to content

Commit

Permalink
Remove duplicities in dependencies
Browse files Browse the repository at this point in the history
This creates a Dependency class to make dependencies hashable. Thanks to this
change, we can easily find duplicates and remove them. This change is really
necessary for Job dependencies, where the duplicates can be easily created by
adding the same dependency to a test and job.

Signed-off-by: Jan Richter <[email protected]>
Signed-off-by: Cleber Rosa <[email protected]>
  • Loading branch information
richtja authored and clebergnu committed Jun 11, 2024
1 parent dc4e30f commit e5bed0e
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 19 deletions.
70 changes: 70 additions & 0 deletions avocado/core/dependencies/dependency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#
# See LICENSE for more details.
#
# Copyright: Red Hat Inc. 2024
# Authors: Jan Richter <[email protected]>

from avocado.core.nrunner.runnable import Runnable


class Dependency:
"""
Data holder for dependency.
"""

def __init__(self, kind=None, uri=None, args=(), kwargs=None):
self._kind = kind
self._uri = uri
self._args = args
self._kwargs = kwargs or {}

@property
def kind(self):
return self._kind

@property
def uri(self):
return self._uri

@property
def args(self):
return self._args

@property
def kwargs(self):
return self._kwargs

def __hash__(self):
return hash(
(
self.kind,
self.uri,
tuple(sorted(self.args)),
tuple(sorted(self.kwargs.items())),
)
)

def __eq__(self, other):
if isinstance(other, Dependency):
return hash(self) == hash(other)
return False

def to_runnable(self, config):
return Runnable(self.kind, self.uri, *self.args, config=config, **self.kwargs)

@classmethod
def from_dictionary(cls, dictionary):
return cls(
dictionary.pop("type", None),
dictionary.pop("uri", None),
dictionary.pop("args", ()),
dictionary,
)
6 changes: 5 additions & 1 deletion avocado/core/safeloader/docstring.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import json
import re

from avocado.core.dependencies.dependency import Dependency

#: Gets the docstring directive value from a string. Used to tweak
#: test behavior in various ways
DOCSTRING_DIRECTIVE_RE_RAW = (
Expand Down Expand Up @@ -78,7 +80,9 @@ def get_docstring_directives_dependencies(docstring):
if item.startswith("dependency="):
_, dependency_str = item.split("dependency=", 1)
try:
dependencies.append(json.loads(dependency_str))
dependencies.append(
Dependency.from_dictionary(json.loads(dependency_str))
)
except json.decoder.JSONDecodeError:
# ignore dependencies in case of malformed dictionary
continue
Expand Down
15 changes: 3 additions & 12 deletions avocado/plugins/dependency.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# Copyright: Red Hat Inc. 2021
# Authors: Willian Rampazzo <[email protected]>

from avocado.core.nrunner.runnable import Runnable
from avocado.core.plugin_interfaces import PreTest


Expand All @@ -33,15 +32,7 @@ def pre_test_runnables(test_runnable, suite_config=None): # pylint: disable=W02
if not test_runnable.dependencies:
return []
dependency_runnables = []
for dependency in test_runnable.dependencies:
# make a copy to change the dictionary and do not affect the
# original `dependencies` dictionary from the test
dependency_copy = dependency.copy()
kind = dependency_copy.pop("type")
uri = dependency_copy.pop("uri", None)
args = dependency_copy.pop("args", ())
dependency_runnable = Runnable(
kind, uri, *args, config=test_runnable.config, **dependency_copy
)
dependency_runnables.append(dependency_runnable)
unique_dependencies = list(dict.fromkeys(test_runnable.dependencies))
for dependency in unique_dependencies:
dependency_runnables.append(dependency.to_runnable(test_runnable.config))
return dependency_runnables
4 changes: 2 additions & 2 deletions selftests/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"job-api-6": 4,
"job-api-7": 1,
"nrunner-interface": 70,
"nrunner-requirement": 16,
"nrunner-requirement": 20,
"unit": 668,
"jobs": 11,
"functional-parallel": 298,
"functional-serial": 4,
"functional-serial": 5,
"optional-plugins": 0,
"optional-plugins-golang": 2,
"optional-plugins-html": 3,
Expand Down
34 changes: 34 additions & 0 deletions selftests/functional/serial/requirements.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,26 @@ def test_c(self):
"""
'''

SINGLE_SUCCESS_DUPLCITIES = '''from avocado import Test
from avocado.utils import process
class SuccessTest(Test):
def check_hello(self):
result = process.run("hello", ignore_status=True)
self.assertEqual(result.exit_status, 0)
self.assertIn('Hello, world!', result.stdout_text,)
def test_a(self):
"""
:avocado: dependency={"type": "package", "name": "hello"}
:avocado: dependency={"type": "package", "name": "hello"}
:avocado: dependency={"type": "package", "name": "hello"}
"""
self.check_hello()
'''


class BasicTest(TestCaseTmpDir, Test):

Expand Down Expand Up @@ -223,6 +243,20 @@ def test_multiple_fails(self):
result.stdout_text,
)

@skipUnless(os.getenv("CI"), skip_install_message)
def test_dependency_duplicates(self):
with script.Script(
os.path.join(self.tmpdir.name, "test_single_success.py"),
SINGLE_SUCCESS_DUPLCITIES,
) as test:
command = self.get_command(test.path)
result = process.run(command, ignore_status=True)
self.assertEqual(result.exit_status, exit_codes.AVOCADO_ALL_OK)
self.assertIn(
"PASS 1",
result.stdout_text,
)


if __name__ == "__main__":
unittest.main()
5 changes: 3 additions & 2 deletions selftests/unit/dependencies_resolver.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

from avocado.core.dependencies.dependency import Dependency
from avocado.core.nrunner.runnable import Runnable
from avocado.plugins.dependency import DependencyResolver

Expand All @@ -12,8 +13,8 @@ def test_dependencies_runnables(self):
kind="package",
uri=None,
dependencies=[
{"type": "package", "name": "foo"},
{"type": "package", "name": "bar"},
Dependency("package", kwargs={"name": "foo"}),
Dependency("package", kwargs={"name": "bar"}),
],
)
dependency_runnables = DependencyResolver.pre_test_runnables(runnable)
Expand Down
5 changes: 3 additions & 2 deletions selftests/unit/safeloader_docstring.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest

from avocado.core.dependencies.dependency import Dependency
from avocado.core.safeloader.docstring import (
DOCSTRING_DIRECTIVE_RE,
check_docstring_directive,
Expand Down Expand Up @@ -165,12 +166,12 @@ def test_get_dependency_empty(self):

def test_dependency_single(self):
raw = ':avocado: dependency={"foo":"bar"}'
exp = [{"foo": "bar"}]
exp = [Dependency(kwargs={"foo": "bar"})]
self.assertEqual(get_docstring_directives_dependencies(raw), exp)

def test_dependency_double(self):
raw = ':avocado: dependency={"foo":"bar"}\n:avocado: dependency={"uri":"http://foo.bar"}'
exp = [{"foo": "bar"}, {"uri": "http://foo.bar"}]
exp = [Dependency(kwargs={"foo": "bar"}), Dependency(uri="http://foo.bar")]
self.assertEqual(get_docstring_directives_dependencies(raw), exp)

def test_directives_regex(self):
Expand Down

0 comments on commit e5bed0e

Please sign in to comment.