[master 4/5] Add a test for SimpleConfig.write(use_tmp=True).

dashea installerbot-noreply at redhat.com
Tue Jul 14 21:56:36 UTC 2015


From: David Shea <dshea at redhat.com>

Ensure that using a temporary file replaces the original file instead of
overwriting it, and vice versa.
---
 tests/pyanaconda_tests/simpleconfig_test.py | 58 +++++++++++++++++++++++++++++
 1 file changed, 58 insertions(+)

diff --git a/tests/pyanaconda_tests/simpleconfig_test.py b/tests/pyanaconda_tests/simpleconfig_test.py
index c40b69e..4d1879a 100644
--- a/tests/pyanaconda_tests/simpleconfig_test.py
+++ b/tests/pyanaconda_tests/simpleconfig_test.py
@@ -131,3 +131,61 @@ def remove_key_test(self):
             scf.reset()
             scf.read(testconfig.name)
             self.assertEqual(scf.get("BOOT"), "")
+
+    def use_tmp_test(self):
+        with tempfile.NamedTemporaryFile(mode="w+t") as testconfig:
+            # Write a starting file and keep the handle open
+            testconfig.write("KEY1=value1\n")
+            testconfig.flush()
+            testconfig.seek(0)
+
+            # Overwrite the value and write a new file
+            scf = SimpleConfigFile()
+            scf.read(testconfig.name)
+            scf.set(('key1', 'value2'))
+            scf.write(testconfig.name, use_tmp=True)
+
+            # Check the new contents
+            self.assertEqual(open(testconfig.name).read(), 'KEY1=value2\n')
+
+            # Check that the original file handle still points to the old contents
+            self.assertEqual(testconfig.read(), 'KEY1=value1\n')
+
+    def use_tmp_multifs_test(self):
+        # Open a file on a non-default filesystem
+        with tempfile.NamedTemporaryFile(dir='/dev/shm', mode='w+t') as testconfig:
+            # Write a starting file and keep the handle open
+            testconfig.write("KEY1=value1\n")
+            testconfig.flush()
+            testconfig.seek(0)
+
+            # Overwrite the value and write a new file
+            scf = SimpleConfigFile()
+            scf.read(testconfig.name)
+            scf.set(('key1', 'value2'))
+            scf.write(testconfig.name, use_tmp=True)
+
+            # Check the new contents
+            self.assertEqual(open(testconfig.name).read(), 'KEY1=value2\n')
+
+            # Check that the original file handle still points to the old contents
+            self.assertEqual(testconfig.read(), 'KEY1=value1\n')
+
+    def no_use_tmp_test(self):
+        with tempfile.NamedTemporaryFile(mode="w+t") as testconfig:
+            # Write a starting file and keep the handle open
+            testconfig.write("KEY1=value1\n")
+            testconfig.flush()
+            testconfig.seek(0)
+
+            # Overwrite the value and write a new file
+            scf = SimpleConfigFile()
+            scf.read(testconfig.name)
+            scf.set(('key1', 'value2'))
+            scf.write(testconfig.name, use_tmp=False)
+
+            # Check the new contents
+            self.assertEqual(open(testconfig.name).read(), 'KEY1=value2\n')
+
+            # Check that the original file handle points to the replaced contents
+            self.assertEqual(testconfig.read(), 'KEY1=value2\n')


-- 
To view this commit on github, visit https://github.com/rhinstaller/anaconda/commit/0a2bbf09528b6663f4674f1a3890006141e5d88c


More information about the anaconda-patches mailing list