From d29a486e31e1b0fc0a71251308d4cf634cbc7ab2 Mon Sep 17 00:00:00 2001 From: Jochen Sprickerhof Date: Tue, 22 Nov 2022 17:17:45 +0100 Subject: [PATCH] tests: use context manager and/or standard setup temp files --- tests/build.TestCase | 189 ++++++++-------- tests/common.TestCase | 360 +++++++++++-------------------- tests/import_subcommand.TestCase | 39 ++-- tests/index.TestCase | 33 ++- tests/init.TestCase | 24 +-- tests/metadata.TestCase | 70 +++--- tests/nightly.TestCase | 1 + tests/publish.TestCase | 54 ++--- tests/scanner.TestCase | 42 ++-- tests/testcommon.py | 8 + tests/update.TestCase | 309 +++++++++++--------------- tests/vcs.TestCase | 16 +- 12 files changed, 465 insertions(+), 680 deletions(-) diff --git a/tests/build.TestCase b/tests/build.TestCase index 0bfee1be..b6ceee10 100755 --- a/tests/build.TestCase +++ b/tests/build.TestCase @@ -22,11 +22,14 @@ print('localmodule: ' + localmodule) if localmodule not in sys.path: sys.path.insert(0, localmodule) +from testcommon import TmpCwd + import fdroidserver.build import fdroidserver.common import fdroidserver.metadata import fdroidserver.scanner import fdroidserver.vmtools +from testcommon import mkdtemp class FakeProcess: @@ -45,12 +48,15 @@ class BuildTest(unittest.TestCase): logger = logging.getLogger('androguard.axml') logger.setLevel(logging.INFO) # tame the axml debug messages 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.build.config = None + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + os.chdir(self.basedir) + self._td.cleanup() def create_fake_android_home(self, d): os.makedirs(os.path.join(d, 'build-tools'), exist_ok=True) @@ -210,106 +216,100 @@ class BuildTest(unittest.TestCase): def test_build_local_ndk(self): """Test if `fdroid build` detects installed NDKs and auto-installs when missing""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + with tempfile.TemporaryDirectory() as testdir, TmpCwd( + testdir + ), tempfile.TemporaryDirectory() as sdk_path: + config = {'ndk_paths': {}, 'sdk_path': sdk_path} + fdroidserver.common.config = config + fdroidserver.build.config = config + fdroidserver.build.options = mock.Mock() + fdroidserver.build.options.scan_binary = False + fdroidserver.build.options.notarball = True + fdroidserver.build.options.skipscan = True - config = {'ndk_paths': {}, 'sdk_path': tempfile.mkdtemp(prefix='android-sdk-')} - fdroidserver.common.config = config - fdroidserver.build.config = config - fdroidserver.build.options = mock.Mock() - fdroidserver.build.options.scan_binary = False - fdroidserver.build.options.notarball = True - fdroidserver.build.options.skipscan = True + app = fdroidserver.metadata.App() + app.id = 'mocked.app.id' + build = fdroidserver.metadata.Build() + build.commit = '1.0' + build.output = app.id + '.apk' + build.versionCode = 1 + build.versionName = '1.0' + build.ndk = 'r21e' # aka 21.4.7075529 + vcs = mock.Mock() - app = fdroidserver.metadata.App() - app.id = 'mocked.app.id' - build = fdroidserver.metadata.Build() - build.commit = '1.0' - build.output = app.id + '.apk' - build.versionCode = 1 - build.versionName = '1.0' - build.ndk = 'r21e' # aka 21.4.7075529 - vcs = mock.Mock() + def make_fake_apk(output, build): + with open(build.output, 'w') as fp: + fp.write('APK PLACEHOLDER') + return output - def make_fake_apk(output, build): - with open(build.output, 'w') as fp: - fp.write('APK PLACEHOLDER') - return output + def fake_download_file(_ignored, local_filename): + _ignored # silence the linters + with zipfile.ZipFile(local_filename, 'x') as zipfp: + zipfp.writestr( + 'android-ndk-r21e/source.properties', + 'Pkg.Revision = 21.4.7075529\n', + ) - def fake_download_file(_ignored, local_filename): - _ignored # silence the linters - with zipfile.ZipFile(local_filename, 'x') as zipfp: - zipfp.writestr( - 'android-ndk-r21e/source.properties', - 'Pkg.Revision = 21.4.7075529\n', - ) - - # use "as _ignored" just to make a pretty layout - with mock.patch( - 'fdroidserver.common.replace_build_vars', wraps=make_fake_apk - ) as _ignored, mock.patch( - 'fdroidserver.common.get_native_code', return_value='x86' - ) as _ignored, mock.patch( - 'fdroidserver.common.get_apk_id', - return_value=(app.id, build.versionCode, build.versionName), - ) as _ignored, mock.patch( - 'fdroidserver.common.is_apk_and_debuggable', return_value=False - ) as _ignored, mock.patch( - 'fdroidserver.common.sha256sum', - return_value='ad7ce5467e18d40050dc51b8e7affc3e635c85bd8c59be62de32352328ed467e', - ) as _ignored, mock.patch( - 'fdroidserver.common.is_apk_and_debuggable', return_value=False - ) as _ignored, mock.patch( - 'fdroidserver.build.FDroidPopen', FakeProcess - ) as _ignored, mock.patch( - 'fdroidserver.net.download_file', wraps=fake_download_file - ) as _ignored: - _ignored # silence the linters - with self.assertRaises( - fdroidserver.exception.FDroidException, - msg="No NDK setup, `fdroid build` should fail with error", - ): + # use "as _ignored" just to make a pretty layout + with mock.patch( + 'fdroidserver.common.replace_build_vars', wraps=make_fake_apk + ) as _ignored, mock.patch( + 'fdroidserver.common.get_native_code', return_value='x86' + ) as _ignored, mock.patch( + 'fdroidserver.common.get_apk_id', + return_value=(app.id, build.versionCode, build.versionName), + ) as _ignored, mock.patch( + 'fdroidserver.common.is_apk_and_debuggable', return_value=False + ) as _ignored, mock.patch( + 'fdroidserver.common.sha256sum', + return_value='ad7ce5467e18d40050dc51b8e7affc3e635c85bd8c59be62de32352328ed467e', + ) as _ignored, mock.patch( + 'fdroidserver.common.is_apk_and_debuggable', return_value=False + ) as _ignored, mock.patch( + 'fdroidserver.build.FDroidPopen', FakeProcess + ) as _ignored, mock.patch( + 'fdroidserver.net.download_file', wraps=fake_download_file + ) as _ignored: + _ignored # silence the linters + with self.assertRaises( + fdroidserver.exception.FDroidException, + msg="No NDK setup, `fdroid build` should fail with error", + ): + fdroidserver.build.build_local( + app, + build, + vcs, + build_dir=testdir, + output_dir=testdir, + log_dir=None, + srclib_dir=None, + extlib_dir=None, + tmp_dir=None, + force=False, + onserver=False, + refresh=False, + ) + # now run `fdroid build --onserver` + self.assertTrue('r21e' not in config['ndk_paths']) fdroidserver.build.build_local( app, build, vcs, build_dir=testdir, output_dir=testdir, - log_dir=None, + log_dir=os.getcwd(), srclib_dir=None, extlib_dir=None, tmp_dir=None, force=False, - onserver=False, + onserver=True, refresh=False, ) - # now run `fdroid build --onserver` - self.assertTrue('r21e' not in config['ndk_paths']) - fdroidserver.build.build_local( - app, - build, - vcs, - build_dir=testdir, - output_dir=testdir, - log_dir=os.getcwd(), - srclib_dir=None, - extlib_dir=None, - tmp_dir=None, - force=False, - onserver=True, - refresh=False, - ) - self.assertTrue(os.path.exists(config['ndk_paths']['r21e'])) + self.assertTrue(os.path.exists(config['ndk_paths']['r21e'])) def test_build_local_clean(self): """Test if `fdroid build` cleans ant and gradle build products""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -374,8 +374,8 @@ class BuildTest(unittest.TestCase): app, build, vcs, - build_dir=testdir, - output_dir=testdir, + build_dir=self.testdir, + output_dir=self.testdir, log_dir=None, srclib_dir=None, extlib_dir=None, @@ -396,10 +396,7 @@ class BuildTest(unittest.TestCase): self.assertFalse(os.path.exists('gradle-wrapper.jar')) def test_scan_with_extlib(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir("build") config = fdroidserver.common.get_config() @@ -446,11 +443,8 @@ class BuildTest(unittest.TestCase): def test_failed_verifies_are_not_in_unsigned(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - sdk_path = os.path.join(testdir, 'android-sdk') + os.chdir(self.testdir) + sdk_path = os.path.join(self.testdir, 'android-sdk') self.create_fake_android_home(sdk_path) with open('config.yml', 'w') as fp: yaml.dump({'sdk_path': sdk_path}, fp) @@ -567,10 +561,7 @@ class BuildTest(unittest.TestCase): else: self.assertFalse(flag in args, flag + ' should not be present') - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('tmp') chan = mock.MagicMock() diff --git a/tests/common.TestCase b/tests/common.TestCase index a5d8daf1..43ca5823 100755 --- a/tests/common.TestCase +++ b/tests/common.TestCase @@ -38,7 +38,7 @@ import fdroidserver.index import fdroidserver.signindex import fdroidserver.common import fdroidserver.metadata -from testcommon import TmpCwd +from testcommon import TmpCwd, mkdtemp from fdroidserver.exception import FDroidException, VCSException,\ MetaDataException, VerificationException @@ -60,8 +60,13 @@ class CommonTest(unittest.TestCase): fdroidserver.common.options.verbose = False self.path = os.environ['PATH'] self.android_home = os.environ.get('ANDROID_HOME') + self._td = mkdtemp() + self.testdir = self._td.name def tearDown(self): + os.chdir(self.basedir) + self._td.cleanup() + shutil.rmtree(self.tmpdir) os.environ['PATH'] = self.path if self.android_home: os.environ['ANDROID_HOME'] = self.android_home @@ -142,10 +147,7 @@ class CommonTest(unittest.TestCase): print('no build-tools found: ' + build_tools) def test_find_java_root_path(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) all_pathlists = [ ( @@ -282,16 +284,13 @@ class CommonTest(unittest.TestCase): testint = 99999999 teststr = 'FAKE_STR_FOR_TESTING' - 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(testdir, 'source-files'), + os.path.join(self.tmpdir, 'source-files'), ) fdroidclient_testdir = os.path.join( - testdir, 'source-files', 'fdroid', 'fdroidclient' + self.tmpdir, 'source-files', 'fdroid', 'fdroidclient' ) config = dict() @@ -340,10 +339,7 @@ class CommonTest(unittest.TestCase): @unittest.skipIf(os.name == 'nt', "`fdroid build` assumes POSIX scripting") def test_prepare_sources_with_prebuild_subdir(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - app_build_dir = os.path.join(testdir, 'build', 'com.example') + app_build_dir = os.path.join(self.testdir, 'build', 'com.example') shutil.copytree( os.path.join(self.basedir, 'source-files', 'fdroid', 'fdroidclient'), app_build_dir, @@ -360,7 +356,7 @@ class CommonTest(unittest.TestCase): fdroidserver.common.config = config srclibname = 'FakeSrcLib' - srclib_testdir = os.path.join(testdir, 'build', 'srclib') + srclib_testdir = os.path.join(self.testdir, 'build', 'srclib') os.makedirs(os.path.join(srclib_testdir, srclibname, 'testdirshouldexist')) fdroidserver.metadata.srclibs = { srclibname: { @@ -397,11 +393,7 @@ class CommonTest(unittest.TestCase): def test_prepare_sources_refresh(self): packageName = 'org.fdroid.ci.test.app' - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - print('testdir', testdir) - os.chdir(testdir) + os.chdir(self.tmpdir) os.mkdir('build') os.mkdir('metadata') @@ -419,7 +411,7 @@ class CommonTest(unittest.TestCase): with open(os.path.join('metadata', packageName + '.yml'), 'w') as fp: yaml.dump(metadata, fp) - gitrepo = os.path.join(testdir, 'build', packageName) + gitrepo = os.path.join(self.tmpdir, 'build', packageName) vcs0 = fdroidserver.common.getvcs('git', git_url, gitrepo) vcs0.gotorevision('0.3', refresh=True) vcs1 = fdroidserver.common.getvcs('git', git_url, gitrepo) @@ -445,18 +437,18 @@ class CommonTest(unittest.TestCase): fdroidserver.signindex.config = config sourcedir = os.path.join(self.basedir, 'signindex') - testsdir = tempfile.mkdtemp( + with tempfile.TemporaryDirectory( prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - for f in ('testy.jar', 'guardianproject.jar'): - sourcefile = os.path.join(sourcedir, f) - testfile = os.path.join(testsdir, f) - shutil.copy(sourcefile, testsdir) - fdroidserver.signindex.sign_jar(testfile, use_old_algs=True) - # these should be resigned, and therefore different - self.assertNotEqual( - open(sourcefile, 'rb').read(), open(testfile, 'rb').read() - ) + ) as testsdir: + for f in ('testy.jar', 'guardianproject.jar'): + sourcefile = os.path.join(sourcedir, f) + testfile = os.path.join(testsdir, f) + shutil.copy(sourcefile, testsdir) + fdroidserver.signindex.sign_jar(testfile, use_old_algs=True) + # these should be resigned, and therefore different + self.assertNotEqual( + open(sourcefile, 'rb').read(), open(testfile, 'rb').read() + ) def test_verify_apk_signature(self): config = fdroidserver.common.read_config(fdroidserver.common.options) @@ -519,19 +511,14 @@ class CommonTest(unittest.TestCase): sourceapk = os.path.join(self.basedir, 'urzip.apk') - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - print('testdir', testdir) - - copyapk = os.path.join(testdir, 'urzip-copy.apk') + copyapk = os.path.join(self.testdir, 'urzip-copy.apk') shutil.copy(sourceapk, copyapk) self.assertTrue(fdroidserver.common.verify_apk_signature(copyapk)) self.assertIsNone( fdroidserver.common.verify_apks(sourceapk, copyapk, self.tmpdir) ) - unsignedapk = os.path.join(testdir, 'urzip-unsigned.apk') + unsignedapk = os.path.join(self.testdir, 'urzip-unsigned.apk') with ZipFile(sourceapk, 'r') as apk: with ZipFile(unsignedapk, 'w') as testapk: for info in apk.infolist(): @@ -541,7 +528,7 @@ class CommonTest(unittest.TestCase): fdroidserver.common.verify_apks(sourceapk, unsignedapk, self.tmpdir) ) - twosigapk = os.path.join(testdir, 'urzip-twosig.apk') + twosigapk = os.path.join(self.testdir, 'urzip-twosig.apk') otherapk = ZipFile(os.path.join(self.basedir, 'urzip-release.apk'), 'r') with ZipFile(sourceapk, 'r') as apk: with ZipFile(twosigapk, 'w') as testapk: @@ -715,17 +702,14 @@ class CommonTest(unittest.TestCase): def test_find_apksigner_config_overrides(self): """apksigner should come from config before any auto-detection""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - android_home = os.path.join(testdir, 'ANDROID_HOME') + os.chdir(self.tmpdir) + android_home = os.path.join(self.tmpdir, 'ANDROID_HOME') do_not_use = os.path.join(android_home, 'build-tools', '30.0.3', 'apksigner') os.makedirs(os.path.dirname(do_not_use)) with open(do_not_use, 'w') as fp: fp.write('#!/bin/sh\ndate\n') os.chmod(do_not_use, 0o0755) - apksigner = os.path.join(testdir, 'apksigner') + apksigner = os.path.join(self.tmpdir, 'apksigner') config = {'apksigner': apksigner} os.environ['ANDROID_HOME'] = android_home os.environ['PATH'] = '%s:/usr/local/bin:/usr/bin:/bin' % android_home @@ -734,17 +718,13 @@ class CommonTest(unittest.TestCase): def test_find_apksigner_prefer_path(self): """apksigner should come from PATH before ANDROID_HOME""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - - apksigner = os.path.join(testdir, 'apksigner') + os.chdir(self.tmpdir) + apksigner = os.path.join(self.tmpdir, 'apksigner') with open(apksigner, 'w') as fp: fp.write('#!/bin/sh\ndate\n') os.chmod(apksigner, 0o0755) - android_home = os.path.join(testdir, 'ANDROID_HOME') + android_home = os.path.join(self.tmpdir, 'ANDROID_HOME') do_not_use = os.path.join(android_home, 'build-tools', '30.0.3', 'apksigner') os.makedirs(os.path.dirname(do_not_use)) with open(do_not_use, 'w') as fp: @@ -759,11 +739,8 @@ class CommonTest(unittest.TestCase): def test_find_apksigner_prefer_newest(self): """apksigner should be the newest available in ANDROID_HOME""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - android_home = os.path.join(testdir, 'ANDROID_HOME') + os.chdir(self.tmpdir) + android_home = os.path.join(self.tmpdir, 'ANDROID_HOME') apksigner = os.path.join(android_home, 'build-tools', '30.0.3', 'apksigner') os.makedirs(os.path.dirname(apksigner)) @@ -784,10 +761,7 @@ class CommonTest(unittest.TestCase): def test_find_apksigner_system_package_android_home(self): """Test that apksigner v30 or newer is found""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) android_home = os.getenv('ANDROID_HOME') if not android_home or not os.path.isdir(android_home): self.skipTest('SKIPPING since ANDROID_HOME (%s) is not a dir!' % android_home) @@ -825,12 +799,9 @@ class CommonTest(unittest.TestCase): fdroidserver.common.config = config fdroidserver.signindex.config = config - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - unsigned = os.path.join(testdir, 'urzip-release-unsigned.apk') - signed = os.path.join(testdir, 'urzip-release.apk') - shutil.copy(os.path.join(self.basedir, 'urzip-release-unsigned.apk'), testdir) + unsigned = os.path.join(self.testdir, 'urzip-release-unsigned.apk') + signed = os.path.join(self.testdir, 'urzip-release.apk') + shutil.copy(os.path.join(self.basedir, 'urzip-release-unsigned.apk'), self.testdir) self.assertFalse(fdroidserver.common.verify_apk_signature(unsigned)) @@ -840,8 +811,8 @@ class CommonTest(unittest.TestCase): self.assertTrue(fdroidserver.common.verify_apk_signature(signed)) # now sign an APK with minSdkVersion >= 18 - unsigned = os.path.join(testdir, 'duplicate.permisssions_9999999-unsigned.apk') - signed = os.path.join(testdir, 'duplicate.permisssions_9999999.apk') + unsigned = os.path.join(self.testdir, 'duplicate.permisssions_9999999-unsigned.apk') + signed = os.path.join(self.testdir, 'duplicate.permisssions_9999999.apk') shutil.copy( os.path.join(self.basedir, 'repo', 'duplicate.permisssions_9999999.apk'), os.path.join(unsigned), @@ -853,9 +824,9 @@ class CommonTest(unittest.TestCase): self.assertTrue(fdroidserver.common.verify_apk_signature(signed)) self.assertEqual('18', fdroidserver.common._get_androguard_APK(signed).get_min_sdk_version()) - shutil.copy(os.path.join(self.basedir, 'minimal_targetsdk_30_unsigned.apk'), testdir) - unsigned = os.path.join(testdir, 'minimal_targetsdk_30_unsigned.apk') - signed = os.path.join(testdir, 'minimal_targetsdk_30.apk') + shutil.copy(os.path.join(self.basedir, 'minimal_targetsdk_30_unsigned.apk'), self.testdir) + unsigned = os.path.join(self.testdir, 'minimal_targetsdk_30_unsigned.apk') + signed = os.path.join(self.testdir, 'minimal_targetsdk_30.apk') self.assertFalse(fdroidserver.common.verify_apk_signature(unsigned)) fdroidserver.common.sign_apk(unsigned, signed, config['keyalias']) @@ -866,17 +837,17 @@ class CommonTest(unittest.TestCase): # verify it has a v2 signature self.assertTrue(fdroidserver.common._get_androguard_APK(signed).is_signed_v2()) - shutil.copy(os.path.join(self.basedir, 'no_targetsdk_minsdk30_unsigned.apk'), testdir) - unsigned = os.path.join(testdir, 'no_targetsdk_minsdk30_unsigned.apk') - signed = os.path.join(testdir, 'no_targetsdk_minsdk30_signed.apk') + shutil.copy(os.path.join(self.basedir, 'no_targetsdk_minsdk30_unsigned.apk'), self.testdir) + unsigned = os.path.join(self.testdir, 'no_targetsdk_minsdk30_unsigned.apk') + signed = os.path.join(self.testdir, 'no_targetsdk_minsdk30_signed.apk') fdroidserver.common.sign_apk(unsigned, signed, config['keyalias']) self.assertTrue(fdroidserver.common.verify_apk_signature(signed)) self.assertTrue(fdroidserver.common._get_androguard_APK(signed).is_signed_v2()) - shutil.copy(os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk'), testdir) - unsigned = os.path.join(testdir, 'no_targetsdk_minsdk1_unsigned.apk') - signed = os.path.join(testdir, 'no_targetsdk_minsdk1_signed.apk') + shutil.copy(os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk'), self.testdir) + unsigned = os.path.join(self.testdir, 'no_targetsdk_minsdk1_unsigned.apk') + signed = os.path.join(self.testdir, 'no_targetsdk_minsdk1_signed.apk') self.assertFalse(fdroidserver.common.verify_apk_signature(unsigned)) fdroidserver.common.sign_apk(unsigned, signed, config['keyalias']) @@ -901,10 +872,7 @@ class CommonTest(unittest.TestCase): fdroidserver.common.config = config fdroidserver.signindex.config = config - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) os.mkdir('unsigned') os.mkdir('repo') @@ -1431,15 +1399,11 @@ class CommonTest(unittest.TestCase): self.assertEqual(fdroidserver.common.parse_srclib_spec('@multi@at-signs@')) def test_remove_signing_keys(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - print(testdir) shutil.copytree( os.path.join(self.basedir, 'source-files'), - os.path.join(testdir, 'source-files'), + os.path.join(self.tmpdir, 'source-files'), ) - os.chdir(testdir) + os.chdir(self.tmpdir) with_signingConfigs = [ 'source-files/com.seafile.seadroid2/app/build.gradle', 'source-files/eu.siacs.conversations/build.gradle', @@ -1608,14 +1572,11 @@ class CommonTest(unittest.TestCase): self.assertEqual(f.read(), mocklogcontent) def test_deploy_status_json(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - + os.chdir(self.tmpdir) fakesubcommand = 'fakesubcommand' fake_timestamp = 1234567890 fakeserver = 'example.com:/var/www/fbot/' - expected_dir = os.path.join(testdir, fakeserver.replace(':', ''), 'repo', 'status') + expected_dir = os.path.join(self.tmpdir, fakeserver.replace(':', ''), 'repo', 'status') fdroidserver.common.options = mock.Mock() fdroidserver.common.config = {} @@ -1623,7 +1584,7 @@ class CommonTest(unittest.TestCase): fdroidserver.common.config['identity_file'] = 'ssh/id_rsa' def assert_subprocess_call(cmd): - dest_path = os.path.join(testdir, cmd[-1].replace(':', '')) + dest_path = os.path.join(self.tmpdir, cmd[-1].replace(':', '')) if not os.path.exists(dest_path): os.makedirs(dest_path) return subprocess.run(cmd[:-1] + [dest_path]).returncode @@ -1779,10 +1740,7 @@ class CommonTest(unittest.TestCase): def test_with_no_config(self): """It should set defaults if no config file is found""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) self.assertFalse(os.path.exists('config.yml')) self.assertFalse(os.path.exists('config.py')) config = fdroidserver.common.read_config(fdroidserver.common.options) @@ -1791,8 +1749,7 @@ class CommonTest(unittest.TestCase): def test_with_zero_size_config(self): """It should set defaults if config file has nothing in it""" - testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) - os.chdir(testdir) + os.chdir(self.tmpdir) open('config.yml', 'w').close() self.assertTrue(os.path.exists('config.yml')) self.assertFalse(os.path.exists('config.py')) @@ -1802,10 +1759,7 @@ class CommonTest(unittest.TestCase): def test_with_config_yml(self): """Make sure it is possible to use config.yml alone.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('apksigner: yml') self.assertTrue(os.path.exists('config.yml')) @@ -1815,10 +1769,7 @@ class CommonTest(unittest.TestCase): def test_with_config_yml_utf8(self): """Make sure it is possible to use config.yml in UTF-8 encoding.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) teststr = '/πÇÇ现代通用字-български-عربي1/ö/yml' with open('config.yml', 'w', encoding='utf-8') as fp: fp.write('apksigner: ' + teststr) @@ -1829,10 +1780,7 @@ class CommonTest(unittest.TestCase): def test_with_config_yml_utf8_as_ascii(self): """Make sure it is possible to use config.yml Unicode encoded as ASCII.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) teststr = '/πÇÇ现代通用字-български-عربي1/ö/yml' with open('config.yml', 'w') as fp: yaml.dump({'apksigner': teststr}, fp) @@ -1843,10 +1791,7 @@ class CommonTest(unittest.TestCase): def test_with_config_yml_with_env_var(self): """Make sure it is possible to use config.yml alone.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) os.environ['SECRET'] = 'mysecretpassword' with open('config.yml', 'w') as fp: fp.write("""keypass: {'env': 'SECRET'}""") @@ -1857,10 +1802,7 @@ class CommonTest(unittest.TestCase): def test_with_config_py(self): """Make sure it is still possible to use config.py alone.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.py', 'w') as fp: fp.write('apksigner = "py"') self.assertFalse(os.path.exists('config.yml')) @@ -1870,10 +1812,7 @@ class CommonTest(unittest.TestCase): def test_config_perm_warning(self): """Exercise the code path that issues a warning about unsafe permissions.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('keystore: foo.jks') self.assertTrue(os.path.exists(fp.name)) @@ -1890,10 +1829,7 @@ class CommonTest(unittest.TestCase): def test_with_both_config_yml_py(self): """If config.yml and config.py are present, config.py should be ignored.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('apksigner: yml') with open('config.py', 'w') as fp: @@ -1905,10 +1841,7 @@ class CommonTest(unittest.TestCase): def test_config_repo_url(self): """repo_url ends in /repo, archive_url ends in /archive.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/repo\n') fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/archive') @@ -1918,10 +1851,7 @@ class CommonTest(unittest.TestCase): def test_config_repo_url_extra_slash(self): """repo_url ends in /repo, archive_url ends in /archive.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/repo/') with self.assertRaises(FDroidException): @@ -1929,10 +1859,7 @@ class CommonTest(unittest.TestCase): def test_config_repo_url_not_repo(self): """repo_url ends in /repo, archive_url ends in /archive.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('repo_url: https://MyFirstFDroidRepo.org/fdroid/foo') with self.assertRaises(FDroidException): @@ -1940,10 +1867,7 @@ class CommonTest(unittest.TestCase): def test_config_archive_url_extra_slash(self): """repo_url ends in /repo, archive_url ends in /archive.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/archive/') with self.assertRaises(FDroidException): @@ -1951,20 +1875,14 @@ class CommonTest(unittest.TestCase): def test_config_archive_url_not_repo(self): """repo_url ends in /repo, archive_url ends in /archive.""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('archive_url: https://MyFirstFDroidRepo.org/fdroid/foo') with self.assertRaises(FDroidException): fdroidserver.common.read_config() def test_write_to_config_yml(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('apksigner: yml') self.assertTrue(os.path.exists(fp.name)) @@ -1980,10 +1898,7 @@ class CommonTest(unittest.TestCase): self.assertEqual('mysecretpassword', config['keypass']) def test_write_to_config_py(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.py', 'w') as fp: fp.write('apksigner = "py"') self.assertTrue(os.path.exists(fp.name)) @@ -1997,10 +1912,7 @@ class CommonTest(unittest.TestCase): self.assertEqual('mysecretpassword', config['keypass']) def test_config_dict_with_int_keys(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write('java_paths:\n 8: /usr/lib/jvm/java-8-openjdk\n') self.assertTrue(os.path.exists(fp.name)) @@ -2010,17 +1922,14 @@ class CommonTest(unittest.TestCase): def test_loading_config_buildserver_yml(self): """Smoke check to make sure this file is properly parsed""" - testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) - os.chdir(testdir) + os.chdir(self.tmpdir) shutil.copy(os.path.join(self.basedir, '..', 'buildserver', 'config.buildserver.yml'), 'config.yml') self.assertFalse(os.path.exists('config.py')) fdroidserver.common.read_config(fdroidserver.common.options) def test_setup_status_output(self): - testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) - print(testdir) - os.chdir(testdir) + os.chdir(self.tmpdir) start_timestamp = time.gmtime() subcommand = 'test' @@ -2036,13 +1945,9 @@ class CommonTest(unittest.TestCase): self.assertEqual(subcommand, data['subcommand']) def test_setup_status_output_in_git_repo(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - + os.chdir(self.tmpdir) logging.getLogger('git.cmd').setLevel(logging.INFO) - git_repo = git.Repo.init(testdir) + git_repo = git.Repo.init(self.tmpdir) file_in_git = 'README.md' with open(file_in_git, 'w') as fp: fp.write('this is just a test') @@ -2153,11 +2058,8 @@ class CommonTest(unittest.TestCase): ) def test_apk_strip_v1_signatures(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) before = os.path.join(self.basedir, 'no_targetsdk_minsdk1_unsigned.apk') - after = os.path.join(testdir, 'after.apk') + after = os.path.join(self.testdir, 'after.apk') shutil.copy(before, after) fdroidserver.common.apk_strip_v1_signatures(after, strip_manifest=False) @@ -2229,13 +2131,10 @@ class CommonTest(unittest.TestCase): @unittest.skip("This test downloads and unzips a 1GB file.") def test_install_ndk(self): """NDK r10e is a special case since its missing source.properties""" - sdk_path = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - config = {'sdk_path': sdk_path} + config = {'sdk_path': self.tmpdir} fdroidserver.common.config = config fdroidserver.common._install_ndk('r10e') - r10e = os.path.join(sdk_path, 'ndk', 'r10e') + r10e = os.path.join(self.tmpdir, 'ndk', 'r10e') self.assertEqual('r10e', fdroidserver.common.get_ndk_version(r10e)) fdroidserver.common.fill_config_defaults(config) self.assertEqual({'r10e': r10e}, config['ndk_paths']) @@ -2248,31 +2147,31 @@ class CommonTest(unittest.TestCase): with ZipFile(zipball, 'w') as zipfp: zipfp.writestr(os.path.basename(url), url) - sdk_path = tempfile.mkdtemp( + with tempfile.TemporaryDirectory( prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - config = {'sdk_path': sdk_path} - fdroidserver.common.config = config - for r, sha256 in ( - ( - 'r10e', - 'ee5f405f3b57c4f5c3b3b8b5d495ae12b660e03d2112e4ed5c728d349f1e520c', - ), - ('r20', '57435158f109162f41f2f43d5563d2164e4d5d0364783a9a6fab3ef12cb06ce0'), - ( - '23.0.7599858', - 'e3eacf80016b91d4cd2c8ca9f34eebd32df912bb799c859cc5450b6b19277b4f', - ), - ): - with mock.patch( - 'fdroidserver.net.download_file', side_effect=fake_download - ) as _ignored, mock.patch( - 'fdroidserver.common.get_ndk_version', return_value=r - ) as _ignored, mock.patch( - 'fdroidserver.common.sha256sum', return_value=sha256 + ) as sdk_path: + config = {'sdk_path': sdk_path} + fdroidserver.common.config = config + for r, sha256 in ( + ( + 'r10e', + 'ee5f405f3b57c4f5c3b3b8b5d495ae12b660e03d2112e4ed5c728d349f1e520c', + ), + ('r20', '57435158f109162f41f2f43d5563d2164e4d5d0364783a9a6fab3ef12cb06ce0'), + ( + '23.0.7599858', + 'e3eacf80016b91d4cd2c8ca9f34eebd32df912bb799c859cc5450b6b19277b4f', + ), ): - _ignored # silence the linters - fdroidserver.common._install_ndk(r) + with mock.patch( + 'fdroidserver.net.download_file', side_effect=fake_download + ) as _ignored, mock.patch( + 'fdroidserver.common.get_ndk_version', return_value=r + ) as _ignored, mock.patch( + 'fdroidserver.common.sha256sum', return_value=sha256 + ): + _ignored # silence the linters + fdroidserver.common._install_ndk(r) def test_install_ndk_with_symlinks(self): """Some NDK zipballs might have symlinks in them.""" @@ -2314,10 +2213,7 @@ class CommonTest(unittest.TestCase): zipInfo.external_attr = unix_st_mode << 16 zipfp.writestr(zipInfo, 'foo/../../../../../../../../../etc/passwd') - sdk_path = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - config = {'sdk_path': sdk_path} + config = {'sdk_path': self.tmpdir} fdroidserver.common.config = config r = 'r20' sha256 = '57435158f109162f41f2f43d5563d2164e4d5d0364783a9a6fab3ef12cb06ce0' @@ -2331,40 +2227,36 @@ class CommonTest(unittest.TestCase): _ignored # silence the linters fdroidserver.common._install_ndk(r) - self.assertTrue(os.path.exists(os.path.join(sdk_path, 'ndk', 'r20'))) - self.assertTrue(os.path.exists(os.path.join(sdk_path, 'ndk', 'r20', 'basename'))) - self.assertFalse(os.path.exists(os.path.join(sdk_path, 'ndk', 'r20', 'bad_abs_link'))) - self.assertFalse(os.path.exists(os.path.join(sdk_path, 'ndk', 'r20', 'bad_rel_link'))) - self.assertFalse(os.path.exists(os.path.join(sdk_path, 'ndk', 'r20', 'bad_rel_link2'))) - os.system('ls -l ' + os.path.join(sdk_path, 'ndk', 'r20')) + self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'ndk', 'r20'))) + self.assertTrue(os.path.exists(os.path.join(self.tmpdir, 'ndk', 'r20', 'basename'))) + self.assertFalse(os.path.exists(os.path.join(self.tmpdir, 'ndk', 'r20', 'bad_abs_link'))) + self.assertFalse(os.path.exists(os.path.join(self.tmpdir, 'ndk', 'r20', 'bad_rel_link'))) + self.assertFalse(os.path.exists(os.path.join(self.tmpdir, 'ndk', 'r20', 'bad_rel_link2'))) + os.system('ls -l ' + os.path.join(self.tmpdir, 'ndk', 'r20')) def test_fill_config_defaults(self): """Test the auto-detection of NDKs installed in standard paths""" - sdk_path = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - - ndk_bundle = os.path.join(sdk_path, 'ndk-bundle') + ndk_bundle = os.path.join(self.tmpdir, 'ndk-bundle') os.makedirs(ndk_bundle) with open(os.path.join(ndk_bundle, 'source.properties'), 'w') as fp: fp.write('Pkg.Desc = Android NDK\nPkg.Revision = 17.2.4988734\n') - config = {'sdk_path': sdk_path} + config = {'sdk_path': self.tmpdir} fdroidserver.common.fill_config_defaults(config) self.assertEqual({'r17c': ndk_bundle}, config['ndk_paths']) - r21e = os.path.join(sdk_path, 'ndk', '21.4.7075529') + r21e = os.path.join(self.tmpdir, 'ndk', '21.4.7075529') os.makedirs(r21e) with open(os.path.join(r21e, 'source.properties'), 'w') as fp: fp.write('Pkg.Desc = Android NDK\nPkg.Revision = 21.4.7075529\n') - config = {'sdk_path': sdk_path} + config = {'sdk_path': self.tmpdir} fdroidserver.common.fill_config_defaults(config) self.assertEqual({'r17c': ndk_bundle, 'r21e': r21e}, config['ndk_paths']) - r10e = os.path.join(sdk_path, 'ndk', 'r10e') + r10e = os.path.join(self.tmpdir, 'ndk', 'r10e') os.makedirs(r10e) with open(os.path.join(r10e, 'RELEASE.TXT'), 'w') as fp: fp.write('r10e-rc4 (64-bit)\n') - config = {'sdk_path': sdk_path} + config = {'sdk_path': self.tmpdir} fdroidserver.common.fill_config_defaults(config) self.assertEqual( {'r10e': r10e, 'r17c': ndk_bundle, 'r21e': r21e}, config['ndk_paths'] @@ -2516,10 +2408,7 @@ class CommonTest(unittest.TestCase): self.assertFalse(is_repo_file(f), f + ' not repo file') def test_get_apksigner_smartcardoptions(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: d = { 'smartcardoptions': '-storetype PKCS11' @@ -2547,10 +2436,7 @@ class CommonTest(unittest.TestCase): ) def test_get_smartcardoptions_list(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write( textwrap.dedent( @@ -2585,10 +2471,7 @@ class CommonTest(unittest.TestCase): ) def test_get_smartcardoptions_spaces(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.yml', 'w') as fp: fp.write( textwrap.dedent( @@ -2616,10 +2499,7 @@ class CommonTest(unittest.TestCase): ) def test_get_smartcardoptions_config_py(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.tmpdir) with open('config.py', 'w') as fp: fp.write( textwrap.dedent( diff --git a/tests/import_subcommand.TestCase b/tests/import_subcommand.TestCase index 307f2bca..dbe6ec38 100755 --- a/tests/import_subcommand.TestCase +++ b/tests/import_subcommand.TestCase @@ -4,7 +4,6 @@ import logging import optparse -import os import shutil import sys import tempfile @@ -31,36 +30,30 @@ class ImportTest(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.DEBUG) self.basedir = localmodule / 'tests' - self.tmpdir = localmodule / '.testfiles' - self.tmpdir.mkdir(exist_ok=True) - # TODO: Python3.6: Accepts a path-like object. - os.chdir(str(self.basedir)) fdroidserver.import_subcommand.options = mock.Mock() fdroidserver.import_subcommand.options.rev = None def test_import_gitlab(self): - # FDroidPopen needs some config to work - config = dict() - fdroidserver.common.fill_config_defaults(config) - fdroidserver.common.config = config + with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir): + # FDroidPopen needs some config to work + config = dict() + fdroidserver.common.fill_config_defaults(config) + fdroidserver.common.config = config - url = 'https://gitlab.com/fdroid/ci-test-app' - r = requests.head(url, timeout=300) - if r.status_code != 200: - print("ERROR", url, 'unreachable (', r.status_code, ')') - print('Skipping ImportTest!') - return + url = 'https://gitlab.com/fdroid/ci-test-app' + r = requests.head(url, timeout=300) + if r.status_code != 200: + print("ERROR", url, 'unreachable (', r.status_code, ')') + print('Skipping ImportTest!') + return - app = fdroidserver.import_subcommand.get_app_from_url(url) - fdroidserver.import_subcommand.clone_to_tmp_dir(app) - self.assertEqual(app.RepoType, 'git') - self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git') + app = fdroidserver.import_subcommand.get_app_from_url(url) + fdroidserver.import_subcommand.clone_to_tmp_dir(app) + self.assertEqual(app.RepoType, 'git') + self.assertEqual(app.Repo, 'https://gitlab.com/fdroid/ci-test-app.git') def test_get_app_from_url(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir, TmpCwd( - testdir - ): + with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir): testdir = Path(testdir) (testdir / 'tmp').mkdir() tmp_importer = testdir / 'tmp/importer' diff --git a/tests/index.TestCase b/tests/index.TestCase index 1c8e9854..9797a0b3 100755 --- a/tests/index.TestCase +++ b/tests/index.TestCase @@ -26,7 +26,7 @@ import fdroidserver.index import fdroidserver.net import fdroidserver.signindex import fdroidserver.publish -from testcommon import TmpCwd +from testcommon import TmpCwd, mkdtemp from pathlib import Path @@ -44,10 +44,6 @@ class IndexTest(unittest.TestCase): logging.basicConfig(level=logging.DEBUG) self.basedir = os.path.join(localmodule, 'tests') os.chmod(os.path.join(self.basedir, 'config.py'), 0o600) - 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.options = Options @@ -60,6 +56,13 @@ class IndexTest(unittest.TestCase): fdroidserver.signindex.sign_index( os.path.join(self.basedir, 'repo'), 'index-v1.json' ) + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + os.chdir(self.basedir) + self._td.cleanup() + os.remove('repo/index-v1.jar') def test_get_public_key_from_jar_succeeds(self): source_dir = os.path.join(self.basedir, 'signindex') @@ -269,10 +272,7 @@ class IndexTest(unittest.TestCase): self.assertEqual(json.dumps(i, indent=2), json.dumps(o, indent=2)) def test_make_v0_repo_only(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) os.mkdir('repo') repo_icons_dir = os.path.join('repo', 'icons') self.assertFalse(os.path.isdir(repo_icons_dir)) @@ -298,10 +298,7 @@ class IndexTest(unittest.TestCase): self.assertTrue(os.path.exists(os.path.join('repo', 'index.xml'))) def test_make_v0(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) os.mkdir('metadata') os.mkdir('repo') metadatafile = 'metadata/info.zwanenburg.caffeinetile.yml' @@ -368,10 +365,7 @@ class IndexTest(unittest.TestCase): present. """ - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) os.mkdir('repo') repo_icons_dir = os.path.join('repo', 'icons') self.assertFalse(os.path.isdir(repo_icons_dir)) @@ -463,10 +457,7 @@ class IndexTest(unittest.TestCase): ) def test_make_website(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) os.mkdir('metadata') os.mkdir('repo') diff --git a/tests/init.TestCase b/tests/init.TestCase index f19c59e5..8e1042c3 100755 --- a/tests/init.TestCase +++ b/tests/init.TestCase @@ -8,7 +8,6 @@ import os import optparse import shutil import sys -import tempfile import unittest @@ -20,6 +19,7 @@ if localmodule not in sys.path: sys.path.insert(0, localmodule) import fdroidserver.init +from testcommon import mkdtemp class InitTest(unittest.TestCase): @@ -28,18 +28,17 @@ class InitTest(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 fdroidserver.init.config = None + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + self._td.cleanup() + os.chdir(self.basedir) def test_disable_in_config(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) with open('config.yml', 'w') as fp: fp.write('keystore: NONE\n') fp.write('keypass: mysupersecrets\n') @@ -57,12 +56,9 @@ class InitTest(unittest.TestCase): @unittest.skipIf(os.name == 'nt', "calling main() like this hangs on Windows") def test_main_in_empty_dir(self): """Test that `fdroid init` will find apksigner and add it to the config""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) - shutil.copy(os.path.join(self.basedir, 'keystore.jks'), testdir) + shutil.copy(os.path.join(self.basedir, 'keystore.jks'), self.testdir) bindir = os.path.join(os.getcwd(), 'bin') os.mkdir(bindir) diff --git a/tests/metadata.TestCase b/tests/metadata.TestCase index ccc76a32..7d96bf5b 100755 --- a/tests/metadata.TestCase +++ b/tests/metadata.TestCase @@ -38,10 +38,18 @@ class MetadataTest(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.DEBUG) self.basedir = localmodule / 'tests' - self.tmpdir = localmodule / '.testfiles' - self.tmpdir.mkdir(exist_ok=True) - # TODO: Python3.6: Accepts a path-like object. - os.chdir(str(self.basedir)) + os.chdir(self.basedir) + + def tearDown(self): + # auto-generated dirs by functions, not tests, so they are not always cleaned up + try: + os.rmdir("srclibs") + except OSError: + pass + try: + os.rmdir("tmp") + except OSError: + pass def test_fieldtypes_key_exist(self): for k in fdroidserver.metadata.fieldtypes.keys(): @@ -218,8 +226,7 @@ class MetadataTest(unittest.TestCase): # yaml.dump(frommeta, f, default_flow_style=False) def test_rewrite_yaml_fakeotaupdate(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir: + with tempfile.TemporaryDirectory() as testdir: testdir = Path(testdir) fdroidserver.common.config = {'accepted_formats': ['yml']} fdroidserver.metadata.warnings_action = None @@ -241,8 +248,7 @@ class MetadataTest(unittest.TestCase): ) def test_rewrite_yaml_fdroidclient(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir: + with tempfile.TemporaryDirectory() as testdir: testdir = Path(testdir) fdroidserver.common.config = {'accepted_formats': ['yml']} @@ -263,8 +269,7 @@ class MetadataTest(unittest.TestCase): ) def test_rewrite_yaml_special_build_params(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir: + with tempfile.TemporaryDirectory() as testdir: testdir = Path(testdir) # rewrite metadata @@ -334,10 +339,7 @@ class MetadataTest(unittest.TestCase): self.assertEqual('1234567890', yamldata['Builds'][0]['commit']) def test_read_metadata_sort_by_time(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir, TmpCwd( - testdir - ): + with tempfile.TemporaryDirectory() as testdir, TmpCwd(testdir): testdir = Path(testdir) metadatadir = testdir / 'metadata' metadatadir.mkdir() @@ -393,8 +395,7 @@ class MetadataTest(unittest.TestCase): fdroidserver.metadata.parse_yaml_metadata(mf, {}) def test_parse_yaml_srclib_corrupt_file(self): - # TODO: Pytohn3.6: The dir parameter now accepts a path-like object. - with tempfile.TemporaryDirectory(dir=str(self.tmpdir)) as testdir: + with tempfile.TemporaryDirectory() as testdir: testdir = Path(testdir) srclibfile = testdir / 'srclib/mock.yml' srclibfile.parent.mkdir() @@ -1117,29 +1118,30 @@ class MetadataTest(unittest.TestCase): def test_build_ndk_path(self): """""" - config = {'ndk_paths': {}, 'sdk_path': tempfile.mkdtemp(prefix='android-sdk-')} - fdroidserver.common.config = config + with tempfile.TemporaryDirectory(prefix='android-sdk-') as sdk_path: + config = {'ndk_paths': {}, 'sdk_path': sdk_path} + fdroidserver.common.config = config - build = fdroidserver.metadata.Build() - build.ndk = 'r10e' - self.assertEqual('', build.ndk_path()) + build = fdroidserver.metadata.Build() + build.ndk = 'r10e' + self.assertEqual('', build.ndk_path()) - correct = '/fake/path/ndk/r21b' - config['ndk_paths'] = {'r21b': correct} - self.assertEqual('', build.ndk_path()) - config['ndk_paths'] = {'r10e': correct} - self.assertEqual(correct, build.ndk_path()) + correct = '/fake/path/ndk/r21b' + config['ndk_paths'] = {'r21b': correct} + self.assertEqual('', build.ndk_path()) + config['ndk_paths'] = {'r10e': correct} + self.assertEqual(correct, build.ndk_path()) - r10e = '/fake/path/ndk/r10e' - r22b = '/fake/path/ndk/r22e' - config['ndk_paths'] = {'r10e': r10e, 'r22b': r22b} - self.assertEqual(r10e, build.ndk_path()) + r10e = '/fake/path/ndk/r10e' + r22b = '/fake/path/ndk/r22e' + config['ndk_paths'] = {'r10e': r10e, 'r22b': r22b} + self.assertEqual(r10e, build.ndk_path()) - build.ndk = ['r10e', 'r22b'] - self.assertEqual(r10e, build.ndk_path()) + build.ndk = ['r10e', 'r22b'] + self.assertEqual(r10e, build.ndk_path()) - build.ndk = ['r22b', 'r10e'] - self.assertEqual(r22b, build.ndk_path()) + build.ndk = ['r22b', 'r10e'] + self.assertEqual(r22b, build.ndk_path()) if __name__ == "__main__": diff --git a/tests/nightly.TestCase b/tests/nightly.TestCase index 85a75abd..f93d3b66 100755 --- a/tests/nightly.TestCase +++ b/tests/nightly.TestCase @@ -122,6 +122,7 @@ class NightlyTest(unittest.TestCase): assert '-----BEGIN RSA PRIVATE KEY-----' in fp.read() with open(ssh_private_key_file + '.pub') as fp: assert fp.read(8) == 'ssh-rsa ' + shutil.rmtree(os.path.dirname(ssh_private_key_file)) @patch.dict(os.environ, clear=True) @patch('sys.argv', ['fdroid nightly', '--verbose']) diff --git a/tests/publish.TestCase b/tests/publish.TestCase index ccad450f..7542c5f7 100755 --- a/tests/publish.TestCase +++ b/tests/publish.TestCase @@ -34,6 +34,7 @@ from fdroidserver import common from fdroidserver import metadata from fdroidserver import signatures from fdroidserver.exception import FDroidException +from testcommon import mkdtemp class PublishTest(unittest.TestCase): @@ -42,9 +43,12 @@ class PublishTest(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) + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + self._td.cleanup() os.chdir(self.basedir) def test_key_alias(self): @@ -88,7 +92,7 @@ class PublishTest(unittest.TestCase): publish.config = common.config publish.config['keystorepass'] = '123456' publish.config['keypass'] = '123456' - publish.config['keystore'] = os.path.join(os.getcwd(), 'dummy-keystore.jks') + publish.config['keystore'] = os.path.join(self.basedir, 'dummy-keystore.jks') publish.config['repo_keyalias'] = 'repokey' appids = [ @@ -99,10 +103,7 @@ class PublishTest(unittest.TestCase): 'org.org.org', ] - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) with open('config.py', 'w') as f: pass @@ -142,14 +143,11 @@ class PublishTest(unittest.TestCase): publish.config = common.config publish.config['keystorepass'] = '123456' publish.config['keypass'] = '123456' - publish.config['keystore'] = os.path.join(os.getcwd(), 'dummy-keystore.jks') + publish.config['keystore'] = os.path.join(self.basedir, 'dummy-keystore.jks') publish.config['repo_keyalias'] = 'repokey' publish.config['repo_key_sha256'] = 'bad bad bad bad bad bad bad bad bad bad bad bad' - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) publish.store_stats_fdroid_signing_key_fingerprints({}, indent=2) with self.assertRaises(FDroidException): common.load_stats_fdroid_signing_key_fingerprints() @@ -162,24 +160,20 @@ class PublishTest(unittest.TestCase): publish.config['repo_keyalias'] = 'sova' publish.config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' publish.config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - - shutil.copy('keystore.jks', testdir) - os.mkdir(os.path.join(testdir, 'repo')) - metadata_dir = os.path.join(testdir, 'metadata') + shutil.copy('keystore.jks', self.testdir) + os.mkdir(os.path.join(self.testdir, 'repo')) + metadata_dir = os.path.join(self.testdir, 'metadata') os.mkdir(metadata_dir) shutil.copy(os.path.join('metadata', 'com.politedroid.yml'), metadata_dir) with open(os.path.join(metadata_dir, 'com.politedroid.yml'), 'a') as fp: fp.write('\nBinaries: https://placeholder/foo%v.apk\n') - os.mkdir(os.path.join(testdir, 'unsigned')) - shutil.copy('repo/com.politedroid_6.apk', os.path.join(testdir, 'unsigned')) - os.mkdir(os.path.join(testdir, 'unsigned', 'binaries')) + os.mkdir(os.path.join(self.testdir, 'unsigned')) + shutil.copy('repo/com.politedroid_6.apk', os.path.join(self.testdir, 'unsigned')) + os.mkdir(os.path.join(self.testdir, 'unsigned', 'binaries')) shutil.copy('repo/com.politedroid_6.apk', - os.path.join(testdir, 'unsigned', 'binaries', 'com.politedroid_6.binary.apk')) + os.path.join(self.testdir, 'unsigned', 'binaries', 'com.politedroid_6.binary.apk')) - os.chdir(testdir) + os.chdir(self.testdir) with mock.patch.object(sys, 'argv', ['fdroid fakesubcommand']): publish.main() @@ -222,8 +216,7 @@ class PublishTest(unittest.TestCase): publish.config['keypass'] = '654321' publish.config['keystore'] = "keystore.jks" publish.config['keydname'] = 'CN=Birdman, OU=Cell, O=Alcatraz, L=Alcatraz, S=California, C=US' - testdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir) - os.chdir(testdir) + os.chdir(self.testdir) keystore = jks.KeyStore.new("jks", []) keystore.save(publish.config['keystore'], publish.config['keystorepass']) @@ -272,10 +265,7 @@ class PublishTest(unittest.TestCase): class Options: verbose = False - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) config = common.read_config(Options) if 'apksigner' not in config: @@ -283,7 +273,7 @@ class PublishTest(unittest.TestCase): config['repo_keyalias'] = 'sova' config['keystorepass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' config['keypass'] = 'r9aquRHYoI8+dYz6jKrLntQ5/NJNASFBacJh7Jv2BlI=' - shutil.copy(os.path.join(self.basedir, 'keystore.jks'), testdir) + shutil.copy(os.path.join(self.basedir, 'keystore.jks'), self.testdir) config['keystore'] = 'keystore.jks' config['keydname'] = 'CN=Birdman, OU=Cell, O=Alcatraz, L=Alcatraz, S=California, C=US' publish.config = config diff --git a/tests/scanner.TestCase b/tests/scanner.TestCase index 49efee00..9e17750a 100755 --- a/tests/scanner.TestCase +++ b/tests/scanner.TestCase @@ -30,17 +30,20 @@ import fdroidserver.build import fdroidserver.common import fdroidserver.metadata import fdroidserver.scanner -from testcommon import TmpCwd, mock_open_to_str +from testcommon import TmpCwd, mkdtemp, mock_open_to_str class ScannerTest(unittest.TestCase): def setUp(self): logging.basicConfig(level=logging.INFO) 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) + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + os.chdir(self.basedir) + self._td.cleanup() def test_scan_source_files(self): fdroidserver.scanner.options = mock.Mock() @@ -97,10 +100,7 @@ class ScannerTest(unittest.TestCase): def test_scan_source_files_sneaky_maven(self): """Check for sneaking in banned maven repos""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) fdroidserver.scanner.config = None fdroidserver.scanner.options = mock.Mock() fdroidserver.scanner.options.json = True @@ -119,7 +119,7 @@ class ScannerTest(unittest.TestCase): """ ) ) - count = fdroidserver.scanner.scan_source(testdir) + count = fdroidserver.scanner.scan_source(self.testdir) self.assertEqual(2, count, 'there should be this many errors') def test_scan_source_file_types(self): @@ -129,11 +129,8 @@ class ScannerTest(unittest.TestCase): difference between absolute and relative paths. """ - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) build_dir = os.path.join('build', 'fake.app') - abs_build_dir = os.path.join(testdir, build_dir) + abs_build_dir = os.path.join(self.testdir, build_dir) os.makedirs(abs_build_dir, exist_ok=True) os.chdir(abs_build_dir) @@ -174,7 +171,7 @@ class ScannerTest(unittest.TestCase): os.system('ls -l fake.png') # run scanner as if from `fdroid build` - os.chdir(testdir) + os.chdir(self.testdir) count = fdroidserver.scanner.scan_source(build_dir) self.assertEqual(6, count, 'there should be this many errors') os.chdir(build_dir) @@ -224,11 +221,7 @@ class ScannerTest(unittest.TestCase): def test_build_local_scanner(self): """`fdroid build` calls scanner functions, test them here""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) - + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -284,8 +277,8 @@ class ScannerTest(unittest.TestCase): app, build, vcs, - build_dir=testdir, - output_dir=testdir, + build_dir=self.testdir, + output_dir=self.testdir, log_dir=None, srclib_dir=None, extlib_dir=None, @@ -314,10 +307,7 @@ class ScannerTest(unittest.TestCase): def test_scan_gradle_file_with_multiple_problems(self): """Check that the scanner can handle scandelete with gradle files with multiple problems""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) fdroidserver.scanner.config = None fdroidserver.scanner.options = mock.Mock() build = fdroidserver.metadata.Build() @@ -335,7 +325,7 @@ class ScannerTest(unittest.TestCase): """ ) ) - count = fdroidserver.scanner.scan_source(testdir, build) + count = fdroidserver.scanner.scan_source(self.testdir, build) self.assertFalse(os.path.exists("build.gradle")) self.assertEqual(0, count, 'there should be this many errors') diff --git a/tests/testcommon.py b/tests/testcommon.py index 29b3f9b6..61c1a904 100644 --- a/tests/testcommon.py +++ b/tests/testcommon.py @@ -17,6 +17,7 @@ import os import sys +import tempfile class TmpCwd(): @@ -58,3 +59,10 @@ def mock_open_to_str(mock): return "".join([ x.args[0] for x in mock.mock_calls if str(x).startswith("call().write(") ]) + + +def mkdtemp(): + if sys.version_info < (3, 10): # ignore_cleanup_errors was added in 3.10 + return tempfile.TemporaryDirectory() + else: + return tempfile.TemporaryDirectory(ignore_cleanup_errors=True) diff --git a/tests/update.TestCase b/tests/update.TestCase index 1582968e..0a102dfd 100755 --- a/tests/update.TestCase +++ b/tests/update.TestCase @@ -22,7 +22,7 @@ import zipfile import textwrap from datetime import datetime from distutils.version import LooseVersion -from testcommon import TmpCwd +from testcommon import TmpCwd, mkdtemp from unittest import mock try: @@ -79,19 +79,19 @@ class UpdateTest(unittest.TestCase): logging.getLogger(PngImagePlugin.__name__).setLevel(logging.INFO) 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) + self._td = mkdtemp() + self.testdir = self._td.name fdroidserver.common.config = None fdroidserver.common.options = None + def tearDown(self): + os.chdir(self.basedir) + self._td.cleanup() + def test_insert_store_metadata(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) @@ -124,7 +124,7 @@ class UpdateTest(unittest.TestCase): ): shutil.copytree( os.path.join(self.basedir, 'source-files', packageName), - os.path.join(tmptestsdir, 'build', packageName), + os.path.join(self.testdir, 'build', packageName), ) testfilename = 'icon_yAfSvPRJukZzMMfUzvbYqwaD1XmHXNtiPBtuPVHW-6s=.png' @@ -215,10 +215,7 @@ class UpdateTest(unittest.TestCase): https://docs.fastlane.tools/actions/supply/#changelogs-whats-new """ - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) @@ -254,11 +251,12 @@ class UpdateTest(unittest.TestCase): def test_name_title_scraping(self): """metadata file --> fdroiddata localized files --> fastlane/triple-t in app source --> APK""" + shutil.copytree(self.basedir, self.testdir, dirs_exist_ok=True) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - os.chdir(os.path.join(localmodule, 'tests')) + os.chdir(self.testdir) fdroidserver.common.options = Options fdroidserver.update.options = fdroidserver.common.options fdroidserver.update.options.clean = True @@ -340,6 +338,8 @@ class UpdateTest(unittest.TestCase): self.assertEqual(testvalue, app['localized']['en-US']['name']) def test_insert_missing_app_names_from_apks_from_repo(self): + os.chdir(self.testdir) + shutil.copytree(self.basedir, self.testdir, dirs_exist_ok=True) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -393,9 +393,7 @@ class UpdateTest(unittest.TestCase): if not os.path.isdir(importer): logging.warning('skipping test_insert_triple_t_metadata, import.TestCase must run first!') return - tmptestsdir = tempfile.mkdtemp(prefix=inspect.currentframe().f_code.co_name, - dir=self.tmpdir) - packageDir = os.path.join(tmptestsdir, 'build', packageName) + packageDir = os.path.join(self.testdir, 'build', packageName) shutil.copytree(importer, packageDir) # always use the same commit so these tests work when ci-test-app.git is updated @@ -405,17 +403,17 @@ class UpdateTest(unittest.TestCase): repo.git.reset('--hard', 'b9e5d1a0d8d6fc31d4674b2f0514fef10762ed4f') repo.git.clean('-fdx') - os.mkdir(os.path.join(tmptestsdir, 'metadata')) + os.mkdir(os.path.join(self.testdir, 'metadata')) metadata = dict() metadata['Description'] = 'This is just a test app' - with open(os.path.join(tmptestsdir, 'metadata', packageName + '.yml'), 'w') as fp: + with open(os.path.join(self.testdir, 'metadata', packageName + '.yml'), 'w') as fp: yaml.dump(metadata, fp) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - os.chdir(tmptestsdir) + os.chdir(self.testdir) apps = fdroidserver.metadata.read_metadata() fdroidserver.update.copy_triple_t_store_metadata(apps) @@ -436,12 +434,8 @@ class UpdateTest(unittest.TestCase): def test_insert_triple_t_2_metadata(self): packageName = 'org.piwigo.android' - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.rmdir(tmptestsdir) - shutil.copytree(os.path.join(self.basedir, 'triple-t-2'), tmptestsdir) - os.chdir(tmptestsdir) + shutil.copytree(os.path.join(self.basedir, 'triple-t-2'), self.testdir, dirs_exist_ok=True) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) @@ -478,12 +472,8 @@ class UpdateTest(unittest.TestCase): packages = ('com.anysoftkeyboard.languagepack.dutch', 'com.menny.android.anysoftkeyboard') names = ('Dutch for AnySoftKeyboard', 'AnySoftKeyboard') - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.rmdir(tmptestsdir) - shutil.copytree(os.path.join(self.basedir, 'triple-t-anysoftkeyboard'), tmptestsdir) - os.chdir(tmptestsdir) + shutil.copytree(os.path.join(self.basedir, 'triple-t-anysoftkeyboard'), self.testdir, dirs_exist_ok=True) + os.chdir(self.testdir) for packageName, name in zip(packages, names): config = dict() @@ -503,12 +493,8 @@ class UpdateTest(unittest.TestCase): packages = ('verifier', 'wallet') names = dict(verifier='COVID Certificate Check', wallet='COVID Certificate') - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.rmdir(tmptestsdir) - shutil.copytree(os.path.join(self.basedir, 'triple-t-multiple'), tmptestsdir) - os.chdir(tmptestsdir) + shutil.copytree(os.path.join(self.basedir, 'triple-t-multiple'), self.testdir, dirs_exist_ok=True) + os.chdir(self.testdir) for p in packages: packageName = namespace + p @@ -527,12 +513,8 @@ class UpdateTest(unittest.TestCase): def test_insert_triple_t_flutter(self): packageName = 'fr.emersion.goguma' - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.rmdir(tmptestsdir) - shutil.copytree(os.path.join(self.basedir, 'triple-t-flutter'), tmptestsdir) - os.chdir(tmptestsdir) + shutil.copytree(os.path.join(self.basedir, 'triple-t-flutter'), self.testdir, dirs_exist_ok=True) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) @@ -609,11 +591,7 @@ class UpdateTest(unittest.TestCase): "python sig was: " + str(sig)) def testScanApksAndObbs(self): - os.chdir(os.path.join(localmodule, 'tests')) - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata') config = dict() @@ -668,11 +646,7 @@ class UpdateTest(unittest.TestCase): def test_apkcache_json(self): """test the migration from pickle to json""" - os.chdir(os.path.join(localmodule, 'tests')) - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) @@ -711,10 +685,7 @@ class UpdateTest(unittest.TestCase): fdroidserver.common.config = config fdroidserver.update.config = config - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('repo') os.mkdir('stats') with open(os.path.join('stats', 'known_apks.txt'), 'w') as fp: @@ -736,25 +707,27 @@ class UpdateTest(unittest.TestCase): ) def test_read_added_date_from_all_apks(self): + os.chdir(self.testdir) + shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config fdroidserver.common.options = Options - os.chdir(os.path.join(localmodule, 'tests')) apps = fdroidserver.metadata.read_metadata() knownapks = fdroidserver.common.KnownApks() apks, cachechanged = fdroidserver.update.process_apks({}, 'repo', knownapks) fdroidserver.update.read_added_date_from_all_apks(apps, apks) def test_apply_info_from_latest_apk(self): + os.chdir(self.testdir) + shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config fdroidserver.common.options = Options fdroidserver.update.options = fdroidserver.common.options - os.chdir(os.path.join(localmodule, 'tests')) apps = fdroidserver.metadata.read_metadata() knownapks = fdroidserver.common.KnownApks() apks, cachechanged = fdroidserver.update.process_apks({}, 'repo', knownapks) @@ -765,7 +738,7 @@ class UpdateTest(unittest.TestCase): fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - os.chdir(os.path.join(localmodule, 'tests')) + os.chdir(self.basedir) if 'apksigner' in config: apk_info = fdroidserver.update.scan_apk('v2.only.sig_2.apk') @@ -873,7 +846,7 @@ class UpdateTest(unittest.TestCase): fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - os.chdir(os.path.join(localmodule, 'tests')) + os.chdir(self.basedir) if os.path.basename(os.getcwd()) != 'tests': raise Exception('This test must be run in the "tests/" subdir') @@ -885,10 +858,7 @@ class UpdateTest(unittest.TestCase): fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('repo') apkfile = 'repo/badzip_1.apk' with open(apkfile, 'w') as fp: @@ -935,11 +905,13 @@ class UpdateTest(unittest.TestCase): '''Creates a YAML representation of a Build instance''' return dumper.represent_dict(data) + os.chdir(self.testdir) + shutil.copytree(self.basedir, 'tests') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config fdroidserver.update.config = config - os.chdir(os.path.join(localmodule, 'tests')) + os.chdir("tests") config['ndk_paths'] = dict() fdroidserver.common.config = config @@ -1016,87 +988,83 @@ class UpdateTest(unittest.TestCase): knownapks = fdroidserver.common.KnownApks() - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - print('tmptestsdir', tmptestsdir) - os.chdir(tmptestsdir) - os.mkdir('repo') - os.mkdir('archive') - # setup the repo, create icons dirs, etc. - fdroidserver.update.process_apks({}, 'repo', knownapks) - fdroidserver.update.process_apks({}, 'archive', knownapks) + with tempfile.TemporaryDirectory() as tmptestsdir, TmpCwd(tmptestsdir): + os.mkdir('repo') + os.mkdir('archive') + # setup the repo, create icons dirs, etc. + fdroidserver.update.process_apks({}, 'repo', knownapks) + fdroidserver.update.process_apks({}, 'archive', knownapks) - disabledsigs = ['org.bitbucket.tickytacky.mirrormirror_2.apk'] - for apkName in disabledsigs: - shutil.copy(os.path.join(self.basedir, apkName), - os.path.join(tmptestsdir, 'repo')) + disabledsigs = ['org.bitbucket.tickytacky.mirrormirror_2.apk'] + for apkName in disabledsigs: + shutil.copy(os.path.join(self.basedir, apkName), + os.path.join(tmptestsdir, 'repo')) - skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', - knownapks, - allow_disabled_algorithms=True, - archive_bad_sig=False) - self.assertFalse(skip) - self.assertIsNotNone(apk) - self.assertTrue(cachechanged) - self.assertFalse(os.path.exists(os.path.join('archive', apkName))) - self.assertTrue(os.path.exists(os.path.join('repo', apkName))) + skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', + knownapks, + allow_disabled_algorithms=True, + archive_bad_sig=False) + self.assertFalse(skip) + self.assertIsNotNone(apk) + self.assertTrue(cachechanged) + self.assertFalse(os.path.exists(os.path.join('archive', apkName))) + self.assertTrue(os.path.exists(os.path.join('repo', apkName))) - if os.path.exists('/usr/bin/apksigner') or 'apksigner' in config: - print('SKIPPING: apksigner installed and it allows MD5 signatures') - return + if os.path.exists('/usr/bin/apksigner') or 'apksigner' in config: + print('SKIPPING: apksigner installed and it allows MD5 signatures') + return - javac = config['jarsigner'].replace('jarsigner', 'javac') - v = subprocess.check_output([javac, '-version'], stderr=subprocess.STDOUT)[6:-1].decode('utf-8') - if LooseVersion(v) < LooseVersion('1.8.0_132'): - print('SKIPPING: running tests with old Java (' + v + ')') - return + javac = config['jarsigner'].replace('jarsigner', 'javac') + v = subprocess.check_output([javac, '-version'], stderr=subprocess.STDOUT)[6:-1].decode('utf-8') + if LooseVersion(v) < LooseVersion('1.8.0_132'): + print('SKIPPING: running tests with old Java (' + v + ')') + return - # this test only works on systems with fully updated Java/jarsigner - # that has MD5 listed in jdk.jar.disabledAlgorithms in java.security - # https://blogs.oracle.com/java-platform-group/oracle-jre-will-no-longer-trust-md5-signed-code-by-default - skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', - knownapks, - allow_disabled_algorithms=False, - archive_bad_sig=True) - self.assertTrue(skip) - self.assertIsNone(apk) - self.assertFalse(cachechanged) - self.assertTrue(os.path.exists(os.path.join('archive', apkName))) - self.assertFalse(os.path.exists(os.path.join('repo', apkName))) + # this test only works on systems with fully updated Java/jarsigner + # that has MD5 listed in jdk.jar.disabledAlgorithms in java.security + # https://blogs.oracle.com/java-platform-group/oracle-jre-will-no-longer-trust-md5-signed-code-by-default + skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', + knownapks, + allow_disabled_algorithms=False, + archive_bad_sig=True) + self.assertTrue(skip) + self.assertIsNone(apk) + self.assertFalse(cachechanged) + self.assertTrue(os.path.exists(os.path.join('archive', apkName))) + self.assertFalse(os.path.exists(os.path.join('repo', apkName))) - skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'archive', - knownapks, - allow_disabled_algorithms=False, - archive_bad_sig=False) - self.assertFalse(skip) - self.assertIsNotNone(apk) - self.assertTrue(cachechanged) - self.assertTrue(os.path.exists(os.path.join('archive', apkName))) - self.assertFalse(os.path.exists(os.path.join('repo', apkName))) + skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'archive', + knownapks, + allow_disabled_algorithms=False, + archive_bad_sig=False) + self.assertFalse(skip) + self.assertIsNotNone(apk) + self.assertTrue(cachechanged) + self.assertTrue(os.path.exists(os.path.join('archive', apkName))) + self.assertFalse(os.path.exists(os.path.join('repo', apkName))) - # ensure that icons have been moved to the archive as well - for density in fdroidserver.update.screen_densities: - icon_path = os.path.join(fdroidserver.update.get_icon_dir('archive', density), - apk['icon']) - self.assertTrue(os.path.isfile(icon_path)) - self.assertTrue(os.path.getsize(icon_path) > 1) + # ensure that icons have been moved to the archive as well + for density in fdroidserver.update.screen_densities: + icon_path = os.path.join(fdroidserver.update.get_icon_dir('archive', density), + apk['icon']) + self.assertTrue(os.path.isfile(icon_path)) + self.assertTrue(os.path.getsize(icon_path) > 1) - badsigs = ['urzip-badcert.apk', 'urzip-badsig.apk', 'urzip-release-unsigned.apk', ] - for apkName in badsigs: - shutil.copy(os.path.join(self.basedir, apkName), - os.path.join(tmptestsdir, 'repo')) + badsigs = ['urzip-badcert.apk', 'urzip-badsig.apk', 'urzip-release-unsigned.apk', ] + for apkName in badsigs: + shutil.copy(os.path.join(self.basedir, apkName), + os.path.join(self.testdir, 'repo')) - skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', - knownapks, - allow_disabled_algorithms=False, - archive_bad_sig=False) - self.assertTrue(skip) - self.assertIsNone(apk) - self.assertFalse(cachechanged) + skip, apk, cachechanged = fdroidserver.update.process_apk({}, apkName, 'repo', + knownapks, + allow_disabled_algorithms=False, + archive_bad_sig=False) + self.assertTrue(skip) + self.assertIsNone(apk) + self.assertFalse(cachechanged) def test_process_invalid_apk(self): - os.chdir(os.path.join(localmodule, 'tests')) + os.chdir(self.basedir) if os.path.basename(os.getcwd()) != 'tests': raise Exception('This test must be run in the "tests/" subdir') @@ -1119,6 +1087,8 @@ class UpdateTest(unittest.TestCase): def test_get_apks_without_allowed_signatures(self): """Test when no AllowedAPKSigningKeys is specified""" + os.chdir(self.testdir) + shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -1127,6 +1097,7 @@ class UpdateTest(unittest.TestCase): app = fdroidserver.metadata.App() knownapks = fdroidserver.common.KnownApks() + apks, cachechanged = fdroidserver.update.process_apks({}, 'repo', knownapks) apkfile = 'v1.v2.sig_1020.apk' (skip, apk, cachechanged) = fdroidserver.update.process_apk( {}, apkfile, 'repo', knownapks, False @@ -1137,6 +1108,8 @@ class UpdateTest(unittest.TestCase): def test_get_apks_without_allowed_signatures_allowed(self): """Test when the APK matches the specified AllowedAPKSigningKeys""" + os.chdir(self.testdir) + shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -1149,6 +1122,7 @@ class UpdateTest(unittest.TestCase): } ) knownapks = fdroidserver.common.KnownApks() + apks, cachechanged = fdroidserver.update.process_apks({}, 'repo', knownapks) apkfile = 'v1.v2.sig_1020.apk' (skip, apk, cachechanged) = fdroidserver.update.process_apk( {}, apkfile, 'repo', knownapks, False @@ -1159,6 +1133,8 @@ class UpdateTest(unittest.TestCase): def test_get_apks_without_allowed_signatures_blocked(self): """Test when the APK does not match any specified AllowedAPKSigningKeys""" + os.chdir(self.testdir) + shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.common.config = config @@ -1171,6 +1147,7 @@ class UpdateTest(unittest.TestCase): } ) knownapks = fdroidserver.common.KnownApks() + apks, cachechanged = fdroidserver.update.process_apks({}, 'repo', knownapks) apkfile = 'v1.v2.sig_1020.apk' (skip, apk, cachechanged) = fdroidserver.update.process_apk( {}, apkfile, 'repo', knownapks, False @@ -1181,11 +1158,7 @@ class UpdateTest(unittest.TestCase): def test_update_with_AllowedAPKSigningKeys(self): """Test that APKs without allowed signatures get deleted.""" - # Prepare test environment - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('repo') testapk = os.path.join('repo', 'com.politedroid_6.apk') shutil.copy(os.path.join(self.basedir, testapk), testapk) @@ -1229,11 +1202,7 @@ class UpdateTest(unittest.TestCase): self.assertFalse(os.path.exists(testapk)) def test_translate_per_build_anti_features(self): - os.chdir(os.path.join(localmodule, 'tests')) - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) shutil.copytree(os.path.join(self.basedir, 'repo'), 'repo') shutil.copytree(os.path.join(self.basedir, 'metadata'), 'metadata') config = dict() @@ -1262,11 +1231,7 @@ class UpdateTest(unittest.TestCase): self.assertTrue(foundtest) def test_create_metadata_from_template(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - print('tmptestsdir', tmptestsdir) - os.chdir(tmptestsdir) + os.chdir(self.testdir) os.mkdir('repo') os.mkdir('metadata') shutil.copy(os.path.join(localmodule, 'tests', 'urzip.apk'), 'repo') @@ -1308,7 +1273,7 @@ class UpdateTest(unittest.TestCase): # test using external template.yml os.remove(testfile) self.assertFalse(os.path.exists(testfile)) - shutil.copy(os.path.join(localmodule, 'examples', 'template.yml'), tmptestsdir) + shutil.copy(os.path.join(localmodule, 'examples', 'template.yml'), self.testdir) fdroidserver.update.create_metadata_from_template(apk) self.assertTrue(os.path.exists(testfile)) apps = fdroidserver.metadata.read_metadata() @@ -1363,17 +1328,13 @@ class UpdateTest(unittest.TestCase): assert not icons_src def test_strip_and_copy_image(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - in_file = os.path.join(self.basedir, 'metadata', 'info.guardianproject.urzip', 'en-US', 'images', 'icon.png') - out_file = os.path.join(tmptestsdir, 'icon.png') + out_file = os.path.join(self.testdir, 'icon.png') fdroidserver.update._strip_and_copy_image(in_file, out_file) self.assertTrue(os.path.exists(out_file)) in_file = os.path.join(self.basedir, 'corrupt-featureGraphic.png') - out_file = os.path.join(tmptestsdir, 'corrupt-featureGraphic.png') + out_file = os.path.join(self.testdir, 'corrupt-featureGraphic.png') fdroidserver.update._strip_and_copy_image(in_file, out_file) self.assertFalse(os.path.exists(out_file)) @@ -1463,10 +1424,7 @@ class UpdateTest(unittest.TestCase): ) def test_insert_funding_yml_donation_links(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('build') content = textwrap.dedent( """ @@ -1507,10 +1465,7 @@ class UpdateTest(unittest.TestCase): def test_insert_funding_yml_donation_links_one_at_a_time(self): """Exercise the FUNDING.yml code one entry at a time""" - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('build') app = fdroidserver.metadata.App() @@ -1551,10 +1506,7 @@ class UpdateTest(unittest.TestCase): self.assertEqual(app.get('Donate', '').split('/')[-1], v) def test_insert_funding_yml_donation_links_with_corrupt_file(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('build') app = fdroidserver.metadata.App() app.id = 'fake.app.id' @@ -1598,10 +1550,7 @@ class UpdateTest(unittest.TestCase): self.assertIsNotNone(fdroidserver.update.sanitize_funding_yml_entry(['first', 'second'])) def test_set_localized_text_entry(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.update.config = config @@ -1630,10 +1579,7 @@ class UpdateTest(unittest.TestCase): self.assertIsNone(app['localized'].get(locale, {}).get(key)) def test_set_author_entry(self): - tmptestsdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(tmptestsdir) + os.chdir(self.testdir) config = dict() fdroidserver.common.fill_config_defaults(config) fdroidserver.update.config = config @@ -1760,10 +1706,7 @@ class UpdateTest(unittest.TestCase): self.assertEqual(apkaapt, apkandroguard) def test_exclude_disabled_apks(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) os.mkdir('repo') testapk = os.path.join('repo', 'com.politedroid_6.apk') testapk_new = os.path.join('repo', 'Politedroid-1.5.apk') diff --git a/tests/vcs.TestCase b/tests/vcs.TestCase index 2a408685..4eff7dec 100755 --- a/tests/vcs.TestCase +++ b/tests/vcs.TestCase @@ -7,7 +7,6 @@ import logging import optparse import os import sys -import tempfile import unittest from git import Repo @@ -23,6 +22,7 @@ import fdroidserver.build import fdroidserver.common import fdroidserver.metadata import fdroidserver.scanner +from testcommon import mkdtemp class VCSTest(unittest.TestCase): @@ -31,16 +31,16 @@ class VCSTest(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) + self._td = mkdtemp() + self.testdir = self._td.name + + def tearDown(self): + self._td.cleanup() os.chdir(self.basedir) def test_remote_set_head_can_fail(self): - testdir = tempfile.mkdtemp( - prefix=inspect.currentframe().f_code.co_name, dir=self.tmpdir - ) - os.chdir(testdir) + os.chdir(self.testdir) # First create an upstream repo with one commit upstream_repo = Repo.init("upstream_repo") with open(upstream_repo.working_dir + "/file", 'w') as f: