1- from unittest .mock import Mock , patch , sentinel
1+ from unittest .mock import patch , sentinel
22
3- from pytest import mark , raises
3+ from pytest import raises
44
55from getdents import DT_DIR , DT_REG , DT_UNKNOWN , O_GETDENTS , getdents
66
77
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-
208@patch ('getdents.os' )
219@patch ('getdents.getdents_raw' )
2210def test_path (mock_getdents_raw , mock_os ):
23- mock_path = Mock (spec = '/tmp' )
2411 mock_os .open .return_value = sentinel .fd
2512
26- list (getdents (mock_path , sentinel .size ))
13+ list (getdents ('/tmp' , sentinel .size ))
2714
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 )
3018
3119
32- @mark .parametrize (['close_fd' ], [(True ,), (False ,)])
3320@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
4324
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 ))
4827
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 )
5031
5132
33+ @patch ('getdents.os' )
5234@patch ('getdents.getdents_raw' , return_value = iter ([
5335 (1 , DT_DIR , '.' ),
5436 (2 , DT_DIR , '..' ),
@@ -57,7 +39,7 @@ def test_alien(mock_getdents_raw):
5739 (5 , DT_UNKNOWN , '???' ),
5840 (0 , DT_REG , 'deleted' ),
5941]))
60- def test_filtering (mock_getdents_raw ):
42+ def test_filtering (mock_getdents_raw , mock_os ):
6143 assert list (getdents (0 )) == [
6244 (3 , DT_DIR , 'dir' ),
6345 (4 , DT_REG , 'file' ),
0 commit comments