Skip to content

Commit 4cea495

Browse files
committed
test: improve test_load_filtered_policy_with_comments in test_filter.py
1 parent 2fb4770 commit 4cea495

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

tests/test_filter.py

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,20 @@ def test_filter_words_edge_cases(self):
291291

292292
def test_load_filtered_policy_with_comments(self):
293293
"""Test loading filtering policies with comments."""
294-
temp_file = tempfile.NamedTemporaryFile(delete=False)
295-
try:
296-
shutil.copyfile(get_examples("rbac_with_domains_policy.csv"), temp_file.name)
294+
import tempfile
295+
import shutil
296+
297+
with tempfile.NamedTemporaryFile(mode="w+", delete=False) as temp_file:
298+
with open(get_examples("rbac_with_domains_policy.csv"), "r") as source:
299+
shutil.copyfileobj(source, temp_file)
297300

298-
with open(temp_file.name, "a") as f:
299-
f.write("\n# This is a comment\np, admin, domain1, data3, read")
301+
temp_file.write("\n# This is a comment\np, admin, domain1, data3, read")
302+
temp_file.flush()
300303

301-
adapter = FilteredFileAdapter(temp_file.name)
304+
temp_path = temp_file.name
305+
306+
try:
307+
adapter = FilteredFileAdapter(temp_path)
302308
e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), adapter)
303309
filter = Filter()
304310
filter.P = ["", "domain1"]
@@ -307,4 +313,7 @@ def test_load_filtered_policy_with_comments(self):
307313
e.load_filtered_policy(filter)
308314
self.assertTrue(e.has_policy(["admin", "domain1", "data3", "read"]))
309315
finally:
310-
os.unlink(temp_file.name)
316+
try:
317+
os.unlink(temp_path)
318+
except OSError:
319+
pass

0 commit comments

Comments
 (0)