test_main.py 1.5 KB

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