test_gh_pr.py 1.3 KB

1234567891011121314151617181920212223242526
  1. import unittest
  2. from pathlib import Path
  3. from unittest.mock import patch
  4. from gh_pr import create_pr
  5. class GhPrTests(unittest.TestCase):
  6. def test_wrong_account_stops_before_writes(self):
  7. with patch('gh_pr.subprocess.check_output', return_value='Light-Beacon\n') as command:
  8. with self.assertRaises(RuntimeError): create_pr('topic', 'title', Path('body.md'))
  9. self.assertEqual(command.call_count, 1)
  10. def test_existing_pr_is_reused(self):
  11. with patch('gh_pr.subprocess.check_output', side_effect=['Hilda-Bot\n', '[{"url":"https://github.com/test/pull/1","headRepositoryOwner":{"login":"Hilda-Bot"}}]']) as command:
  12. self.assertEqual(create_pr('topic', 'title', Path('body.md')), 'https://github.com/test/pull/1')
  13. self.assertEqual(command.call_count, 2)
  14. def test_create_with_explicit_head_and_body_file(self):
  15. with patch('gh_pr.subprocess.check_output', side_effect=['Hilda-Bot\n', '[]', 'https://github.com/test/pull/2\n']) as command:
  16. self.assertEqual(create_pr('topic', 'title', Path('body.md')), 'https://github.com/test/pull/2')
  17. args = command.call_args.args[0]
  18. self.assertIn('Hilda-Bot:topic', args)
  19. self.assertIn('--body-file', args)
  20. if __name__ == '__main__': unittest.main()