17
17
from packaging .version import Version
18
18
19
19
from setuptools_scm ._integration import setuptools as setuptools_integration
20
+ from setuptools_scm ._integration .pyproject_reading import PyProjectData
20
21
from setuptools_scm ._requirement_cls import extract_package_name
21
22
22
23
if TYPE_CHECKING :
@@ -939,33 +940,35 @@ def create_clean_distribution(name: str) -> setuptools.Distribution:
939
940
return dist
940
941
941
942
942
- def version_keyword_default (dist : setuptools .Distribution ) -> None :
943
+ def version_keyword_default (
944
+ dist : setuptools .Distribution , pyproject_data : PyProjectData | None = None
945
+ ) -> None :
943
946
"""Helper to call version_keyword with default config and return the result."""
944
947
945
- setuptools_integration .version_keyword (dist , "use_scm_version" , True )
948
+ setuptools_integration .version_keyword (
949
+ dist , "use_scm_version" , True , _given_pyproject_data = pyproject_data
950
+ )
946
951
947
952
948
- def version_keyword_calver (dist : setuptools .Distribution ) -> None :
953
+ def version_keyword_calver (
954
+ dist : setuptools .Distribution , pyproject_data : PyProjectData | None = None
955
+ ) -> None :
949
956
"""Helper to call version_keyword with calver-by-date scheme and return the result."""
950
957
951
958
setuptools_integration .version_keyword (
952
- dist , "use_scm_version" , {"version_scheme" : "calver-by-date" }
959
+ dist ,
960
+ "use_scm_version" ,
961
+ {"version_scheme" : "calver-by-date" },
962
+ _given_pyproject_data = pyproject_data ,
953
963
)
954
964
955
965
956
- # Test cases: (first_func, second_func, expected_final_version)
957
- # We use a controlled date to make calver deterministic
958
- TEST_CASES = [
959
- # Real-world scenarios: infer_version and version_keyword can be called in either order
960
- (setuptools_integration .infer_version , version_keyword_default , "1.0.1.dev1" ),
961
- (
962
- setuptools_integration .infer_version ,
963
- version_keyword_calver ,
964
- "9.2.13.0.dev1" ,
965
- ), # calver should win but doesn't
966
- (version_keyword_default , setuptools_integration .infer_version , "1.0.1.dev1" ),
967
- (version_keyword_calver , setuptools_integration .infer_version , "9.2.13.0.dev1" ),
968
- ]
966
+ def infer_version_with_data (
967
+ dist : setuptools .Distribution , pyproject_data : PyProjectData | None = None
968
+ ) -> None :
969
+ """Helper to call infer_version with pyproject data."""
970
+
971
+ setuptools_integration .infer_version (dist , _given_pyproject_data = pyproject_data )
969
972
970
973
971
974
@pytest .mark .issue ("https://github.com/pypa/setuptools_scm/issues/1022" )
@@ -975,7 +978,13 @@ def version_keyword_calver(dist: setuptools.Distribution) -> None:
975
978
)
976
979
@pytest .mark .parametrize (
977
980
("first_integration" , "second_integration" , "expected_final_version" ),
978
- TEST_CASES ,
981
+ [
982
+ # infer_version and version_keyword can be called in either order
983
+ (infer_version_with_data , version_keyword_default , "1.0.1.dev1" ),
984
+ (infer_version_with_data , version_keyword_calver , "9.2.13.0.dev1" ),
985
+ (version_keyword_default , infer_version_with_data , "1.0.1.dev1" ),
986
+ (version_keyword_calver , infer_version_with_data , "9.2.13.0.dev1" ),
987
+ ],
979
988
)
980
989
def test_integration_function_call_order (
981
990
wd : WorkDir ,
@@ -992,36 +1001,33 @@ def test_integration_function_call_order(
992
1001
# Set up controlled environment for deterministic versions
993
1002
monkeypatch .setenv ("SOURCE_DATE_EPOCH" , "1234567890" ) # 2009-02-13T23:31:30+00:00
994
1003
# Override node_date to get consistent calver versions
995
- monkeypatch .setenv ("SETUPTOOLS_SCM_PRETEND_METADATA" , "{node_date=2009-02-13}" )
1004
+ monkeypatch .setenv (
1005
+ "SETUPTOOLS_SCM_PRETEND_METADATA_FOR_TEST_CALL_ORDER" , "{node_date=2009-02-13}"
1006
+ )
996
1007
997
1008
# Set up a git repository with a tag and known commit hash
998
1009
wd .commit_testfile ("test" )
999
1010
wd ("git tag 1.0.0" )
1000
1011
wd .commit_testfile ("test2" ) # Add another commit to get distance
1001
1012
monkeypatch .chdir (wd .cwd )
1002
1013
1003
- # Create a pyproject.toml file
1004
- pyproject_content = f"""
1005
- [build-system]
1006
- requires = ["setuptools", "setuptools_scm"]
1007
- build-backend = "setuptools.build_meta"
1008
-
1009
- [project]
1010
- name = "test-pkg-{ first_integration .__name__ } -{ second_integration .__name__ } "
1011
- dynamic = ["version"]
1012
-
1013
- [tool.setuptools_scm]
1014
- local_scheme = "no-local-version"
1015
- """
1016
- wd .write ("pyproject.toml" , pyproject_content )
1017
-
1018
- dist = create_clean_distribution (
1019
- f"test-pkg-{ first_integration .__name__ } -{ second_integration .__name__ } "
1014
+ # Create PyProjectData with equivalent configuration - no file I/O!
1015
+ project_name = "test-call-order"
1016
+ pyproject_data = PyProjectData (
1017
+ path = Path ("pyproject.toml" ),
1018
+ tool_name = "setuptools_scm" ,
1019
+ project = {"name" : project_name , "dynamic" : ["version" ]},
1020
+ section = {"local_scheme" : "no-local-version" }, # [tool.setuptools_scm] config
1021
+ is_required = True , # setuptools_scm in build-system.requires
1022
+ section_present = True , # [tool.setuptools_scm] section exists
1023
+ project_present = True , # [project] section exists
1020
1024
)
1021
1025
1022
- # Call both integration functions in order
1023
- first_integration (dist )
1024
- second_integration (dist )
1026
+ dist = create_clean_distribution (project_name )
1027
+
1028
+ # Call both integration functions in order with direct data injection
1029
+ first_integration (dist , pyproject_data )
1030
+ second_integration (dist , pyproject_data )
1025
1031
1026
1032
# Get the final version directly from the distribution
1027
1033
final_version = dist .metadata .version
0 commit comments