1from pathlib import Path
2from typing import Optional
3
4from qbpm import profiles
5from qbpm.profiles import Profile
6
7
8def check_is_empty(path: Path):
9 assert len(list(path.iterdir())) == 0
10
11
12def check_empty_profile(profile: Optional[Profile]):
13 assert profile
14 config_dir = profile.root / "config"
15 assert list(profile.root.iterdir()) == [config_dir]
16 assert list(config_dir.iterdir()) == []
17
18
19def check_new_profile(profile: Profile):
20 assert profile
21 config_dir = profile.root / "config"
22 assert list(profile.root.iterdir()) == [config_dir]
23 assert list(config_dir.iterdir()) == [config_dir / "config.py"]
24
25
26def test_set_profile(tmp_path: Path):
27 assert Profile("test", tmp_path).root == tmp_path / "test"
28
29
30def test_create_profile(tmp_path: Path):
31 profile = Profile("test", tmp_path)
32 assert profiles.create_profile(profile)
33 assert list(tmp_path.iterdir()) == [profile.root]
34 check_empty_profile(profile)
35
36
37def test_create_profile_conflict(tmp_path: Path):
38 (tmp_path / "test").touch()
39 profile = Profile("test", tmp_path)
40 assert not profiles.create_profile(profile)
41
42
43def test_create_profile_parent(tmp_path: Path):
44 profile = Profile("../test", tmp_path / "profiles")
45 assert not profiles.create_profile(profile)
46 assert not (tmp_path / "test").exists()
47
48
49def test_create_profile_nested_conflict(tmp_path: Path):
50 assert profiles.create_profile(Profile("test", tmp_path))
51 assert not profiles.create_profile(Profile("test/a", tmp_path))
52
53
54def test_create_config(tmp_path: Path):
55 profile = Profile("test", tmp_path)
56 config_dir = profile.root / "config"
57 config_dir.mkdir(parents=True)
58 profiles.create_config(profile)
59 assert list(config_dir.iterdir()) == [config_dir / "config.py"]
60
61
62def test_overwrite_config(tmp_path: Path):
63 profile = Profile("test", tmp_path)
64 url = "http://example.com"
65 config_dir = profile.root / "config"
66 config_dir.mkdir(parents=True)
67 profiles.create_config(profile)
68 profiles.create_config(profile, url, True)
69 assert list(config_dir.iterdir()) == [config_dir / "config.py"]
70 with open(config_dir / "config.py") as conf:
71 for line in conf:
72 if url in line:
73 return
74 raise AssertionError()
75
76
77def test_ensure_profile_exists_exists(tmp_path: Path):
78 profile = Profile("test", tmp_path)
79 profile.root.mkdir()
80 assert profiles.ensure_profile_exists(profile, False)
81 assert profiles.ensure_profile_exists(profile, True)
82 check_is_empty(profile.root)
83
84
85def test_ensure_profile_exists_does_not_exist(tmp_path: Path):
86 assert not profiles.ensure_profile_exists(Profile("test", tmp_path), False)
87 check_is_empty(tmp_path)
88
89
90def test_ensure_profile_exists_not_dir(tmp_path: Path):
91 profile = Profile("test", tmp_path)
92 profile.root.touch()
93 assert not profiles.ensure_profile_exists(profile, False)
94 assert not profiles.ensure_profile_exists(profile, True)
95
96
97def test_ensure_profile_exists_create(tmp_path: Path):
98 profile = Profile("test", tmp_path)
99 assert profiles.ensure_profile_exists(profile, True)
100 check_new_profile(profile)
101
102
103def test_new_profile(tmp_path: Path):
104 profile = Profile("test", tmp_path)
105 assert profiles.new_profile(profile)
106 check_new_profile(profile)