File: test_debhelper.py

package info (click to toggle)
user-session-migration 0.5.0
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 196 kB
  • sloc: python: 281; ansic: 279; perl: 27; makefile: 11; sh: 10
file content (102 lines) | stat: -rw-r--r-- 4,038 bytes parent folder | download
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/python
# Copyright (C) 2012 Canonical
#
# Authors:
#  Didier Roche <didrocks@ubuntu.com>
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Lesser General Public License as published
# by the Free Software Foundation;  either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

import logging
import os
import shutil
import subprocess
import tempfile
import unittest

log = logging.getLogger(__name__)

class DebhelperTests(unittest.TestCase):

    def setUp(self):
        self.srcdir = os.path.abspath(os.path.join(
            os.path.dirname(__file__), 'data', 'melticecream'))
        self.workdir = tempfile.mkdtemp()
        self.pkgdir = os.path.join(self.workdir, 'melticecream')
        shutil.copytree(self.srcdir, self.pkgdir)

        env = os.environ.copy()

        # do not depend on host induced build options
        try:
            del env['DEB_BUILD_OPTIONS']
        except KeyError:
            pass
        try:
            del env['DH_INTERNAL_OPTIONS']
        except KeyError:
            pass

        # point to local debhelper sequencer
        d = os.path.join(self.workdir, 'Debian', 'Debhelper', 'Sequence')
        os.makedirs(d)
        shutil.copy(os.path.join(os.path.dirname(__file__), '..',
                                 'debhelper', 'user_session_migration.pm'), d)
        env['PERLLIB'] = '%s:%s' % (self.workdir, env.get('PERLLIB', ''))

        # make dh_user-session-migration available in $PATH
        shutil.copy(os.path.join(os.path.dirname(__file__), '..',
                                 'debhelper', 'dh_user-session-migration'),
                    os.path.join(self.workdir, 'dh_user-session-migration'))
        env['PATH'] = self.workdir + ':' + env.get('PATH', '')
        self.env = env

    def tearDown(self):
        shutil.rmtree(self.workdir)

    def buildpackage(self, env):
        '''helper to build a package'''
        build = subprocess.Popen(['dpkg-buildpackage', '-us', '-uc', '-b'],
        stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
        cwd=self.pkgdir, env=env)
        return build.communicate()[0].decode()

    def test_build_with_migrations(self):
        '''build the package with migration argument'''

        log.debug(self.env)

        stdout = self.buildpackage(self.env)
        self.assertTrue("dh_user-session-migration" in stdout)

        # check the scripts are installed and executable
        scripts_path = os.path.join(self.pkgdir, 'debian/vanilla/usr/share/user-session-migration/scripts')
        self.assertTrue(os.path.isdir(scripts_path))
        self.assertTrue(os.path.isfile(os.path.join(scripts_path, "script1.sh")))
        self.assertTrue(os.path.isfile(os.path.join(scripts_path, "script2.sh")))
        self.assertTrue(os.access(os.path.join(scripts_path, "script1.sh"), os.X_OK))
        self.assertTrue(os.access(os.path.join(scripts_path, "script2.sh"), os.X_OK))

        # check the dep was added:
        with open(os.path.join(self.pkgdir, 'debian/vanilla/DEBIAN/control')) as f:
            self.assertEqual(f.read().count("user-session-migration"), 1)

    def test_build_with_missing_script(self):
        '''ensure assert when there is a typo in the script path or doesn't exist'''
        os.remove(os.path.join(self.pkgdir, "script1.sh"))
        stdout = self.buildpackage(self.env)
        self.assertTrue("dh_user-session-migration: error: install -p -m755 script1.sh debian/vanilla/usr/share/user-session-migration/scripts returned exit code 1" in stdout)


if __name__ == '__main__':
    unittest.main()