[Treepkg-commits] r85 - in trunk: test treepkg

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jun 19 14:25:31 CEST 2008


Author: bh
Date: 2008-06-19 14:25:30 +0200 (Thu, 19 Jun 2008)
New Revision: 85

Modified:
   trunk/test/test_util.py
   trunk/treepkg/util.py
Log:
Make treepkg/utilreplace_in_file return whether any substitutions were made.
Add a corresponding test.


Modified: trunk/test/test_util.py
===================================================================
--- trunk/test/test_util.py	2008-06-19 10:13:39 UTC (rev 84)
+++ trunk/test/test_util.py	2008-06-19 12:25:30 UTC (rev 85)
@@ -18,7 +18,9 @@
 
     def runtest(self, orig_contents, expected_contents, pattern, replacement):
         filename = self.create_temp_file(self.id(), orig_contents)
-        replace_in_file(filename, pattern, replacement)
+        changed = replace_in_file(filename, pattern, replacement)
+        print "TestReplaceInFile.runtest", changed
+        self.assertEquals(changed, orig_contents != expected_contents)
         self.checkFileContents(filename, expected_contents)
 
     def test_version_replacement(self):
@@ -33,3 +35,17 @@
                     "and more filler")
         self.runtest(template % dict(rev=0), template % dict(rev=321),
                      r"1\.0-svn0", "1.0-svn321")
+
+    def test_no_matches(self):
+        """Tests replace_in_file when no matches are found"""
+        template = ("project foo version 1.0-svn%(rev)d"
+                    "Some filler"
+                    "text that sometimes"
+                    "looks similar to the pattern"
+                    "1.0-"
+                    "foo 1.2-svn2"
+                    "echo foo version 1.0-svn%(rev)d"
+                    ""
+                    "and more filler")
+        self.runtest(template % dict(rev=0), template % dict(rev=0),
+                     r"0\.9-svn0", "1.0-svn321")

Modified: trunk/treepkg/util.py
===================================================================
--- trunk/treepkg/util.py	2008-06-19 10:13:39 UTC (rev 84)
+++ trunk/treepkg/util.py	2008-06-19 12:25:30 UTC (rev 85)
@@ -123,9 +123,13 @@
     the re.sub function.  The pattern and replacement parameter are passed
     through to re.sub unmodified, so their semantics are determined by
     re.sub.
+
+    The return value is True if the contents of the file have been
+    changed, False otherwise.
     """
     contents = open(filename).read()
     modified = re.sub(pattern, replacement, contents)
     f = open(filename, "w")
     f.write(modified)
     f.close()
+    return modified != contents



More information about the Treepkg-commits mailing list