Mercurial > treepkg
view test/test_util.py @ 539:94ff51f691a2
bugfix: revsions are strings now due to git short revision
author | Bjoern Ricks <bricks@intevation.de> |
---|---|
date | Tue, 11 Jan 2011 15:52:40 +0000 |
parents | ca95be9d033a |
children |
line wrap: on
line source
# Copyright (C) 2007, 2008 by Intevation GmbH # Authors: # Bernhard Herzog <bh@intevation.de> # Bjoern Ricks <bjoern.ricks@intevation.de> # # This program is free software under the GPL (>=v2) # Read the file COPYING coming with the software for details. """Tests for the treepkg.util module""" import os import unittest import shutil from filesupport import FileTestMixin from treepkg.util import replace_in_file, listdir_abs, md5sum, \ remove_trailing_slashes, expand_filename, \ compress_all_logs class TestReplaceInFile(unittest.TestCase, FileTestMixin): def runtest(self, orig_contents, expected_contents, pattern, replacement): filename = self.create_temp_file("orig", orig_contents) changed = replace_in_file(filename, pattern, replacement) self.assertEquals(changed, orig_contents != expected_contents) self.check_file_contents(filename, expected_contents) def test_version_replacement(self): 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=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") class TestListDirAbs(unittest.TestCase, FileTestMixin): def setUp(self): self.directory = self.create_temp_dir("a_directory") def test_listdir_abs(self): directory = self.create_files("dir", [("foo.orig.tgz", ""), ("foo.dsc", ""), ("foo.diff.gz", ""),]) self.assertEquals(sorted(listdir_abs(directory)), sorted([os.path.join(directory, d) for d in ["foo.orig.tgz", "foo.dsc", "foo.diff.gz"]])) def test_listdir_abs_pattern(self): directory = self.create_files("dir", [("foo.orig.tgz", ""), ("foo.dsc", ""), ("foo.diff.gz", ""),]) self.assertEquals(sorted(listdir_abs(directory, '*.dsc')), [os.path.join(directory, "foo.dsc")]) class TestMd5sum(unittest.TestCase, FileTestMixin): content = "this is a test content" def setUp(self): self.testfile = self.create_temp_file("testmd5.txt", self.content) def test_md5sum(self): sum = md5sum(self.testfile) self.assertEquals("a12511153555c1f0f0a1eda200733a3f", sum) class TestRemoveTrailingSlashes(unittest.TestCase): def test_remove_trailing_slashes(self): dir_w_slash = "/tmp/dir/" dir_wo_slash = "/tmp/dir" self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_w_slash)) self.assertEquals(dir_wo_slash, remove_trailing_slashes(dir_wo_slash)) class TestExpandFilename(unittest.TestCase): def test_expand_filenam(self): os.environ['MY_TEST_VAR'] = "def" path = "/abc/${MY_TEST_VAR}/" self.assertEquals("/abc/def/", expand_filename(path)) class TestCompressAllLogs(unittest.TestCase, FileTestMixin): def test_compress_all_logs(self): log_dir = self.create_test_specific_temp_dir() try: for i in range(15): f = open(os.path.join(log_dir, "log_%d.txt" % i), "w") print >> f, "World domination is right at hand!" f.close() ref_log = os.path.join(log_dir, "log_0.txt") compress_all_logs(ref_log) for i in range(15): self.assertTrue(os.path.isfile(os.path.join( log_dir, "log_%d.txt.gz" % i))) finally: shutil.rmtree(log_dir, ignore_errors=True)