1+ from pathlib import Path
12import unittest
23from unittest .mock import patch
34
45from update_checkout .update_checkout import UpdateArguments , _is_any_repository_locked
56
7+ FAKE_PATH = Path ("/fake_path" )
68
7- def _update_arguments_with_fake_path (repo_name : str , path : str ) -> UpdateArguments :
9+
10+ def _update_arguments_with_fake_path (repo_name : str , path : Path ) -> UpdateArguments :
811 return UpdateArguments (
912 repo_name = repo_name ,
1013 source_root = path ,
@@ -23,87 +26,93 @@ def _update_arguments_with_fake_path(repo_name: str, path: str) -> UpdateArgumen
2326
2427
2528class TestIsAnyRepositoryLocked (unittest .TestCase ):
26- @patch ("os.path .exists" )
27- @patch ("os.path.isdir" )
28- @patch ("os.listdir" )
29- def test_repository_with_lock_file (self , mock_listdir , mock_isdir , mock_exists ):
29+ @patch ("pathlib.Path .exists" , autospec = True )
30+ @patch ("pathlib.Path.is_dir" , autospec = True )
31+ @patch ("pathlib.Path.iterdir" , autospec = True )
32+ def test_repository_with_lock_file (self , mock_iterdir , mock_is_dir , mock_exists ):
3033 pool_args = [
31- _update_arguments_with_fake_path ("repo1" , "/fake_path" ),
32- _update_arguments_with_fake_path ("repo2" , "/fake_path" ),
34+ _update_arguments_with_fake_path ("repo1" , FAKE_PATH ),
35+ _update_arguments_with_fake_path ("repo2" , FAKE_PATH ),
3336 ]
3437
35- def listdir_side_effect (path ):
36- if "repo1" in path :
37- return ["index.lock" , "config" ]
38- elif "repo2" in path :
39- return ["HEAD" , "config" ]
38+ def iterdir_side_effect (path : Path ):
39+ if "repo1" in path . as_posix () :
40+ return [path . joinpath ( "index.lock" ), path . joinpath ( "config" ) ]
41+ elif "repo2" in path . as_posix () :
42+ return [path . joinpath ( "HEAD" ), path . joinpath ( "config" ) ]
4043 return []
4144
4245 mock_exists .return_value = True
43- mock_isdir .return_value = True
44- mock_listdir .side_effect = listdir_side_effect
46+ mock_is_dir .return_value = True
47+ mock_iterdir .side_effect = iterdir_side_effect
4548
4649 result = _is_any_repository_locked (pool_args )
4750 self .assertEqual (result , {"repo1" })
4851
49- @patch ("os.path .exists" )
50- @patch ("os.path.isdir " )
51- @patch ("os.listdir " )
52- def test_repository_without_git_dir (self , mock_listdir , mock_isdir , mock_exists ):
52+ @patch ("pathlib.Path .exists" )
53+ @patch ("pathlib.Path.is_dir " )
54+ @patch ("pathlib.Path.iterdir " )
55+ def test_repository_without_git_dir (self , mock_iterdir , mock_is_dir , mock_exists ):
5356 pool_args = [
54- _update_arguments_with_fake_path ("repo1" , "/fake_path" ),
57+ _update_arguments_with_fake_path ("repo1" , FAKE_PATH ),
5558 ]
5659
5760 mock_exists .return_value = False
58- mock_isdir .return_value = False
59- mock_listdir .return_value = []
61+ mock_is_dir .return_value = False
62+ mock_iterdir .return_value = []
6063
6164 result = _is_any_repository_locked (pool_args )
6265 self .assertEqual (result , set ())
6366
64- @patch ("os.path .exists" )
65- @patch ("os.path.isdir " )
66- @patch ("os.listdir " )
67- def test_repository_with_git_file (self , mock_listdir , mock_isdir , mock_exists ):
67+ @patch ("pathlib.Path .exists" )
68+ @patch ("pathlib.Path.is_dir " )
69+ @patch ("pathlib.Path.iterdir " )
70+ def test_repository_with_git_file (self , mock_iterdir , mock_is_dir , mock_exists ):
6871 pool_args = [
69- _update_arguments_with_fake_path ("repo1" , "/fake_path" ),
72+ _update_arguments_with_fake_path ("repo1" , FAKE_PATH ),
7073 ]
7174
7275 mock_exists .return_value = True
73- mock_isdir .return_value = False
74- mock_listdir .return_value = []
76+ mock_is_dir .return_value = False
77+ mock_iterdir .return_value = []
7578
7679 result = _is_any_repository_locked (pool_args )
7780 self .assertEqual (result , set ())
7881
79- @patch ("os.path .exists" )
80- @patch ("os.path.isdir " )
81- @patch ("os.listdir " )
82+ @patch ("pathlib.Path .exists" )
83+ @patch ("pathlib.Path.is_dir " )
84+ @patch ("pathlib.Path.iterdir " )
8285 def test_repository_with_multiple_lock_files (
83- self , mock_listdir , mock_isdir , mock_exists
86+ self , mock_iterdir , mock_is_dir , mock_exists
8487 ):
8588 pool_args = [
86- _update_arguments_with_fake_path ("repo1" , "/fake_path" ),
89+ _update_arguments_with_fake_path ("repo1" , FAKE_PATH ),
8790 ]
8891
8992 mock_exists .return_value = True
90- mock_isdir .return_value = True
91- mock_listdir .return_value = ["index.lock" , "merge.lock" , "HEAD" ]
93+ mock_is_dir .return_value = True
94+ mock_iterdir .return_value = [
95+ FAKE_PATH .joinpath (x ) for x in ("index.lock" , "merge.lock" , "HEAD" )
96+ ]
9297
9398 result = _is_any_repository_locked (pool_args )
9499 self .assertEqual (result , {"repo1" })
95100
96- @patch ("os.path.exists" )
97- @patch ("os.path.isdir" )
98- @patch ("os.listdir" )
99- def test_repository_with_no_lock_files (self , mock_listdir , mock_isdir , mock_exists ):
101+ @patch ("pathlib.Path.exists" )
102+ @patch ("pathlib.Path.is_dir" )
103+ @patch ("pathlib.Path.iterdir" )
104+ def test_repository_with_no_lock_files (
105+ self , mock_iterdir , mock_is_dir , mock_exists
106+ ):
100107 pool_args = [
101- _update_arguments_with_fake_path ("repo1" , "/fake_path" ),
108+ _update_arguments_with_fake_path ("repo1" , FAKE_PATH ),
102109 ]
103110
104111 mock_exists .return_value = True
105- mock_isdir .return_value = True
106- mock_listdir .return_value = ["HEAD" , "config" , "logs" ]
112+ mock_is_dir .return_value = True
113+ mock_iterdir .return_value = [
114+ FAKE_PATH .joinpath (x ) for x in ("HEAD" , "config" , "logs" )
115+ ]
107116
108117 result = _is_any_repository_locked (pool_args )
109118 self .assertEqual (result , set ())
0 commit comments