Package translate :: Package storage :: Module test_directory

[hide private]
[frames] | no frames]

# Source Code for Module translate.storage.test_directory

     1  #!/usr/bin/env python
     2
     3  """Tests for the directory module"""
     4
     5  from translate.storage import directory
     6  import os
     7
     8 -class TestDirectory(object):
     9      """a test class to run tests on a test Pootle Server"""
    10
    11 -    def setup_method(self, method):
    12          """sets up a test directory"""
    13          print "setup_method called on", self.__class__.__name__
    14          self.testdir = "%s_testdir" % (self.__class__.__name__)
    15          self.cleardir(self.testdir)
    16          os.mkdir(self.testdir)
    17
    18 -    def teardown_method(self, method):
    19          """removes the attributes set up by setup_method"""
    20          self.cleardir(self.testdir)
    21
    22 -    def cleardir(self, dirname):
    23          """removes the given directory"""
    24          if os.path.exists(dirname):
    25              for dirpath, subdirs, filenames in os.walk(dirname, topdown=False):
    26                  for name in filenames:
    27                      os.remove(os.path.join(dirpath, name))
    28                  for name in subdirs:
    29                      os.rmdir(os.path.join(dirpath, name))
    30          if os.path.exists(dirname): os.rmdir(dirname)
    31          assert not os.path.exists(dirname)
    32
    33 -    def touchfiles(self, dir, filenames, content=None):
    34          for filename in filenames:
    35              f = open(os.path.join(dir, filename), "w")
    36              if content:
    37                  f.write(content)
    38              f.close()
    39
    40 -    def mkdir(self, dir):
    41          """Makes a directory inside self.testdir."""
    42          os.mkdir(os.path.join(self.testdir, dir))
    43
    44 -    def test_created(self):
    45          """test that the directory actually exists"""
    46          print self.testdir
    47          assert os.path.isdir(self.testdir)
    48
    49 -    def test_basic(self):
    50          """Tests basic functionality."""
    51          files = ["a.po", "b.po", "c.po"]
    52          files.sort()
    53          self.touchfiles(self.testdir, files)
    54
    55          d = directory.Directory(self.testdir)
    56          filenames = [name for dir, name in d.getfiles()]
    57          filenames.sort()
    58          assert filenames == files
    59
    60 -    def test_structure(self):
    61          """Tests a small directory structure."""
    62          files = ["a.po", "b.po", "c.po"]
    63          self.touchfiles(self.testdir, files)
    64          self.mkdir("bla")
    65          self.touchfiles(os.path.join(self.testdir, "bla"), files)
    66
    67          d = directory.Directory(self.testdir)
    68          filenames = [name for dirname, name in d.getfiles()]
    69          filenames.sort()
    70          files = files*2
    71          files.sort()
    72          assert filenames == files
    73
    74 -    def test_getunits(self):
    75          """Tests basic functionality."""
    76          files = ["a.po", "b.po", "c.po"]
    77          posource = '''msgid "bla"\nmsgstr "blabla"\n'''
    78          self.touchfiles(self.testdir, files, posource)
    79
    80          d = directory.Directory(self.testdir)
    81          for unit in d.getunits():
    82              assert unit.target == "blabla"
    83          assert len(d.getunits()) == 3
    84