#!/usr/bin/env python3

import glob
import inspect
import logging
import optparse
import os
import sys
import unittest

localmodule = os.path.realpath(
    os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
print('localmodule: ' + localmodule)
if localmodule not in sys.path:
    sys.path.insert(0, localmodule)

import fdroidserver.common
import fdroidserver.metadata
import fdroidserver.scanner


class ScannerTest(unittest.TestCase):

    def setUp(self):
        logging.basicConfig(level=logging.INFO)
        self.basedir = os.path.join(localmodule, 'tests')

    def test_scan_source_files(self):
        source_files = os.path.join(self.basedir, 'source-files')
        projects = {
            'Zillode': 1,
            'firebase-suspect': 1,
            'org.mozilla.rocket': 3,
            'realm': 1,
        }
        for d in glob.glob(os.path.join(source_files, '*')):
            build = fdroidserver.metadata.Build()
            fatal_problems = fdroidserver.scanner.scan_source(d, build)
            should = projects.get(os.path.basename(d), 0)
            self.assertEqual(should, fatal_problems,
                             "%s should have %d errors!" % (d, should))


if __name__ == "__main__":
    os.chdir(os.path.dirname(__file__))

    parser = optparse.OptionParser()
    parser.add_option("-v", "--verbose", action="store_true", default=False,
                      help="Spew out even more information than normal")
    (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])

    newSuite = unittest.TestSuite()
    newSuite.addTest(unittest.makeSuite(ScannerTest))
    unittest.main(failfast=False)
