test_show_versions.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import json
  2. import os
  3. import re
  4. from pandas.util._print_versions import (
  5. _get_dependency_info,
  6. _get_sys_info,
  7. )
  8. import pandas as pd
  9. def test_show_versions(tmpdir):
  10. # GH39701
  11. as_json = os.path.join(tmpdir, "test_output.json")
  12. pd.show_versions(as_json=as_json)
  13. with open(as_json) as fd:
  14. # check if file output is valid JSON, will raise an exception if not
  15. result = json.load(fd)
  16. # Basic check that each version element is found in output
  17. expected = {
  18. "system": _get_sys_info(),
  19. "dependencies": _get_dependency_info(),
  20. }
  21. assert result == expected
  22. def test_show_versions_console_json(capsys):
  23. # GH39701
  24. pd.show_versions(as_json=True)
  25. stdout = capsys.readouterr().out
  26. # check valid json is printed to the console if as_json is True
  27. result = json.loads(stdout)
  28. # Basic check that each version element is found in output
  29. expected = {
  30. "system": _get_sys_info(),
  31. "dependencies": _get_dependency_info(),
  32. }
  33. assert result == expected
  34. def test_show_versions_console(capsys):
  35. # gh-32041
  36. # gh-32041
  37. pd.show_versions(as_json=False)
  38. result = capsys.readouterr().out
  39. # check header
  40. assert "INSTALLED VERSIONS" in result
  41. # check full commit hash
  42. assert re.search(r"commit\s*:\s[0-9a-f]{40}\n", result)
  43. # check required dependency
  44. # 2020-12-09 npdev has "dirty" in the tag
  45. # 2022-05-25 npdev released with RC wo/ "dirty".
  46. # Just ensure we match [0-9]+\..* since npdev version is variable
  47. assert re.search(r"numpy\s*:\s[0-9]+\..*\n", result)
  48. # check optional dependency
  49. assert re.search(r"pyarrow\s*:\s([0-9]+.*|None)\n", result)
  50. def test_json_output_match(capsys, tmpdir):
  51. # GH39701
  52. pd.show_versions(as_json=True)
  53. result_console = capsys.readouterr().out
  54. out_path = os.path.join(tmpdir, "test_json.json")
  55. pd.show_versions(as_json=out_path)
  56. with open(out_path) as out_fd:
  57. result_file = out_fd.read()
  58. assert result_console == result_file