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@185: filename = self.create_temp_file("orig", orig_contents) bh@160: changed = replace_in_file(filename, pattern, replacement) bh@160: self.assertEquals(changed, orig_contents != expected_contents) bh@195: self.check_file_contents(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@185: self.directory = self.create_temp_dir("a_directory") bh@169: bh@169: def test_listdir_abs(self): bh@185: directory = self.create_files("dir", [("foo.orig.tgz", ""), bh@185: ("foo.dsc", ""), bh@185: ("foo.diff.gz", ""),]) bh@185: self.assertEquals(sorted(listdir_abs(directory)), bh@185: sorted([os.path.join(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@185: directory = self.create_files("dir", [("foo.orig.tgz", ""), bh@185: ("foo.dsc", ""), bh@185: ("foo.diff.gz", ""),]) bh@185: self.assertEquals(sorted(listdir_abs(directory, '*.dsc')), bh@185: [os.path.join(directory, "foo.dsc")]) bh@169: