summaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorMike Frysinger <vapier@google.com>2021-01-07 22:14:25 -0500
committerMike Frysinger <vapier@google.com>2021-01-19 16:48:21 +0000
commite5670c881225ed025c77e0362a7c7edcc912ef9f (patch)
tree39bff697aee8c2b318e7e10c7a5e92f365b6f439 /tests
parent48b2d10d8f7565173ca53bed0d0be15323512de4 (diff)
downloadgit-repo-e5670c881225ed025c77e0362a7c7edcc912ef9f.tar.gz
launcher: add a requirements framework to declare version dependencies
Currently we don't have a way for the checked out repo version to declare the version of tools it needs before we start running it. For somethings, like git, it's not a big deal as it can handle all the asserts itself. But for things like Python, it's impossible to reliably check before executing. We're in this state now: - we've been allowing Python 3.4, so the launcher accepts it - the repo codebase starts using Python 3.6 features - launcher tries to import us but hits syntax errors - user is left confused and assuming new repo is broken because they're seeing syntax errors This scenario is playing out with old launchers that still accept Python 2, and will continue to play out as time goes on and we want to require newer versions of Python 3. Lets create a JSON file to declare all these system requirements. That file format is extremely stable, so loading & parsing from even ancient versions of Python shouldn't be a problem. Then the launcher can read these settings and check the system state before attempting to execute any code. If the tools are too old, it can clearly diagnose & display information to the user as to the real problem (and not emit tracebacks or syntax errors). We have a couple of different tool version checks already (git, python, ssh) and can harmonize them in a single place. This also allows us to assert a reverse dependency if the need ever comes up: force the user to upgrade their `repo` launcher before we'll let them run us. Even though the launcher warns whenever a newer release is available, some users seem to ignore that, or they don't use repo that often (on the scale of years), and their upgrade jump is so dramatic that they fall back into the syntax error pit. Hopefully by the end of the year we can assume enough people have upgraded their launcher such that we can delete all of the duplicate version checks in the codebase. But until then, we'll keep them to maintain coverage. Change-Id: I5c12bbffdfd0a8ce978f39aa7f4674026fe9f4f8 Reviewed-on: https://gerrit-review.googlesource.com/c/git-repo/+/293003 Reviewed-by: Michael Mortensen <mmortensen@google.com> Tested-by: Mike Frysinger <vapier@google.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/test_wrapper.py76
1 files changed, 76 insertions, 0 deletions
diff --git a/tests/test_wrapper.py b/tests/test_wrapper.py
index d8713738..6400faf4 100644
--- a/tests/test_wrapper.py
+++ b/tests/test_wrapper.py
@@ -19,6 +19,7 @@ from io import StringIO
19import os 19import os
20import re 20import re
21import shutil 21import shutil
22import sys
22import tempfile 23import tempfile
23import unittest 24import unittest
24from unittest import mock 25from unittest import mock
@@ -255,6 +256,81 @@ class CheckGitVersion(RepoWrapperTestCase):
255 self.wrapper._CheckGitVersion() 256 self.wrapper._CheckGitVersion()
256 257
257 258
259class Requirements(RepoWrapperTestCase):
260 """Check Requirements handling."""
261
262 def test_missing_file(self):
263 """Don't crash if the file is missing (old version)."""
264 testdir = os.path.dirname(os.path.realpath(__file__))
265 self.assertIsNone(self.wrapper.Requirements.from_dir(testdir))
266 self.assertIsNone(self.wrapper.Requirements.from_file(
267 os.path.join(testdir, 'xxxxxxxxxxxxxxxxxxxxxxxx')))
268
269 def test_corrupt_data(self):
270 """If the file can't be parsed, don't blow up."""
271 self.assertIsNone(self.wrapper.Requirements.from_file(__file__))
272 self.assertIsNone(self.wrapper.Requirements.from_data(b'x'))
273
274 def test_valid_data(self):
275 """Make sure we can parse the file we ship."""
276 self.assertIsNotNone(self.wrapper.Requirements.from_data(b'{}'))
277 rootdir = os.path.dirname(os.path.dirname(os.path.realpath(__file__)))
278 self.assertIsNotNone(self.wrapper.Requirements.from_dir(rootdir))
279 self.assertIsNotNone(self.wrapper.Requirements.from_file(os.path.join(
280 rootdir, 'requirements.json')))
281
282 def test_format_ver(self):
283 """Check format_ver can format."""
284 self.assertEqual('1.2.3', self.wrapper.Requirements._format_ver((1, 2, 3)))
285 self.assertEqual('1', self.wrapper.Requirements._format_ver([1]))
286
287 def test_assert_all_unknown(self):
288 """Check assert_all works with incompatible file."""
289 reqs = self.wrapper.Requirements({})
290 reqs.assert_all()
291
292 def test_assert_all_new_repo(self):
293 """Check assert_all accepts new enough repo."""
294 reqs = self.wrapper.Requirements({'repo': {'hard': [1, 0]}})
295 reqs.assert_all()
296
297 def test_assert_all_old_repo(self):
298 """Check assert_all rejects old repo."""
299 reqs = self.wrapper.Requirements({'repo': {'hard': [99999, 0]}})
300 with self.assertRaises(SystemExit):
301 reqs.assert_all()
302
303 def test_assert_all_new_python(self):
304 """Check assert_all accepts new enough python."""
305 reqs = self.wrapper.Requirements({'python': {'hard': sys.version_info}})
306 reqs.assert_all()
307
308 def test_assert_all_old_repo(self):
309 """Check assert_all rejects old repo."""
310 reqs = self.wrapper.Requirements({'python': {'hard': [99999, 0]}})
311 with self.assertRaises(SystemExit):
312 reqs.assert_all()
313
314 def test_assert_ver_unknown(self):
315 """Check assert_ver works with incompatible file."""
316 reqs = self.wrapper.Requirements({})
317 reqs.assert_ver('xxx', (1, 0))
318
319 def test_assert_ver_new(self):
320 """Check assert_ver allows new enough versions."""
321 reqs = self.wrapper.Requirements({'git': {'hard': [1, 0], 'soft': [2, 0]}})
322 reqs.assert_ver('git', (1, 0))
323 reqs.assert_ver('git', (1, 5))
324 reqs.assert_ver('git', (2, 0))
325 reqs.assert_ver('git', (2, 5))
326
327 def test_assert_ver_old(self):
328 """Check assert_ver rejects old versions."""
329 reqs = self.wrapper.Requirements({'git': {'hard': [1, 0], 'soft': [2, 0]}})
330 with self.assertRaises(SystemExit):
331 reqs.assert_ver('git', (0, 5))
332
333
258class NeedSetupGnuPG(RepoWrapperTestCase): 334class NeedSetupGnuPG(RepoWrapperTestCase):
259 """Check NeedSetupGnuPG behavior.""" 335 """Check NeedSetupGnuPG behavior."""
260 336