From 4b2ab34be2fb87e5886a1368e19752978068bc76 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Thu, 20 Feb 2020 14:19:14 -0800 Subject: [PATCH 1/2] TST: show_versions test with unmerged commits --- pandas/tests/util/test_show_versions.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/pandas/tests/util/test_show_versions.py b/pandas/tests/util/test_show_versions.py index 0d2c81c4ea6c7..64c71e1751a0b 100644 --- a/pandas/tests/util/test_show_versions.py +++ b/pandas/tests/util/test_show_versions.py @@ -1,8 +1,26 @@ import re +import pytest + import pandas as pd +@pytest.mark.filterwarnings( + # openpyxl + "ignore:defusedxml.lxml is no longer supported:DeprecationWarning" +) +@pytest.mark.filterwarnings( + # html5lib + "ignore:Using or importing the ABCs from:DeprecationWarning" +) +@pytest.mark.filterwarnings( + # fastparquet + "ignore:pandas.core.index is deprecated:FutureWarning" +) +@pytest.mark.filterwarnings( + # pandas_datareader + "ignore:pandas.util.testing is deprecated:FutureWarning" +) def test_show_versions(capsys): # gh-32041 pd.show_versions() @@ -13,7 +31,11 @@ def test_show_versions(capsys): assert "INSTALLED VERSIONS" in result # check full commit hash - assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result) + if not re.search(r"commit\s*:\s[0-9a-f]{40}\n", result): + # GH#32120 If test is being run in a branch that has uncommited + # changes, then we will not see the full commit hash, but this + # should show up in the pandas version number. + assert re.search(r"pandas\s*: .*\.dirty\n", result) # check required dependency assert re.search(r"numpy\s*:\s([0-9\.\+a-f]|dev)+\n", result) From cbeee67cac2227879abaecf7cf80d53085482f13 Mon Sep 17 00:00:00 2001 From: jbrockmendel Date: Mon, 24 Feb 2020 09:01:12 -0800 Subject: [PATCH 2/2] BUG: use _version code instead of subprocess to get git hash --- pandas/tests/util/test_show_versions.py | 6 +----- pandas/util/_print_versions.py | 27 +++++++++++-------------- 2 files changed, 13 insertions(+), 20 deletions(-) diff --git a/pandas/tests/util/test_show_versions.py b/pandas/tests/util/test_show_versions.py index 64c71e1751a0b..e36ea662fac8b 100644 --- a/pandas/tests/util/test_show_versions.py +++ b/pandas/tests/util/test_show_versions.py @@ -31,11 +31,7 @@ def test_show_versions(capsys): assert "INSTALLED VERSIONS" in result # check full commit hash - if not re.search(r"commit\s*:\s[0-9a-f]{40}\n", result): - # GH#32120 If test is being run in a branch that has uncommited - # changes, then we will not see the full commit hash, but this - # should show up in the pandas version number. - assert re.search(r"pandas\s*: .*\.dirty\n", result) + assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result) # check required dependency assert re.search(r"numpy\s*:\s([0-9\.\+a-f]|dev)+\n", result) diff --git a/pandas/util/_print_versions.py b/pandas/util/_print_versions.py index 99b2b9e9f5f6e..f9502cc22b0c6 100644 --- a/pandas/util/_print_versions.py +++ b/pandas/util/_print_versions.py @@ -4,13 +4,23 @@ import os import platform import struct -import subprocess import sys from typing import List, Optional, Tuple, Union from pandas.compat._optional import VERSIONS, _get_version, import_optional_dependency +def _get_commit_hash() -> Optional[str]: + """ + Use vendored versioneer code to get git hash, which handles + git worktree correctly. + """ + from pandas._version import get_versions + + versions = get_versions() + return versions["full-revisionid"] + + def get_sys_info() -> List[Tuple[str, Optional[Union[str, int]]]]: """ Returns system information as a list @@ -18,20 +28,7 @@ def get_sys_info() -> List[Tuple[str, Optional[Union[str, int]]]]: blob: List[Tuple[str, Optional[Union[str, int]]]] = [] # get full commit hash - commit = None - if os.path.isdir(".git") and os.path.isdir("pandas"): - try: - pipe = subprocess.Popen( - 'git log --format="%H" -n 1'.split(" "), - stdout=subprocess.PIPE, - stderr=subprocess.PIPE, - ) - so, serr = pipe.communicate() - except (OSError, ValueError): - pass - else: - if pipe.returncode == 0: - commit = so.decode("utf-8").strip().strip('"') + commit = _get_commit_hash() blob.append(("commit", commit))