File: test_check.py

package info (click to toggle)
qtile 0.34.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,004 kB
  • sloc: python: 49,959; ansic: 4,371; xml: 324; sh: 260; makefile: 218
file content (109 lines) | stat: -rw-r--r-- 2,818 bytes parent folder | download
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import os
import platform
import shutil
import subprocess
import sys
import tempfile
import textwrap

import pytest


def is_cpython():
    # https://mypy.readthedocs.io/en/stable/faq.html#does-it-run-on-pypy
    return platform.python_implementation() == "CPython"


def have_mypy():
    return shutil.which("mypy") is not None


pytestmark = pytest.mark.skipif(not is_cpython() or not have_mypy(), reason="needs mypy")


def run_qtile_check(config):
    # make sure we have mypy so we can type check the config
    if shutil.which("mypy") is None:
        raise Exception("missing mypy in test environment")
    cmd = os.path.join(os.path.dirname(__file__), "..", "libqtile", "scripts", "main.py")
    argv = [sys.executable, cmd, "check", "-c", config]
    try:
        newenv = os.environ.copy()
        old_pp = newenv.get("PYTHONPATH", "")
        newenv["PYTHONPATH"] = os.path.join(os.path.dirname(__file__), "..") + ":" + old_pp
        subprocess.check_call(argv, env=newenv)
    except subprocess.CalledProcessError:
        return False
    else:
        return True


def test_check_default_config():
    # the default config should always be ok
    default_config = os.path.join(
        os.path.dirname(__file__), "..", "libqtile", "resources", "default_config.py"
    )
    assert run_qtile_check(default_config)


def check_literal_config(config):
    with tempfile.TemporaryDirectory() as tempdir:
        with tempfile.NamedTemporaryFile(dir=tempdir, suffix=".py") as temp:
            temp.write(textwrap.dedent(config).encode("utf-8"))
            temp.flush()
            return run_qtile_check(temp.name)


def test_check_bad_syntax():
    assert not check_literal_config(
        """
        meshuggah rocks!
    """
    )


def test_check_bad_key_arg():
    assert not check_literal_config(
        """
            Key([1], 1)
    """
    )


def test_check_good_key_arg():
    assert check_literal_config(
        """
        from libqtile.config import Key
        from libqtile.lazy import lazy

        keys = [Key(["mod4"], "x", lazy.kill())]
        cursor_warp = True
    """
    )


def test_check_bad_config_type():
    assert not check_literal_config(
        """
        keys = "i am not a List[Key]"
        cursor_warp = "i am not a bool"
    """
    )


def test_extra_vars_are_ok():
    assert check_literal_config(
        """
        this_is_an_extra_config_var = "yes it is"
    """
    )


def test_extra_files_are_ok():
    with tempfile.TemporaryDirectory() as tempdir:
        config_file = os.path.join(tempdir, "config.py")
        with open(config_file, "w") as config:
            config.write("from bar import foo\n")
        with open(os.path.join(tempdir, "bar.py"), "w") as config:
            config.write("foo = 42")
        assert run_qtile_check(config_file)