33from git import Actor , Repo
44
55def test_empty_file (tmp_path ):
6-
6+
77 # Create empty file
88 file_name = os .path .join (tmp_path , 'new-file' )
99 open (file_name , 'a' ).close ()
@@ -13,24 +13,24 @@ def test_empty_file(tmp_path):
1313 instance = util .Util (tmp_path )
1414 authors = instance .get_authors (path = file_name )
1515 assert authors == []
16-
16+
1717 # Get authors of empty, committed file
1818 r .index .add ([file_name ])
1919 author = Actor (
'Tim' ,
'[email protected] ' )
2020 r .index .commit ("initial commit" , author = author )
2121 authors = instance .get_authors (path = file_name )
2222 assert authors == []
23-
23+
2424def test_retrieve_authors (tmp_path ):
2525 """
2626 Builds a fake git project with some commits.
27-
27+
2828 pytest offers a `tmp_path`. You can reproduce locally with
2929 >>> import tempfile
3030 >>> from pathlib import Path
3131 >>> tmp_path = Path(tempfile.gettempdir()) / 'pytest-retrieve-authors'
3232 >>> os.mkdir(tmp_path)
33-
33+
3434 Args:
3535 tmp_path (PosixPath): Directory of a tempdir
3636 """
@@ -45,52 +45,52 @@ def test_retrieve_authors(tmp_path):
4545 r .index .add ([file_name ])
4646 author = Actor (
'Tim' ,
'[email protected] ' )
4747 r .index .commit ("initial commit" , author = author )
48-
48+
4949 instance = util .Util (tmp_path )
5050 authors = instance .get_authors (path = file_name )
5151 # We don't want to test datetime
5252 authors [0 ]['last_datetime' ] = None
53-
53+
5454 assert authors == [{
5555 'name' : "Tim" ,
56565757 'last_datetime' : None ,
5858 'lines' : 1 ,
5959 'contribution' : '100.0%'
6060 }]
61-
62- # Now add a line to the file
61+
62+ # Now add a line to the file
6363 # From a second author with same email
6464 with open (file_name , 'a+' ) as the_file :
6565 the_file .write ('World\n ' )
6666 r .index .add ([file_name ])
6767 author = Actor (
'Tim2' ,
'[email protected] ' )
68- r .index .commit ("another commit" , author = author )
69-
68+ r .index .commit ("another commit" , author = author )
69+
7070 instance = util .Util (tmp_path )
7171 authors = instance .get_authors (path = file_name )
72- authors [0 ]['last_datetime' ] = None
73-
72+ authors [0 ]['last_datetime' ] = None
73+
7474 assert authors == [{
7575 'name' : "Tim" ,
76767777 'last_datetime' : None ,
7878 'lines' : 2 ,
7979 'contribution' : '100.0%'
8080 }]
81-
81+
8282 # Then a third commit from a new author
8383 with open (file_name , 'a+' ) as the_file :
8484 the_file .write ('A new line\n ' )
8585 r .index .add ([file_name ])
8686 author = Actor (
'John' ,
'[email protected] ' )
87- r .index .commit ("third commit" , author = author )
88-
89- instance = util .Util (tmp_path )
87+ r .index .commit ("third commit" , author = author )
88+
89+ instance = util .Util (tmp_path )
9090 authors = instance .get_authors (path = file_name )
91- authors [0 ]['last_datetime' ] = None
92- authors [1 ]['last_datetime' ] = None
93-
91+ authors [0 ]['last_datetime' ] = None
92+ authors [1 ]['last_datetime' ] = None
93+
9494 assert authors == [{
9595 'name' : "John" ,
9696@@ -103,15 +103,33 @@ def test_retrieve_authors(tmp_path):
103103 'last_datetime' : None ,
104104 'lines' : 2 ,
105105 'contribution' : '66.67%'
106- }]
106+ }]
107107
108108def test_summarize_authors ():
109-
109+
110110 authors = [
111111 {'name' : 'Tim' ,
112- 112+ 113+ 'contribution' : '64.23%'
113114 }
114115 ]
115-
116- summary = util .Util ().summarize (authors )
117- assert summary == "<span class='git-authors'><a href='mailto:[email protected] '>Tim</a></span>" 116+
117+ # Default case: don't show contribution
118+ config = { 'show_contribution' : False }
119+ summary = util .Util ().summarize (authors , config )
120+ assert summary == "<span class='git-authors'><a href='mailto:[email protected] '>Tim</a></span>" 121+
122+ # Do show contribution, but hide it because there's only one author
123+ config = { 'show_contribution' : True }
124+ summary = util .Util ().summarize (authors , config )
125+ assert summary == "<span class='git-authors'><a href='mailto:[email protected] '>Tim</a></span>" 126+
127+ # Add another author
128+ authors .append ({
129+ 'name' : 'Tom' ,
130+ 131+ 'contribution' : '35.77%'
132+ })
133+ # Now contribution is displayed
134+ summary = util .Util ().summarize (authors , config )
135+ assert summary == "<span class='git-authors'><a href='mailto:[email protected] '>Tim</a> (64.23%), <a href='mailto:[email protected] '>Tom</a> (35.77%)</span>"
0 commit comments