holoviz / panel
1 7
import os
2 7
import shutil
3

4 7
from collections import OrderedDict
5

6 7
import pytest
7

8 7
from panel.widgets import FileSelector
9

10 7
@pytest.fixture
11 2
def test_dir():
12 7
    test_dir = os.path.expanduser(os.path.join('~','test_dir'))
13 7
    os.mkdir(test_dir)
14 7
    os.mkdir(os.path.expanduser(os.path.join('~','test_dir','subdir1')))
15 7
    with open(os.path.expanduser(os.path.join('~','test_dir','subdir1','a')), 'a'):
16 7
        pass
17 7
    with open(os.path.expanduser(os.path.join('~','test_dir','subdir1','b')), 'a'):
18 7
        pass
19 7
    os.mkdir(os.path.expanduser(os.path.join('~','test_dir','subdir2')))
20 7
    yield test_dir
21 7
    shutil.rmtree(os.path.expanduser(os.path.join('~','test_dir')))
22

23

24 7
def test_file_selector_init(test_dir):
25 7
    selector = FileSelector(test_dir)
26

27 7
    assert selector._selector.options == {
28
        '\U0001f4c1subdir1': os.path.join(test_dir, 'subdir1'),
29
        '\U0001f4c1subdir2': os.path.join(test_dir, 'subdir2')
30
    }
31

32

33 7
def test_file_selector_address_bar(test_dir):
34 7
    selector = FileSelector(test_dir)
35

36 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
37

38 7
    assert not selector._go.disabled
39

40 7
    selector._go.clicks = 1
41

42 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
43 7
    assert selector._go.disabled
44 7
    assert selector._forward.disabled
45 7
    assert not selector._back.disabled
46 7
    assert selector._selector.options == {
47
        'a': os.path.join(test_dir, 'subdir1', 'a'),
48
        'b': os.path.join(test_dir, 'subdir1', 'b')
49
    }
50

51 7
    selector._up.clicks = 1
52

53 7
    selector._selector._lists[False].value = ['subdir1']
54

55 7
    assert selector._directory.value == os.path.join(test_dir, 'subdir1')
56

57 7
    selector._selector._lists[False].value = []
58

59 7
    assert selector._directory.value == test_dir
60

61

62 7
def test_file_selector_back_and_forward(test_dir):
63 7
    selector = FileSelector(test_dir)
64

65 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
66 7
    selector._go.clicks = 1
67

68 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
69 7
    assert not selector._back.disabled
70 7
    assert selector._forward.disabled
71

72 7
    selector._back.clicks = 1
73

74 7
    assert selector._cwd == test_dir
75 7
    assert selector._back.disabled
76 7
    assert not selector._forward.disabled
77

78 7
    selector._forward.clicks = 1
79

80 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
81

82

83 7
def test_file_selector_up(test_dir):
84 7
    selector = FileSelector(test_dir)
85

86 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
87 7
    selector._go.clicks = 1
88

89 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
90

91 7
    selector._up.clicks = 1
92

93 7
    assert selector._cwd == test_dir
94

95

96 7
def test_file_selector_select_files(test_dir):
97 7
    selector = FileSelector(test_dir)
98

99 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
100 7
    selector._go.clicks = 1
101

102 7
    selector._selector._lists[False].value = ['a']
103 7
    selector._selector._buttons[True].clicks = 1
104

105 7
    assert selector.value == [os.path.join(test_dir, 'subdir1', 'a')]
106

107 7
    selector._selector._lists[False].value = ['b']
108 7
    selector._selector._buttons[True].clicks = 2
109

110 7
    assert selector.value == [
111
        os.path.join(test_dir, 'subdir1', 'a'),
112
        os.path.join(test_dir, 'subdir1', 'b')
113
    ]
114

115 7
    selector._selector._lists[True].value = ['a', 'b']
116 7
    selector._selector._buttons[False].clicks = 2
117

118 7
    assert selector.value == []
119

120

121 7
def test_file_selector_only_files(test_dir):
122 7
    selector = FileSelector(test_dir, only_files=True)
123

124 7
    selector._selector._lists[False].value = ['\U0001f4c1subdir1']
125 7
    selector._selector._buttons[True].clicks = 1
126

127 7
    assert selector.value == []
128 7
    assert selector._selector._lists[False].options == ['\U0001f4c1subdir1', '\U0001f4c1subdir2']
129

130

131 7
def test_file_selector_file_pattern(test_dir):
132 7
    selector = FileSelector(test_dir, file_pattern='a')
133

134 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
135 7
    selector._go.clicks = 1
136

137 7
    assert selector._selector._lists[False].options == ['a']
138

139

140 7
def test_file_selector_multiple_across_dirs(test_dir):
141 7
    selector = FileSelector(test_dir)
142

143 7
    selector._selector._lists[False].value = ['\U0001f4c1subdir2']
144 7
    selector._selector._buttons[True].clicks = 1
145

146 7
    assert selector.value == [os.path.join(test_dir, 'subdir2')]
147

148 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
149 7
    selector._go.clicks = 1
150

151 7
    selector._selector._lists[False].value = ['a']
152 7
    selector._selector._buttons[True].clicks = 2
153

154 7
    assert selector.value == [os.path.join(test_dir, 'subdir2'),
155
                              os.path.join(test_dir, 'subdir1', 'a')]
156

157 7
    selector._selector._lists[True].value = ['\U0001f4c1'+os.path.join('..', 'subdir2')]
158 7
    selector._selector._buttons[False].clicks = 1
159

160 7
    assert selector._selector.options == OrderedDict([
161
        ('a', os.path.join(test_dir, 'subdir1', 'a')),
162
        ('b', os.path.join(test_dir, 'subdir1', 'b'))
163
    ])
164 7
    assert selector._selector._lists[False].options == ['b']
165 7
    assert selector.value == [os.path.join(test_dir, 'subdir1', 'a')]

Read our documentation on viewing source code .

Loading