diff --git a/tests/build.TestCase b/tests/build.TestCase index b595f3fa..c9ffe97c 100755 --- a/tests/build.TestCase +++ b/tests/build.TestCase @@ -3,6 +3,7 @@ # http://www.drdobbs.com/testing/unit-testing-with-python/240165163 import inspect +import logging import optparse import os import re @@ -46,22 +47,27 @@ class BuildTest(unittest.TestCase): self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.isfile(path)) + def setUp(self): + logging.basicConfig(level=logging.DEBUG) + self.basedir = os.path.join(localmodule, 'tests') + self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) + if not os.path.exists(self.tmpdir): + os.makedirs(self.tmpdir) + os.chdir(self.basedir) + def test_force_gradle_build_tools(self): - testsbase = os.path.join(os.path.dirname(__file__), '..', '.testfiles') - if not os.path.exists(testsbase): - os.makedirs(testsbase) - testsdir = tempfile.mkdtemp(prefix='test_adapt_gradle', dir=testsbase) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) shutil.copytree(os.path.join(os.path.dirname(__file__), 'source-files'), - os.path.join(testsdir, 'source-files')) + os.path.join(testdir, 'source-files')) teststring = 'FAKE_VERSION_FOR_TESTING' - fdroidserver.build.force_gradle_build_tools(testsdir, teststring) + fdroidserver.build.force_gradle_build_tools(testdir, teststring) pattern = re.compile(bytes("buildToolsVersion[\s=]+'%s'\s+" % teststring, 'utf8')) for p in ('source-files/fdroid/fdroidclient/build.gradle', 'source-files/Zillode/syncthing-silk/build.gradle', 'source-files/open-keychain/open-keychain/build.gradle', 'source-files/osmandapp/osmand/build.gradle', 'source-files/open-keychain/open-keychain/OpenKeychain/build.gradle'): - with open(os.path.join(testsdir, p), 'rb') as f: + with open(os.path.join(testdir, p), 'rb') as f: filedata = f.read() self.assertIsNotNone(pattern.search(filedata)) diff --git a/tests/common.TestCase b/tests/common.TestCase index 87e9da74..dfb4380a 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -170,11 +170,11 @@ class CommonTest(unittest.TestCase): testint = 99999999 teststr = 'FAKE_STR_FOR_TESTING' - tmptestsdir = tempfile.mkdtemp(prefix='test_prepare_sources', dir=self.tmpdir) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) shutil.copytree(os.path.join(self.basedir, 'source-files'), - os.path.join(tmptestsdir, 'source-files')) + os.path.join(testdir, 'source-files')) - testdir = os.path.join(tmptestsdir, 'source-files', 'fdroid', 'fdroidclient') + fdroidclient_testdir = os.path.join(testdir, 'source-files', 'fdroid', 'fdroidclient') config = dict() config['sdk_path'] = os.getenv('ANDROID_HOME') @@ -201,13 +201,14 @@ class CommonTest(unittest.TestCase): def getsrclib(self): return None - fdroidserver.common.prepare_source(FakeVcs(), app, build, testdir, testdir, testdir) + fdroidserver.common.prepare_source(FakeVcs(), app, build, + fdroidclient_testdir, fdroidclient_testdir, fdroidclient_testdir) - with open(os.path.join(testdir, 'build.gradle'), 'r') as f: + with open(os.path.join(fdroidclient_testdir, 'build.gradle'), 'r') as f: filedata = f.read() self.assertIsNotNone(re.search("\s+compileSdkVersion %s\s+" % testint, filedata)) - with open(os.path.join(testdir, 'AndroidManifest.xml')) as f: + with open(os.path.join(fdroidclient_testdir, 'AndroidManifest.xml')) as f: filedata = f.read() self.assertIsNone(re.search('android:debuggable', filedata)) self.assertIsNotNone(re.search('android:versionName="%s"' % build.versionName, filedata)) @@ -529,4 +530,4 @@ if __name__ == "__main__": newSuite = unittest.TestSuite() newSuite.addTest(unittest.makeSuite(CommonTest)) - unittest.main(failfast=True) + unittest.main(failfast=False) diff --git a/tests/import.TestCase b/tests/import.TestCase index a41028c9..76427c59 100755 --- a/tests/import.TestCase +++ b/tests/import.TestCase @@ -3,6 +3,7 @@ # http://www.drdobbs.com/testing/unit-testing-with-python/240165163 import inspect +import logging import optparse import os import requests @@ -24,8 +25,15 @@ import import_proxy class ImportTest(unittest.TestCase): '''fdroid import''' + def setUp(self): + logging.basicConfig(level=logging.DEBUG) + self.basedir = os.path.join(localmodule, 'tests') + self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) + if not os.path.exists(self.tmpdir): + os.makedirs(self.tmpdir) + os.chdir(self.basedir) + def test_import_gitlab(self): - os.chdir(os.path.dirname(__file__)) # FDroidPopen needs some config to work config = dict() fdroidserver.common.fill_config_defaults(config) diff --git a/tests/index.TestCase b/tests/index.TestCase index 4a7463fb..671370c3 100755 --- a/tests/index.TestCase +++ b/tests/index.TestCase @@ -1,6 +1,7 @@ #!/usr/bin/env python3 import inspect +import logging import optparse import os import sys @@ -31,30 +32,33 @@ GP_FINGERPRINT = 'B7C2EEFD8DAC7806AF67DFCD92EB18126BC08312A7F2D6F3862E46013C7A61 class IndexTest(unittest.TestCase): def setUp(self): + logging.basicConfig(level=logging.DEBUG) + self.basedir = os.path.join(localmodule, 'tests') + self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) + if not os.path.exists(self.tmpdir): + os.makedirs(self.tmpdir) + os.chdir(self.basedir) + fdroidserver.common.config = None config = fdroidserver.common.read_config(fdroidserver.common.options) config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner') fdroidserver.common.config = config fdroidserver.signindex.config = config - @staticmethod - def test_verify_jar_signature_succeeds(): - basedir = os.path.dirname(__file__) - source_dir = os.path.join(basedir, 'signindex') + def test_verify_jar_signature_succeeds(self): + source_dir = os.path.join(self.basedir, 'signindex') for f in ('testy.jar', 'guardianproject.jar'): testfile = os.path.join(source_dir, f) fdroidserver.common.verify_jar_signature(testfile) def test_verify_jar_signature_fails(self): - basedir = os.path.dirname(__file__) - source_dir = os.path.join(basedir, 'signindex') + source_dir = os.path.join(self.basedir, 'signindex') testfile = os.path.join(source_dir, 'unsigned.jar') with self.assertRaises(fdroidserver.index.VerificationException): fdroidserver.common.verify_jar_signature(testfile) def test_get_public_key_from_jar_succeeds(self): - basedir = os.path.dirname(__file__) - source_dir = os.path.join(basedir, 'signindex') + source_dir = os.path.join(self.basedir, 'signindex') for f in ('testy.jar', 'guardianproject.jar'): testfile = os.path.join(source_dir, f) jar = zipfile.ZipFile(testfile) @@ -68,8 +72,7 @@ class IndexTest(unittest.TestCase): self.assertTrue(fingerprint == GP_FINGERPRINT) def test_get_public_key_from_jar_fails(self): - basedir = os.path.dirname(__file__) - source_dir = os.path.join(basedir, 'signindex') + source_dir = os.path.join(self.basedir, 'signindex') testfile = os.path.join(source_dir, 'unsigned.jar') jar = zipfile.ZipFile(testfile) with self.assertRaises(fdroidserver.index.VerificationException): @@ -226,9 +229,6 @@ class IndexTest(unittest.TestCase): if __name__ == "__main__": - if os.path.basename(os.getcwd()) != 'tests' and os.path.isdir('tests'): - os.chdir('tests') - parser = optparse.OptionParser() parser.add_option("-v", "--verbose", action="store_true", default=False, help="Spew out even more information than normal") diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index a9fdd129..247f6a89 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -4,6 +4,7 @@ import glob import inspect +import logging import optparse import os import random @@ -26,15 +27,20 @@ import fdroidserver.metadata class MetadataTest(unittest.TestCase): '''fdroidserver/metadata.py''' + def setUp(self): + logging.basicConfig(level=logging.DEBUG) + self.basedir = os.path.join(localmodule, 'tests') + self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) + if not os.path.exists(self.tmpdir): + os.makedirs(self.tmpdir) + os.chdir(self.basedir) + def test_read_metadata(self): def _build_yaml_representer(dumper, data): '''Creates a YAML representation of a Build instance''' return dumper.represent_dict(data) - testsdir = os.path.dirname(__file__) - os.chdir(testsdir) - self.maxDiff = None # these need to be set to prevent code running on None, only @@ -61,12 +67,7 @@ class MetadataTest(unittest.TestCase): # yaml.dump(frommeta, f, default_flow_style=False) def test_rewrite_yaml_fakeotaupdate(self): - - # setup/reset test dir if necessary and setup params - tmpdir = os.path.join(os.path.dirname(__file__), '..', '.testfiles') - if not os.path.exists(tmpdir): - os.makedirs(tmpdir) - testdir = tempfile.mkdtemp(prefix='test_rewrite_metadata_', dir=tmpdir) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} # rewrite metadata @@ -82,12 +83,7 @@ class MetadataTest(unittest.TestCase): self.assertEqual(result.read(), orig.read()) def test_rewrite_yaml_fdroidclient(self): - - # setup/reset test dir if necessary and setup params - tmpdir = os.path.join(os.path.dirname(__file__), '..', '.testfiles') - if not os.path.exists(tmpdir): - os.makedirs(tmpdir) - testdir = tempfile.mkdtemp(prefix='test_rewrite_metadata_', dir=tmpdir) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} # rewrite metadata @@ -103,12 +99,7 @@ class MetadataTest(unittest.TestCase): self.assertEqual(result.read(), orig.read()) def test_rewrite_yaml_special_build_params(self): - - # setup/reset test dir if necessary and setup params - tmpdir = os.path.join(os.path.dirname(__file__), '..', '.testfiles') - if not os.path.exists(tmpdir): - os.makedirs(tmpdir) - testdir = tempfile.mkdtemp(prefix='test_rewrite_metadata_', dir=tmpdir) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} # rewrite metadata @@ -124,18 +115,13 @@ class MetadataTest(unittest.TestCase): self.assertEqual(result.read(), orig.read()) def test_read_metadata_sort_by_time(self): - # setup/reset test dir if necessary and setup params - testbasedir = os.path.dirname(__file__) - tmpdir = os.path.join(testbasedir, '..', '.testfiles') - if not os.path.exists(tmpdir): - os.makedirs(tmpdir) - testdir = tempfile.mkdtemp(prefix='test_read_metadata_sort_by_time_', dir=tmpdir) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) metadatadir = os.path.join(testdir, 'metadata') os.makedirs(metadatadir) fdroidserver.common.config = {'accepted_formats': ['txt']} randomlist = [] - randomapps = glob.glob(os.path.join(testbasedir, 'metadata', '*.txt')) + randomapps = glob.glob(os.path.join(self.basedir, 'metadata', '*.txt')) random.shuffle(randomapps) i = 1 for f in randomapps: diff --git a/tests/publish.TestCase b/tests/publish.TestCase index 8e165608..e296a48a 100755 --- a/tests/publish.TestCase +++ b/tests/publish.TestCase @@ -11,6 +11,7 @@ # import inspect +import logging import optparse import os import sys @@ -32,6 +33,14 @@ from fdroidserver.exception import FDroidException class PublishTest(unittest.TestCase): '''fdroidserver/publish.py''' + def setUp(self): + logging.basicConfig(level=logging.DEBUG) + self.basedir = os.path.join(localmodule, 'tests') + self.tmpdir = os.path.abspath(os.path.join(self.basedir, '..', '.testfiles')) + if not os.path.exists(self.tmpdir): + os.makedirs(self.tmpdir) + os.chdir(self.basedir) + def test_key_alias(self): publish.config = {} self.assertEqual('a163ec9b', publish.key_alias('com.example.app')) @@ -77,39 +86,35 @@ class PublishTest(unittest.TestCase): 'com.example.anotherapp', 'org.org.org'] - with tempfile.TemporaryDirectory() as tmpdir: - orig_cwd = os.getcwd() - try: - os.chdir(tmpdir) - with open('config.py', 'w') as f: - pass + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) + os.chdir(testdir) + with open('config.py', 'w') as f: + pass - publish.store_stats_fdroid_signing_key_fingerprints(appids, indent=2) + publish.store_stats_fdroid_signing_key_fingerprints(appids, indent=2) - self.maxDiff = None - expected = { - "com.example.anotherapp": { - "signer": "fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3" - }, - "com.example.app": { - "signer": "d34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4" - }, - "org.org.org": { - "signer": "277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82" - }, - "org.test.testy": { - "signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c" - } - } - self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints()) + self.maxDiff = None + expected = { + "com.example.anotherapp": { + "signer": "fa3f6a017541ee7fe797be084b1bcfbf92418a7589ef1f7fdeb46741b6d2e9c3" + }, + "com.example.app": { + "signer": "d34f678afbaa8f2fa6cc0edd6f0c2d1d2e2e9eb08bea521b24c740806016bff4" + }, + "org.org.org": { + "signer": "277655a6235bc6b0ef2d824396c51ba947f5ebc738c293d887e7083ff338af82" + }, + "org.test.testy": { + "signer": "6ae5355157a47ddcc3834a71f57f6fb5a8c2621c8e0dc739e9ddf59f865e497c" + } + } + self.assertEqual(expected, common.load_stats_fdroid_signing_key_fingerprints()) - with open('config.py', 'r') as f: - self.assertEqual(textwrap.dedent('''\ + with open('config.py', 'r') as f: + self.assertEqual(textwrap.dedent('''\ - repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41" - '''), f.read()) - finally: - os.chdir(orig_cwd) + repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41" + '''), f.read()) def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self): common.config = {} @@ -122,21 +127,14 @@ class PublishTest(unittest.TestCase): publish.config['repo_keyalias'] = 'repokey' publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad' - with tempfile.TemporaryDirectory() as tmpdir: - orig_cwd = os.getcwd() - try: - os.chdir(tmpdir) - publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2) - with self.assertRaises(FDroidException): - common.load_stats_fdroid_signing_key_fingerprints() - finally: - os.chdir(orig_cwd) + testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) + os.chdir(testdir) + publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2) + with self.assertRaises(FDroidException): + common.load_stats_fdroid_signing_key_fingerprints() if __name__ == "__main__": - if os.path.basename(os.getcwd()) != 'tests' and os.path.isdir('tests'): - os.chdir('tests') - parser = optparse.OptionParser() parser.add_option("-v", "--verbose", action="store_true", default=False, help="Spew out even more information than normal")