Skip to content

Commit 1a0f1c7

Browse files
committed
BUG: Add tests and ensure proper testing of as_json values (pandas-dev#39701)
1 parent 4195f41 commit 1a0f1c7

File tree

1 file changed

+41
-11
lines changed

1 file changed

+41
-11
lines changed

pandas/tests/util/test_show_versions.py

+41-11
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
1+
import json
2+
import os
13
import re
24

35
import pytest
46

57
import pandas as pd
68

79

10+
@pytest.fixture
11+
def as_json(request):
12+
return request.param
13+
14+
815
@pytest.mark.filterwarnings(
916
# openpyxl
1017
"ignore:defusedxml.lxml is no longer supported:DeprecationWarning"
@@ -26,21 +33,44 @@
2633
"ignore:Distutils:UserWarning"
2734
)
2835
@pytest.mark.filterwarnings("ignore:Setuptools is replacing distutils:UserWarning")
29-
def test_show_versions(capsys):
36+
@pytest.mark.parametrize('as_json', [True, False, 'test_output.json'], indirect=True)
37+
def test_show_versions(capsys, as_json, tmpdir):
3038
# gh-32041
31-
pd.show_versions()
39+
if isinstance(as_json, str):
40+
as_json = os.path.join(tmpdir, as_json)
41+
42+
pd.show_versions(as_json=as_json)
3243
captured = capsys.readouterr()
3344
result = captured.out
3445

35-
# check header
36-
assert "INSTALLED VERSIONS" in result
46+
# check valid json is printed to the console if as_json is True
47+
if as_json is True:
48+
json.loads(result)
49+
50+
# check header for non-JSON console output
51+
elif as_json is False:
52+
assert "INSTALLED VERSIONS" in result
53+
54+
# check full commit hash
55+
assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result)
56+
57+
# check required dependency
58+
# 2020-12-09 npdev has "dirty" in the tag
59+
assert re.search(r"numpy\s*:\s([0-9\.\+a-g\_]|dev)+(dirty)?\n", result)
60+
61+
# check optional dependency
62+
assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result)
63+
64+
elif isinstance(as_json, str):
65+
# make sure that the file was created
66+
assert os.path.exists(as_json)
3767

38-
# check full commit hash
39-
assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result)
68+
with open(as_json, 'r') as fd:
69+
contents = fd.readlines()
70+
str_contents = ''.join(contents)
4071

41-
# check required dependency
42-
# 2020-12-09 npdev has "dirty" in the tag
43-
assert re.search(r"numpy\s*:\s([0-9\.\+a-g\_]|dev)+(dirty)?\n", result)
72+
# make sure that there was output to the file
73+
assert str_contents
4474

45-
# check optional dependency
46-
assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result)
75+
# check if file output is valid JSON
76+
json.loads(str_contents)

0 commit comments

Comments
 (0)