5
5
import tarfile
6
6
import unittest
7
7
import tempfile
8
+ from pkg_resources import parse_version , SetuptoolsLegacyVersion
8
9
9
10
sys .path .insert (0 , "src" )
11
+ from git .from_vcs import git_parse_vcs_describe
10
12
from git .from_keywords import git_versions_from_keywords
11
13
from subprocess_helper import run_command
12
14
13
15
GITS = ["git" ]
14
16
if sys .platform == "win32" :
15
17
GITS = ["git.cmd" , "git.exe" ]
16
18
19
+ class ParseGitDescribe (unittest .TestCase ):
20
+ def test_parse (self ):
21
+ def pv (git_describe ):
22
+ return git_parse_vcs_describe (git_describe , "v" )
23
+ self .assertEqual (pv ("1f" ), ("0+untagged.g1f" , False ))
24
+ self .assertEqual (pv ("1f-dirty" ), ("0+untagged.g1f.dirty" , True ))
25
+ self .assertEqual (pv ("v1.0-0-g1f" ), ("1.0" , False ))
26
+ self .assertEqual (pv ("v1.0-0-g1f-dirty" ), ("1.0+0.g1f.dirty" , True ))
27
+ self .assertEqual (pv ("v1.0-1-g1f" ), ("1.0+1.g1f" , False ))
28
+ self .assertEqual (pv ("v1.0-1-g1f-dirty" ), ("1.0+1.g1f.dirty" , True ))
29
+
30
+ def p (git_describe ):
31
+ return git_parse_vcs_describe (git_describe , "" )
32
+ self .assertEqual (p ("1f" ), ("0+untagged.g1f" , False ))
33
+ self .assertEqual (p ("1f-dirty" ), ("0+untagged.g1f.dirty" , True ))
34
+ self .assertEqual (p ("1.0-0-g1f" ), ("1.0" , False ))
35
+ self .assertEqual (p ("1.0-0-g1f-dirty" ), ("1.0+0.g1f.dirty" , True ))
36
+ self .assertEqual (p ("1.0-1-g1f" ), ("1.0+1.g1f" , False ))
37
+ self .assertEqual (p ("1.0-1-g1f-dirty" ), ("1.0+1.g1f.dirty" , True ))
38
+
39
+
17
40
class Keywords (unittest .TestCase ):
18
41
def parse (self , refnames , full , prefix = "" ):
19
42
return git_versions_from_keywords ({"refnames" : refnames , "full" : full },
@@ -40,12 +63,12 @@ def test_unexpanded(self):
40
63
41
64
def test_no_tags (self ):
42
65
v = self .parse ("(HEAD, master)" , "full" )
43
- self .assertEqual (v ["version" ], "full " )
66
+ self .assertEqual (v ["version" ], "0+unknown " )
44
67
self .assertEqual (v ["full" ], "full" )
45
68
46
69
def test_no_prefix (self ):
47
70
v = self .parse ("(HEAD, master, 1.23)" , "full" , "missingprefix-" )
48
- self .assertEqual (v ["version" ], "full " )
71
+ self .assertEqual (v ["version" ], "0+unkn
17AE
own " )
49
72
self .assertEqual (v ["full" ], "full" )
50
73
51
74
VERBOSE = False
@@ -72,6 +95,7 @@ def subpath(self, path):
72
95
# SA: sitting on the 1.0 tag
73
96
# SB: dirtying the tree after 1.0
74
97
# SC: making a new commit after 1.0, clean tree
98
+ # SD: dirtying the tree after the post-1.0 commit
75
99
#
76
100
# Then we're interested in 5 kinds of trees:
77
101
# TA: source tree (with .git)
@@ -134,11 +158,12 @@ def run_test(self, demoapp_dir, script_only):
134
158
self .git ("add" , "--all" )
135
159
self .git ("commit" , "-m" , "comment" )
136
160
161
+ full = self .git ("rev-parse" , "HEAD" )
137
162
v = self .python ("setup.py" , "--version" )
138
- self .assertEqual (v , "unknown" )
163
+ self .assertEqual (v , "0+untagged.g%s" % full [: 7 ] )
139
164
v = self .python (os .path .join (self .subpath ("demoapp" ), "setup.py" ),
140
165
"--version" , workdir = self .testdir )
141
- self .assertEqual (v , "unknown" )
166
+ self .assertEqual (v , "0+untagged.g%s" % full [: 7 ] )
142
167
143
168
out = self .python ("setup.py" , "versioneer" ).splitlines ()
144
169
self .assertEqual (out [0 ], "running versioneer" )
@@ -195,15 +220,24 @@ def run_test(self, demoapp_dir, script_only):
195
220
f = open (self .subpath ("demoapp/setup.py" ),"a" )
196
221
f .write ("# dirty\n " )
197
222
f .close ()
198
- self .do_checks ("1.0-dirty" , full + "-dirty" , dirty = True , state = "SB" )
223
+ short = "1.0+0.g%s.dirty" % full [:7 ]
224
+ self .do_checks (short , full + ".dirty" , dirty = True , state = "SB" )
199
225
200
226
# SC: now we make one commit past the tag
201
227
self .git ("add" , "setup.py" )
202
228
self .git ("commit" , "-m" , "dirty" )
203
229
full = self .git ("rev-parse" , "HEAD" )
204
- short = "1.0-1- g%s" % full [:7 ]
230
+ short = "1.0+1. g%s" % full [:7 ]
205
231
self .do_checks (short , full , dirty = False , state = "SC" )
206
232
233
+ # SD: dirty the post-tag tree
234
+ f = open (self .subpath ("demoapp/setup.py" ),"a" )
235
+ f .write ("# more dirty\n " )
236
+ f .close ()
237
+ full = self .git ("rev-parse" , "HEAD" )
238
+ short = "1.0+1.g%s.dirty" % full [:7 ]
239
+ self .do_checks (short , full + ".dirty" , dirty = True , state = "SD" )
240
+
207
241
208
242
def do_checks (self , exp_short , exp_long , dirty , state ):
209
243
if os .path .exists (self .subpath ("out" )):
@@ -218,7 +252,7 @@ def do_checks(self, exp_short, exp_long, dirty, state):
218
252
target = self .subpath ("out/demoapp-TB" )
219
253
shutil .copytree (self .subpath ("demoapp" ), target )
220
254
shutil .rmtree (os .path .join (target , ".git" ))
221
- self .check_version (target , "unknown" , "unknown" , False , state , tree = "TB" )
255
+ self .check_version (target , "0+ unknown" , "unknown" , False , state , tree = "TB" )
222
256
223
257
# TC: source tree in versionprefix-named parentdir
224
258
target = self .subpath ("out/demo-1.1" )
@@ -239,8 +273,8 @@ def do_checks(self, exp_short, exp_long, dirty, state):
239
273
# expanded keywords only tell us about tags and full revisionids,
240
274
# not how many patches we are beyond a tag. So we can't expect
241
275
# the short version to be like 1.0-1-gHEXID. The code falls back
242
- # to short=long
243
- exp_short_TD = exp_long
276
+ # to short="unknown"
277
+ exp_short_TD = "0+unknown"
244
278
self .check_version (target , exp_short_TD , exp_long , False , state , tree = "TD" )
245
279
246
280
# TE: unpacked setup.py sdist tarball
@@ -260,10 +294,18 @@ def do_checks(self, exp_short, exp_long, dirty, state):
260
294
self .assertTrue (os .path .isdir (target ))
261
295
self .check_version (target , exp_short , exp_long , False , state , tree = "TE" )
262
296
263
- def compare (self , got , expected , state , tree , runtime ):
297
+ def compare (self , got , expected , state , tree , runtime , pep440 ):
264
298
where = "/" .join ([state , tree , runtime ])
265
299
self .assertEqual (got , expected , "%s: got '%s' != expected '%s'"
266
300
% (where , got , expected ))
301
+ if pep440 :
302
+ pv = parse_version (got )
303
+ self .assertFalse (isinstance (pv , SetuptoolsLegacyVersion ),
304
+ "%s: '%s' was not pep440-compatible"
305
+ % (where , got ))
<
10000
td data-grid-cell-id="diff-84c4b72d5c62026240fad69041a453efc37f7336a89035b69672cce0e8beaca1-266-306-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">306
+ self .assertEqual (str (pv ), got ,
307
+ "%s: '%s' pep440-normalized to '%s'"
308
+ % (where , got , str (pv )))
267
309
if VERBOSE : print (" good %s" % where )
268
310
269
311
def check_version (self , workdir , exp_short , exp_long , dirty , state , tree ):
@@ -275,11 +317,12 @@ def check_version(self, workdir, exp_short, exp_long, dirty, state, tree):
275
317
print (self .python ("setup.py" , "version" , workdir = workdir ))
276
318
# setup.py --version gives us get_version() with verbose=False.
277
319
v = self .python ("setup.py" , "--version" , workdir = workdir )
278
- self .compare (v , exp_short , state , tree , "RA1" )
320
+ self .compare (v , exp_short , state , tree , "RA1" , pep440 = True )
321
+
279
322
# and test again from outside the tree
280
323
v = self .python (os .path .join (workdir , "setup.py" ), "--version" ,
281
324
workdir = self .testdir )
282
- self .compare (v , exp_short , state , tree , "RA2" )
325
+ self .compare (v , exp_short , state , tree , "RA2" , pep440 = True )
283
326
284
327
if dirty :
285
328
return # cannot detect dirty files in a build # XXX really?
@@ -292,9 +335,12 @@ def check_version(self, workdir, exp_short, exp_long, dirty, state, tree):
292
335
build_lib = os .path .join (workdir , "build" , "lib" )
293
336
out = self .python ("rundemo" , "--version" , workdir = build_lib )
294
337
data = dict ([line .split (":" ,1 ) for line in out .splitlines ()])
295
- self .compare (data ["__version__" ], exp_short , state , tree , "RB" )
296
- self .compare (data ["shortversion" ], exp_short , state , tree , "RB" )
297
- self .compare (data ["longversion" ], exp_long , state , tree , "RB" )
338
+ self .compare (data ["__version__" ], exp_short , state , tree , "RB" ,
339
+ pep440 = True )
340
+ self .compare (data ["shortversion" ], exp_short , state , tree , "RB" ,
341
+ pep440 = True )
342
+ self .compare (data ["longversion" ], exp_long , state , tree , "RB" ,
343
+ pep440 = False )
298
344
299
345
300
346
if __name__ == '__main__' :
0 commit comments