anybadge cli accessible via python -m (#79)

* anybadge cli can be called via python -m

* fix: anybadge should be shown in cli usage string

* also allow executing anybadge.server as module

* test: same output regardless of how cli is called
This commit is contained in:
Jannis Mainczyk
2022-08-24 21:08:08 +02:00
committed by GitHub
parent 2af319bc6b
commit 2bd52f41cd
4 changed files with 33 additions and 0 deletions

9
anybadge/__main__.py Normal file
View File

@@ -0,0 +1,9 @@
from .cli import main
if __name__ == "__main__":
# ensure that `anybadge` shows in usage (not __main__.py)
import sys
sys.argv[0] = "anybadge"
main()

View File

@@ -0,0 +1,9 @@
from .cli import main
if __name__ == "__main__":
# ensure that `anybadge-server` shows in usage (not `__main__.py`)
import sys
sys.argv[0] = "anybadge-server"
main()

View File

@@ -1,3 +1,4 @@
import subprocess
from pathlib import Path
from unittest import TestCase
from anybadge import Badge
@@ -403,3 +404,9 @@ class TestAnybadge(TestCase):
semver=True,
)
self.assertEqual("orange", badge.badge_color)
def test_module_same_output_as_main_cli(self):
"""Test that `python -m anybadge` is equivalent to calling `anybadge` directly."""
output_module = subprocess.check_output(["python", "-m", "anybadge", "--help"])
output_script = subprocess.check_output(["anybadge", "--help"])
self.assertEqual(output_module, output_script)

View File

@@ -43,3 +43,11 @@ class TestAnybadgeServer(TestCase):
self.assertTrue(
response.content.startswith(b'<?xml version="1.0" encoding="UTF-8"?>\n<svg')
)
def test_server_module_same_output_as_server_cli(self):
"""Test that `python -m anybadge.server` is equivalent to calling `anybadge-server` directly."""
output_module = subprocess.check_output(
["python", "-m", "anybadge.server", "--help"]
)
output_script = subprocess.check_output(["anybadge-server", "--help"])
self.assertEqual(output_module, output_script)