diff --git a/CHANGES.rst b/CHANGES.rst index b8c7e492..1b7e74bf 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -2,6 +2,15 @@ Change log ========== +WIP +=== + +- Allow execution as a Python module (@stefansjs in `#400`_):: + + python -m qrcode --output qrcode.png "hello world" + +.. _#400: https://github.com/lincolnloop/python-qrcode/pull/400 + 8.2 (01 May 2025) ================= diff --git a/qrcode/__main__.py b/qrcode/__main__.py new file mode 100644 index 00000000..8026a67f --- /dev/null +++ b/qrcode/__main__.py @@ -0,0 +1,3 @@ +from .console_scripts import main + +main() diff --git a/qrcode/tests/test_module.py b/qrcode/tests/test_module.py new file mode 100644 index 00000000..82785b0c --- /dev/null +++ b/qrcode/tests/test_module.py @@ -0,0 +1,62 @@ +import subprocess +import sys +import tempfile +from pathlib import Path + +import pytest + +try: + from PIL import Image, ImageDraw # noqa: F401 + + PIL_NOT_INSTALLED = False +except ImportError: + PIL_NOT_INSTALLED = True + + +def test_module_help(): + """Test that the module can be executed with the help flag.""" + result = subprocess.run( + [sys.executable, "-m", "qrcode", "-h"], + capture_output=True, + text=True, + check=False, + ) + + # Check that the command executed successfully + assert result.returncode == 0 + + # Check that the help output contains expected information + assert "qr - Convert stdin" in result.stdout + assert "--output" in result.stdout + assert "--factory" in result.stdout + + +@pytest.mark.skipif(PIL_NOT_INSTALLED, reason="PIL is not installed") +def test_module_generate_qrcode(): + """Test that the module can generate a QR code image.""" + with tempfile.TemporaryDirectory() as temp_dir: + output_path = Path(temp_dir) / "qrcode.png" + + # Run the command to generate a QR code + result = subprocess.run( + [ + sys.executable, + "-m", + "qrcode", + "--output", + str(output_path), + "https://github.com/lincolnloop/python-qrcode", + ], + capture_output=True, + text=True, + check=False, + ) + + # Check that the command executed successfully + assert result.returncode == 0 + + # Check that the output file was created + assert output_path.exists() + + # Check that the file is not empty and is a valid image file + assert output_path.stat().st_size > 0