test___main__.py 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. # test___main__.py -- tests for __main__.py
  2. # Copyright (C) 2025 Jelmer Vernooij <jelmer@jelmer.uk>
  3. #
  4. # SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  5. # Dulwich is dual-licensed under the Apache License, Version 2.0 and the GNU
  6. # General Public License as published by the Free Software Foundation; version 2.0
  7. # or (at your option) any later version. You can redistribute it and/or
  8. # modify it under the terms of either of these two licenses.
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # You should have received a copy of the licenses; if not, see
  17. # <http://www.gnu.org/licenses/> for a copy of the GNU General Public License
  18. # and <http://www.apache.org/licenses/LICENSE-2.0> for a copy of the Apache
  19. # License, Version 2.0.
  20. #
  21. """Tests for __main__.py module entry point."""
  22. import subprocess
  23. import sys
  24. from . import TestCase
  25. class MainModuleTests(TestCase):
  26. """Tests for the __main__.py module entry point."""
  27. def test_main_module_help_flag(self) -> None:
  28. """Test that running dulwich as a module with --help works."""
  29. # Run dulwich as a module using python -m
  30. result = subprocess.run(
  31. [sys.executable, "-m", "dulwich", "--help"],
  32. capture_output=True,
  33. text=True,
  34. )
  35. # Help command exits with code 1 (standard behavior when no command is given)
  36. self.assertEqual(1, result.returncode)
  37. # Should start with usage line
  38. self.assertTrue(result.stdout.startswith("usage: dulwich"))
  39. def test_main_module_help_command(self) -> None:
  40. """Test that running dulwich as a module with help command works."""
  41. result = subprocess.run(
  42. [sys.executable, "-m", "dulwich", "help"],
  43. capture_output=True,
  44. text=True,
  45. )
  46. # Help command should succeed
  47. self.assertEqual(0, result.returncode)
  48. # Check exact output (help goes to stderr)
  49. expected = (
  50. "The dulwich command line tool is currently a very basic frontend for the\n"
  51. "Dulwich python module. For full functionality, please see the API reference.\n"
  52. "\n"
  53. "For a list of supported commands, see 'dulwich help -a'.\n"
  54. )
  55. self.assertEqual(expected, result.stderr)
  56. def test_main_module_no_args(self) -> None:
  57. """Test that running dulwich as a module with no arguments shows help."""
  58. result = subprocess.run(
  59. [sys.executable, "-m", "dulwich"],
  60. capture_output=True,
  61. text=True,
  62. )
  63. # No arguments should show help and exit with code 1
  64. self.assertEqual(1, result.returncode)
  65. # Should start with usage line
  66. self.assertTrue(result.stdout.startswith("usage: dulwich"))