From c8b2d767bf2ff1b8e23b3cf89011bf75ee510c27 Mon Sep 17 00:00:00 2001 From: Jacopos311 Date: Tue, 18 Aug 2026 23:34:30 +0200 Subject: [PATCH] fix(core): make subprocess and temporary file tests portable on Windows (#39664) Co-authored-by: Pu Jingnan <149932541+Puuuuup@users.noreply.github.com> Co-authored-by: Chester Curme --- .../tests/unit_tests/prompts/test_prompt.py | 24 +++++++++---------- libs/core/tests/unit_tests/test_imports.py | 3 ++- 2 files changed, 13 insertions(+), 14 deletions(-) diff --git a/libs/core/tests/unit_tests/prompts/test_prompt.py b/libs/core/tests/unit_tests/prompts/test_prompt.py index ed7449fd7e..49525b75c7 100644 --- a/libs/core/tests/unit_tests/prompts/test_prompt.py +++ b/libs/core/tests/unit_tests/prompts/test_prompt.py @@ -2,7 +2,7 @@ import re from collections import ChainMap, UserDict -from tempfile import NamedTemporaryFile +from pathlib import Path from types import MappingProxyType from typing import Any, Literal from unittest import mock @@ -39,25 +39,23 @@ def test_prompt_valid() -> None: assert prompt.input_variables == input_variables -def test_from_file_encoding() -> None: +def test_from_file_encoding(tmp_path: Path) -> None: """Test that we can load a template from a file with a non utf-8 encoding.""" template = "This is a {foo} test with special character €." input_variables = ["foo"] # First write to a file using CP-1252 encoding. - with NamedTemporaryFile(delete=True, mode="w", encoding="cp1252") as f: - f.write(template) - f.flush() - file_name = f.name + file_path = tmp_path / "template.txt" + file_path.write_text(template, encoding="cp1252") - # Now read from the file using CP-1252 encoding and test - prompt = PromptTemplate.from_file(file_name, encoding="cp1252") - assert prompt.template == template - assert prompt.input_variables == input_variables + # Now read from the file using CP-1252 encoding and test + prompt = PromptTemplate.from_file(file_path, encoding="cp1252") + assert prompt.template == template + assert prompt.input_variables == input_variables - # Now read from the file using UTF-8 encoding and test - with pytest.raises(UnicodeDecodeError): - PromptTemplate.from_file(file_name, encoding="utf-8") + # Now read from the file using UTF-8 encoding and test + with pytest.raises(UnicodeDecodeError): + PromptTemplate.from_file(file_path, encoding="utf-8") def test_prompt_from_template() -> None: diff --git a/libs/core/tests/unit_tests/test_imports.py b/libs/core/tests/unit_tests/test_imports.py index 22a1a9a823..f035595c8c 100644 --- a/libs/core/tests/unit_tests/test_imports.py +++ b/libs/core/tests/unit_tests/test_imports.py @@ -1,6 +1,7 @@ import concurrent.futures import importlib import subprocess +import sys from pathlib import Path @@ -26,7 +27,7 @@ def try_to_import(module_name: str) -> tuple[int, str]: getattr(module, cls_) result = subprocess.run( - ["python", "-c", f"import langchain_core.{module_name}"], check=True + [sys.executable, "-c", f"import langchain_core.{module_name}"], check=True ) return result.returncode, module_name