diff options
| author | Gavin Mak <gavinmak@google.com> | 2023-07-22 02:56:44 +0000 |
|---|---|---|
| committer | LUCI <gerrit-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2023-07-28 18:55:04 +0000 |
| commit | 1d2e99d0289a36e8c2a53ff3bf5690f0f780ba63 (patch) | |
| tree | 2df20c20871fa0dceb8d545e76b75ff76b9e755d /tests | |
| parent | c657844efe40b97700c3654989bdbe3a33e409d7 (diff) | |
| download | git-repo-1d2e99d0289a36e8c2a53ff3bf5690f0f780ba63.tar.gz | |
sync: Track last completed fetch/checkout
Save the latest time any project is fetched and checked out. This will
be used to detect partial checkouts.
Bug: b/286126621
Change-Id: I53b264dc70ba168d506076dbd693ef79a696b61d
Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/380514
Commit-Queue: Gavin Mak <gavinmak@google.com>
Reviewed-by: Joanna Wang <jojwang@google.com>
Tested-by: Gavin Mak <gavinmak@google.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_subcmds_sync.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/tests/test_subcmds_sync.py b/tests/test_subcmds_sync.py index 5c8e606e..057478ef 100644 --- a/tests/test_subcmds_sync.py +++ b/tests/test_subcmds_sync.py | |||
| @@ -14,6 +14,8 @@ | |||
| 14 | """Unittests for the subcmds/sync.py module.""" | 14 | """Unittests for the subcmds/sync.py module.""" |
| 15 | 15 | ||
| 16 | import os | 16 | import os |
| 17 | import shutil | ||
| 18 | import tempfile | ||
| 17 | import unittest | 19 | import unittest |
| 18 | from unittest import mock | 20 | from unittest import mock |
| 19 | 21 | ||
| @@ -104,6 +106,79 @@ def test_cli_jobs(argv, jobs_manifest, jobs, jobs_net, jobs_check): | |||
| 104 | assert opts.jobs_checkout == jobs_check | 106 | assert opts.jobs_checkout == jobs_check |
| 105 | 107 | ||
| 106 | 108 | ||
| 109 | class LocalSyncState(unittest.TestCase): | ||
| 110 | """Tests for _LocalSyncState.""" | ||
| 111 | |||
| 112 | _TIME = 10 | ||
| 113 | |||
| 114 | def setUp(self): | ||
| 115 | """Common setup.""" | ||
| 116 | self.repodir = tempfile.mkdtemp(".repo") | ||
| 117 | self.manifest = mock.MagicMock( | ||
| 118 | repodir=self.repodir, | ||
| 119 | ) | ||
| 120 | self.state = self._new_state() | ||
| 121 | |||
| 122 | def tearDown(self): | ||
| 123 | """Common teardown.""" | ||
| 124 | shutil.rmtree(self.repodir) | ||
| 125 | |||
| 126 | def _new_state(self): | ||
| 127 | with mock.patch("time.time", return_value=self._TIME): | ||
| 128 | return sync._LocalSyncState(self.manifest) | ||
| 129 | |||
| 130 | def test_set(self): | ||
| 131 | """Times are set.""" | ||
| 132 | p = mock.MagicMock(relpath="projA") | ||
| 133 | self.state.SetFetchTime(p) | ||
| 134 | self.state.SetCheckoutTime(p) | ||
| 135 | self.assertEqual(self.state.GetFetchTime(p), self._TIME) | ||
| 136 | self.assertEqual(self.state.GetCheckoutTime(p), self._TIME) | ||
| 137 | |||
| 138 | def test_update(self): | ||
| 139 | """Times are updated.""" | ||
| 140 | with open(self.state._path, "w") as f: | ||
| 141 | f.write( | ||
| 142 | """ | ||
| 143 | { | ||
| 144 | "projB": { | ||
| 145 | "last_fetch": 5, | ||
| 146 | "last_checkout": 7 | ||
| 147 | } | ||
| 148 | } | ||
| 149 | """ | ||
| 150 | ) | ||
| 151 | |||
| 152 | # Initialize state to read from the new file. | ||
| 153 | self.state = self._new_state() | ||
| 154 | projA = mock.MagicMock(relpath="projA") | ||
| 155 | projB = mock.MagicMock(relpath="projB") | ||
| 156 | self.assertEqual(self.state.GetFetchTime(projA), None) | ||
| 157 | self.assertEqual(self.state.GetFetchTime(projB), 5) | ||
| 158 | self.assertEqual(self.state.GetCheckoutTime(projB), 7) | ||
| 159 | |||
| 160 | self.state.SetFetchTime(projA) | ||
| 161 | self.state.SetFetchTime(projB) | ||
| 162 | self.assertEqual(self.state.GetFetchTime(projA), self._TIME) | ||
| 163 | self.assertEqual(self.state.GetFetchTime(projB), self._TIME) | ||
| 164 | self.assertEqual(self.state.GetCheckoutTime(projB), 7) | ||
| 165 | |||
| 166 | def test_save_to_file(self): | ||
| 167 | """Data is saved under repodir.""" | ||
| 168 | p = mock.MagicMock(relpath="projA") | ||
| 169 | self.state.SetFetchTime(p) | ||
| 170 | self.state.Save() | ||
| 171 | self.assertEqual( | ||
| 172 | os.listdir(self.repodir), [".repo_localsyncstate.json"] | ||
| 173 | ) | ||
| 174 | |||
| 175 | def test_nonexistent_project(self): | ||
| 176 | """Unsaved projects don't have data.""" | ||
| 177 | p = mock.MagicMock(relpath="projC") | ||
| 178 | self.assertEqual(self.state.GetFetchTime(p), None) | ||
| 179 | self.assertEqual(self.state.GetCheckoutTime(p), None) | ||
| 180 | |||
| 181 | |||
| 107 | class GetPreciousObjectsState(unittest.TestCase): | 182 | class GetPreciousObjectsState(unittest.TestCase): |
| 108 | """Tests for _GetPreciousObjectsState.""" | 183 | """Tests for _GetPreciousObjectsState.""" |
| 109 | 184 | ||
