This is an automated email from the ASF dual-hosted git repository.

hsluoyz pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/casbin-pycasbin.git


The following commit(s) were added to refs/heads/master by this push:
     new f6d71d7  test: stop rewriting checked-in example policy files
f6d71d7 is described below

commit f6d71d70f366ca67cdaa44dc5d6ce170362bfc7f
Author: Yang Luo <[email protected]>
AuthorDate: Tue Aug 4 14:33:43 2026 +0800

    test: stop rewriting checked-in example policy files
---
 tests/test_enforcer.py   | 21 +++++++++++++++++++++
 tests/test_filter.py     |  6 +++---
 tests/test_watcher_ex.py | 12 ++++++------
 3 files changed, 30 insertions(+), 9 deletions(-)

diff --git a/tests/test_enforcer.py b/tests/test_enforcer.py
index d1b5f32..a4c5672 100644
--- a/tests/test_enforcer.py
+++ b/tests/test_enforcer.py
@@ -12,9 +12,13 @@
 # See the License for the specific language governing permissions and
 # limitations under the License.
 
+import atexit
 import copy
 import os
+import shutil
+import tempfile
 import time
+from itertools import count
 from unittest import TestCase, IsolatedAsyncioTestCase
 
 import casbin
@@ -26,6 +30,23 @@ def get_examples(path):
     return os.path.abspath(examples_path + path)
 
 
+_scratch_dir = tempfile.mkdtemp(prefix="pycasbin-tests-")
+atexit.register(shutil.rmtree, _scratch_dir, ignore_errors=True)
+_scratch_counter = count()
+
+
+def get_examples_copy(path):
+    """Returns the path of a throwaway copy of an example file.
+
+    Tests that call save_policy() must use this instead of get_examples(), so 
that
+    they do not rewrite the checked-in examples (which, on Windows, also flips 
their
+    line endings to CRLF and leaves the working tree dirty).
+    """
+    destination = os.path.join(_scratch_dir, 
"{}-{}".format(next(_scratch_counter), os.path.basename(path)))
+    shutil.copyfile(get_examples(path), destination)
+    return destination
+
+
 class MockSub:
     def __init__(self, name, age):
         self.name = name
diff --git a/tests/test_filter.py b/tests/test_filter.py
index 0160325..a0c569f 100644
--- a/tests/test_filter.py
+++ b/tests/test_filter.py
@@ -15,7 +15,7 @@
 import os
 from unittest import TestCase
 import casbin
-from tests.test_enforcer import get_examples
+from tests.test_enforcer import get_examples, get_examples_copy
 from casbin.persist.adapters import FilteredFileAdapter
 from casbin.persist.adapters.filtered_file_adapter import filter_line, 
filter_words
 
@@ -103,7 +103,7 @@ class TestFilteredFileAdapter(TestCase):
             e.load_filtered_policy(filter)
 
     def test_filtered_policy_empty_filter(self):
-        adapter = 
FilteredFileAdapter(get_examples("rbac_with_domains_policy.csv"))
+        adapter = 
FilteredFileAdapter(get_examples_copy("rbac_with_domains_policy.csv"))
         e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), 
adapter)
 
         try:
@@ -211,7 +211,7 @@ class TestFilteredFileAdapter(TestCase):
 
     def test_empty_string_filter(self):
         """Test the filter for all empty strings."""
-        adapter = 
FilteredFileAdapter(get_examples("rbac_with_domains_policy.csv"))
+        adapter = 
FilteredFileAdapter(get_examples_copy("rbac_with_domains_policy.csv"))
         e = casbin.Enforcer(get_examples("rbac_with_domains_model.conf"), 
adapter)
         filter = Filter()
         filter.P = ["", "", ""]
diff --git a/tests/test_watcher_ex.py b/tests/test_watcher_ex.py
index bfcd557..deb8347 100644
--- a/tests/test_watcher_ex.py
+++ b/tests/test_watcher_ex.py
@@ -13,7 +13,7 @@
 # limitations under the License.
 
 import casbin
-from tests.test_enforcer import get_examples, TestCaseBase
+from tests.test_enforcer import get_examples, get_examples_copy, TestCaseBase
 from unittest import IsolatedAsyncioTestCase
 
 
@@ -221,7 +221,7 @@ class TestWatcherEx(TestCaseBase):
     def test_auto_notify_enabled(self):
         e = self.get_enforcer(
             get_examples("basic_model.conf"),
-            get_examples("basic_policy.csv"),
+            get_examples_copy("basic_policy.csv"),
         )
         w = SampleWatcher()
         e.set_watcher(w)
@@ -254,7 +254,7 @@ class TestWatcherEx(TestCaseBase):
     def test_auto_notify_disabled(self):
         e = self.get_enforcer(
             get_examples("basic_model.conf"),
-            get_examples("basic_policy.csv"),
+            get_examples_copy("basic_policy.csv"),
         )
         w = SampleWatcher()
         e.set_watcher(w)
@@ -315,7 +315,7 @@ class TestAsyncWatcherEx(IsolatedAsyncioTestCase):
     async def test_auto_notify_enabled(self):
         e = self.get_enforcer(
             get_examples("basic_model.conf"),
-            get_examples("basic_policy.csv"),
+            get_examples_copy("basic_policy.csv"),
         )
         await e.load_policy()
 
@@ -350,7 +350,7 @@ class TestAsyncWatcherEx(IsolatedAsyncioTestCase):
     async def test_auto_notify_disabled(self):
         e = self.get_enforcer(
             get_examples("basic_model.conf"),
-            get_examples("basic_policy.csv"),
+            get_examples_copy("basic_policy.csv"),
         )
         await e.load_policy()
 
@@ -388,7 +388,7 @@ class TestAsyncWatcherEx(IsolatedAsyncioTestCase):
         """Test that a watcher with only async update() method works 
properly."""
         e = self.get_enforcer(
             get_examples("basic_model.conf"),
-            get_examples("basic_policy.csv"),
+            get_examples_copy("basic_policy.csv"),
         )
         await e.load_policy()
 


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to