Skip to content

Commit 142aec6

Browse files
committed
tests: export testgen class to a new package/repo
1 parent 2908bdb commit 142aec6

File tree

5 files changed

+39
-41
lines changed

5 files changed

+39
-41
lines changed

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ test = [
6262
"types-paramiko>=2.7,<4",
6363
"types-python-dateutil>2,<3",
6464
"types-PyYAML>6,<7",
65+
"pyinfra-testgen==0.1.1",
6566
]
6667

6768
docs = [

tests/test_facts.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
from os import listdir, path
55
from unittest import TestCase
66

7+
from testgen import TestGenerator
8+
79
from pyinfra.api import StringCommand
810
from pyinfra.api.facts import ShortFactBase
911
from pyinfra.context import ctx_host, ctx_state
1012
from pyinfra_cli.util import json_encode
1113

12-
from .util import FakeState, YamlTest, create_host, get_command_string
14+
from .util import FakeState, create_host, get_command_string
1315

1416
# show full diff on json
1517
TestCase.maxDiff = None
@@ -32,16 +34,19 @@ def make_fact_tests(folder_name):
3234
module = import_module("pyinfra.facts.{0}".format(module_name))
3335
fact = getattr(module, fact_name)()
3436

35-
class TestTests(TestCase, metaclass=YamlTest):
36-
yaml_test_dir = path.join("tests", "facts", folder_name)
37-
yaml_test_prefix = "test_{0}_".format(fact.name)
38-
37+
class TestTests(
38+
TestCase,
39+
metaclass=TestGenerator,
40+
tests_dir=path.join("tests", "facts", folder_name),
41+
test_prefix="test_{0}_".format(fact.name),
42+
test_method="_test",
43+
):
3944
@classmethod
4045
def setUpClass(cls):
4146
# Create a global fake state that attach to context state
4247
cls.state = FakeState()
4348

44-
def yaml_test_function(self, test_name, test_data, fact=fact):
49+
def _test(self, test_name, test_data, fact=fact):
4550
host = create_host(self.state, facts=test_data.get("facts", {}))
4651
with ctx_state.use(self.state):
4752
with ctx_host.use(host):

tests/test_operations.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@
66
from unittest import TestCase
77
from unittest.mock import patch
88

9+
from testgen import TestGenerator
10+
911
from pyinfra.api import FileDownloadCommand, FileUploadCommand, FunctionCommand, StringCommand
1012
from pyinfra.context import ctx_host, ctx_state
1113
from pyinfra_cli.util import json_encode
1214

13-
from .util import FakeState, YamlTest, create_host, get_command_string, parse_value, patch_files
15+
from .util import FakeState, create_host, get_command_string, parse_value, patch_files
1416

1517
PLATFORM_NAME = platform.system()
1618

@@ -92,16 +94,19 @@ def make_operation_tests(arg):
9294
# Generate a test class
9395
@patch("pyinfra.operations.files.get_timestamp", lambda: "a-timestamp")
9496
@patch("pyinfra.operations.util.files.get_timestamp", lambda: "a-timestamp")
95-
class TestTests(TestCase, metaclass=YamlTest):
96-
yaml_test_dir = path.join("tests", "operations", arg)
97-
yaml_test_prefix = "test_{0}_{1}_".format(module_name, op_name)
98-
97+
class TestTests(
98+
TestCase,
99+
metaclass=TestGenerator,
100+
tests_dir=path.join("tests", "operations", arg),
101+
test_prefix="test_{0}_{1}_".format(module_name, op_name),
102+
test_method="_test",
103+
):
99104
@classmethod
100105
def setUpClass(cls):
101106
# Create a global fake state that attach to context state
102107
cls.state = FakeState()
103108

104-
def yaml_test_function(self, test_name, test_data):
109+
def _test(self, test_name, test_data):
105110
if (
106111
"require_platform" in test_data
107112
and PLATFORM_NAME not in test_data["require_platform"]

tests/util.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
from pathlib import Path
99
from unittest.mock import patch
1010

11-
import yaml
12-
1311
from pyinfra.api import Config, Inventory
1412
from pyinfra.api.util import get_kwargs_str
1513

@@ -479,30 +477,3 @@ def create_host(state, name=None, facts=None, data=None):
479477
real_facts[name] = fact_data
480478

481479
return FakeHost(state, name, facts=real_facts, data=data)
482-
483-
484-
class YamlTest(type):
485-
def __new__(cls, name, bases, attrs):
486-
test_suffixes = {".yaml", ".yml", ".json"}
487-
488-
tests_dir = Path(attrs["yaml_test_dir"])
489-
490-
test_files = [f for f in tests_dir.iterdir() if f.suffix in test_suffixes]
491-
492-
test_prefix = attrs.get("yaml_test_prefix", "test_")
493-
494-
def gen_test(test_name, test_file):
495-
def test(self):
496-
test_data = yaml.safe_load(test_file.open(encoding="utf-8").read())
497-
self.yaml_test_function(test_name, test_data)
498-
499-
return test
500-
501-
# Loop them and create class methods to call the yaml_test_function
502-
for test_file in test_files:
503-
test_name = test_file.stem
504-
# Attach the method
505-
method_name = "{0}{1}".format(test_prefix, test_name)
506-
attrs[method_name] = gen_test(test_name, test_file)
507-
508-
return type.__new__(cls, name, bases, attrs)

uv.lock

Lines changed: 16 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)