Skip to content

Commit

Permalink
BUG: Various minor improvements on test_show_versions (pandas-dev#39701)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexprincel committed Feb 21, 2021
1 parent 691b1a2 commit 23d34c9
Showing 1 changed file with 15 additions and 21 deletions.
36 changes: 15 additions & 21 deletions pandas/tests/util/test_show_versions.py
Expand Up @@ -12,6 +12,10 @@
import pandas as pd


def get_captured_output(capsys):
return capsys.readouterr().out


@pytest.mark.filterwarnings(
# openpyxl
"ignore:defusedxml.lxml is no longer supported:DeprecationWarning"
Expand All @@ -38,51 +42,41 @@ def test_show_versions(tmpdir):

pd.show_versions(as_json=as_json)

# make sure that the file was created
assert os.path.exists(as_json)

with open(as_json) as fd:
contents = fd.readlines()
str_contents = "".join(contents)

# make sure that there was output to the file
assert str_contents

# check if file output is valid JSON, will raise an exception if not
dict_check = json.loads(str_contents)
result = json.load(fd)

# Basic check that each version element is found in output
version_elements = {
expected = {
"system": _get_sys_info(),
"dependencies": _get_dependency_info(),
}

assert version_elements == dict_check
assert result == expected


def test_show_versions_console_json(capsys):
pd.show_versions(as_json=True)
captured = capsys.readouterr()
result = captured.out
result = get_captured_output(capsys)

# check valid json is printed to the console if as_json is True
dict_check = json.loads(result)
result = json.loads(result)

# Basic check that each version element is found in output
version_elements = {
expected = {
"system": _get_sys_info(),
"dependencies": _get_dependency_info(),
}

assert version_elements == dict_check
assert result == expected


def test_show_versions_console(capsys):
# gh-32041
pd.show_versions(as_json=False)
captured = capsys.readouterr()
result = captured.out
result = get_captured_output(capsys)

# check header
assert "INSTALLED VERSIONS" in result

# check full commit hash
Expand All @@ -98,11 +92,11 @@ def test_show_versions_console(capsys):

def test_json_output_match(capsys, tmpdir):
pd.show_versions(as_json=True)
result_console = capsys.readouterr().out
result_console = get_captured_output(capsys)

out_path = os.path.join(tmpdir, "test_json.json")
pd.show_versions(as_json=out_path)
with open(out_path) as out_fd:
result_file = "".join(out_fd.readlines())
result_file = out_fd.read()

assert result_console == result_file

0 comments on commit 23d34c9

Please sign in to comment.