new_proposal_test.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. parsed_args = new_proposal._parse_args(["foo"])
  35. content = new_proposal._fill_template(
  36. os.path.join(
  37. new_proposal._get_proposals_dir(parsed_args), "template.md"
  38. ),
  39. "TITLE",
  40. 123,
  41. )
  42. self.assertTrue(content.startswith("# TITLE\n\n"), content)
  43. self.assertTrue(
  44. "[Pull request](https://github.com/carbon-language/carbon-lang/"
  45. "pull/123)" in content,
  46. content,
  47. )
  48. def test_run_success(self):
  49. new_proposal._run(["true"])
  50. def test_run_failure(self):
  51. with mock.patch(
  52. "carbon.proposals.scripts.new_proposal._exit",
  53. side_effect=_fake_exit,
  54. ):
  55. self.assertRaises(FakeExitError, new_proposal._run, ["false"])
  56. if __name__ == "__main__":
  57. unittest.main()