bh@111: # Copyright (C) 2007, 2008 by Intevation GmbH bh@111: # Authors: bh@111: # Bernhard Herzog bh@111: # bh@111: # This program is free software under the GPL (>=v2) bh@111: # Read the file COPYING coming with the software for details. bh@111: bh@111: """Tests for the treepkg.util module""" bh@111: bh@169: import os bh@111: import unittest bh@111: bh@111: from filesupport import FileTestMixin bh@111: bh@169: from treepkg.util import replace_in_file, listdir_abs bh@111: bh@111: bh@111: class TestReplaceInFile(unittest.TestCase, FileTestMixin): bh@111: bh@111: def runtest(self, orig_contents, expected_contents, pattern, replacement): bh@111: filename = self.create_temp_file(self.id(), orig_contents) bh@160: changed = replace_in_file(filename, pattern, replacement) bh@160: self.assertEquals(changed, orig_contents != expected_contents) bh@111: self.checkFileContents(filename, expected_contents) bh@111: bh@111: def test_version_replacement(self): bh@111: template = ("project foo version 1.0-svn%(rev)d" bh@111: "Some filler" bh@111: "text that sometimes" bh@111: "looks similar to the pattern" bh@111: "1.0-" bh@111: "foo 1.2-svn2" bh@111: "echo foo version 1.0-svn%(rev)d" bh@111: "" bh@111: "and more filler") bh@111: self.runtest(template % dict(rev=0), template % dict(rev=321), bh@111: r"1\.0-svn0", "1.0-svn321") bh@160: bh@160: def test_no_matches(self): bh@160: """Tests replace_in_file when no matches are found""" bh@160: template = ("project foo version 1.0-svn%(rev)d" bh@160: "Some filler" bh@160: "text that sometimes" bh@160: "looks similar to the pattern" bh@160: "1.0-" bh@160: "foo 1.2-svn2" bh@160: "echo foo version 1.0-svn%(rev)d" bh@160: "" bh@160: "and more filler") bh@160: self.runtest(template % dict(rev=0), template % dict(rev=0), bh@160: r"0\.9-svn0", "1.0-svn321") bh@169: bh@169: bh@169: class TestListDirAbs(unittest.TestCase, FileTestMixin): bh@169: bh@169: def setUp(self): bh@169: self.directory = self.create_temp_dir(self.id()) bh@169: bh@169: def test_listdir_abs(self): bh@169: self.create_files(self.directory, [("foo.orig.tgz", ""), bh@169: ("foo.dsc", ""), bh@169: ("foo.diff.gz", ""),]) bh@169: self.assertEquals(sorted(listdir_abs(self.directory)), bh@169: sorted([os.path.join(self.directory, d) bh@169: for d in ["foo.orig.tgz", "foo.dsc", bh@169: "foo.diff.gz"]])) bh@169: bh@169: def test_listdir_abs_pattern(self): bh@169: self.create_files(self.directory, [("foo.orig.tgz", ""), bh@169: ("foo.dsc", ""), bh@169: ("foo.diff.gz", ""),]) bh@169: self.assertEquals(sorted(listdir_abs(self.directory, '*.dsc')), bh@169: [os.path.join(self.directory, "foo.dsc")]) bh@169: