|
| 1 | +import json |
| 2 | +import os |
1 | 3 | import re
|
2 | 4 |
|
3 | 5 | import pytest
|
4 | 6 |
|
5 | 7 | import pandas as pd
|
6 | 8 |
|
7 | 9 |
|
| 10 | +@pytest.fixture |
| 11 | +def as_json(request): |
| 12 | + return request.param |
| 13 | + |
| 14 | + |
8 | 15 | @pytest.mark.filterwarnings(
|
9 | 16 | # openpyxl
|
10 | 17 | "ignore:defusedxml.lxml is no longer supported:DeprecationWarning"
|
|
26 | 33 | "ignore:Distutils:UserWarning"
|
27 | 34 | )
|
28 | 35 | @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): |
30 | 38 | # 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) |
32 | 43 | captured = capsys.readouterr()
|
33 | 44 | result = captured.out
|
34 | 45 |
|
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) |
37 | 67 |
|
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) |
40 | 71 |
|
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 |
44 | 74 |
|
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