~launchpad-pqm/launchpad/devel

10154.8.3 by Jeroen Vermeulen
Working on script & testing.
1
# Copyright 2010 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
4
import os
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
5
from StringIO import StringIO
10427.23.5 by Henning Eggers
Added zipup method and its test.
6
import tarfile
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
7
14339.1.1 by mbp at canonical
Delete canonical.buildd again
8
from lpbuildd import pottery
9
from lpbuildd.pottery.generate_translation_templates import (
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
10
    GenerateTranslationTemplates,
11
    )
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
12
14593.2.5 by Curtis Hovey
Moved script to lp.testing.
13
from lp.testing.script import run_script
14604.1.1 by Curtis Hovey
Separate test-authoring classes from test-running classes.
14
from lp.testing.layers import ZopelessDatabaseLayer
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
15
from lp.code.model.directbranchcommit import DirectBranchCommit
16
from lp.testing import TestCaseWithFactory
14550.1.1 by Steve Kowalik
Run format-imports over lib/lp and lib/canonical/launchpad
17
from lp.testing.fakemethod import FakeMethod
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
18
19
20
class TestGenerateTranslationTemplates(TestCaseWithFactory):
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
21
    """Test slave-side generate-translation-templates script."""
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
22
    layer = ZopelessDatabaseLayer
23
10427.23.8 by Henning Eggers
Central place to define result name.
24
    result_name = "translation-templates.tar.gz"
25
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
26
    def test_getBranch_url(self):
27
        # If passed a branch URL, the template generation script will
28
        # check out that branch into a directory called "source-tree."
29
        branch_url = 'lp://~my/translation/branch'
30
10427.20.11 by Henning Eggers
Test passing again.
31
        generator = GenerateTranslationTemplates(
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
32
            branch_url, self.result_name, self.makeTemporaryDirectory(),
33
            log_file=StringIO())
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
34
        generator._checkout = FakeMethod()
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
35
        generator._getBranch()
36
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
37
        self.assertEqual(1, generator._checkout.call_count)
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
38
        self.assertTrue(generator.branch_dir.endswith('source-tree'))
39
40
    def test_getBranch_dir(self):
41
        # If passed a branch directory, the template generation script
42
        # works directly in that directory.
43
        branch_dir = '/home/me/branch'
44
10427.20.11 by Henning Eggers
Test passing again.
45
        generator = GenerateTranslationTemplates(
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
46
            branch_dir, self.result_name, self.makeTemporaryDirectory(),
47
            log_file=StringIO())
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
48
        generator._checkout = FakeMethod()
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
49
        generator._getBranch()
50
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
51
        self.assertEqual(0, generator._checkout.call_count)
10154.8.14 by Jeroen Vermeulen
Tested url/directory logic.
52
        self.assertEqual(branch_dir, generator.branch_dir)
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
53
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
54
    def _createBranch(self, content_map=None):
55
        """Create a working branch.
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
56
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
57
        :param content_map: optional dict mapping file names to file contents.
58
            Each of these files with their contents will be written to the
59
            branch.
60
61
        :return: a fresh lp.code.model.Branch backed by a real bzr branch.
62
        """
9590.1.110 by Michael Hudson
fix test_generate_translation_templates
63
        db_branch, tree = self.create_branch_and_tree()
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
64
        populist = DirectBranchCommit(db_branch)
65
        last_revision = populist.bzrbranch.last_revision()
66
        db_branch.last_scanned_id = populist.last_scanned_id = last_revision
67
68
        if content_map is not None:
69
            for name, contents in content_map.iteritems():
70
                populist.writeFile(name, contents)
71
            populist.commit("Populating branch.")
72
73
        return db_branch
74
75
    def test_getBranch_bzr(self):
76
        # _getBranch can retrieve branch contents from a branch URL.
9590.1.110 by Michael Hudson
fix test_generate_translation_templates
77
        self.useBzrBranches(direct_database=True)
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
78
        marker_text = "Ceci n'est pas cet branch."
79
        branch = self._createBranch({'marker.txt': marker_text})
80
10427.20.11 by Henning Eggers
Test passing again.
81
        generator = GenerateTranslationTemplates(
9590.1.110 by Michael Hudson
fix test_generate_translation_templates
82
            branch.getInternalBzrUrl(), self.result_name,
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
83
            self.makeTemporaryDirectory(), log_file=StringIO())
10424.1.1 by Jeroen Vermeulen
Taught slave-side templates generation script to retrieve branch contents.
84
        generator.branch_dir = self.makeTemporaryDirectory()
85
        generator._getBranch()
86
87
        marker_file = file(os.path.join(generator.branch_dir, 'marker.txt'))
88
        self.assertEqual(marker_text, marker_file.read())
89
10427.23.6 by Henning Eggers
Saved the world.
90
    def test_templates_tarball(self):
10427.23.5 by Henning Eggers
Added zipup method and its test.
91
        # Create a tarball from pot files.
92
        workdir = self.makeTemporaryDirectory()
93
        branchdir = os.path.join(workdir, 'branchdir')
94
        dummy_tar = os.path.join(
95
            os.path.dirname(__file__),'dummy_templates.tar.gz')
96
        tar = tarfile.open(dummy_tar, 'r|*')
97
        tar.extractall(branchdir)
10637.2.11 by Max Bowsher
Deal with tarfile in 2.6 not using a trailing slash on directory member names.
98
        potnames = [member.name for member in tar.getmembers() if not member.isdir()]
10427.23.5 by Henning Eggers
Added zipup method and its test.
99
        tar.close()
100
10427.23.8 by Henning Eggers
Central place to define result name.
101
        generator = GenerateTranslationTemplates(
12144.1.2 by Jonathan Lange
Don't dump stuff to stderr
102
            branchdir, self.result_name, workdir, log_file=StringIO())
10427.23.5 by Henning Eggers
Added zipup method and its test.
103
        generator._getBranch()
10427.23.18 by Henning Eggers
Reviewer comments and suggestions.
104
        generator._makeTarball(potnames)
10427.23.8 by Henning Eggers
Central place to define result name.
105
        tar = tarfile.open(os.path.join(workdir, self.result_name), 'r|*')
10427.23.5 by Henning Eggers
Added zipup method and its test.
106
        tarnames = tar.getnames()
107
        tar.close()
108
        self.assertContentEqual(potnames, tarnames)
109
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
110
    def test_script(self):
111
        tempdir = self.makeTemporaryDirectory()
10427.20.11 by Henning Eggers
Test passing again.
112
        workdir = self.makeTemporaryDirectory()
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
113
        (retval, out, err) = run_script(
14339.1.1 by mbp at canonical
Delete canonical.buildd again
114
            os.path.join(
115
                os.path.dirname(pottery.__file__),
116
                'generate_translation_templates.py'),
10427.23.8 by Henning Eggers
Central place to define result name.
117
            args=[tempdir, self.result_name, workdir])
10154.8.3 by Jeroen Vermeulen
Working on script & testing.
118
        self.assertEqual(0, retval)