test_main.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import subprocess
  2. import unittest.mock
  3. import _pytest.capture
  4. import pubmed_bibtex
  5. from pubmed_bibtex.__main__ import _main
  6. # pylint: disable=wrong-import-order; false positive
  7. from conftest import TEST_BIBTEX_ENTRY, TEST_PMID
  8. def test__main(capsys: _pytest.capture.CaptureFixture) -> None:
  9. with unittest.mock.patch("sys.argv", ["", TEST_PMID]):
  10. _main()
  11. out, err = capsys.readouterr()
  12. assert not err
  13. assert out == TEST_BIBTEX_ENTRY
  14. def test_script_module() -> None:
  15. proc_info = subprocess.run(
  16. ["python", "-m", "pubmed_bibtex", TEST_PMID],
  17. check=True,
  18. stdout=subprocess.PIPE,
  19. stderr=subprocess.PIPE,
  20. )
  21. assert all(l.startswith(b"attempt #") for l in proc_info.stderr.splitlines())
  22. assert proc_info.stdout == TEST_BIBTEX_ENTRY.encode()
  23. def test_script() -> None:
  24. proc_info = subprocess.run(
  25. ["pubmed-bibtex", TEST_PMID],
  26. check=True,
  27. stdout=subprocess.PIPE,
  28. stderr=subprocess.PIPE,
  29. )
  30. assert all(l.startswith(b"attempt #") for l in proc_info.stderr.splitlines())
  31. assert proc_info.stdout == TEST_BIBTEX_ENTRY.encode()
  32. def test_version() -> None:
  33. proc_info = subprocess.run(
  34. ["pubmed-bibtex", "--version"],
  35. check=True,
  36. stdout=subprocess.PIPE,
  37. stderr=subprocess.PIPE,
  38. )
  39. assert not proc_info.stderr
  40. assert proc_info.stdout == pubmed_bibtex.__version__.encode() + b"\n"