bh@111: # Copyright (C) 2007, 2008 by Intevation GmbH bh@111: # Authors: bh@111: # Bernhard Herzog bricks@495: # Bjoern Ricks 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 bricks@461: import shutil bh@111: bh@111: from filesupport import FileTestMixin bh@111: bricks@439: from treepkg.util import replace_in_file, listdir_abs, md5sum, \ bricks@461: remove_trailing_slashes, expand_filename, \ bricks@461: compress_all_logs 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: bricks@394: class TestMd5sum(unittest.TestCase, FileTestMixin): bricks@394: bricks@394: content = "this is a test content" bricks@394: bricks@394: def setUp(self): bricks@394: self.testfile = self.create_temp_file("testmd5.txt", self.content) bricks@394: bricks@394: def test_md5sum(self): bricks@394: sum = md5sum(self.testfile) bricks@394: self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum) bricks@439: bricks@439: class TestRemoveTrailingSlashes(unittest.TestCase): bricks@439: bricks@439: def test_remove_trailing_slashes(self): bricks@439: dir_w_slash = "/tmp/dir/" bricks@439: dir_wo_slash = "/tmp/dir" bricks@439: bricks@439: self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_w_slash)) bricks@439: self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_wo_slash)) bricks@439: bricks@439: class TestExpandFilename(unittest.TestCase): bricks@439: bricks@439: def test_expand_filenam(self): bricks@439: os.environ['MY_TEST_VAR'] = "def" bricks@439: path = "/abc/${MY_TEST_VAR}/" bricks@439: bricks@439: self.assertEquals("/abc/def/", expand_filename(path)) bricks@439: bricks@461: class TestCompressAllLogs(unittest.TestCase, FileTestMixin): bricks@461: bricks@461: def test_compress_all_logs(self): bricks@461: log_dir = self.create_test_specific_temp_dir() bricks@461: try: bricks@461: for i in range(15): bricks@461: f = open(os.path.join(log_dir, "log_%d.txt" % i), "w") bricks@461: print >> f, "World domination is right at hand!" bricks@461: f.close() bricks@461: ref_log = os.path.join(log_dir, "log_0.txt") bricks@461: compress_all_logs(ref_log) bricks@461: for i in range(15): bricks@461: self.assertTrue(os.path.isfile(os.path.join( bricks@461: log_dir, "log_%d.txt.gz" % i))) bricks@461: finally: bricks@461: shutil.rmtree(log_dir, ignore_errors=True)