Skip to content

Commit e8d3832

Browse files
committed
BUG: Add more tests, make console JSON == JSON file output
1 parent 726ddaa commit e8d3832

File tree

3 files changed

+24
-7
lines changed

3 files changed

+24
-7
lines changed

doc/source/whatsnew/v1.3.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -457,6 +457,7 @@ Other
457457
- Bug in :class:`Styler` where ``subset`` arg in methods raised an error for some valid multiindex slices (:issue:`33562`)
458458
- :class:`Styler` rendered HTML output minor alterations to support w3 good code standard (:issue:`39626`)
459459
- Bug in :meth:`DataFrame.equals`, :meth:`Series.equals`, :meth:`Index.equals` with object-dtype containing ``np.datetime64("NaT")`` or ``np.timedelta64("NaT")`` (:issue:`39650`)
460+
- Bug in :func: `pandas.util.show_versions` where console JSON output was not proper JSON (:issue:`39701`)
460461

461462

462463
.. ---------------------------------------------------------------------------

pandas/tests/util/test_show_versions.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@
55
import pytest
66

77
import pandas as pd
8-
9-
10-
@pytest.fixture
11-
def as_json(request):
12-
return request.param
8+
from pandas.util._print_versions import _get_sys_info, _get_dependency_info
139

1410

1511
@pytest.mark.filterwarnings(
@@ -33,7 +29,7 @@ def as_json(request):
3329
"ignore:Distutils:UserWarning"
3430
)
3531
@pytest.mark.filterwarnings("ignore:Setuptools is replacing distutils:UserWarning")
36-
@pytest.mark.parametrize("as_json", [True, False, "test_output.json"], indirect=True)
32+
@pytest.mark.parametrize("as_json", [True, False, "test_output.json"])
3733
def test_show_versions(capsys, as_json, tmpdir):
3834
# gh-32041
3935
if isinstance(as_json, str):
@@ -42,6 +38,7 @@ def test_show_versions(capsys, as_json, tmpdir):
4238
pd.show_versions(as_json=as_json)
4339
captured = capsys.readouterr()
4440
result = captured.out
41+
string_check = result
4542

4643
# check valid json is printed to the console if as_json is True
4744
if as_json is True:
@@ -74,3 +71,22 @@ def test_show_versions(capsys, as_json, tmpdir):
7471

7572
# check if file output is valid JSON
7673
json.loads(str_contents)
74+
75+
# prepare string for checking for specific keys
76+
string_check = str_contents
77+
78+
# Basic check that each version element is found in output
79+
version_elements = {**_get_sys_info, **_get_dependency_info}
80+
assert all([v in string_check and k in string_check for k, v in version_elements])
81+
82+
83+
def test_json_output_match(capsys, tmpdir):
84+
pd.show_versions(as_json=True)
85+
result_console = capsys.readouterr().out
86+
87+
out_path = os.path.join(tmpdir, "test_json.json")
88+
pd.show_versions(as_json=out_path)
89+
with open(out_path) as out_fd:
90+
result_file = "".join(out_fd.readlines())
91+
92+
assert result_console == result_file

pandas/util/_print_versions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def show_versions(as_json: Union[str, bool] = False) -> None:
107107
j = {"system": sys_info, "dependencies": deps}
108108

109109
if as_json is True:
110-
print(json.dumps(j))
110+
sys.stdout.writelines(json.dumps(j, indent=2))
111111
else:
112112
assert isinstance(as_json, str) # needed for mypy
113113
with codecs.open(as_json, "wb", encoding="utf8") as f:

0 commit comments

Comments
 (0)