|
| 1 | +import pytest |
| 2 | +import tempfile |
| 3 | +import os |
| 4 | +import subprocess |
| 5 | +import sys |
| 6 | +import locale |
| 7 | + |
| 8 | + |
| 9 | +def test_cli_output_flag_with_unicode(): |
| 10 | + """Regression test for GitHub issues #2, #57, #59, #68, #113, #123, #129. |
| 11 | +
|
| 12 | + Tests that the CLI tool can write Unicode characters to files using --output flag. |
| 13 | + This should fail on Windows without proper encoding handling. |
| 14 | + """ |
| 15 | + # Minimal source with all problematic Unicode characters from reported issues |
| 16 | + source_code = 'print("❌ ✓ 🐍 Привет © ∀")' |
| 17 | + |
| 18 | + # Create temporary source file |
| 19 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as source_file: |
| 20 | + source_file.write(source_code) |
| 21 | + source_path = source_file.name |
| 22 | + |
| 23 | + # Create temporary output file path |
| 24 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.min.py', delete=False) as output_file: |
| 25 | + output_path = output_file.name |
| 26 | + |
| 27 | + try: |
| 28 | + # Remove output file so pyminify can create it |
| 29 | + os.unlink(output_path) |
| 30 | + |
| 31 | + # Run pyminify CLI with --output flag (this should reproduce Windows encoding errors) |
| 32 | + result = subprocess.run([ |
| 33 | + sys.executable, '-m', 'python_minifier', |
| 34 | + source_path, '--output', output_path |
| 35 | + ], capture_output=True, text=True, timeout=30) |
| 36 | + |
| 37 | + if result.returncode != 0: |
| 38 | + # This is expected on Windows with encoding issues |
| 39 | + print(f"CLI failed as expected on Windows: {result.stderr}") |
| 40 | + assert "UnicodeEncodeError" in result.stderr or "charmap" in result.stderr or "gbk" in result.stderr |
| 41 | + else: |
| 42 | + # If it succeeds, verify the output file contains Unicode characters |
| 43 | + with open(output_path, 'r', encoding='utf-8') as f: |
| 44 | + minified_content = f.read() |
| 45 | + |
| 46 | + # Verify problematic Unicode characters are preserved |
| 47 | + assert "❌" in minified_content # Issue #113 |
| 48 | + assert "✓" in minified_content # Issue #129 |
| 49 | + assert "🐍" in minified_content # General emoji |
| 50 | + assert "Привет мир" in minified_content # Issue #123 |
| 51 | + assert "©" in minified_content # Issue #59 |
| 52 | + assert "∀" in minified_content # Mathematical symbols |
| 53 | + |
| 54 | + finally: |
| 55 | + # Cleanup |
| 56 | + if os.path.exists(source_path): |
| 57 | + os.unlink(source_path) |
| 58 | + if os.path.exists(output_path): |
| 59 | + os.unlink(output_path) |
| 60 | + |
| 61 | + |
| 62 | +def test_cli_in_place_with_unicode(): |
| 63 | + """Regression test for --in-place flag with Unicode characters. |
| 64 | +
|
| 65 | + Tests GitHub issues #57, #68 where --in-place fails on Windows. |
| 66 | + """ |
| 67 | + source_code = 'print("❌ ✓ 🐍 Привет © ∀")' |
| 68 | + |
| 69 | + # Create temporary file |
| 70 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as temp_file: |
| 71 | + temp_file.write(source_code) |
| 72 | + temp_path = temp_file.name |
| 73 | + |
| 74 | + try: |
| 75 | + # Run pyminify with --in-place flag |
| 76 | + result = subprocess.run([ |
| 77 | + sys.executable, '-m', 'python_minifier', |
| 78 | + temp_path, '--in-place' |
| 79 | + ], capture_output=True, text=True, timeout=30) |
| 80 | + |
| 81 | + if result.returncode != 0: |
| 82 | + # Expected failure on Windows |
| 83 | + print(f"--in-place failed as expected on Windows: {result.stderr}") |
| 84 | + assert "UnicodeEncodeError" in result.stderr or "charmap" in result.stderr or "gbk" in result.stderr |
| 85 | + else: |
| 86 | + # If successful, verify Unicode characters are preserved |
| 87 | + with open(temp_path, 'r', encoding='utf-8') as f: |
| 88 | + content = f.read() |
| 89 | + |
| 90 | + assert "✓" in content |
| 91 | + assert "❌" in content |
| 92 | + assert "🐍" in content |
| 93 | + assert "Привет" in content |
| 94 | + assert "©" in content |
| 95 | + |
| 96 | + finally: |
| 97 | + if os.path.exists(temp_path): |
| 98 | + os.unlink(temp_path) |
| 99 | + |
| 100 | + |
| 101 | +def test_cli_stdout_works_with_unicode(): |
| 102 | + """Verify that stdout output works fine (as reported in issues). |
| 103 | +
|
| 104 | + All GitHub issues mention that stdout output works, only file output fails. |
| 105 | + """ |
| 106 | + source_code = 'print("❌ ✓ 🐍 Привет © ∀")' |
| 107 | + |
| 108 | + with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False, encoding='utf-8') as temp_file: |
| 109 | + temp_file.write(source_code) |
| 110 | + temp_path = temp_file.name |
| 111 | + |
| 112 | + try: |
| 113 | + # Run without --output or --in-place (should output to stdout) |
| 114 | + result = subprocess.run([ |
| 115 | + sys.executable, '-m', 'python_minifier', temp_path |
| 116 | + ], capture_output=True, text=True, timeout=30) |
| 117 | + |
| 118 | + # This should always succeed (as confirmed by GitHub issues) |
| 119 | + assert result.returncode == 0, f"Stdout output failed: {result.stderr}" |
| 120 | + |
| 121 | + # Verify Unicode characters in stdout |
| 122 | + assert "❌" in result.stdout |
| 123 | + assert "✓" in result.stdout |
| 124 | + assert "🐍" in result.stdout |
| 125 | + assert "Привет" in result.stdout |
| 126 | + assert "©" in result.stdout |
| 127 | + |
| 128 | + finally: |
| 129 | + os.unlink(temp_path) |
| 130 | + |
| 131 | + |
| 132 | +@pytest.mark.skipif(os.name != 'nt', reason="Windows-specific encoding test") |
| 133 | +def test_windows_default_encoding_detection(): |
| 134 | + """Test to detect Windows default encoding that causes issues.""" |
| 135 | + |
| 136 | + # Check what encoding Python would use on Windows for file operations |
| 137 | + default_encoding = locale.getpreferredencoding() |
| 138 | + |
| 139 | + # On problematic Windows systems, this is often cp1252, gbk, or similar |
| 140 | + print(f"Windows default encoding: {default_encoding}") |
| 141 | + |
| 142 | + # This test documents the encoding environment for debugging |
| 143 | + assert default_encoding is not None |
| 144 | + |
| 145 | + |
| 146 | +def test_system_encoding_info(): |
| 147 | + """Diagnostic test to understand system encoding setup.""" |
| 148 | + |
| 149 | + print(f"System default encoding: {sys.getdefaultencoding()}") |
| 150 | + print(f"Filesystem encoding: {sys.getfilesystemencoding()}") |
| 151 | + print(f"Preferred encoding: {locale.getpreferredencoding()}") |
| 152 | + print(f"Platform: {sys.platform}") |
| 153 | + |
| 154 | + # This test always passes but provides diagnostic information |
| 155 | + assert True |
| 156 | + |
0 commit comments