4
4
from datetime import datetime
5
5
from datetime import timezone
6
6
from os .path import join as opj
7
+ from textwrap import dedent
8
+ from typing import Dict
7
9
from unittest .mock import Mock
8
10
from unittest .mock import patch
9
11
10
12
import pytest
11
13
14
+ from .wd_wrapper import WorkDir
12
15
from setuptools_scm import Configuration
13
16
from setuptools_scm import format_version
14
17
from setuptools_scm import git
19
22
from setuptools_scm .utils import do
20
23
from setuptools_scm .utils import has_command
21
24
22
-
23
25
pytestmark = pytest .mark .skipif (
24
26
not has_command ("git" , warn = False ), reason = "git executable not found"
25
27
)
@@ -58,11 +60,11 @@ def test_root_relative_to(tmpdir, wd, monkeypatch):
58
60
"relative_to": __file__})
59
61
"""
60
62
)
61
- res = do (( sys .executable , "setup.py" , "--version" ) , p )
63
+ res = do ([ sys .executable , "setup.py" , "--version" ] , p )
62
64
assert res == "0.1.dev0"
63
65
64
66
65
- def test_root_search_parent_directories (tmpdir , wd , monkeypatch ):
67
+ def test_root_search_parent_directories (tmpdir , wd : WorkDir , monkeypatch ):
66
68
monkeypatch .delenv ("SETUPTOOLS_SCM_DEBUG" )
67
69
p = wd .cwd .joinpath ("sub/package" )
68
70
p .mkdir (parents = True )
@@ -71,7 +73,7 @@ def test_root_search_parent_directories(tmpdir, wd, monkeypatch):
71
73
setup(use_scm_version={"search_parent_directories": True})
72
74
"""
73
75
)
74
- res = do (( sys .executable , "setup.py" , "--version" ) , p )
76
+ res = do ([ sys .executable , "setup.py" , "--version" ] , p )
75
77
assert res == "0.1.dev0"
76
78
77
79
@@ -137,60 +139,59 @@ def test_version_from_git(wd):
137
139
)
138
140
139
141
140
- @pytest .mark .parametrize ("with_class" , [False , type , str ])
141
- def test_git_version_unnormalized_setuptools (with_class , tmpdir , wd , monkeypatch ):
142
+ setup_py_with_normalize : Dict [str , str ] = {
143
+ "false" : """
144
+ from setuptools import setup
145
+ setup(use_scm_version={'normalize': False, 'write_to': 'VERSION.txt'})
146
+ """ ,
147
+ "with_created_class" : """
148
+ from setuptools import setup
149
+
150
+ class MyVersion:
151
+ def __init__(self, tag_str: str):
152
+ self.version = tag_str
153
+
154
+ def __repr__(self):
155
+ return self.version
156
+
157
+ setup(use_scm_version={'version_cls': MyVersion, 'write_to': 'VERSION.txt'})
158
+ """ ,
159
+ "with_named_import" : """
160
+ from setuptools import setup
161
+ setup(use_scm_version={
162
+ 'version_cls': 'setuptools_scm.NonNormalizedVersion',
163
+ 'write_to': 'VERSION.txt'
164
+ })
165
+ """ ,
166
+ }
167
+
168
+
169
+ @pytest .mark .parametrize (
170
+ "setup_py_txt" ,
171
+ [pytest .param (text , id = key ) for key , text in setup_py_with_normalize .items ()],
172
+ )
173
+ def test_git_version_unnormalized_setuptools (
174
+ setup_py_txt : str , wd : WorkDir , monkeypatch
175
+ ):
142
176
"""
143
177
Test that when integrating with setuptools without normalization,
144
178
the version is not normalized in write_to files,
145
179
but still normalized by setuptools for the final dist metadata.
146
180
"""
147
181
monkeypatch .delenv ("SETUPTOOLS_SCM_DEBUG" )
148
- p = wd .cwd
149
-
150
- # create a setup.py
151
- dest_file = str (tmpdir .join ("VERSION.txt" )).replace ("\\ " , "/" )
152
- if with_class is False :
153
- # try normalize = False
154
- setup_py = """
155
- from setuptools import setup
156
- setup(use_scm_version={'normalize': False, 'write_to': '%s'})
157
- """
158
- elif with_class is type :
159
- # custom non-normalizing class
160
- setup_py = """
161
- from setuptools import setup
162
-
163
- class MyVersion:
164
- def __init__(self, tag_str: str):
165
- self.version = tag_str
166
-
167
- def __repr__(self):
168
- return self.version
169
-
170
- setup(use_scm_version={'version_cls': MyVersion, 'write_to': '%s'})
171
- """
172
- elif with_class is str :
173
- # non-normalizing class referenced by name
174
- setup_py = """from setuptools import setup
175
- setup(use_scm_version={
176
- 'version_cls': 'setuptools_scm.NonNormalizedVersion',
177
- 'write_to': '%s'
178
- })
179
- """
180
182
181
- # finally write the setup.py file
182
- p .joinpath ("setup.py" ).write_text (setup_py % dest_file )
183
+ wd .write ("setup.py" , dedent (setup_py_txt ))
183
184
184
185
# do git operations and tag
185
186
wd .commit_testfile ()
186
187
wd ("git tag 17.33.0-rc1" )
187
188
188
189
# setuptools still normalizes using packaging.Version (removing the dash)
189
- res = do (( sys .executable , "setup.py" , "--version" ), p )
190
+ res = wd ([ sys .executable , "setup.py" , "--version" ] )
190
191
assert res == "17.33.0rc1"
191
192
192
193
# but the version tag in the file is non-normalized (with the dash)
193
- assert tmpdir . join ("VERSION.txt" ).read () == "17.33.0-rc1"
194
+ assert wd . cwd . joinpath ("VERSION.txt" ).read_text () == "17.33.0-rc1"
194
195
195
196
196
197
@pytest .mark .issue (179 )
0 commit comments