#!/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()
