| 1 |
import os |
| 2 |
import platform |
| 3 |
import sys |
| 4 |
from unittest.mock import Mock, patch |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from celery.utils.imports import (NotAPackage, cwd_in_path, find_module, gen_task_name, module_file, qualname, |
| 9 |
reload_from_cwd) |
| 10 |
|
| 11 |
|
| 12 |
def test_find_module(): |
| 13 |
def imp_side_effect(module): |
| 14 |
if module == 'foo': |
| 15 |
return None |
| 16 |
else: |
| 17 |
raise ImportError(module) |
| 18 |
|
| 19 |
assert find_module('celery') |
| 20 |
imp = Mock() |
| 21 |
imp.side_effect = imp_side_effect |
| 22 |
with pytest.raises(NotAPackage) as exc_info: |
| 23 |
find_module('foo.bar.baz', imp=imp) |
| 24 |
assert exc_info.value.args[0] == 'foo' |
| 25 |
assert find_module('celery.worker.request') |
| 26 |
|
| 27 |
|
| 28 |
def test_find_module_legacy_namespace_package(tmp_path, monkeypatch): |
| 29 |
monkeypatch.chdir(str(tmp_path)) |
| 30 |
(tmp_path / 'pkg' / 'foo').mkdir(parents=True) |
| 31 |
(tmp_path / 'pkg' / '__init__.py').write_text( |
| 32 |
'from pkgutil import extend_path\n' |
| 33 |
'__path__ = extend_path(__path__, __name__)\n') |
| 34 |
(tmp_path / 'pkg' / 'foo' / '__init__.py').write_text('') |
| 35 |
(tmp_path / 'pkg' / 'foo' / 'bar.py').write_text('') |
| 36 |
with patch.dict(sys.modules): |
| 37 |
for modname in list(sys.modules): |
| 38 |
if modname == 'pkg' or modname.startswith('pkg.'): |
| 39 |
del sys.modules[modname] |
| 40 |
with pytest.raises(ImportError): |
| 41 |
find_module('pkg.missing') |
| 42 |
with pytest.raises(ImportError): |
| 43 |
find_module('pkg.foo.missing') |
| 44 |
assert find_module('pkg.foo.bar') |
| 45 |
with pytest.raises(NotAPackage) as exc_info: |
| 46 |
find_module('pkg.foo.bar.missing') |
| 47 |
assert exc_info.value.args[0] == 'pkg.foo.bar' |
| 48 |
|
| 49 |
|
| 50 |
def test_find_module_pep420_namespace_package(tmp_path, monkeypatch): |
| 51 |
monkeypatch.chdir(str(tmp_path)) |
| 52 |
(tmp_path / 'pkg' / 'foo').mkdir(parents=True) |
| 53 |
(tmp_path / 'pkg' / 'foo' / '__init__.py').write_text('') |
| 54 |
(tmp_path / 'pkg' / 'foo' / 'bar.py').write_text('') |
| 55 |
with patch.dict(sys.modules): |
| 56 |
for modname in list(sys.modules): |
| 57 |
if modname == 'pkg' or modname.startswith('pkg.'): |
| 58 |
del sys.modules[modname] |
| 59 |
with pytest.raises(ImportError): |
| 60 |
find_module('pkg.missing') |
| 61 |
with pytest.raises(ImportError): |
| 62 |
find_module('pkg.foo.missing') |
| 63 |
assert find_module('pkg.foo.bar') |
| 64 |
with pytest.raises(NotAPackage) as exc_info: |
| 65 |
find_module('pkg.foo.bar.missing') |
| 66 |
assert exc_info.value.args[0] == 'pkg.foo.bar' |
| 67 |
|
| 68 |
|
| 69 |
def test_qualname(): |
| 70 |
Class = type('Fox', (object,), { |
| 71 |
'__module__': 'quick.brown', |
| 72 |
}) |
| 73 |
assert qualname(Class) == 'quick.brown.Fox' |
| 74 |
assert qualname(Class()) == 'quick.brown.Fox' |
| 75 |
|
| 76 |
|
| 77 |
def test_reload_from_cwd(patching): |
| 78 |
reload = patching('celery.utils.imports.reload') |
| 79 |
reload_from_cwd('foo') |
| 80 |
reload.assert_called() |
| 81 |
|
| 82 |
|
| 83 |
def test_reload_from_cwd_custom_reloader(): |
| 84 |
reload = Mock() |
| 85 |
reload_from_cwd('foo', reload) |
| 86 |
reload.assert_called() |
| 87 |
|
| 88 |
|
| 89 |
def test_module_file(): |
| 90 |
m1 = Mock() |
| 91 |
m1.__file__ = '/opt/foo/xyz.pyc' |
| 92 |
assert module_file(m1) == '/opt/foo/xyz.py' |
| 93 |
m2 = Mock() |
| 94 |
m2.__file__ = '/opt/foo/xyz.py' |
| 95 |
assert module_file(m1) == '/opt/foo/xyz.py' |
| 96 |
|
| 97 |
|
| 98 |
def test_cwd_in_path(tmp_path, monkeypatch): |
| 99 |
now_cwd = os.getcwd() |
| 100 |
t = str(tmp_path) + "/foo" |
| 101 |
os.mkdir(t) |
| 102 |
os.chdir(t) |
| 103 |
with cwd_in_path(): |
| 104 |
assert os.path.exists(t) is True |
| 105 |
|
| 106 |
if sys.platform == "win32" or "Windows" in platform.platform(): |
| 107 |
# If it is a Windows server, other processes cannot delete the current working directory being used by celery |
| 108 |
# . If you want to delete it, you need to terminate the celery process. If it is a Linux server, the current |
| 109 |
# working directory of celery can be deleted by other processes. |
| 110 |
pass |
| 111 |
else: |
| 112 |
os.rmdir(t) |
| 113 |
with cwd_in_path(): |
| 114 |
assert os.path.exists(t) is False |
| 115 |
os.chdir(now_cwd) |
| 116 |
|
| 117 |
|
| 118 |
class test_gen_task_name: |
| 119 |
|
| 120 |
def test_no_module(self): |
| 121 |
app = Mock() |
| 122 |
app.name == '__main__' |
| 123 |
assert gen_task_name(app, 'foo', 'axsadaewe') |