1
0
mirror of https://gitlab.com/fdroid/fdroidserver.git synced 2024-11-18 20:50:10 +01:00

tests: use standard dir setup so all tests start in same dir

This commit is contained in:
Hans-Christoph Steiner 2017-11-30 10:14:38 +01:00
parent 3ff4b656c6
commit 5b22ff7dc6
6 changed files with 96 additions and 97 deletions

View File

@ -3,6 +3,7 @@
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
import inspect import inspect
import logging
import optparse import optparse
import os import os
import re import re
@ -46,22 +47,27 @@ class BuildTest(unittest.TestCase):
self.assertTrue(os.path.exists(path)) self.assertTrue(os.path.exists(path))
self.assertTrue(os.path.isfile(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): def test_force_gradle_build_tools(self):
testsbase = os.path.join(os.path.dirname(__file__), '..', '.testfiles') testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
if not os.path.exists(testsbase):
os.makedirs(testsbase)
testsdir = tempfile.mkdtemp(prefix='test_adapt_gradle', dir=testsbase)
shutil.copytree(os.path.join(os.path.dirname(__file__), 'source-files'), 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' 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')) pattern = re.compile(bytes("buildToolsVersion[\s=]+'%s'\s+" % teststring, 'utf8'))
for p in ('source-files/fdroid/fdroidclient/build.gradle', for p in ('source-files/fdroid/fdroidclient/build.gradle',
'source-files/Zillode/syncthing-silk/build.gradle', 'source-files/Zillode/syncthing-silk/build.gradle',
'source-files/open-keychain/open-keychain/build.gradle', 'source-files/open-keychain/open-keychain/build.gradle',
'source-files/osmandapp/osmand/build.gradle', 'source-files/osmandapp/osmand/build.gradle',
'source-files/open-keychain/open-keychain/OpenKeychain/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() filedata = f.read()
self.assertIsNotNone(pattern.search(filedata)) self.assertIsNotNone(pattern.search(filedata))

View File

@ -170,11 +170,11 @@ class CommonTest(unittest.TestCase):
testint = 99999999 testint = 99999999
teststr = 'FAKE_STR_FOR_TESTING' 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'), 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 = dict()
config['sdk_path'] = os.getenv('ANDROID_HOME') config['sdk_path'] = os.getenv('ANDROID_HOME')
@ -201,13 +201,14 @@ class CommonTest(unittest.TestCase):
def getsrclib(self): def getsrclib(self):
return None 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() filedata = f.read()
self.assertIsNotNone(re.search("\s+compileSdkVersion %s\s+" % testint, filedata)) 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() filedata = f.read()
self.assertIsNone(re.search('android:debuggable', filedata)) self.assertIsNone(re.search('android:debuggable', filedata))
self.assertIsNotNone(re.search('android:versionName="%s"' % build.versionName, filedata)) self.assertIsNotNone(re.search('android:versionName="%s"' % build.versionName, filedata))
@ -529,4 +530,4 @@ if __name__ == "__main__":
newSuite = unittest.TestSuite() newSuite = unittest.TestSuite()
newSuite.addTest(unittest.makeSuite(CommonTest)) newSuite.addTest(unittest.makeSuite(CommonTest))
unittest.main(failfast=True) unittest.main(failfast=False)

View File

@ -3,6 +3,7 @@
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
import inspect import inspect
import logging
import optparse import optparse
import os import os
import requests import requests
@ -24,8 +25,15 @@ import import_proxy
class ImportTest(unittest.TestCase): class ImportTest(unittest.TestCase):
'''fdroid import''' '''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): def test_import_gitlab(self):
os.chdir(os.path.dirname(__file__))
# FDroidPopen needs some config to work # FDroidPopen needs some config to work
config = dict() config = dict()
fdroidserver.common.fill_config_defaults(config) fdroidserver.common.fill_config_defaults(config)

View File

@ -1,6 +1,7 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
import inspect import inspect
import logging
import optparse import optparse
import os import os
import sys import sys
@ -31,30 +32,33 @@ GP_FINGERPRINT = 'B7C2EEFD8DAC7806AF67DFCD92EB18126BC08312A7F2D6F3862E46013C7A61
class IndexTest(unittest.TestCase): class IndexTest(unittest.TestCase):
def setUp(self): 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 fdroidserver.common.config = None
config = fdroidserver.common.read_config(fdroidserver.common.options) config = fdroidserver.common.read_config(fdroidserver.common.options)
config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner') config['jarsigner'] = fdroidserver.common.find_sdk_tools_cmd('jarsigner')
fdroidserver.common.config = config fdroidserver.common.config = config
fdroidserver.signindex.config = config fdroidserver.signindex.config = config
@staticmethod def test_verify_jar_signature_succeeds(self):
def test_verify_jar_signature_succeeds(): source_dir = os.path.join(self.basedir, 'signindex')
basedir = os.path.dirname(__file__)
source_dir = os.path.join(basedir, 'signindex')
for f in ('testy.jar', 'guardianproject.jar'): for f in ('testy.jar', 'guardianproject.jar'):
testfile = os.path.join(source_dir, f) testfile = os.path.join(source_dir, f)
fdroidserver.common.verify_jar_signature(testfile) fdroidserver.common.verify_jar_signature(testfile)
def test_verify_jar_signature_fails(self): def test_verify_jar_signature_fails(self):
basedir = os.path.dirname(__file__) source_dir = os.path.join(self.basedir, 'signindex')
source_dir = os.path.join(basedir, 'signindex')
testfile = os.path.join(source_dir, 'unsigned.jar') testfile = os.path.join(source_dir, 'unsigned.jar')
with self.assertRaises(fdroidserver.index.VerificationException): with self.assertRaises(fdroidserver.index.VerificationException):
fdroidserver.common.verify_jar_signature(testfile) fdroidserver.common.verify_jar_signature(testfile)
def test_get_public_key_from_jar_succeeds(self): def test_get_public_key_from_jar_succeeds(self):
basedir = os.path.dirname(__file__) source_dir = os.path.join(self.basedir, 'signindex')
source_dir = os.path.join(basedir, 'signindex')
for f in ('testy.jar', 'guardianproject.jar'): for f in ('testy.jar', 'guardianproject.jar'):
testfile = os.path.join(source_dir, f) testfile = os.path.join(source_dir, f)
jar = zipfile.ZipFile(testfile) jar = zipfile.ZipFile(testfile)
@ -68,8 +72,7 @@ class IndexTest(unittest.TestCase):
self.assertTrue(fingerprint == GP_FINGERPRINT) self.assertTrue(fingerprint == GP_FINGERPRINT)
def test_get_public_key_from_jar_fails(self): def test_get_public_key_from_jar_fails(self):
basedir = os.path.dirname(__file__) source_dir = os.path.join(self.basedir, 'signindex')
source_dir = os.path.join(basedir, 'signindex')
testfile = os.path.join(source_dir, 'unsigned.jar') testfile = os.path.join(source_dir, 'unsigned.jar')
jar = zipfile.ZipFile(testfile) jar = zipfile.ZipFile(testfile)
with self.assertRaises(fdroidserver.index.VerificationException): with self.assertRaises(fdroidserver.index.VerificationException):
@ -226,9 +229,6 @@ class IndexTest(unittest.TestCase):
if __name__ == "__main__": if __name__ == "__main__":
if os.path.basename(os.getcwd()) != 'tests' and os.path.isdir('tests'):
os.chdir('tests')
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal") help="Spew out even more information than normal")

View File

@ -4,6 +4,7 @@
import glob import glob
import inspect import inspect
import logging
import optparse import optparse
import os import os
import random import random
@ -26,15 +27,20 @@ import fdroidserver.metadata
class MetadataTest(unittest.TestCase): class MetadataTest(unittest.TestCase):
'''fdroidserver/metadata.py''' '''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 test_read_metadata(self):
def _build_yaml_representer(dumper, data): def _build_yaml_representer(dumper, data):
'''Creates a YAML representation of a Build instance''' '''Creates a YAML representation of a Build instance'''
return dumper.represent_dict(data) return dumper.represent_dict(data)
testsdir = os.path.dirname(__file__)
os.chdir(testsdir)
self.maxDiff = None self.maxDiff = None
# these need to be set to prevent code running on None, only # 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) # yaml.dump(frommeta, f, default_flow_style=False)
def test_rewrite_yaml_fakeotaupdate(self): def test_rewrite_yaml_fakeotaupdate(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
# 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)
fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']}
# rewrite metadata # rewrite metadata
@ -82,12 +83,7 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(result.read(), orig.read()) self.assertEqual(result.read(), orig.read())
def test_rewrite_yaml_fdroidclient(self): def test_rewrite_yaml_fdroidclient(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
# 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)
fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']}
# rewrite metadata # rewrite metadata
@ -103,12 +99,7 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(result.read(), orig.read()) self.assertEqual(result.read(), orig.read())
def test_rewrite_yaml_special_build_params(self): def test_rewrite_yaml_special_build_params(self):
testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
# 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)
fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']} fdroidserver.common.config = {'accepted_formats': ['txt', 'yml']}
# rewrite metadata # rewrite metadata
@ -124,18 +115,13 @@ class MetadataTest(unittest.TestCase):
self.assertEqual(result.read(), orig.read()) self.assertEqual(result.read(), orig.read())
def test_read_metadata_sort_by_time(self): def test_read_metadata_sort_by_time(self):
# setup/reset test dir if necessary and setup params testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
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)
metadatadir = os.path.join(testdir, 'metadata') metadatadir = os.path.join(testdir, 'metadata')
os.makedirs(metadatadir) os.makedirs(metadatadir)
fdroidserver.common.config = {'accepted_formats': ['txt']} fdroidserver.common.config = {'accepted_formats': ['txt']}
randomlist = [] randomlist = []
randomapps = glob.glob(os.path.join(testbasedir, 'metadata', '*.txt')) randomapps = glob.glob(os.path.join(self.basedir, 'metadata', '*.txt'))
random.shuffle(randomapps) random.shuffle(randomapps)
i = 1 i = 1
for f in randomapps: for f in randomapps:

View File

@ -11,6 +11,7 @@
# #
import inspect import inspect
import logging
import optparse import optparse
import os import os
import sys import sys
@ -32,6 +33,14 @@ from fdroidserver.exception import FDroidException
class PublishTest(unittest.TestCase): class PublishTest(unittest.TestCase):
'''fdroidserver/publish.py''' '''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): def test_key_alias(self):
publish.config = {} publish.config = {}
self.assertEqual('a163ec9b', publish.key_alias('com.example.app')) self.assertEqual('a163ec9b', publish.key_alias('com.example.app'))
@ -77,10 +86,8 @@ class PublishTest(unittest.TestCase):
'com.example.anotherapp', 'com.example.anotherapp',
'org.org.org'] 'org.org.org']
with tempfile.TemporaryDirectory() as tmpdir: testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
orig_cwd = os.getcwd() os.chdir(testdir)
try:
os.chdir(tmpdir)
with open('config.py', 'w') as f: with open('config.py', 'w') as f:
pass pass
@ -108,8 +115,6 @@ class PublishTest(unittest.TestCase):
repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41" repo_key_sha256 = "c58460800c7b250a619c30c13b07b7359a43e5af71a4352d86c58ae18c9f6d41"
'''), f.read()) '''), f.read())
finally:
os.chdir(orig_cwd)
def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self): def test_store_and_load_fdroid_signing_key_fingerprints_with_missmatch(self):
common.config = {} common.config = {}
@ -122,21 +127,14 @@ class PublishTest(unittest.TestCase):
publish.config['repo_keyalias'] = 'repokey' publish.config['repo_keyalias'] = 'repokey'
publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad' publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad'
with tempfile.TemporaryDirectory() as tmpdir: testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir)
orig_cwd = os.getcwd() os.chdir(testdir)
try:
os.chdir(tmpdir)
publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2) publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2)
with self.assertRaises(FDroidException): with self.assertRaises(FDroidException):
common.load_stats_fdroid_signing_key_fingerprints() common.load_stats_fdroid_signing_key_fingerprints()
finally:
os.chdir(orig_cwd)
if __name__ == "__main__": if __name__ == "__main__":
if os.path.basename(os.getcwd()) != 'tests' and os.path.isdir('tests'):
os.chdir('tests')
parser = optparse.OptionParser() parser = optparse.OptionParser()
parser.add_option("-v", "--verbose", action="store_true", default=False, parser.add_option("-v", "--verbose", action="store_true", default=False,
help="Spew out even more information than normal") help="Spew out even more information than normal")