|
8 | 8 | import textwrap
|
9 | 9 |
|
10 | 10 | from pathlib import Path
|
| 11 | +from typing import TYPE_CHECKING |
| 12 | +from typing import Any |
11 | 13 |
|
12 | 14 | import pytest
|
13 | 15 |
|
14 | 16 | import setuptools_scm._integration.setuptools
|
15 | 17 |
|
| 18 | +if TYPE_CHECKING: |
| 19 | + import setuptools |
| 20 | + |
16 | 21 | from setuptools_scm import Configuration
|
17 | 22 | from setuptools_scm._integration.setuptools import _extract_package_name
|
18 | 23 | from setuptools_scm._integration.setuptools import _warn_on_old_setuptools
|
@@ -674,3 +679,115 @@ def test_improved_error_message_mentions_both_config_options(
|
674 | 679 | assert "tool.setuptools_scm" in error_msg
|
675 | 680 | assert "build-system" in error_msg
|
676 | 681 | assert "requires" in error_msg
|
| 682 | + |
| 683 | + |
| 684 | +# Helper functions for testing integration point ordering |
| 685 | +def integration_infer_version(dist: setuptools.Distribution) -> str: |
| 686 | + """Helper to call infer_version and return the result.""" |
| 687 | + from setuptools_scm._integration.setuptools import infer_version |
| 688 | + |
| 689 | + infer_version(dist) |
| 690 | + return "infer_version" |
| 691 | + |
| 692 | + |
| 693 | +def integration_version_keyword_default(dist: setuptools.Distribution) -> str: |
| 694 | + """Helper to call version_keyword with default config and return the result.""" |
| 695 | + from setuptools_scm._integration.setuptools import version_keyword |
| 696 | + |
| 697 | + version_keyword(dist, "use_scm_version", True) |
| 698 | + return "version_keyword_default" |
| 699 | + |
| 700 | + |
| 701 | +def integration_version_keyword_calver(dist: setuptools.Distribution) -> str: |
| 702 | + """Helper to call version_keyword with calver-by-date scheme and return the result.""" |
| 703 | + from setuptools_scm._integration.setuptools import version_keyword |
| 704 | + |
| 705 | + version_keyword(dist, "use_scm_version", {"version_scheme": "calver-by-date"}) |
| 706 | + return "version_keyword_calver" |
| 707 | + |
| 708 | + |
| 709 | +# Test cases: (first_func, second_func, expected_final_version) |
| 710 | +# We use a controlled date to make calver deterministic |
| 711 | +TEST_CASES = [ |
| 712 | + # Real-world scenarios: infer_version and version_keyword can be called in either order |
| 713 | + (integration_infer_version, integration_version_keyword_default, "1.0.1.dev1"), |
| 714 | + ( |
| 715 | + integration_infer_version, |
| 716 | + integration_version_keyword_calver, |
| 717 | + "9.2.13.0.dev1", |
| 718 | + ), # calver should win but doesn't |
| 719 | + (integration_version_keyword_default, integration_infer_version, "1.0.1.dev1"), |
| 720 | + (integration_version_keyword_calver, integration_infer_version, "9.2.13.0.dev1"), |
| 721 | +] |
| 722 | + |
| 723 | + |
| 724 | +@pytest.mark.issue("https://github.com/pypa/setuptools_scm/issues/1022") |
| 725 | +@pytest.mark.filterwarnings("ignore:version of .* already set:UserWarning") |
| 726 | +@pytest.mark.filterwarnings( |
| 727 | + "ignore:.* does not correspond to a valid versioning date.*:UserWarning" |
| 728 | +) |
| 729 | +@pytest.mark.parametrize( |
| 730 | + ("first_integration", "second_integration", "expected_final_version"), |
| 731 | + TEST_CASES, |
| 732 | +) |
| 733 | +def test_integration_function_call_order( |
| 734 | + wd: WorkDir, |
| 735 | + monkeypatch: pytest.MonkeyPatch, |
| 736 | + first_integration: Any, |
| 737 | + second_integration: Any, |
| 738 | + expected_final_version: str, |
| 739 | +) -> None: |
| 740 | + """Test that integration functions can be called in any order. |
| 741 | +
|
| 742 | + version_keyword should always win when it specifies configuration, but currently doesn't. |
| 743 | + Some tests will fail, showing the bug. |
| 744 | + """ |
| 745 | + # Set up controlled environment for deterministic versions |
| 746 | + monkeypatch.setenv("SOURCE_DATE_EPOCH", "1234567890") # 2009-02-13T23:31:30+00:00 |
| 747 | + # Override node_date to get consistent calver versions |
| 748 | + monkeypatch.setenv("SETUPTOOLS_SCM_PRETEND_METADATA", "{node_date=2009-02-13}") |
| 749 | + |
| 750 | + # Set up a git repository with a tag and known commit hash |
| 751 | + wd.commit_testfile("test") |
| 752 | + wd("git tag 1.0.0") |
| 753 | + wd.commit_testfile("test2") # Add another commit to get distance |
| 754 | + monkeypatch.chdir(wd.cwd) |
| 755 | + |
| 756 | + # Generate unique distribution name based on the test combination |
| 757 | + first_name = first_integration.__name__.replace("integration_", "") |
| 758 | + second_name = second_integration.__name__.replace("integration_", "") |
| 759 | + dist_name = f"test-pkg-{first_name}-then-{second_name}" |
| 760 | + |
| 761 | + # Create a pyproject.toml file |
| 762 | + pyproject_content = f""" |
| 763 | +[build-system] |
| 764 | +requires = ["setuptools", "setuptools_scm"] |
| 765 | +build-backend = "setuptools.build_meta" |
| 766 | +
|
| 767 | +[project] |
| 768 | +name = "{dist_name}" |
| 769 | +dynamic = ["version"] |
| 770 | +
|
| 771 | +[tool.setuptools_scm] |
| 772 | +local_scheme = "no-local-version" |
| 773 | +""" |
| 774 | + wd.write("pyproject.toml", pyproject_content) |
| 775 | + |
| 776 | + import setuptools |
| 777 | + |
| 778 | + # Create distribution and clear any auto-set version |
| 779 | + dist = setuptools.Distribution({"name": dist_name}) |
| 780 | + dist.metadata.version = None |
| 781 | + |
| 782 | + # Call both integration functions in order |
| 783 | + first_integration(dist) |
| 784 | + second_integration(dist) |
| 785 | + |
| 786 | + # Get the final version directly from the distribution |
| 787 | + final_version = dist.metadata.version |
| 788 | + |
| 789 | + # Assert the final version matches expectation |
| 790 | + # Some tests will fail here, demonstrating the bug where version_keyword doesn't override |
| 791 | + assert final_version == expected_final_version, ( |
| 792 | + f"Expected version '{expected_final_version}' but got '{final_version}'" |
| 793 | + ) |
0 commit comments