Skip to content

Commit 7c0e177

Browse files
committed
test_compile: Change to TemporaryDirectory.
Change NamedTemporaryFile to TemporaryDirectory for easier cleanup via less verbose code.
1 parent 33dc25c commit 7c0e177

File tree

1 file changed

+16
-24
lines changed

1 file changed

+16
-24
lines changed

tests/test_compile.py

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
import os
66
import struct
7-
from tempfile import NamedTemporaryFile
7+
from tempfile import TemporaryDirectory
88

99
import pytest
1010

@@ -14,35 +14,27 @@
1414
@pytest.mark.parametrize("abi", [5, 6])
1515
@pytest.mark.asyncio
1616
async def test_compile_file(abi: int):
17-
with NamedTemporaryFile("w", delete=False, encoding="utf-8") as f:
18-
try:
17+
with TemporaryDirectory() as temp_dir:
18+
with open(os.path.join(temp_dir, "test.py"), "w", encoding="utf-8") as f:
1919
f.write("print('test')")
20-
f.close()
2120

22-
mpy = await compile_file(
23-
os.path.dirname(f.name), os.path.basename(f.name), abi=abi
24-
)
21+
mpy = await compile_file(
22+
os.path.dirname(f.name), os.path.basename(f.name), abi=abi
23+
)
2524

26-
magic, abi_ver, flags, int_bits = struct.unpack_from("<BBBB", mpy)
25+
magic, abi_ver, flags, int_bits = struct.unpack_from("<BBBB", mpy)
2726

28-
assert chr(magic) == "M"
29-
assert abi_ver == abi
30-
assert flags == 0
31-
assert int_bits == 31
32-
finally:
33-
os.unlink(f.name)
27+
assert chr(magic) == "M"
28+
assert abi_ver == abi
29+
assert flags == 0
30+
assert int_bits == 31
3431

3532

3633
@pytest.mark.asyncio
3734
async def test_compile_file_invalid_abi():
38-
with NamedTemporaryFile("w", delete=False, encoding="utf-8") as f:
39-
try:
35+
with TemporaryDirectory() as temp_dir:
36+
with open(os.path.join(temp_dir, "test.py"), "w", encoding="utf-8") as f:
4037
f.write("print('test')")
41-
f.close()
42-
43-
with pytest.raises(ValueError, match="mpy_version must be 5 or 6"):
44-
await compile_file(
45-
os.path.dirname(f.name), os.path.basename(f.name), abi=4
46-
)
47-
finally:
48-
os.unlink(f.name)
38+
39+
with pytest.raises(ValueError, match="mpy_version must be 5 or 6"):
40+
await compile_file(os.path.dirname(f.name), os.path.basename(f.name), abi=4)

0 commit comments

Comments
 (0)