-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_handle.py
More file actions
367 lines (238 loc) · 11.6 KB
/
test_handle.py
File metadata and controls
367 lines (238 loc) · 11.6 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
"""Tests for handle.py charset conversion logic."""
import os
import sys
import tempfile
import pytest
import handle
# ---------------------------------------------------------------------------
# get_language
# ---------------------------------------------------------------------------
def test_get_language_simple():
assert handle.get_language(os.path.join("russian", "subdir")) == "russian"
def test_get_language_top_level():
assert handle.get_language("english") == "english"
def test_get_language_locale_code():
assert handle.get_language(os.path.join("ru_RU", "subdir")) == "ru_RU"
# ---------------------------------------------------------------------------
# get_filename
# ---------------------------------------------------------------------------
def test_get_filename_lowercases():
assert handle.get_filename("/path/to/Setup.TRA") == "setup.tra"
def test_get_filename_already_lower():
assert handle.get_filename("/path/component.tra") == "component.tra"
# ---------------------------------------------------------------------------
# get_win_encoding
# ---------------------------------------------------------------------------
def test_get_win_encoding_russian_console():
# Russian console files use DOS cp866
assert handle.get_win_encoding("russian", "setup.tra") == "cp866"
assert handle.get_win_encoding("russian", "install.tra") == "cp866"
def test_get_win_encoding_ukrainian_console():
assert handle.get_win_encoding("ukrainian", "setup.tra") == "cp866"
def test_get_win_encoding_ru_RU_console():
assert handle.get_win_encoding("ru_RU", "setup.tra") == "cp866"
def test_get_win_encoding_russian_non_console():
assert handle.get_win_encoding("russian", "component.tra") == "cp1251"
def test_get_win_encoding_english():
assert handle.get_win_encoding("english", "component.tra") == "cp1252"
def test_get_win_encoding_case_insensitive():
assert handle.get_win_encoding("Russian", "component.tra") == "cp1251"
assert handle.get_win_encoding("English", "component.tra") == "cp1252"
def test_get_win_encoding_japanese():
assert handle.get_win_encoding("japanese", "component.tra") == "cp932"
def test_get_win_encoding_ee_tra():
assert handle.get_win_encoding("russian", "ee.tra") == "utf-8"
def test_get_win_encoding_underscore_ee_tra():
assert handle.get_win_encoding("russian", "component_ee.tra") == "utf-8"
def test_get_win_encoding_unknown_language_raises():
with pytest.raises(ValueError, match="klingon"):
handle.get_win_encoding("klingon", "component.tra")
def test_get_win_encoding_locale_codes():
assert handle.get_win_encoding("zh_CN", "component.tra") == "cp936"
assert handle.get_win_encoding("cs_CZ", "component.tra") == "cp1250"
assert handle.get_win_encoding("de_DE", "component.tra") == "cp1252"
# ---------------------------------------------------------------------------
# get_src_encoding
# ---------------------------------------------------------------------------
def test_get_src_encoding_console_always_utf8():
assert handle.get_src_encoding("russian", "setup.tra", from_utf8=False) == "utf-8"
assert handle.get_src_encoding("russian", "install.tra", from_utf8=False) == "utf-8"
def test_get_src_encoding_from_utf8_flag():
assert handle.get_src_encoding("russian", "component.tra", from_utf8=True) == "utf-8"
def test_get_src_encoding_ansi_source():
assert handle.get_src_encoding("russian", "component.tra", from_utf8=False) == "cp1251"
def test_get_src_encoding_english_ansi():
assert handle.get_src_encoding("english", "component.tra", from_utf8=False) == "cp1252"
# ---------------------------------------------------------------------------
# get_dst_encoding
# ---------------------------------------------------------------------------
def test_get_dst_encoding_console_utf8_by_default():
assert handle.get_dst_encoding("russian", "setup.tra", from_utf8=False, split_console=False) == "utf-8"
assert handle.get_dst_encoding("english", "install.tra", from_utf8=False, split_console=False) == "utf-8"
def test_get_dst_encoding_console_split_russian():
# split_console=True → ANSI; Russian console = cp866
assert handle.get_dst_encoding("russian", "setup.tra", from_utf8=False, split_console=True) == "cp866"
def test_get_dst_encoding_console_split_english():
assert handle.get_dst_encoding("english", "setup.tra", from_utf8=False, split_console=True) == "cp1252"
def test_get_dst_encoding_non_console_to_utf8():
assert handle.get_dst_encoding("russian", "component.tra", from_utf8=False, split_console=False) == "utf-8"
def test_get_dst_encoding_from_utf8_reverse():
assert handle.get_dst_encoding("russian", "component.tra", from_utf8=True, split_console=False) == "cp1251"
# ---------------------------------------------------------------------------
# get_relpath / get_dirpath
# ---------------------------------------------------------------------------
def test_get_relpath():
relpath = handle.get_relpath("/tra/russian/setup.tra", "/tra")
assert relpath == os.path.join("russian", "setup.tra")
def test_get_dirpath():
assert handle.get_dirpath(os.path.join("russian", "setup.tra")) == "russian"
def test_get_dirpath_top_level():
assert handle.get_dirpath("setup.tra") == ""
# ---------------------------------------------------------------------------
# get_os_path
# ---------------------------------------------------------------------------
def test_get_os_path_win32():
result = handle.get_os_path(os.path.join("russian", "setup.tra"), "win32")
assert result == os.path.join("russian", "setup-win32.tra")
def test_get_os_path_unix():
result = handle.get_os_path(os.path.join("english", "install.tra"), "unix")
assert result == os.path.join("english", "install-unix.tra")
def test_get_os_path_osx():
result = handle.get_os_path(os.path.join("french", "setup.tra"), "osx")
assert result == os.path.join("french", "setup-osx.tra")
# ---------------------------------------------------------------------------
# find_files
# ---------------------------------------------------------------------------
def test_find_files_finds_tra(tmp_path):
(tmp_path / "russian").mkdir()
(tmp_path / "russian" / "setup.tra").write_text("@1 = ~hello~")
(tmp_path / "russian" / "component.tra").write_text("@1 = ~world~")
(tmp_path / "russian" / "readme.txt").write_text("ignore me")
result = handle.find_files(str(tmp_path), "tra")
assert len(result) == 2
assert all(f.endswith(".tra") for f in result)
def test_find_files_sorted(tmp_path):
(tmp_path / "b.tra").write_text("")
(tmp_path / "a.tra").write_text("")
result = handle.find_files(str(tmp_path), "tra")
assert result == sorted(result)
def test_find_files_recursive(tmp_path):
(tmp_path / "sub").mkdir()
(tmp_path / "sub" / "nested.tra").write_text("")
(tmp_path / "top.tra").write_text("")
result = handle.find_files(str(tmp_path), "tra")
assert len(result) == 2
def test_find_files_case_insensitive_ext(tmp_path):
(tmp_path / "file.TRA").write_text("")
result = handle.find_files(str(tmp_path), "tra")
assert len(result) == 1
def test_find_files_empty_dir(tmp_path):
assert handle.find_files(str(tmp_path), "tra") == []
# ---------------------------------------------------------------------------
# resave_file
# ---------------------------------------------------------------------------
def test_resave_file_converts_encoding(tmp_path):
src = tmp_path / "src" / "file.tra"
src.parent.mkdir()
src.write_text("@1 = ~Привет~", encoding="cp1251")
dst = tmp_path / "out" / "file.tra"
handle.resave_file(str(src), "cp1251", str(dst), "utf-8")
content = dst.read_text(encoding="utf-8")
assert "Привет" in content
assert content.startswith("// Do not edit.")
def test_resave_file_creates_dst_dir(tmp_path):
src = tmp_path / "file.tra"
src.write_text("@1 = ~hello~", encoding="utf-8")
dst = tmp_path / "deep" / "nested" / "file.tra"
handle.resave_file(str(src), "utf-8", str(dst), "utf-8")
assert dst.exists()
def test_resave_file_prepends_comment(tmp_path):
src = tmp_path / "file.tra"
src.write_text("@1 = ~hello~", encoding="utf-8")
dst = tmp_path / "out.tra"
handle.resave_file(str(src), "utf-8", str(dst), "utf-8")
content = dst.read_text(encoding="utf-8")
assert content.startswith(handle.COMMENT_NO_MANUAL)
def test_resave_file_dst_path_is_file_not_dir(tmp_path):
"""If dst path exists as a file (not dir), it should be overwritten cleanly."""
src = tmp_path / "src.tra"
src.write_text("new content", encoding="utf-8")
dst = tmp_path / "dst.tra"
dst.write_text("old content", encoding="utf-8")
handle.resave_file(str(src), "utf-8", str(dst), "utf-8")
assert "new content" in dst.read_text(encoding="utf-8")
# ---------------------------------------------------------------------------
# main — integration
# ---------------------------------------------------------------------------
def test_main_basic_conversion(tmp_path, monkeypatch):
tra_path = tmp_path / "tra"
out_path = tmp_path / "out"
(tra_path / "russian").mkdir(parents=True)
(tra_path / "russian" / "component.tra").write_text("@1 = ~Привет~", encoding="cp1251")
monkeypatch.setattr(
sys,
"argv",
["handle.py", "--tra-path", str(tra_path), "--out-path", str(out_path)],
)
handle.main()
result = (out_path / "russian" / "component.tra").read_text(encoding="utf-8")
assert "Привет" in result
assert result.startswith("// Do not edit.")
def test_main_console_file_always_utf8(tmp_path, monkeypatch):
tra_path = tmp_path / "tra"
out_path = tmp_path / "out"
(tra_path / "russian").mkdir(parents=True)
(tra_path / "russian" / "setup.tra").write_text("@1 = ~Привет~", encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
["handle.py", "--tra-path", str(tra_path), "--out-path", str(out_path)],
)
handle.main()
result = (out_path / "russian" / "setup.tra").read_text(encoding="utf-8")
assert "Привет" in result
def test_main_split_console(tmp_path, monkeypatch):
tra_path = tmp_path / "tra"
out_path = tmp_path / "out"
(tra_path / "russian").mkdir(parents=True)
(tra_path / "russian" / "setup.tra").write_text("@1 = ~Привет~", encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
[
"handle.py",
"--tra-path",
str(tra_path),
"--out-path",
str(out_path),
"--split-console",
],
)
handle.main()
assert (out_path / "russian" / "setup-win32.tra").exists()
assert (out_path / "russian" / "setup-unix.tra").exists()
assert (out_path / "russian" / "setup-osx.tra").exists()
win32 = (out_path / "russian" / "setup-win32.tra").read_text(encoding="cp866")
assert "Привет" in win32
unix = (out_path / "russian" / "setup-unix.tra").read_text(encoding="utf-8")
assert "Привет" in unix
def test_main_from_utf8(tmp_path, monkeypatch):
tra_path = tmp_path / "tra"
out_path = tmp_path / "out"
(tra_path / "russian").mkdir(parents=True)
(tra_path / "russian" / "component.tra").write_text("@1 = ~Привет~", encoding="utf-8")
monkeypatch.setattr(
sys,
"argv",
[
"handle.py",
"--tra-path",
str(tra_path),
"--out-path",
str(out_path),
"--from-utf8",
],
)
handle.main()
result = (out_path / "russian" / "component.tra").read_text(encoding="cp1251")
assert "Привет" in result