~pvsr/qpm

qpm/tests/test_profiles.py -rw-r--r-- 3.2 KiB
8b6b26a0Peter Rice document dmenu-wl support 2 months ago
                                                                                
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
from pathlib import Path
from typing import Optional

from qpm import profiles
from qpm.profiles import Profile


def check_is_empty(path: Path):
    assert len(list(path.iterdir())) == 0


def check_empty_profile(profile: Optional[Profile]):
    assert profile
    config_dir = profile.root / "config"
    assert list(profile.root.iterdir()) == [config_dir]
    assert list(config_dir.iterdir()) == []


def check_new_profile(profile: Profile):
    assert profile
    config_dir = profile.root / "config"
    assert list(profile.root.iterdir()) == [config_dir]
    assert list(config_dir.iterdir()) == [config_dir / "config.py"]


def test_set_profile(tmp_path: Path):
    assert Profile("test", tmp_path).root == tmp_path / "test"


def test_create_profile(tmp_path: Path):
    profile = Profile("test", tmp_path)
    assert profiles.create_profile(profile)
    assert list(tmp_path.iterdir()) == [profile.root]
    check_empty_profile(profile)


def test_create_profile_conflict(tmp_path: Path):
    (tmp_path / "test").touch()
    profile = Profile("test", tmp_path)
    assert not profiles.create_profile(profile)


def test_create_profile_parent(tmp_path: Path):
    profile = Profile("../test", tmp_path / "profiles")
    assert not profiles.create_profile(profile)
    assert not (tmp_path / "test").exists()


def test_create_profile_nested_conflict(tmp_path: Path):
    assert profiles.create_profile(Profile("test", tmp_path))
    assert not profiles.create_profile(Profile("test/a", tmp_path))


def test_create_config(tmp_path: Path):
    profile = Profile("test", tmp_path)
    config_dir = profile.root / "config"
    config_dir.mkdir(parents=True)
    profiles.create_config(profile)
    assert list(config_dir.iterdir()) == [config_dir / "config.py"]


def test_overwrite_config(tmp_path: Path):
    profile = Profile("test", tmp_path)
    url = "http://example.com"
    config_dir = profile.root / "config"
    config_dir.mkdir(parents=True)
    profiles.create_config(profile)
    profiles.create_config(profile, url, True)
    assert list(config_dir.iterdir()) == [config_dir / "config.py"]
    with open(config_dir / "config.py") as conf:
        for line in conf:
            if url in line:
                return
    assert False


def test_ensure_profile_exists_exists(tmp_path: Path):
    profile = Profile("test", tmp_path)
    profile.root.mkdir()
    assert profiles.ensure_profile_exists(profile, False)
    assert profiles.ensure_profile_exists(profile, True)
    check_is_empty(profile.root)


def test_ensure_profile_exists_does_not_exist(tmp_path: Path):
    assert not profiles.ensure_profile_exists(Profile("test", tmp_path), False)
    check_is_empty(tmp_path)


def test_ensure_profile_exists_not_dir(tmp_path: Path):
    profile = Profile("test", tmp_path)
    profile.root.touch()
    assert not profiles.ensure_profile_exists(profile, False)
    assert not profiles.ensure_profile_exists(profile, True)


def test_ensure_profile_exists_create(tmp_path: Path):
    profile = Profile("test", tmp_path)
    assert profiles.ensure_profile_exists(profile, True)
    check_new_profile(profile)


def test_new_profile(tmp_path: Path):
    profile = Profile("test", tmp_path)
    assert profiles.new_profile(profile)
    check_new_profile(profile)