Skip to content

Commit b1788b0

Browse files
fix: resolve NamedTemporaryFile permission errors on Windows in tests (#485)
* fix: decode_array stream corruption for lists with 24+ items (closes #478, relates to #402) * fix: resolve NamedTemporaryFile permission errors on Windows in tests
1 parent 1d2e825 commit b1788b0

5 files changed

Lines changed: 65 additions & 24 deletions

File tree

test/pycardano/test_address.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import tempfile
23

34
import pytest
@@ -219,7 +220,14 @@ def test_save_load_address():
219220
address_string = "addr_test1vr2p8st5t5cxqglyjky7vk98k7jtfhdpvhl4e97cezuhn0cqcexl7"
220221
address = Address.from_primitive(address_string)
221222

222-
with tempfile.NamedTemporaryFile() as f:
223-
address.save(f.name)
224-
loaded_address = Address.load(f.name)
223+
# On Windows, NamedTemporaryFile keeps the file locked while open, so
224+
# save() cannot open it for writing. Use delete=False and close the handle
225+
# first, then clean up manually afterward.
226+
with tempfile.NamedTemporaryFile(delete=False) as f:
227+
tmp_path = f.name
228+
try:
229+
address.save(tmp_path)
230+
loaded_address = Address.load(tmp_path)
225231
assert address == loaded_address
232+
finally:
233+
os.unlink(tmp_path)

test/pycardano/test_key.py

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import pathlib
34
import tempfile
45

@@ -216,25 +217,42 @@ def test_stake_pool_key_load():
216217

217218

218219
def test_key_save():
219-
with tempfile.NamedTemporaryFile() as f:
220-
SK.save(f.name)
221-
sk = PaymentSigningKey.load(f.name)
220+
# On Windows, NamedTemporaryFile keeps the file locked while open.
221+
# Use delete=False and close the handle first, then clean up manually.
222+
with tempfile.NamedTemporaryFile(delete=False) as f:
223+
tmp_path = f.name
224+
try:
225+
SK.save(tmp_path)
226+
sk = PaymentSigningKey.load(tmp_path)
222227
assert SK == sk
228+
finally:
229+
os.unlink(tmp_path)
223230

224231

225232
def test_key_save_invalid_address():
226-
with tempfile.NamedTemporaryFile() as f:
227-
SK.save(f.name)
233+
with tempfile.NamedTemporaryFile(delete=False) as f:
234+
tmp_path = f.name
235+
try:
236+
SK.save(tmp_path)
228237
with pytest.raises(IOError):
229-
VK.save(f.name)
238+
VK.save(tmp_path)
239+
finally:
240+
os.unlink(tmp_path)
230241

231242

232243
def test_stake_pool_key_save():
233-
with tempfile.NamedTemporaryFile() as skf, tempfile.NamedTemporaryFile() as vkf:
234-
SPSK.save(skf.name)
235-
sk = StakePoolSigningKey.load(skf.name)
236-
SPVK.save(vkf.name)
237-
vk = StakePoolSigningKey.load(vkf.name)
244+
with tempfile.NamedTemporaryFile(delete=False) as skf:
245+
sk_path = skf.name
246+
with tempfile.NamedTemporaryFile(delete=False) as vkf:
247+
vk_path = vkf.name
248+
try:
249+
SPSK.save(sk_path)
250+
sk = StakePoolSigningKey.load(sk_path)
251+
SPVK.save(vk_path)
252+
vk = StakePoolSigningKey.load(vk_path)
253+
finally:
254+
os.unlink(sk_path)
255+
os.unlink(vk_path)
238256
assert SPSK == sk
239257
assert SPVK == vk
240258

test/pycardano/test_serialization.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import tempfile
34
from collections import defaultdict, deque
45
from copy import deepcopy
@@ -1047,13 +1048,17 @@ def to_shallow_primitive(self) -> Union[Primitive, CBORSerializable]:
10471048
assert test1_json["description"] == "Test Description"
10481049
assert test1_json["cborHex"] == test1.to_cbor_hex()
10491050

1050-
with tempfile.NamedTemporaryFile() as f:
1051-
test1.save(f.name)
1052-
loaded = Test1.load(f.name)
1051+
with tempfile.NamedTemporaryFile(delete=False) as f:
1052+
tmp_path = f.name
1053+
try:
1054+
test1.save(tmp_path)
1055+
loaded = Test1.load(tmp_path)
10531056
assert test1 == loaded
10541057

10551058
with pytest.raises(IOError):
1056-
test1.save(f.name)
1059+
test1.save(tmp_path)
1060+
finally:
1061+
os.unlink(tmp_path)
10571062

10581063

10591064
def test_ordered_set_as_key_in_dict():

test/pycardano/test_transaction.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
import tempfile
23
from dataclasses import dataclass
34
from fractions import Fraction
@@ -265,10 +266,14 @@ def test_transaction_save_load():
265266
)
266267
tx = Transaction.from_cbor(tx_cbor)
267268

268-
with tempfile.NamedTemporaryFile() as f:
269-
tx.save(f.name)
270-
loaded_tx = Transaction.load(f.name)
269+
with tempfile.NamedTemporaryFile(delete=False) as f:
270+
tmp_path = f.name
271+
try:
272+
tx.save(tmp_path)
273+
loaded_tx = Transaction.load(tmp_path)
271274
assert tx == loaded_tx
275+
finally:
276+
os.unlink(tmp_path)
272277

273278

274279
def test_multi_asset():

test/pycardano/test_witness.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
import tempfile
34

45
from pycardano import (
@@ -19,12 +20,16 @@ def test_witness_save_load():
1920
signature=sk.sign(b"test"),
2021
)
2122

22-
with tempfile.NamedTemporaryFile() as f:
23-
witness.save(f.name)
24-
loaded_witness = VerificationKeyWitness.load(f.name)
23+
with tempfile.NamedTemporaryFile(delete=False) as f:
24+
tmp_path = f.name
25+
try:
26+
witness.save(tmp_path)
27+
loaded_witness = VerificationKeyWitness.load(tmp_path)
2528
assert witness == loaded_witness
2629

2730
assert witness != vk
31+
finally:
32+
os.unlink(tmp_path)
2833

2934

3035
def test_redeemer_decode():

0 commit comments

Comments
 (0)