| 1 |
import os |
| 2 |
import tempfile |
| 3 |
from configparser import ConfigParser |
| 4 |
from pathlib import Path |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
import errbot.repo_manager |
| 9 |
from errbot import plugin_manager |
| 10 |
from errbot.plugin_info import PluginInfo |
| 11 |
from errbot.plugin_manager import IncompatiblePluginException |
| 12 |
from errbot.utils import collect_roots, entry_point_plugins, find_roots |
| 13 |
|
| 14 |
CORE_PLUGINS = plugin_manager.CORE_PLUGINS |
| 15 |
|
| 16 |
|
| 17 |
def touch(name): |
| 18 |
with open(name, "a"): |
| 19 |
os.utime(name, None) |
| 20 |
|
| 21 |
|
| 22 |
assets = Path(__file__).parent / "assets" |
| 23 |
|
| 24 |
|
| 25 |
def test_check_dependencies(): |
| 26 |
response, deps = errbot.repo_manager.check_dependencies( |
| 27 |
assets / "requirements_never_there.txt" |
| 28 |
) |
| 29 |
assert "You need these dependencies for" in response |
| 30 |
assert "impossible_requirement" in response |
| 31 |
assert ["impossible_requirement"] == deps |
| 32 |
|
| 33 |
|
| 34 |
def test_check_dependencies_no_requirements_file(): |
| 35 |
response, deps = errbot.repo_manager.check_dependencies( |
| 36 |
assets / "requirements_non_existent.txt" |
| 37 |
) |
| 38 |
assert response is None |
| 39 |
|
| 40 |
|
| 41 |
def test_check_dependencies_requirements_file_all_installed(): |
| 42 |
response, deps = errbot.repo_manager.check_dependencies( |
| 43 |
assets / "requirements_already_there.txt" |
| 44 |
) |
| 45 |
assert response is None |
| 46 |
|
| 47 |
|
| 48 |
def test_find_plugin_roots(): |
| 49 |
root = tempfile.mkdtemp() |
| 50 |
a = os.path.join(root, "a") |
| 51 |
b = os.path.join(a, "b") |
| 52 |
c = os.path.join(root, "c") |
| 53 |
os.mkdir(a) |
| 54 |
os.mkdir(b) |
| 55 |
os.mkdir(c) |
| 56 |
touch(os.path.join(a, "toto.plug")) |
| 57 |
touch(os.path.join(b, "titi.plug")) |
| 58 |
touch(os.path.join(root, "tutu.plug")) |
| 59 |
roots = find_roots(root) |
| 60 |
assert root in roots |
| 61 |
assert a in roots |
| 62 |
assert b in roots |
| 63 |
assert c not in roots |
| 64 |
|
| 65 |
|
| 66 |
def test_collect_roots(): |
| 67 |
toto = tempfile.mkdtemp() |
| 68 |
touch(os.path.join(toto, "toto.plug")) |
| 69 |
touch(os.path.join(toto, "titi.plug")) |
| 70 |
titi = tempfile.mkdtemp() |
| 71 |
touch(os.path.join(titi, "tata.plug")) |
| 72 |
tutu = tempfile.mkdtemp() |
| 73 |
subtutu = os.path.join(tutu, "subtutu") |
| 74 |
os.mkdir(subtutu) |
| 75 |
touch(os.path.join(subtutu, "tutu.plug")) |
| 76 |
|
| 77 |
assert collect_roots((CORE_PLUGINS, None)) == [CORE_PLUGINS] |
| 78 |
assert collect_roots((CORE_PLUGINS, toto)) == [CORE_PLUGINS, toto] |
| 79 |
assert collect_roots((CORE_PLUGINS, [toto, titi])) == [CORE_PLUGINS, toto, titi] |
| 80 |
assert collect_roots((CORE_PLUGINS, toto, titi, "nothing")) == [ |
| 81 |
CORE_PLUGINS, |
| 82 |
toto, |
| 83 |
titi, |
| 84 |
] |
| 85 |
assert collect_roots((toto, tutu)) == [toto, subtutu] |
| 86 |
|
| 87 |
|
| 88 |
def test_ignore_dotted_directories(): |
| 89 |
root = tempfile.mkdtemp() |
| 90 |
a = os.path.join(root, ".invisible") |
| 91 |
os.mkdir(a) |
| 92 |
touch(os.path.join(a, "toto.plug")) |
| 93 |
assert collect_roots((CORE_PLUGINS, root)) == [CORE_PLUGINS] |
| 94 |
|
| 95 |
|
| 96 |
def dummy_config_parser() -> ConfigParser: |
| 97 |
cp = ConfigParser() |
| 98 |
cp.add_section("Core") |
| 99 |
cp.set("Core", "Name", "dummy") |
| 100 |
cp.set("Core", "Module", "dummy") |
| 101 |
cp.add_section("Errbot") |
| 102 |
return cp |
| 103 |
|
| 104 |
|
| 105 |
def test_errbot_version_check(): |
| 106 |
real_version = plugin_manager.VERSION |
| 107 |
|
| 108 |
too_high_min_1 = dummy_config_parser() |
| 109 |
too_high_min_1.set("Errbot", "Min", "1.6.0") |
| 110 |
|
| 111 |
too_high_min_2 = dummy_config_parser() |
| 112 |
too_high_min_2.set("Errbot", "Min", "1.6.0") |
| 113 |
too_high_min_2.set("Errbot", "Max", "2.0.0") |
| 114 |
|
| 115 |
too_low_max_1 = dummy_config_parser() |
| 116 |
too_low_max_1.set("Errbot", "Max", "1.0.1-beta") |
| 117 |
|
| 118 |
too_low_max_2 = dummy_config_parser() |
| 119 |
too_low_max_2.set("Errbot", "Min", "0.9.0-rc2") |
| 120 |
too_low_max_2.set("Errbot", "Max", "1.0.1-beta") |
| 121 |
|
| 122 |
ok1 = dummy_config_parser() # empty section |
| 123 |
|
| 124 |
ok2 = dummy_config_parser() |
| 125 |
ok2.set("Errbot", "Min", "1.4.0") |
| 126 |
|
| 127 |
ok3 = dummy_config_parser() |
| 128 |
ok3.set("Errbot", "Max", "1.5.2") |
| 129 |
|
| 130 |
ok4 = dummy_config_parser() |
| 131 |
ok4.set("Errbot", "Min", "1.2.1") |
| 132 |
ok4.set("Errbot", "Max", "1.6.1-rc1") |
| 133 |
|
| 134 |
try: |
| 135 |
plugin_manager.VERSION = "1.5.2" |
| 136 |
for config in (too_high_min_1, too_high_min_2, too_low_max_1, too_low_max_2): |
| 137 |
pi = PluginInfo.parse(config) |
| 138 |
with pytest.raises(IncompatiblePluginException): |
| 139 |
plugin_manager.check_errbot_version(pi) |
| 140 |
|
| 141 |
for config in (ok1, ok2, ok3, ok4): |
| 142 |
pi = PluginInfo.parse(config) |
| 143 |
plugin_manager.check_errbot_version(pi) |
| 144 |
finally: |
| 145 |
plugin_manager.VERSION = real_version |
| 146 |
|
| 147 |
|
| 148 |
def test_entry_point_plugin(): |
| 149 |
no_plugins_found = entry_point_plugins("errbot.no_plugins") |
| 150 |
assert [] == no_plugins_found |