Skip to content

Commit 4b35129

Browse files
committed
Test on windows
1 parent d1d2823 commit 4b35129

1 file changed

Lines changed: 190 additions & 0 deletions

File tree

test/test_windows_encoding.py

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
# -*- coding: utf-8 -*-
2+
import pytest
3+
import tempfile
4+
import os
5+
import subprocess
6+
import sys
7+
import locale
8+
import codecs
9+
10+
# Compatibility for subprocess.run (added in Python 3.5)
11+
def run_subprocess(cmd, timeout=None):
12+
"""Cross-platform subprocess runner for Python 2.7+ compatibility."""
13+
if hasattr(subprocess, 'run'):
14+
# Python 3.5+
15+
return subprocess.run(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, timeout=timeout)
16+
else:
17+
# Python 2.7, 3.3, 3.4
18+
popen = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
19+
stdout, stderr = popen.communicate()
20+
# Create a simple result object similar to subprocess.CompletedProcess
21+
class Result:
22+
def __init__(self, returncode, stdout, stderr):
23+
self.returncode = returncode
24+
self.stdout = stdout
25+
self.stderr = stderr
26+
return Result(popen.returncode, stdout, stderr)
27+
28+
def safe_decode(data, encoding='utf-8', errors='replace'):
29+
"""Safe decode for Python 2.7/3.x compatibility."""
30+
if isinstance(data, bytes):
31+
try:
32+
return data.decode(encoding, errors)
33+
except UnicodeDecodeError:
34+
return data.decode(encoding, 'replace')
35+
return data
36+
37+
38+
def test_cli_output_flag_with_unicode():
39+
"""Regression test for GitHub issues #2, #57, #59, #68, #113, #123, #129.
40+
41+
Tests that the CLI tool can write Unicode characters to files using --output flag.
42+
This should fail on Windows without proper encoding handling.
43+
"""
44+
# Minimal source with all problematic Unicode characters from reported issues
45+
source_code = u'print("❌ ✓ 🐍 Привет © ∀")'
46+
47+
# Create temporary source file
48+
# Python 2.7 doesn't support encoding parameter, so use binary mode
49+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False) as source_file:
50+
source_file.write(source_code.encode('utf-8'))
51+
source_path = source_file.name
52+
53+
# Create temporary output file path
54+
with tempfile.NamedTemporaryFile(mode='w', suffix='.min.py', delete=False) as output_file:
55+
output_path = output_file.name
56+
57+
try:
58+
# Remove output file so pyminify can create it
59+
os.unlink(output_path)
60+
61+
# Run pyminify CLI with --output flag (this should reproduce Windows encoding errors)
62+
result = run_subprocess([
63+
sys.executable, '-m', 'python_minifier',
64+
source_path, '--output', output_path
65+
], timeout=30)
66+
67+
# Test should fail if CLI command fails (indicates Windows encoding bug)
68+
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
69+
70+
# Verify the output file was created and contains Unicode characters
71+
# Python 2.7 doesn't support encoding parameter in open()
72+
with codecs.open(output_path, 'r', encoding='utf-8') as f:
73+
minified_content = f.read()
74+
75+
# Verify problematic Unicode characters are preserved
76+
assert "❌" in minified_content # Issue #113
77+
assert "✓" in minified_content # Issue #129
78+
assert "🐍" in minified_content # General emoji
79+
assert "Привет" in minified_content # Issue #123
80+
assert "©" in minified_content # Issue #59
81+
assert "∀" in minified_content # Mathematical symbols
82+
83+
finally:
84+
# Cleanup
85+
if os.path.exists(source_path):
86+
os.unlink(source_path)
87+
if os.path.exists(output_path):
88+
os.unlink(output_path)
89+
90+
91+
def test_cli_in_place_with_unicode():
92+
"""Regression test for --in-place flag with Unicode characters.
93+
94+
Tests GitHub issues #57, #68 where --in-place fails on Windows.
95+
"""
96+
source_code = u'print("❌ ✓ 🐍 Привет © ∀")'
97+
98+
# Create temporary file
99+
# Python 2.7 doesn't support encoding parameter, so use binary mode
100+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False) as temp_file:
101+
temp_file.write(source_code.encode('utf-8'))
102+
temp_path = temp_file.name
103+
104+
try:
105+
# Run pyminify with --in-place flag
106+
result = run_subprocess([
107+
sys.executable, '-m', 'python_minifier',
108+
temp_path, '--in-place'
109+
], timeout=30)
110+
111+
# Test should fail if CLI command fails (indicates Windows encoding bug)
112+
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
113+
114+
# Verify Unicode characters are preserved in the modified file
115+
# Python 2.7 doesn't support encoding parameter in open()
116+
with codecs.open(temp_path, 'r', encoding='utf-8') as f:
117+
content = f.read()
118+
119+
assert "✓" in content
120+
assert "❌" in content
121+
assert "🐍" in content
122+
assert "Привет" in content
123+
assert "©" in content
124+
assert "∀" in content
125+
126+
finally:
127+
if os.path.exists(temp_path):
128+
os.unlink(temp_path)
129+
130+
131+
def test_cli_stdout_with_unicode():
132+
"""Verify that stdout output works fine (as reported in issues).
133+
134+
All GitHub issues mention that stdout output works, only file output fails.
135+
"""
136+
source_code = u'print("❌ ✓ 🐍 Привет © ∀")'
137+
138+
# Python 2.7 doesn't support encoding parameter, so use binary mode
139+
with tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False) as temp_file:
140+
temp_file.write(source_code.encode('utf-8'))
141+
temp_path = temp_file.name
142+
143+
try:
144+
# Run without --output or --in-place (should output to stdout)
145+
# Use our compatibility function to avoid subprocess decoding issues with Windows
146+
# We'll manually decode as UTF-8 to properly handle Unicode characters
147+
result = run_subprocess([
148+
sys.executable, '-m', 'python_minifier', temp_path
149+
], timeout=30)
150+
151+
assert result.returncode == 0, "Stdout output failed: {}".format(safe_decode(result.stderr))
152+
153+
# Decode stdout and verify Unicode characters are present
154+
stdout_text = safe_decode(result.stdout)
155+
assert "❌" in stdout_text
156+
assert "✓" in stdout_text
157+
assert "🐍" in stdout_text
158+
assert "Привет" in stdout_text
159+
assert "©" in stdout_text
160+
assert "∀" in stdout_text
161+
162+
finally:
163+
os.unlink(temp_path)
164+
165+
166+
@pytest.mark.skipif(os.name != 'nt', reason="Windows-specific encoding test")
167+
def test_windows_default_encoding_detection():
168+
"""Test to detect Windows default encoding that causes issues."""
169+
170+
# Check what encoding Python would use on Windows for file operations
171+
default_encoding = locale.getpreferredencoding()
172+
173+
# On problematic Windows systems, this is often cp1252, gbk, or similar
174+
print("Windows default encoding: {}".format(default_encoding))
175+
176+
# This test documents the encoding environment for debugging
177+
assert default_encoding is not None
178+
179+
180+
def test_system_encoding_info():
181+
"""Diagnostic test to understand system encoding setup."""
182+
183+
print("System default encoding: {}".format(sys.getdefaultencoding()))
184+
print("Filesystem encoding: {}".format(sys.getfilesystemencoding()))
185+
print("Preferred encoding: {}".format(locale.getpreferredencoding()))
186+
print("Platform: {}".format(sys.platform))
187+
188+
# This test always passes but provides diagnostic information
189+
assert True
190+

0 commit comments

Comments
 (0)