Skip to content

Commit 7ca34e2

Browse files
committed
Simplify tests
1 parent 33005ee commit 7ca34e2

File tree

1 file changed

+53
-103
lines changed

1 file changed

+53
-103
lines changed
Lines changed: 53 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# -*- coding: utf-8 -*-
2-
import pytest
32
import tempfile
43
import os
54
import subprocess
65
import sys
7-
import locale
86
import codecs
97

108
# Compatibility for subprocess.run (added in Python 3.5)
@@ -36,108 +34,91 @@ def safe_decode(data, encoding='utf-8', errors='replace'):
3634

3735

3836
def test_cli_output_flag_with_unicode():
39-
"""Regression test for GitHub issues #2, #57, #59, #68, #113, #123, #129.
40-
37+
"""
4138
Tests that the CLI tool can write Unicode characters to files using --output flag.
42-
This should fail on Windows without proper encoding handling.
4339
"""
4440
# Minimal source with all problematic Unicode characters from reported issues
4541
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
4642

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
43+
source_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
44+
source_file.write(source_code.encode('utf-8'))
45+
source_file.close()
5246

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
47+
output_path = source_file.name + '.min.py'
5648

5749
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)
50+
# Run pyminify CLI with --output flag
6251
result = run_subprocess([
6352
sys.executable, '-m', 'python_minifier',
64-
source_path, '--output', output_path
53+
source_file.name, '--output', output_path
6554
], timeout=30)
6655

67-
# Test should fail if CLI command fails (indicates Windows encoding bug)
6856
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
6957

7058
# Verify the output file was created and contains Unicode characters
71-
# Python 2.7 doesn't support encoding parameter in open()
7259
with codecs.open(output_path, 'r', encoding='utf-8') as f:
7360
minified_content = f.read()
7461

7562
# Verify problematic Unicode characters are preserved
7663
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
77-
# PyPy3: Unicode characters are escaped as \\u escapes
78-
assert "\\u274c" in minified_content # ❌ Issue #113
79-
assert "✓" in minified_content # Issue #129
80-
assert "\\U0001f40d" in minified_content # 🐍 General emoji
81-
assert "Привет" in minified_content # Issue #123
82-
assert "©" in minified_content # Issue #59
83-
assert "∀" in minified_content # Mathematical symbols
64+
# PyPy3: Unicode characters may be escaped as \\u escapes
65+
assert "\\u274c" in minified_content
66+
assert "✓" in minified_content
67+
assert "\\U0001f40d" in minified_content
68+
assert "Привет" in minified_content
69+
assert "©" in minified_content
70+
assert "∀" in minified_content
8471
elif hasattr(sys, 'pypy_version_info') and sys.version_info[0] < 3:
8572
# PyPy2: Unicode characters appear as UTF-8 byte sequences
86-
assert "\\xe2\\x9d\\x8c" in minified_content # ❌ Issue #113
87-
assert "\\xe2\\x9c\\x93" in minified_content # ✓ Issue #129
88-
assert "\\xf0\\x9f\\x90\\x8d" in minified_content # 🐍 General emoji
73+
assert "\\xe2\\x9d\\x8c" in minified_content # ❌
74+
assert "\\xe2\\x9c\\x93" in minified_content # ✓
75+
assert "\\xf0\\x9f\\x90\\x8d" in minified_content # 🐍
8976
elif sys.version_info[0] >= 3:
90-
# CPython 3: Unicode characters should appear literally
91-
assert "❌" in minified_content # Issue #113
92-
assert "✓" in minified_content # Issue #129
93-
assert "🐍" in minified_content # General emoji
94-
assert "Привет" in minified_content # Issue #123
95-
assert "©" in minified_content # Issue #59
96-
assert "∀" in minified_content # Mathematical symbols
77+
# CPython 3: Unicode characters should appear literally
78+
assert "❌" in minified_content
79+
assert "✓" in minified_content
80+
assert "🐍" in minified_content
81+
assert "Привет" in minified_content
82+
assert "©" in minified_content
83+
assert "∀" in minified_content
9784
else:
9885
# Python 2.7: Check for escaped sequences or use Unicode literals
99-
assert u"\\xe2\\x9d\\x8c" in minified_content or u"❌" in minified_content # ❌
100-
assert u"\\xe2\\x9c\\x93" in minified_content or u"✓" in minified_content # ✓
101-
assert u"\\xf0\\x9f\\x90\\x8d" in minified_content or u"🐍" in minified_content # 🐍
86+
assert u"\\xe2\\x9d\\x8c" in minified_content
87+
assert u"\\xe2\\x9c\\x93" in minified_content
88+
assert u"\\xf0\\x9f\\x90\\x8d" in minified_content
10289

10390
finally:
10491
# Cleanup
105-
if os.path.exists(source_path):
106-
os.unlink(source_path)
92+
if os.path.exists(source_file.name):
93+
os.unlink(source_file.name)
10794
if os.path.exists(output_path):
10895
os.unlink(output_path)
10996

11097

11198
def test_cli_in_place_with_unicode():
112-
"""Regression test for --in-place flag with Unicode characters.
113-
114-
Tests GitHub issues #57, #68 where --in-place fails on Windows.
99+
"""
100+
Tests that the CLI tool can write Unicode characters to files using --in-place flag.
115101
"""
116102
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
117103

118-
# Create temporary file
119-
# Python 2.7 doesn't support encoding parameter, so use binary mode
120-
with tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False) as temp_file:
121-
temp_file.write(source_code.encode('utf-8'))
122-
temp_path = temp_file.name
104+
temp_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
105+
temp_file.write(source_code.encode('utf-8'))
106+
temp_file.close()
123107

124108
try:
125109
# Run pyminify with --in-place flag
126110
result = run_subprocess([
127111
sys.executable, '-m', 'python_minifier',
128-
temp_path, '--in-place'
112+
temp_file.name, '--in-place'
129113
], timeout=30)
130114

131-
# Test should fail if CLI command fails (indicates Windows encoding bug)
132115
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
133116

134-
# Verify Unicode characters are preserved in the modified file
135-
# Python 2.7 doesn't support encoding parameter in open()
136-
with codecs.open(temp_path, 'r', encoding='utf-8') as f:
117+
with codecs.open(temp_file.name, 'r', encoding='utf-8') as f:
137118
content = f.read()
138119

139120
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
140-
# PyPy3: Unicode characters are escaped as \\u escapes
121+
# PyPy3: Unicode characters may be escaped as \\u escapes
141122
assert "✓" in content
142123
assert "\\u274c" in content # ❌
143124
assert "\\U0001f40d" in content # 🐍
@@ -158,43 +139,38 @@ def test_cli_in_place_with_unicode():
158139
assert "©" in content
159140
assert "∀" in content
160141
else:
161-
# Python 2.7: Check for escaped sequences or Unicode literals
162-
assert u"\\xe2\\x9c\\x93" in content or u"✓" in content # ✓
163-
assert u"\\xe2\\x9d\\x8c" in content or u"❌" in content # ❌
164-
assert u"\\xf0\\x9f\\x90\\x8d" in content or u"🐍" in content # 🐍
142+
# Python 2.7: Unicode characters appear as escaped sequences
143+
assert "\\xe2\\x9d\\x8c" in content # ❌
144+
assert "\\xe2\\x9c\\x93" in content # ✓
145+
assert "\\xf0\\x9f\\x90\\x8d" in content # 🐍
165146

166147
finally:
167-
if os.path.exists(temp_path):
168-
os.unlink(temp_path)
148+
if os.path.exists(temp_file.name):
149+
os.unlink(temp_file.name)
169150

170151

171152
def test_cli_stdout_with_unicode():
172-
"""Verify that stdout output works fine (as reported in issues).
173-
174-
All GitHub issues mention that stdout output works, only file output fails.
153+
"""
154+
Tests that the CLI tool can write Unicode characters to stdout.
175155
"""
176156
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
177157

178-
# Python 2.7 doesn't support encoding parameter, so use binary mode
179-
with tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False) as temp_file:
180-
temp_file.write(source_code.encode('utf-8'))
181-
temp_path = temp_file.name
158+
temp_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
159+
temp_file.write(source_code.encode('utf-8'))
160+
temp_file.close()
182161

183162
try:
184163
# Run without --output or --in-place (should output to stdout)
185-
# Use our compatibility function to avoid subprocess decoding issues with Windows
186-
# We'll manually decode as UTF-8 to properly handle Unicode characters
187164
result = run_subprocess([
188-
sys.executable, '-m', 'python_minifier', temp_path
165+
sys.executable, '-m', 'python_minifier', temp_file.name
189166
], timeout=30)
190167

191168
assert result.returncode == 0, "Stdout output failed: {}".format(safe_decode(result.stderr))
192169

193-
# Decode stdout and verify Unicode characters are present
194170
stdout_text = safe_decode(result.stdout)
195-
171+
196172
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
197-
# PyPy3: Unicode characters are escaped as \\u escapes
173+
# PyPy3: Unicode characters may be escaped as \\u escapes
198174
assert "\\u274c" in stdout_text # ❌
199175
assert "✓" in stdout_text # ✓
200176
assert "\\U0001f40d" in stdout_text # 🐍
@@ -216,31 +192,5 @@ def test_cli_stdout_with_unicode():
216192
assert "\\xf0\\x9f\\x90\\x8d" in stdout_text # 🐍
217193

218194
finally:
219-
os.unlink(temp_path)
220-
221-
222-
@pytest.mark.skipif(os.name != 'nt', reason="Windows-specific encoding test")
223-
def test_windows_default_encoding_detection():
224-
"""Test to detect Windows default encoding that causes issues."""
225-
226-
# Check what encoding Python would use on Windows for file operations
227-
default_encoding = locale.getpreferredencoding()
228-
229-
# On problematic Windows systems, this is often cp1252, gbk, or similar
230-
print("Windows default encoding: {}".format(default_encoding))
231-
232-
# This test documents the encoding environment for debugging
233-
assert default_encoding is not None
234-
235-
236-
def test_system_encoding_info():
237-
"""Diagnostic test to understand system encoding setup."""
238-
239-
print("System default encoding: {}".format(sys.getdefaultencoding()))
240-
print("Filesystem encoding: {}".format(sys.getfilesystemencoding()))
241-
print("Preferred encoding: {}".format(locale.getpreferredencoding()))
242-
print("Platform: {}".format(sys.platform))
243-
244-
# This test always passes but provides diagnostic information
245-
assert True
246-
195+
if os.path.exists(temp_file.name):
196+
os.unlink(temp_file.name)

0 commit comments

Comments
 (0)