(
tmp_path_factory: pytest.TempPathFactory,
monkeypatch: pytest.MonkeyPatch,
data_file_encoding: str,
)
| 234 | ["utf-8", "utf-8-sig", "utf-16-le", "utf-16-be"], |
| 235 | ) |
| 236 | def test_read_utf_data_file( |
| 237 | tmp_path_factory: pytest.TempPathFactory, |
| 238 | monkeypatch: pytest.MonkeyPatch, |
| 239 | data_file_encoding: str, |
| 240 | ) -> None: |
| 241 | src, dst, local = map(tmp_path_factory.mktemp, ("src", "dst", "local")) |
| 242 | |
| 243 | def encode_data(data: dict[str, str]) -> bytes: |
| 244 | dump = yaml.dump(data, allow_unicode=True) |
| 245 | if data_file_encoding.startswith("utf-16"): |
| 246 | dump = "\ufeff" + dump |
| 247 | return dump.encode(data_file_encoding) |
| 248 | |
| 249 | build_file_tree( |
| 250 | { |
| 251 | (src / "copier.yml"): ( |
| 252 | """\ |
| 253 | text1: |
| 254 | type: str |
| 255 | text2: |
| 256 | type: str |
| 257 | """ |
| 258 | ), |
| 259 | (src / "{{ _copier_conf.answers_file }}.jinja"): ( |
| 260 | """\ |
| 261 | # Changes here will be overwritten by Copier |
| 262 | {{ _copier_answers|to_nice_yaml }} |
| 263 | """ |
| 264 | ), |
| 265 | (local / "data.yml"): encode_data( |
| 266 | { |
| 267 | "text1": "\u3053\u3093\u306b\u3061\u306f", # japanese hiragana |
| 268 | "text2": "\U0001f60e", # smiling face with sunglasses |
| 269 | } |
| 270 | ), |
| 271 | } |
| 272 | ) |
| 273 | |
| 274 | with monkeypatch.context() as m: |
| 275 | # Override the factor that determine the default encoding when opening files. |
| 276 | # data.yml should be read correctly regardless of this value. |
| 277 | m.setattr("io.text_encoding", lambda *_args: "cp932") |
| 278 | run_result = CopierApp.run( |
| 279 | [ |
| 280 | "copier", |
| 281 | "copy", |
| 282 | f"--data-file={local / 'data.yml'}", |
| 283 | str(src), |
| 284 | str(dst), |
| 285 | ], |
| 286 | exit=False, |
| 287 | ) |
| 288 | |
| 289 | assert run_result[1] == 0 |
| 290 | expected_answers = { |
| 291 | "_src_path": str(src), |
| 292 | "text1": "\u3053\u3093\u306b\u3061\u306f", |
| 293 | "text2": "\U0001f60e", |
nothing calls this directly
no test coverage detected