#!/usr/bin/python
# -*- coding: utf-8 -*-

# Copyright (C) 2013 Canonical Ltd.
# Author: Stéphane Graber <stgraber@ubuntu.com>

# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 3 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY 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/>.

# Dependencies:
#  - python2 (>= 2.7): python-gpgme, python-coverage
#  - python3 (>= 3.2): python3-gpgme

import glob
import os
import re
import shutil
import sys
import unittest

coverage = True
try:
    from coverage import coverage
    cov = coverage()
    cov.start()
except ImportError:
    print("No coverage report, make sure python-coverage is installed")
    coverage = False

sys.path.insert(0, 'lib')

if len(sys.argv) > 1:
    test_filter = sys.argv[1]
else:
    test_filter = ''

tests = [t[:-3] for t in os.listdir("tests")
         if t.startswith("test_") and t.endswith(".py") and
         re.search(test_filter, t)]
tests.sort()
suite = unittest.TestLoader().loadTestsFromNames(tests)
res = unittest.TextTestRunner(verbosity=2).run(suite)

if coverage:
    if os.path.exists("tests/coverage"):
        shutil.rmtree("tests/coverage")
    cov.stop()
    cov.html_report(include=glob.glob("lib/systemimage/*.py"),
                    directory='tests/coverage')
    print("")
    cov.report(include=glob.glob("lib/systemimage/*.py"))
