|
| 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