1
- from unittest .mock import Mock , patch , sentinel
1
+ from unittest .mock import patch , sentinel
2
2
3
- from pytest import mark , raises
3
+ from pytest import raises
4
4
5
5
from getdents import DT_DIR , DT_REG , DT_UNKNOWN , O_GETDENTS , getdents
6
6
7
7
8
- @patch ('getdents.os' )
9
- @patch ('getdents.getdents_raw' )
10
- def test_file_like (mock_getdents_raw , mock_os ):
11
- mock_file = Mock (spec_set = ['fileno' ])
12
- mock_file .fileno .return_value = sentinel .fd
13
-
14
- list (getdents (mock_file , sentinel .size ))
15
-
16
- assert mock_file .fileno .called
17
- assert mock_getdents_raw .called_once_with (sentinel .fd , sentinel .size )
18
-
19
-
20
8
@patch ('getdents.os' )
21
9
@patch ('getdents.getdents_raw' )
22
10
def test_path (mock_getdents_raw , mock_os ):
23
- mock_path = Mock (spec = '/tmp' )
24
11
mock_os .open .return_value = sentinel .fd
25
12
26
- list (getdents (mock_path , sentinel .size ))
13
+ list (getdents ('/tmp' , sentinel .size ))
27
14
28
- assert mock_os .open .called_once_with (mock_path , O_GETDENTS )
29
- assert mock_getdents_raw .called_once_with (sentinel .fd , sentinel .size )
15
+ mock_os .open .assert_called_once_with ('/tmp' , O_GETDENTS )
16
+ mock_getdents_raw .assert_called_once_with (sentinel .fd , sentinel .size )
17
+ mock_os .close .assert_called_once_with (sentinel .fd )
30
18
31
19
32
- @mark .parametrize (['close_fd' ], [(True ,), (False ,)])
33
20
@patch ('getdents.os' )
34
- @patch ('getdents.getdents_raw' )
35
- def test_fd (mock_getdents_raw , mock_os , close_fd ):
36
- mock_fd = Mock (spec = 4 )
37
-
38
- list (getdents (mock_fd , sentinel .size , close_fd ))
39
-
40
- assert mock_getdents_raw .called_once_with (mock_fd , sentinel .size )
41
- assert mock_os .close .called is close_fd
42
-
21
+ @patch ('getdents.getdents_raw' , side_effect = [Exception ])
22
+ def test_path_err (mock_getdents_raw , mock_os ):
23
+ mock_os .open .return_value = sentinel .fd
43
24
44
- @patch ('getdents.getdents_raw' )
45
- def test_alien (mock_getdents_raw ):
46
- with raises (TypeError ):
47
- list (getdents (object ()))
25
+ with raises (Exception ):
26
+ list (getdents ('/tmp' , sentinel .size ))
48
27
49
- assert not mock_getdents_raw .called
28
+ mock_os .open .assert_called_once_with ('/tmp' , O_GETDENTS )
29
+ mock_getdents_raw .assert_called_once_with (sentinel .fd , sentinel .size )
30
+ mock_os .close .assert_called_once_with (sentinel .fd )
50
31
51
32
33
+ @patch ('getdents.os' )
52
34
@patch ('getdents.getdents_raw' , return_value = iter ([
53
35
(1 , DT_DIR , '.' ),
54
36
(2 , DT_DIR , '..' ),
@@ -57,7 +39,7 @@ def test_alien(mock_getdents_raw):
57
39
(5 , DT_UNKNOWN , '???' ),
58
40
(0 , DT_REG , 'deleted' ),
59
41
]))
60
- def test_filtering (mock_getdents_raw ):
42
+ def test_filtering (mock_getdents_raw , mock_os ):
61
43
assert list (getdents (0 )) == [
62
44
(3 , DT_DIR , 'dir' ),
63
45
(4 , DT_REG , 'file' ),
0 commit comments