Use a temporary directory in place of file name

Different OS auto delete/sharing semantics are just wonky.
This commit is contained in:
Daniël de Kok 2023-01-19 11:48:52 +01:00
parent 8c27fcfd1b
commit 2f9d8f72c6

View File

@ -38,9 +38,9 @@ PLAIN_TEXT_DOC_TOKENIZED = [
@pytest.mark.parametrize("min_length, max_length", [(0, 0), (0, 5), (5, 0), (5, 5)]) @pytest.mark.parametrize("min_length, max_length", [(0, 0), (0, 5), (5, 0), (5, 5)])
def test_plain_text_reader(min_length, max_length): def test_plain_text_reader(min_length, max_length):
nlp = English() nlp = English()
with _string_to_tmp_file(PLAIN_TEXT_DOC) as f: with _string_to_tmp_file(PLAIN_TEXT_DOC) as file_path:
corpus = PlainTextCorpus( corpus = PlainTextCorpus(
Path(f.name), min_length=min_length, max_length=max_length file_path, min_length=min_length, max_length=max_length
) )
check = [ check = [
@ -55,11 +55,12 @@ def test_plain_text_reader(min_length, max_length):
@contextmanager @contextmanager
def _string_to_tmp_file(s: str) -> Generator[IO, None, None]: def _string_to_tmp_file(s: str) -> Generator[Path, None, None]:
with tempfile.NamedTemporaryFile(suffix=".txt") as f: with tempfile.TemporaryDirectory() as d:
f.write(s.encode("utf-8")) file_path = Path(d) / "string.txt"
f.seek(0) with open(file_path, "wb") as f:
yield f f.write(s.encode("utf-8"))
yield file_path
def _examples_to_tokens( def _examples_to_tokens(