|
1
|
|
|
# -*- coding: utf-8 -*- |
|
2
|
|
|
# vi:si:et:sw=4:sts=4:ts=4 |
|
3
|
|
|
|
|
4
|
|
|
import inspect |
|
5
|
|
|
import os |
|
6
|
|
|
import sys |
|
7
|
|
|
|
|
8
|
|
|
import pytest |
|
9
|
|
|
|
|
10
|
|
|
from loafer.utils import import_callable, add_current_dir_to_syspath |
|
11
|
|
|
|
|
12
|
|
|
|
|
13
|
|
|
def test_import_function(): |
|
14
|
|
|
func = import_callable('loafer.utils.import_callable') |
|
15
|
|
|
assert callable(func) |
|
16
|
|
|
assert inspect.isfunction(func) |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
def test_import_class(): |
|
20
|
|
|
klass = import_callable('loafer.exceptions.ConsumerError') |
|
21
|
|
|
assert klass.__name__ == 'ConsumerError' |
|
22
|
|
|
assert inspect.isclass(klass) |
|
23
|
|
|
|
|
24
|
|
|
|
|
25
|
|
|
def test_error_on_method_name(): |
|
26
|
|
|
with pytest.raises(ImportError): |
|
27
|
|
|
import_callable('unittest.mock.Mock.call_count') |
|
28
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
def test_error_on_invalid_name(): |
|
31
|
|
|
with pytest.raises(ImportError): |
|
32
|
|
|
import_callable('invalid-1234') |
|
33
|
|
|
|
|
34
|
|
|
with pytest.raises(ImportError): |
|
35
|
|
|
import_callable('') |
|
36
|
|
|
|
|
37
|
|
|
|
|
38
|
|
|
def test_error_on_module(): |
|
39
|
|
|
with pytest.raises(ImportError): |
|
40
|
|
|
import_callable('loafer.example') |
|
41
|
|
|
|
|
42
|
|
|
|
|
43
|
|
|
def test_error_on_non_callable(): |
|
44
|
|
|
with pytest.raises(ImportError): |
|
45
|
|
|
import_callable('loafer') |
|
46
|
|
|
|
|
47
|
|
|
|
|
48
|
|
|
@pytest.mark.xfail(os.getcwd() == '/tmp', run=False, |
|
49
|
|
|
reason='This test is invalid if you are at /tmp') |
|
50
|
|
|
def test_current_dir_in_syspath(): |
|
51
|
|
|
old_current = os.getcwd() |
|
52
|
|
|
os.chdir('/tmp') |
|
53
|
|
|
current = os.getcwd() |
|
54
|
|
|
if current not in sys.path: |
|
55
|
|
|
sys.path.append(current) |
|
56
|
|
|
|
|
57
|
|
|
@add_current_dir_to_syspath |
|
58
|
|
|
def inner_test(): |
|
59
|
|
|
assert current in sys.path |
|
60
|
|
|
|
|
61
|
|
|
inner_test() |
|
62
|
|
|
assert current in sys.path |
|
63
|
|
|
|
|
64
|
|
|
sys.path.remove(current) |
|
65
|
|
|
os.chdir(old_current) |
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
@pytest.mark.xfail(os.getcwd() == '/tmp', run=False, |
|
69
|
|
|
reason='This test is invalid if you are at /tmp') |
|
70
|
|
|
def test_current_dir_not_in_syspath(): |
|
71
|
|
|
old_current = os.getcwd() |
|
72
|
|
|
os.chdir('/tmp') |
|
73
|
|
|
current = os.getcwd() |
|
74
|
|
|
|
|
75
|
|
|
@add_current_dir_to_syspath |
|
76
|
|
|
def inner_test(): |
|
77
|
|
|
assert current in sys.path |
|
78
|
|
|
|
|
79
|
|
|
inner_test() |
|
80
|
|
|
assert current not in sys.path |
|
81
|
|
|
|
|
82
|
|
|
os.chdir(old_current) |
|
83
|
|
|
|