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 <chester.curme@gmail.com>
This commit is contained in:
authored and GitHub committed 2026-08-18 17:34:30 -04:00
1 parent b5e8e2e85e
commit c8b2d767bf
2 files changed
+13 -14

No files matched your search

@@ -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:
+2 -1
View File
@@ -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