new_proposal_test.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python3
  2. """Tests for new_proposal.py."""
  3. __copyright__ = """
  4. Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  5. Exceptions. See /LICENSE for license information.
  6. SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  7. """
  8. import os
  9. import unittest
  10. from unittest import mock
  11. from carbon.proposals.scripts import new_proposal
  12. class FakeExitError(Exception):
  13. pass
  14. def _fake_exit(message):
  15. raise FakeExitError(message)
  16. class TestNewProposal(unittest.TestCase):
  17. def test_calculate_branch_short(self):
  18. parsed_args = new_proposal._parse_args(["foo bar"])
  19. self.assertEqual(
  20. new_proposal._calculate_branch(parsed_args), "proposal-foo-bar"
  21. )
  22. def test_calculate_branch_long(self):
  23. parsed_args = new_proposal._parse_args(
  24. ["A really long long long title"]
  25. )
  26. self.assertEqual(
  27. new_proposal._calculate_branch(parsed_args),
  28. "proposal-a-really-long-long-l",
  29. )
  30. def test_calculate_branch_flag(self):
  31. parsed_args = new_proposal._parse_args(["--branch=wiz", "foo"])
  32. self.assertEqual(new_proposal._calculate_branch(parsed_args), "wiz")
  33. def test_fill_template(self):
  34. content = new_proposal._fill_template(
  35. os.path.join(new_proposal._get_proposals_dir(), "template.md"),
  36. "TITLE",
  37. 123,
  38. )
  39. self.assertTrue(content.startswith("# TITLE\n\n"), content)
  40. self.assertTrue(
  41. "[Pull request](https://github.com/carbon-language/carbon-lang/"
  42. "pull/123)" in content,
  43. content,
  44. )
  45. def test_run_success(self):
  46. new_proposal._run(["true"])
  47. def test_run_failure(self):
  48. with mock.patch(
  49. "carbon.proposals.scripts.new_proposal._exit",
  50. side_effect=_fake_exit,
  51. ):
  52. self.assertRaises(FakeExitError, new_proposal._run, ["false"])
  53. if __name__ == "__main__":
  54. unittest.main()