| 1 | import os |
| 2 | import pathlib |
| 3 | import pytest |
| 4 | import subprocess |
| 5 | import sys |
| 6 | import zipfile |
| 7 | |
| 8 | import opencc.cli |
| 9 | |
| 10 | _TEXT_ZIP_CONFIG = """{ |
| 11 | "name": "Python CLI Resource Zip Test", |
| 12 | "segmentation": { |
| 13 | "type": "mmseg", |
| 14 | "dict": { |
| 15 | "type": "text", |
| 16 | "file": "STPhrases.txt" |
| 17 | } |
| 18 | }, |
| 19 | "conversion_chain": [ |
| 20 | { |
| 21 | "dict": { |
| 22 | "type": "group", |
| 23 | "dicts": [ |
| 24 | { |
| 25 | "type": "text", |
| 26 | "file": "STPhrases.txt" |
| 27 | }, |
| 28 | { |
| 29 | "type": "text", |
| 30 | "file": "STCharacters.txt" |
| 31 | } |
| 32 | ] |
| 33 | } |
| 34 | }, |
| 35 | { |
| 36 | "dict": { |
| 37 | "type": "text", |
| 38 | "file": "TWPhrases.txt" |
| 39 | } |
| 40 | } |
| 41 | ] |
| 42 | } |
| 43 | """ |
| 44 | |
| 45 | |
| 46 | def _write_resource_zip(path: pathlib.Path) -> None: |
| 47 | with zipfile.ZipFile(path, 'w', compression=zipfile.ZIP_STORED) as archive: |
| 48 | archive.writestr('s2twp.json', _TEXT_ZIP_CONFIG) |
| 49 | archive.writestr('STPhrases.txt', "打印机\t印表機\n") |
| 50 | archive.writestr('STCharacters.txt', "鼠\t鼠\n标\t標\n") |
| 51 | archive.writestr('TWPhrases.txt', "鼠標\t滑鼠\n") |
| 52 | |
| 53 | |
| 54 | def _write_custom_config(config_path: pathlib.Path, dict_path: pathlib.Path) -> None: |
| 55 | config_path.write_text( |
| 56 | '{\n' |
| 57 | ' "name": "Python CLI Custom Config Test",\n' |
| 58 | ' "segmentation": {\n' |
| 59 | ' "type": "mmseg",\n' |
| 60 | ' "dict": {\n' |
| 61 | ' "type": "text",\n' |
| 62 | f' "file": "{dict_path.name}"\n' |
| 63 | ' }\n' |
| 64 | ' },\n' |
| 65 | ' "conversion_chain": [\n' |
| 66 | ' {\n' |
| 67 | ' "dict": {\n' |
| 68 | ' "type": "text",\n' |
| 69 | f' "file": "{dict_path.name}"\n' |
| 70 | ' }\n' |
| 71 | ' }\n' |
| 72 | ' ]\n' |
| 73 | '}\n', |
| 74 | encoding='utf-8', |
| 75 | ) |
| 76 | dict_path.write_text('鼠标\t滑鼠\n', encoding='utf-8') |
| 77 | |
| 78 | |
| 79 | def _run_cli(monkeypatch, *args): |
| 80 | monkeypatch.setattr(sys, 'argv', ['opencc', *args]) |
| 81 | opencc.cli.main() |
| 82 | |
| 83 | |
| 84 | def _subprocess_env(): |
| 85 | env = os.environ.copy() |
| 86 | pythonpath_entries = [p for p in sys.path if p] |
| 87 | if env.get('PYTHONPATH'): |
| 88 | pythonpath_entries.append(env['PYTHONPATH']) |
| 89 | env['PYTHONPATH'] = os.pathsep.join(pythonpath_entries) |
| 90 | return env |
| 91 | |
| 92 | |
| 93 | def test_cli_skips_tofu_risk_dictionaries_by_default(tmp_path, monkeypatch): |
| 94 | input_path = tmp_path / 'input.txt' |
| 95 | output_path = tmp_path / 'output.txt' |
| 96 | input_path.write_text('㑮', encoding='utf-8') |
| 97 | |
| 98 | _run_cli( |
| 99 | monkeypatch, |
| 100 | '-c', 't2s.json', |
| 101 | '-i', str(input_path), |
| 102 | '-o', str(output_path), |
| 103 | ) |
| 104 | |
| 105 | assert output_path.read_text(encoding='utf-8') == '㑮' |
| 106 | |
| 107 | |
| 108 | def test_cli_include_tofu_risk_dictionaries_flag(tmp_path, monkeypatch): |
| 109 | input_path = tmp_path / 'input.txt' |
| 110 | output_path = tmp_path / 'output.txt' |
| 111 | input_path.write_text('㑮', encoding='utf-8') |
| 112 | |
| 113 | _run_cli( |
| 114 | monkeypatch, |
| 115 | '-c', 't2s.json', |
| 116 | '-i', str(input_path), |
| 117 | '-o', str(output_path), |
| 118 | '--include-tofu-risk-dictionaries', |
| 119 | ) |
| 120 | |
| 121 | assert output_path.read_text(encoding='utf-8') == '𫝈' |
| 122 | |
| 123 | |
| 124 | def test_cli_rejects_same_input_and_output_file(tmp_path, monkeypatch, capsys): |
| 125 | input_path = tmp_path / 'same.txt' |
| 126 | input_path.write_text('汉字', encoding='utf-8') |
| 127 | |
| 128 | with pytest.raises(SystemExit) as exc_info: |
| 129 | _run_cli( |
| 130 | monkeypatch, |
| 131 | '-c', 's2t.json', |
| 132 | '-i', str(input_path), |
| 133 | '-o', str(pathlib.Path(input_path)), |
| 134 | ) |
| 135 | |
| 136 | captured = capsys.readouterr() |
| 137 | assert exc_info.value.code == 1 |
| 138 | assert 'input and output refer to the same file' in captured.err |
| 139 | |
| 140 | |
| 141 | def test_cli_resource_zip_support(tmp_path, monkeypatch): |
| 142 | input_path = tmp_path / 'input.txt' |
| 143 | output_path = tmp_path / 'output.txt' |
| 144 | resource_zip = tmp_path / 'opencc-resources.zip' |
| 145 | input_path.write_text('打印机和鼠标', encoding='utf-8') |
| 146 | _write_resource_zip(resource_zip) |
| 147 | |
| 148 | _run_cli( |
| 149 | monkeypatch, |
| 150 | '-c', 's2twp.json', |
| 151 | '-i', str(input_path), |
| 152 | '-o', str(output_path), |
| 153 | '--resource-zip', str(resource_zip), |
| 154 | ) |
| 155 | |
| 156 | assert output_path.read_text(encoding='utf-8') == '印表機和滑鼠' |
| 157 | |
| 158 | |
| 159 | def test_cli_subprocess_integration(tmp_path): |
| 160 | input_path = tmp_path / 'input.txt' |
| 161 | output_path = tmp_path / 'output.txt' |
| 162 | resource_zip = tmp_path / 'opencc-resources.zip' |
| 163 | input_path.write_text('打印机和鼠标', encoding='utf-8') |
| 164 | _write_resource_zip(resource_zip) |
| 165 | |
| 166 | result = subprocess.run( |
| 167 | [ |
| 168 | sys.executable, |
| 169 | '-m', |
| 170 | 'opencc.cli', |
| 171 | '-c', |
| 172 | 's2twp.json', |
| 173 | '-i', |
| 174 | str(input_path), |
| 175 | '-o', |
| 176 | str(output_path), |
| 177 | '--resource-zip', |
| 178 | str(resource_zip), |
| 179 | ], |
| 180 | capture_output=True, |
| 181 | text=True, |
| 182 | env=_subprocess_env(), |
| 183 | check=False, |
| 184 | ) |
| 185 | |
| 186 | assert result.returncode == 0, result.stderr |
| 187 | assert output_path.read_text(encoding='utf-8') == '印表機和滑鼠' |
| 188 | |
| 189 | |
| 190 | def test_cli_builtin_config_stem_supports_omitting_json(tmp_path, monkeypatch): |
| 191 | input_path = tmp_path / 'input.txt' |
| 192 | output_path = tmp_path / 'output.txt' |
| 193 | input_path.write_text('汉字', encoding='utf-8') |
| 194 | |
| 195 | _run_cli( |
| 196 | monkeypatch, |
| 197 | '-c', 's2t', |
| 198 | '-i', str(input_path), |
| 199 | '-o', str(output_path), |
| 200 | ) |
| 201 | |
| 202 | assert output_path.read_text(encoding='utf-8') == '漢字' |
| 203 | |
| 204 | |
| 205 | def test_cli_custom_config_path_without_json_extension(tmp_path, monkeypatch): |
| 206 | input_path = tmp_path / 'input.txt' |
| 207 | output_path = tmp_path / 'output.txt' |
| 208 | config_path = tmp_path / 'custom-config' |
| 209 | dict_path = tmp_path / 'custom-dict.txt' |
| 210 | input_path.write_text('鼠标', encoding='utf-8') |
| 211 | _write_custom_config(config_path, dict_path) |
| 212 | |
| 213 | _run_cli( |
| 214 | monkeypatch, |
| 215 | '-c', str(config_path), |
| 216 | '-i', str(input_path), |
| 217 | '-o', str(output_path), |
| 218 | ) |
| 219 | |
| 220 | assert output_path.read_text(encoding='utf-8') == '滑鼠' |