1 7
from __future__ import absolute_import, division, unicode_literals
2

3 7
import os
4 7
import shutil
5

6 7
from collections import OrderedDict
7

8 7
import pytest
9

10 7
from panel.widgets import FileSelector
11

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

25

26 7
def test_file_selector_init(test_dir):
27 7
    selector = FileSelector(test_dir)
28

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

34

35 7
def test_file_selector_address_bar(test_dir):
36 7
    selector = FileSelector(test_dir)
37

38 7
    selector._directory.value = os.path.join(test_dir, 'subdir1')
39

40 7
    assert not selector._go.disabled
41

42 7
    selector._go.clicks = 1
43

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

53 7
    selector._up.clicks = 1
54

55 7
    selector._selector._lists[False].value = ['subdir1']
56

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

59 7
    selector._selector._lists[False].value = []
60

61 7
    assert selector._directory.value == test_dir
62

63

64 7
def test_file_selector_back_and_forward(test_dir):
65 7
    selector = FileSelector(test_dir)
66

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

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

74 7
    selector._back.clicks = 1
75

76 7
    assert selector._cwd == test_dir
77 7
    assert selector._back.disabled
78 7
    assert not selector._forward.disabled
79

80 7
    selector._forward.clicks = 1
81

82 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
83

84

85 7
def test_file_selector_up(test_dir):
86 7
    selector = FileSelector(test_dir)
87

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

91 7
    assert selector._cwd == os.path.join(test_dir, 'subdir1')
92

93 7
    selector._up.clicks = 1
94

95 7
    assert selector._cwd == test_dir
96

97

98 7
def test_file_selector_select_files(test_dir):
99 7
    selector = FileSelector(test_dir)
100

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

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

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

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

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

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

120 7
    assert selector.value == []
121

122

123 7
def test_file_selector_only_files(test_dir):
124 7
    selector = FileSelector(test_dir, only_files=True)
125

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

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

132

133 7
def test_file_selector_file_pattern(test_dir):
134 7
    selector = FileSelector(test_dir, file_pattern='a')
135

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

139 7
    assert selector._selector._lists[False].options == ['a']
140

141

142 7
def test_file_selector_multiple_across_dirs(test_dir):
143 7
    selector = FileSelector(test_dir)
144

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

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

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

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

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

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

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

Read our documentation on viewing source code .

Loading