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