1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
import subprocess
import sys
import tempfile
import textwrap
from pathlib import Path
import pytest
from libqtile.scripts.migrations._base import NoChange
from test.test_check import run_qtile_check
class SourceCode:
def __init__(self, test):
self.source = textwrap.dedent(test.input)
self.expected = textwrap.dedent(test.output)
self.dir = tempfile.TemporaryDirectory()
self.source_path = Path(self.dir.name) / "config.py"
self.needs_lint = not isinstance(test, NoChange)
def __enter__(self):
self.source_path.write_text(self.source)
return self
def __exit__(self, type, value, traceback):
self.dir.cleanup()
del self.dir
class MigrationTester:
def __init__(self, test_id, test):
self.test_id = test_id
self.test = test
self.cmd = Path(__file__).parent / ".." / ".." / "libqtile" / "scripts" / "main.py"
def assert_migrate(self):
if not self.test.source:
assert False, f"{self.test_id} has no tests."
argv = [
sys.executable,
self.cmd,
"migrate",
"--yes",
"-r",
self.test_id,
"-c",
self.test.source_path,
]
try:
subprocess.check_call(argv)
except subprocess.CalledProcessError:
assert False
updated = Path(f"{self.test.source_path}").read_text()
assert updated == self.test.expected
def assert_lint(self):
argv = [
sys.executable,
self.cmd,
"migrate",
"--lint",
"-r",
self.test_id,
"-c",
self.test.source_path,
]
try:
output = subprocess.run(argv, capture_output=True, check=True)
except subprocess.CalledProcessError:
assert False
assert any(self.test_id in line for line in output.stdout.decode().splitlines())
def assert_check(self):
if not self.test.source:
assert False, f"{self.test_id} has no Check test."
self.assert_migrate()
assert run_qtile_check(self.test.source_path)
@pytest.fixture
def migration_tester(request):
test_id, test = getattr(request, "param", (None, None))
if test is None:
test = NoChange("")
with SourceCode(test) as sc:
yield MigrationTester(test_id, sc)
|