Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/translate-toolkit-1.5.1/translate/storage/test_directory.py
blob: dc4153ae910bb73f2bae3a3be1a4ce274e918b5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#!/usr/bin/env python

"""Tests for the directory module"""

from translate.storage import directory
import os

class TestDirectory(object):
    """a test class to run tests on a test Pootle Server"""

    def setup_method(self, method):
        """sets up a test directory"""
        print "setup_method called on", self.__class__.__name__
        self.testdir = "%s_testdir" % (self.__class__.__name__)
        self.cleardir(self.testdir)
        os.mkdir(self.testdir)

    def teardown_method(self, method):
        """removes the attributes set up by setup_method"""
        self.cleardir(self.testdir)

    def cleardir(self, dirname):
        """removes the given directory"""
        if os.path.exists(dirname):
            for dirpath, subdirs, filenames in os.walk(dirname, topdown=False):
                for name in filenames:
                    os.remove(os.path.join(dirpath, name))
                for name in subdirs:
                    os.rmdir(os.path.join(dirpath, name))
        if os.path.exists(dirname): os.rmdir(dirname)
        assert not os.path.exists(dirname)

    def touchfiles(self, dir, filenames, content=None):
        for filename in filenames:
            f = open(os.path.join(dir, filename), "w")
            if content:
                f.write(content)
            f.close()

    def mkdir(self, dir):
        """Makes a directory inside self.testdir."""
        os.mkdir(os.path.join(self.testdir, dir))

    def test_created(self):
        """test that the directory actually exists"""
        print self.testdir
        assert os.path.isdir(self.testdir)

    def test_basic(self):
        """Tests basic functionality."""
        files = ["a.po", "b.po", "c.po"]
        files.sort()
        self.touchfiles(self.testdir, files)

        d = directory.Directory(self.testdir)
        filenames = [name for dir, name in d.getfiles()]
        filenames.sort()
        assert filenames == files

    def test_structure(self):
        """Tests a small directory structure."""
        files = ["a.po", "b.po", "c.po"]
        self.touchfiles(self.testdir, files)
        self.mkdir("bla")
        self.touchfiles(os.path.join(self.testdir, "bla"), files)
        
        d = directory.Directory(self.testdir)
        filenames = [name for dirname, name in d.getfiles()]
        filenames.sort()
        files = files*2
        files.sort()
        assert filenames == files

    def test_getunits(self):
        """Tests basic functionality."""
        files = ["a.po", "b.po", "c.po"]
        posource = '''msgid "bla"\nmsgstr "blabla"\n'''
        self.touchfiles(self.testdir, files, posource)

        d = directory.Directory(self.testdir)
        for unit in d.getunits():
            assert unit.target == "blabla"
        assert len(d.getunits()) == 3