|
| 1 | +import json |
| 2 | +import os |
1 | 3 | import re
|
2 | 4 |
|
3 | 5 | import pytest
|
4 | 6 |
|
| 7 | +from pandas.util._print_versions import _get_dependency_info, _get_sys_info |
| 8 | + |
5 | 9 | import pandas as pd
|
6 | 10 |
|
7 | 11 |
|
|
26 | 30 | "ignore:Distutils:UserWarning"
|
27 | 31 | )
|
28 | 32 | @pytest.mark.filterwarnings("ignore:Setuptools is replacing distutils:UserWarning")
|
29 |
| -def test_show_versions(capsys): |
| 33 | +@pytest.mark.parametrize("as_json", [True, False, "test_output.json"]) |
| 34 | +def test_show_versions(capsys, as_json, tmpdir): |
30 | 35 | # gh-32041
|
31 |
| - pd.show_versions() |
| 36 | + if isinstance(as_json, str): |
| 37 | + as_json = os.path.join(tmpdir, as_json) |
| 38 | + |
| 39 | + pd.show_versions(as_json=as_json) |
32 | 40 | captured = capsys.readouterr()
|
33 | 41 | result = captured.out
|
34 | 42 |
|
35 |
| - # check header |
36 |
| - assert "INSTALLED VERSIONS" in result |
| 43 | + # check header for non-JSON console output |
| 44 | + if as_json is False: |
| 45 | + assert "INSTALLED VERSIONS" in result |
| 46 | + |
| 47 | + # check full commit hash |
| 48 | + assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result) |
| 49 | + |
| 50 | + # check required dependency |
| 51 | + # 2020-12-09 npdev has "dirty" in the tag |
| 52 | + assert re.search(r"numpy\s*:\s([0-9\.\+a-g\_]|dev)+(dirty)?\n", result) |
| 53 | + |
| 54 | + # check optional dependency |
| 55 | + assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result) |
| 56 | + |
| 57 | + # Dictionary-based asserts |
| 58 | + else: |
| 59 | + # check valid json is printed to the console if as_json is True |
| 60 | + if as_json is True: |
| 61 | + dict_check = json.loads(result) |
| 62 | + elif isinstance(as_json, str): |
| 63 | + # make sure that the file was created |
| 64 | + assert os.path.exists(as_json) |
| 65 | + |
| 66 | + with open(as_json) as fd: |
| 67 | + contents = fd.readlines() |
| 68 | + str_contents = "".join(contents) |
| 69 | + |
| 70 | + # make sure that there was output to the file |
| 71 | + assert str_contents |
| 72 | + |
| 73 | + # check if file output is valid JSON |
| 74 | + dict_check = json.loads(str_contents) |
| 75 | + |
| 76 | + # Basic check that each version element is found in output |
| 77 | + version_elements = { |
| 78 | + "system": _get_sys_info(), |
| 79 | + "dependencies": _get_dependency_info(), |
| 80 | + } |
| 81 | + |
| 82 | + assert version_elements == dict_check |
| 83 | + |
37 | 84 |
|
38 |
| - # check full commit hash |
39 |
| - assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result) |
| 85 | +def test_json_output_match(capsys, tmpdir): |
| 86 | + pd.show_versions(as_json=True) |
| 87 | + result_console = capsys.readouterr().out |
40 | 88 |
|
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) |
| 89 | + out_path = os.path.join(tmpdir, "test_json.json") |
| 90 | + pd.show_versions(as_json=out_path) |
| 91 | + with open(out_path) as out_fd: |
| 92 | + result_file = "".join(out_fd.readlines()) |
44 | 93 |
|
45 |
| - # check optional dependency |
46 |
| - assert re.search(r"pyarrow\s*:\s([0-9\.]+|None)\n", result) |
| 94 | + assert result_console == result_file |
0 commit comments