Skip to content

Commit 0e2e576

Browse files
committed
Merge remote-tracking branch 'origin/main' into project
2 parents 9932218 + e19eddd commit 0e2e576

File tree

3 files changed

+74
-0
lines changed

3 files changed

+74
-0
lines changed

CHANGES.rst

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@
22
Change log
33
==========
44

5+
WIP
6+
===
7+
8+
- Allow execution as a Python module (@stefansjs in `#400`_)::
9+
10+
python -m qrcode --output qrcode.png "hello world"
11+
12+
.. _#400: https://github.com/lincolnloop/python-qrcode/pull/400
13+
514
8.2 (01 May 2025)
615
=================
716

qrcode/__main__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .console_scripts import main
2+
3+
main()

qrcode/tests/test_module.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import subprocess
2+
import sys
3+
import tempfile
4+
from pathlib import Path
5+
6+
import pytest
7+
8+
try:
9+
from PIL import Image, ImageDraw # noqa: F401
10+
11+
PIL_NOT_INSTALLED = False
12+
except ImportError:
13+
PIL_NOT_INSTALLED = True
14+
15+
16+
def test_module_help():
17+
"""Test that the module can be executed with the help flag."""
18+
result = subprocess.run(
19+
[sys.executable, "-m", "qrcode", "-h"],
20+
capture_output=True,
21+
text=True,
22+
check=False,
23+
)
24+
25+
# Check that the command executed successfully
26+
assert result.returncode == 0
27+
28+
# Check that the help output contains expected information
29+
assert "qr - Convert stdin" in result.stdout
30+
assert "--output" in result.stdout
31+
assert "--factory" in result.stdout
32+
33+
34+
@pytest.mark.skipif(PIL_NOT_INSTALLED, reason="PIL is not installed")
35+
def test_module_generate_qrcode():
36+
"""Test that the module can generate a QR code image."""
37+
with tempfile.TemporaryDirectory() as temp_dir:
38+
output_path = Path(temp_dir) / "qrcode.png"
39+
40+
# Run the command to generate a QR code
41+
result = subprocess.run(
42+
[
43+
sys.executable,
44+
"-m",
45+
"qrcode",
46+
"--output",
47+
str(output_path),
48+
"https://github.com/lincolnloop/python-qrcode",
49+
],
50+
capture_output=True,
51+
text=True,
52+
check=False,
53+
)
54+
55+
# Check that the command executed successfully
56+
assert result.returncode == 0
57+
58+
# Check that the output file was created
59+
assert output_path.exists()
60+
61+
# Check that the file is not empty and is a valid image file
62+
assert output_path.stat().st_size > 0

0 commit comments

Comments
 (0)