#!/usr/bin/python3

import copy
import importlib.machinery
import importlib.util
import os
import pathlib
import subprocess
import sys
import unittest

from qmldependstestcase import QmlDependsTestCase, find_pkg_root

def load_module(module_name, file_path):
    loader = importlib.machinery.SourceFileLoader(module_name, str(file_path))
    spec = importlib.util.spec_from_loader(module_name, loader)
    foobar = importlib.util.module_from_spec(spec)
    sys.modules[module_name] = foobar
    spec.loader.exec_module(foobar)

if os.environ.get("PKG_KDE_TOOLS_TEST_INSTALLED", False):
    load_module("dh_qmldeps", "/usr/bin/dh_qmldeps")
else:
    load_module("dh_qmldeps", find_pkg_root()/"dh_qmldeps")

import dh_qmldeps

class TestDHQmlDeps(QmlDependsTestCase):
    def test_noargs_singlepkg(self):
        with self.assertLogs('dh_qmldeps', level='ERROR') as cm:
            with self.assertRaises(SystemExit):
                dh_qmldeps.main([])
        self.assertEqual(cm.output,["ERROR:dh_qmldeps:Missing QML module IAmMissing",])

        (pathlib.Path(self.tempdir.name)/"debian/qmldeps.optional").write_text("IAmMissing foo\n")
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "foo, qml6-module-qtquick, qml6-module-qttest")

    def test_arch_all(self):
        (pathlib.Path(self.tempdir.name)/"debian/qmldeps.optional").write_text("IAmMissing foo\n")
        dh_qmldeps.main(["-i"])
        self.check_substvar("qml6:Depends", "foo, qml6-module-qtquick, qml6-module-qttest")

        pkg = self.dc.packages["testpackage"]
        pkg['Architecture'] = "any"
        self.writeControlFile()
        (pathlib.Path(self.tempdir.name)/"debian/testpackage.substvars").unlink()

        dh_qmldeps.main(["-i"])
        self.assertEqual((pathlib.Path(self.tempdir.name)/"debian/testpackage.substvars").exists(), False)

    def test_arch_any(self):
        (pathlib.Path(self.tempdir.name)/"debian/qmldeps.optional").write_text("IAmMissing foo\n")
        dh_qmldeps.main(["-a"])
        self.assertEqual((pathlib.Path(self.tempdir.name)/"debian/testpackage.substvars").exists(), False)

        pkg = self.dc.packages["testpackage"]
        pkg['Architecture'] = "any"
        self.writeControlFile()

        dh_qmldeps.main(["-a"])
        self.check_substvar("qml6:Depends", "foo, qml6-module-qtquick, qml6-module-qttest")

    def test_installed(self):
        subprocess.check_call(["fakeroot", "dh", "install"])
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "qml6-module-qtquick")
        self.check_substvar("qml6:Recommends", "qml6-module-qttest")

    def test_with_qmlfiles(self):
        (pathlib.Path(self.tempdir.name)/"debian/testpackage.qmlfiles").write_text("-qmlFiles test.qml")
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "qml6-module-qttest")

    def test_with_qmlfiles_vars(self):
        subprocess.check_call(["fakeroot", "dh", "install"])
        (pathlib.Path(self.tempdir.name)/"debian/testpackage.qmlfiles").write_text("-qmlFiles ${QMLDEPS_AUTODETECT_SOURCE_FILES}")
        (pathlib.Path(self.tempdir.name)/"debian/qmldeps.optional").write_text("IAmMissing foo\n")
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "foo, qml6-module-qtquick, qml6-module-qttest")

    def test_qmlfiles_examples(self):
        subprocess.check_call(["fakeroot", "dh", "install"])
        (pathlib.Path(self.tempdir.name)/"debian/testpackage.qmlfiles").write_text("-d Depends -qmlFiles ${QMLDEPS_AUTODETECT_FILES_EXAMPLES} -d Suggests -qmlFiles ${QMLDEPS_AUTODETECT_FILES_LEFTOVER}")
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "qml6-module-qttest")
        self.check_substvar("qml6:Suggests", "qml6-module-qtquick")

    def test_with_qmlfiles_vars_and_fields(self):
        subprocess.check_call(["fakeroot", "dh", "install"])
        (pathlib.Path(self.tempdir.name)/"debian/testpackage.qmlfiles").write_text("-d Recommends -qmlFiles test.qml -d Suggests -qmlFiles ${QMLDEPS_AUTODETECT_FILES}")
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "")
        self.check_substvar("qml6:Recommends", "qml6-module-qttest")
        self.check_substvar("qml6:Suggests", "qml6-module-qtquick, qml6-module-qttest")

    def test_multiple_packages(self):
        pkg = copy.deepcopy(self.dc.packages["testpackage"])
        self.dc.packages['secondpackage'] = pkg
        pkg['Package'] = "secondpackage"
        self.writeControlFile()

        (pathlib.Path(self.tempdir.name)/"debian/secondpackage.install").write_text("both.qml /usr/share/pkg/\n")

        subprocess.check_call(["fakeroot", "dh", "install"])
        dh_qmldeps.main([])
        self.check_substvar("qml6:Depends", "qml6-module-qtquick")
        self.check_substvar("qml6:Recommends", "qml6-module-qttest")
        self.check_substvar("qml6:Depends", "qml6-module-qtquick, qml6-module-qttest", pkg_name="secondpackage")

    @unittest.skipUnless(find_pkg_root() == pathlib.Path("/usr/share/pkg-kde-tools"),
                         "only works if we test the installed pkg-kde-tools")
    def test_dkpgbuildpackage(self):
        subprocess.check_call(["dpkg-buildpackage"])
        self.check_substvar("qml6:Depends", "qml6-module-qtquick")
        self.check_substvar("qml6:Recommends", "qml6-module-qttest")


if __name__ == "__main__":
    unittest.main(verbosity=2)
