Git Office Hours : le test pour Entr'ouvert
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

test.py 4.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #!/usr/bin/env python3
  2. """ Tests for git_oh script """
  3. import datetime
  4. import gc
  5. import os
  6. import random
  7. import shutil
  8. import tempfile
  9. import unittest
  10. import git
  11. import git_oh
  12. class TestGitIterations(unittest.TestCase):
  13. """ Testing iterator on git commits """
  14. def setUp(self):
  15. self.repo_path = tempfile.mkdtemp(prefix="git_oh_test_")
  16. self.repo = git.Repo.init(self.repo_path)
  17. self.actors = [git.Actor(f"User{i:d}", f"user{i:d}@localhost")
  18. for i in range(8)]
  19. def tearDown(self):
  20. shutil.rmtree(self.repo_path)
  21. def test_metadata(self):
  22. """ Fetch commits and their metadata in a simple git repository """
  23. fromisoformat = datetime.datetime.fromisoformat
  24. commit_date_fmt = "2022-12-13T23:32:2%d+0200"
  25. commits = [
  26. self.git_commit_mod(author=self.actors[0],
  27. commit_date=fromisoformat(commit_date_fmt % i))
  28. for i in range(10)]
  29. found_commits = list(git_oh.iter_commits(self.repo))
  30. set_a = {c.hexsha for c in commits}
  31. set_b = {c.hexsha for c in found_commits}
  32. self.assertEqual(set_a, set_b)
  33. for commit in found_commits:
  34. self.assertEqual(commit.author, self.actors[0])
  35. self.assertLessEqual(commit.committed_datetime,
  36. fromisoformat(commit_date_fmt % 9))
  37. self.assertGreaterEqual(commit.committed_datetime,
  38. fromisoformat(commit_date_fmt % 0))
  39. def test_branches(self):
  40. """ Fetch commits from a repo with mutliple branches """
  41. commits = [self.git_commit_mod(author=self.actors[0])
  42. for _ in range(10)]
  43. self.repo.git.checkout("HEAD", b="new_branch")
  44. commits += [self.git_commit_mod(author=self.actors[0])
  45. for _ in range(10)]
  46. self.repo.git.checkout("HEAD", b="another_branch")
  47. commits += [self.git_commit_mod(author=self.actors[0])
  48. for _ in range(10)]
  49. self.repo.git.checkout("new_branch")
  50. found_commits = {commit.hexsha
  51. for commit in git_oh.iter_commits(self.repo)}
  52. commits = {commit.hexsha for commit in commits}
  53. self.assertEqual(commits, found_commits)
  54. def test_remote(self):
  55. """ Testing TempRemoteRepo class commit fetch __iter__ method """
  56. commits = {self.git_commit_mod(author=self.actors[0]).hexsha
  57. for _ in range(10)}
  58. repo = git_oh.TempRemoteRepo(f"file://{self.repo_path:s}")
  59. found_commits = {commit.hexsha for commit in repo}
  60. self.assertEqual(commits, found_commits)
  61. def test_remote_cleanup(self):
  62. """ Testing TempRemoteRepo class cleanup """
  63. repo = git_oh.TempRemoteRepo(f"file://{self.repo_path:s}")
  64. tmppath = repo.temppath
  65. _ = list(repo)
  66. self.assertTrue(os.path.isdir(tmppath))
  67. del repo
  68. gc.collect() # asking gc to call repo.__del__()
  69. self.assertFalse(os.path.isdir(tmppath))
  70. def test_remote_branches(self):
  71. """ Testing TempRemoteRepo branch commit fetch """
  72. commits = []
  73. for i in range(5):
  74. commits += [self.git_commit_mod(author=self.actors[0])
  75. for _ in range(10)]
  76. self.repo.git.checkout("HEAD", b=f"branch-{i:d}")
  77. repo = git_oh.TempRemoteRepo(f"file://{self.repo_path:s}")
  78. found_commits = {commit.hexsha for commit in repo}
  79. commits = {commit.hexsha for commit in commits}
  80. self.assertEqual(commits, found_commits)
  81. def git_commit_mod(self, msg="new commit", filename="foo.txt",
  82. **commit_kwargs):
  83. """ Add a random modification to repository and commit them
  84. Arguments :
  85. - msg : commit message
  86. - filename : the filename to modify/add/commit
  87. - **commit_kwargs : see help(git.index.base.IndexFile.commit)
  88. Returns :
  89. - the commit instance
  90. """
  91. with open(os.path.join(self.repo_path, filename), "w+",
  92. encoding="utf-8") as repo_fp:
  93. repo_fp.write(random.choices("abcdef")[0])
  94. self.repo.index.add([filename])
  95. return self.repo.index.commit(msg, **commit_kwargs)
  96. if __name__ == "__main__":
  97. unittest.main()