test_localcache.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. import re
  2. import subprocess
  3. import unittest
  4. from typing import (
  5. List,
  6. Tuple,
  7. )
  8. from ..localcache import (
  9. cache_object,
  10. get_cache_realm,
  11. )
  12. known_remote_objects = [
  13. (
  14. "https://github.com/datalad/datalad.git",
  15. "b97c71ce4005cd9db0d5f2dda25bdbac303dabe7",
  16. ["commit .*", "Author: Michael Hanke.*"]
  17. ),
  18. (
  19. "https://github.com/datalad/datalad-metalad.git",
  20. "9f718f8623e961b93f7f8b6f43f53d94fb405832",
  21. ["commit .*", "Merge: 9fd8929 898ff01.*"]
  22. )
  23. ]
  24. known_remote_references = [
  25. (
  26. "https://github.com/datalad/datalad.git",
  27. "refs/tags/0.2.1",
  28. ["tag 0.2.1", "Tagger: Yaroslav Halchenko.*"]
  29. ),
  30. (
  31. "https://github.com/datalad/datalad-metalad.git",
  32. "refs/tags/0.2.1",
  33. ["tag 0.2.1", "Tagger: Michael Hanke.*"]
  34. )
  35. ]
  36. class TestLocalCache(unittest.TestCase):
  37. def cache(self, specs: List[Tuple[str, str, List[str]]]):
  38. for repo, object_id, _ in specs:
  39. cache_object(repo, object_id)
  40. def check_cached(self, specs: List[Tuple[str, str, List[str]]]):
  41. for repo, object_id, line_patterns in specs:
  42. result = subprocess.run(
  43. [
  44. "git",
  45. "-P",
  46. f"--git-dir={get_cache_realm(repo) / '.git'}",
  47. "show",
  48. object_id
  49. ],
  50. stdout=subprocess.PIPE
  51. )
  52. self.assertEqual(result.returncode, 0)
  53. lines = result.stdout.decode().splitlines()
  54. for index, pattern in enumerate(line_patterns):
  55. self.assertIsNotNone(re.match(pattern, lines[index]))
  56. def test_local_cache_object(self):
  57. self.cache(known_remote_objects)
  58. self.check_cached(known_remote_objects)
  59. def test_local_cache_reference(self):
  60. # check that identical refs in different repos
  61. # are kept separate
  62. self.cache(known_remote_references)
  63. self.check_cached(known_remote_references)