-
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathtest_unicode_cli.py
More file actions
178 lines (152 loc) · 7.07 KB
/
test_unicode_cli.py
File metadata and controls
178 lines (152 loc) · 7.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# -*- coding: utf-8 -*-
import tempfile
import os
import sys
import codecs
from subprocess_compat import run_subprocess, safe_decode
def test_cli_output_flag_with_unicode():
"""
Tests that the CLI tool can write Unicode characters to files using --output flag.
"""
# Minimal source with all problematic Unicode characters from reported issues
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
source_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
source_file.write(source_code.encode('utf-8'))
source_file.close()
output_path = source_file.name + '.min.py'
try:
# Run pyminify CLI with --output flag
result = run_subprocess([
sys.executable, '-m', 'python_minifier',
source_file.name, '--output', output_path
], timeout=30)
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
# Verify the output file was created and contains Unicode characters
if sys.version_info[0] >= 3:
with open(output_path, 'r', encoding='utf-8') as f:
minified_content = f.read()
else:
with codecs.open(output_path, 'r', encoding='utf-8') as f:
minified_content = f.read()
# Verify problematic Unicode characters are preserved
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
# PyPy3: Unicode characters may be escaped as \\u escapes
assert "\\u274c" in minified_content
assert "✓" in minified_content
assert "\\U0001f40d" in minified_content
assert "Привет" in minified_content
assert "©" in minified_content
assert "∀" in minified_content
elif hasattr(sys, 'pypy_version_info') and sys.version_info[0] < 3:
# PyPy2: Unicode characters appear as UTF-8 byte sequences
assert "\\xe2\\x9d\\x8c" in minified_content # ❌
assert "\\xe2\\x9c\\x93" in minified_content # ✓
assert "\\xf0\\x9f\\x90\\x8d" in minified_content # 🐍
elif sys.version_info[0] >= 3:
# CPython 3: Unicode characters should appear literally
assert "❌" in minified_content
assert "✓" in minified_content
assert "🐍" in minified_content
assert "Привет" in minified_content
assert "©" in minified_content
assert "∀" in minified_content
else:
# Python 2.7: Check for escaped sequences or use Unicode literals
assert u"\\xe2\\x9d\\x8c" in minified_content
assert u"\\xe2\\x9c\\x93" in minified_content
assert u"\\xf0\\x9f\\x90\\x8d" in minified_content
finally:
# Cleanup
if os.path.exists(source_file.name):
os.unlink(source_file.name)
if os.path.exists(output_path):
os.unlink(output_path)
def test_cli_in_place_with_unicode():
"""
Tests that the CLI tool can write Unicode characters to files using --in-place flag.
"""
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
temp_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
temp_file.write(source_code.encode('utf-8'))
temp_file.close()
try:
# Run pyminify with --in-place flag
result = run_subprocess([
sys.executable, '-m', 'python_minifier',
temp_file.name, '--in-place'
], timeout=30)
assert result.returncode == 0, "CLI failed with encoding error: {}".format(safe_decode(result.stderr))
if sys.version_info[0] >= 3:
with open(temp_file.name, 'r', encoding='utf-8') as f:
content = f.read()
else:
with codecs.open(temp_file.name, 'r', encoding='utf-8') as f:
content = f.read()
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
# PyPy3: Unicode characters may be escaped as \\u escapes
assert "✓" in content
assert "\\u274c" in content # ❌
assert "\\U0001f40d" in content # 🐍
assert "Привет" in content
assert "©" in content
assert "∀" in content
elif hasattr(sys, 'pypy_version_info') and sys.version_info[0] < 3:
# PyPy2: Unicode characters appear as UTF-8 byte sequences
assert "\\xe2\\x9c\\x93" in content # ✓
assert "\\xe2\\x9d\\x8c" in content # ❌
assert "\\xf0\\x9f\\x90\\x8d" in content # 🐍
elif sys.version_info[0] >= 3:
# CPython 3: Unicode characters should appear literally
assert "✓" in content
assert "❌" in content
assert "🐍" in content
assert "Привет" in content
assert "©" in content
assert "∀" in content
else:
# Python 2.7: Unicode characters appear as escaped sequences
assert "\\xe2\\x9d\\x8c" in content # ❌
assert "\\xe2\\x9c\\x93" in content # ✓
assert "\\xf0\\x9f\\x90\\x8d" in content # 🐍
finally:
if os.path.exists(temp_file.name):
os.unlink(temp_file.name)
def test_cli_stdout_with_unicode():
"""
Tests that the CLI tool can write Unicode characters to stdout.
"""
source_code = u'print(u"❌ ✓ 🐍 Привет © ∀")'
temp_file = tempfile.NamedTemporaryFile(mode='wb', suffix='.py', delete=False)
temp_file.write(source_code.encode('utf-8'))
temp_file.close()
try:
# Run without --output or --in-place (should output to stdout)
result = run_subprocess([
sys.executable, '-m', 'python_minifier', temp_file.name
], timeout=30)
assert result.returncode == 0, "Stdout output failed: {}".format(safe_decode(result.stderr))
stdout_text = safe_decode(result.stdout)
if hasattr(sys, 'pypy_version_info') and sys.version_info[0] >= 3:
# PyPy3: Unicode characters may be escaped as \\u escapes
assert "\\u274c" in stdout_text # ❌
assert "✓" in stdout_text # ✓
assert "\\U0001f40d" in stdout_text # 🐍
assert "Привет" in stdout_text # Привет
assert "©" in stdout_text # ©
assert "∀" in stdout_text # ∀
elif sys.version_info[0] >= 3:
# CPython 3: Unicode characters should appear literally
assert "❌" in stdout_text
assert "✓" in stdout_text
assert "🐍" in stdout_text
assert "Привет" in stdout_text
assert "©" in stdout_text
assert "∀" in stdout_text
else:
# Python 2.7: Unicode characters appear as escaped sequences
assert "\\xe2\\x9d\\x8c" in stdout_text # ❌
assert "\\xe2\\x9c\\x93" in stdout_text # ✓
assert "\\xf0\\x9f\\x90\\x8d" in stdout_text # 🐍
finally:
if os.path.exists(temp_file.name):
os.unlink(temp_file.name)