|
| 1 | +import unittest |
| 2 | +from unittest.mock import patch |
| 3 | + |
| 4 | +from update_checkout.update_checkout import _is_any_repository_locked |
| 5 | + |
| 6 | +class TestIsAnyRepositoryLocked(unittest.TestCase): |
| 7 | + @patch("os.path.exists") |
| 8 | + @patch("os.path.isdir") |
| 9 | + @patch("os.listdir") |
| 10 | + def test_repository_with_lock_file(self, mock_listdir, mock_isdir, mock_exists): |
| 11 | + pool_args = [ |
| 12 | + ("/fake_path", None, "repo1"), |
| 13 | + ("/fake_path", None, "repo2"), |
| 14 | + ] |
| 15 | + |
| 16 | + def listdir_side_effect(path): |
| 17 | + if "repo1" in path: |
| 18 | + return ["index.lock", "config"] |
| 19 | + elif "repo2" in path: |
| 20 | + return ["HEAD", "config"] |
| 21 | + return [] |
| 22 | + |
| 23 | + mock_exists.return_value = True |
| 24 | + mock_isdir.return_value = True |
| 25 | + mock_listdir.side_effect = listdir_side_effect |
| 26 | + |
| 27 | + result = _is_any_repository_locked(pool_args) |
| 28 | + self.assertEqual(result, {"repo1"}) |
| 29 | + |
| 30 | + @patch("os.path.exists") |
| 31 | + @patch("os.path.isdir") |
| 32 | + @patch("os.listdir") |
| 33 | + def test_repository_without_git_dir(self, mock_listdir, mock_isdir, mock_exists): |
| 34 | + pool_args = [ |
| 35 | + ("/fake_path", None, "repo1"), |
| 36 | + ] |
| 37 | + |
| 38 | + mock_exists.return_value = False |
| 39 | + mock_isdir.return_value = False |
| 40 | + mock_listdir.return_value = [] |
| 41 | + |
| 42 | + result = _is_any_repository_locked(pool_args) |
| 43 | + self.assertEqual(result, set()) |
| 44 | + |
| 45 | + @patch("os.path.exists") |
| 46 | + @patch("os.path.isdir") |
| 47 | + @patch("os.listdir") |
| 48 | + def test_repository_with_git_file(self, mock_listdir, mock_isdir, mock_exists): |
| 49 | + pool_args = [ |
| 50 | + ("/fake_path", None, "repo1"), |
| 51 | + ] |
| 52 | + |
| 53 | + mock_exists.return_value = True |
| 54 | + mock_isdir.return_value = False |
| 55 | + mock_listdir.return_value = [] |
| 56 | + |
| 57 | + result = _is_any_repository_locked(pool_args) |
| 58 | + self.assertEqual(result, set()) |
| 59 | + |
| 60 | + @patch("os.path.exists") |
| 61 | + @patch("os.path.isdir") |
| 62 | + @patch("os.listdir") |
| 63 | + def test_repository_with_multiple_lock_files(self, mock_listdir, mock_isdir, mock_exists): |
| 64 | + pool_args = [ |
| 65 | + ("/fake_path", None, "repo1"), |
| 66 | + ] |
| 67 | + |
| 68 | + mock_exists.return_value = True |
| 69 | + mock_isdir.return_value = True |
| 70 | + mock_listdir.return_value = ["index.lock", "merge.lock", "HEAD"] |
| 71 | + |
| 72 | + result = _is_any_repository_locked(pool_args) |
| 73 | + self.assertEqual(result, {"repo1"}) |
| 74 | + |
| 75 | + @patch("os.path.exists") |
| 76 | + @patch("os.path.isdir") |
| 77 | + @patch("os.listdir") |
| 78 | + def test_repository_with_no_lock_files(self, mock_listdir, mock_isdir, mock_exists): |
| 79 | + pool_args = [ |
| 80 | + ("/fake_path", None, "repo1"), |
| 81 | + ] |
| 82 | + |
| 83 | + mock_exists.return_value = True |
| 84 | + mock_isdir.return_value = True |
| 85 | + mock_listdir.return_value = ["HEAD", "config", "logs"] |
| 86 | + |
| 87 | + result = _is_any_repository_locked(pool_args) |
| 88 | + self.assertEqual(result, set()) |
| 89 | + |
0 commit comments