chiark / gitweb /
tests: use standard dir setup so all tests start in same dir
authorHans-Christoph Steiner <hans@eds.org>
Thu, 30 Nov 2017 09:14:38 +0000 (10:14 +0100)
committerHans-Christoph Steiner <hans@eds.org>
Thu, 30 Nov 2017 16:32:53 +0000 (17:32 +0100)
tests/build.TestCase
tests/common.TestCase
tests/import.TestCase
tests/index.TestCase
tests/metadata.TestCase
tests/publish.TestCase

index b595f3fab7dc32588fd5924aa730d90a9addf68c..c9ffe97c5bcc067e5176d3a094a281510c4786a9 100755 (executable)
@@ -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))
 
index 87e9da747b880e16c474f76df1ff25917e8d18f9..dfb4380a23e9317d0e8ff4fff9dbc9741c7300f1 100755 (executable)
@@ -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)
index a41028c91c81371c0c652c5803527a44fc4365de..76427c5932672be789aa6d6720dd1a30fbbed878 100755 (executable)
@@ -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)
index 4a7463fb523d37aeccd829d677f81644a7db23af..671370c3e318aeb140dd57d93a6f7c5579ad3742 100755 (executable)
@@ -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")
index a9fdd12969370530c996cacfaebb6fdce86b33aa..247f6a89facf8d946776ffc5520ba1459bd35cd6 100755 (executable)
@@ -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:
index 8e165608682141205ad79a25d8c3b1bd0e2b00d9..e296a48a038749d1f1b282319e0f56f256facc63 100755 (executable)
@@ -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
-
-                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())
-
-                with open('config.py', 'r') as f:
-                    self.assertEqual(textwrap.dedent('''\
-
-                        repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41"
-                        '''), f.read())
-            finally:
-                os.chdir(orig_cwd)
+        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)
+
+        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('''\
+
+                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")