2020-10-22 23:34:47 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
# http://www.drdobbs.com/testing/unit-testing-with-python/240165163
|
|
|
|
|
|
|
|
import inspect
|
|
|
|
import logging
|
|
|
|
import os
|
tests: make init apksigner test use existing test keystore
This test does not need to generate a keystore, so using the existing one
reduces the run time from 4 seconds to 0.040 seconds. Also, if makes this
mystery error on macOS go away:
```
Traceback (most recent call last):
File "/Users/travis/build/fdroidtravis/fdroidserver/tests/init.TestCase", line 66, in test_main_in_empty_dir
fdroidserver.init.main()
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/init.py", line 231, in main
common.genkeystore(c)
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/common.py", line 3434, in genkeystore
cmd = [config['keytool'], '-genkey',
KeyError: 'keytool'
```
2021-01-29 09:04:48 +01:00
|
|
|
import shutil
|
2020-10-22 23:34:47 +02:00
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
|
|
|
|
localmodule = os.path.realpath(
|
2021-06-07 11:49:21 +02:00
|
|
|
os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..')
|
|
|
|
)
|
2020-10-22 23:34:47 +02:00
|
|
|
print('localmodule: ' + localmodule)
|
|
|
|
if localmodule not in sys.path:
|
|
|
|
sys.path.insert(0, localmodule)
|
|
|
|
|
|
|
|
import fdroidserver.init
|
2024-05-08 16:26:46 +02:00
|
|
|
from testcommon import mkdtemp, parse_args_for_test
|
2020-10-22 23:34:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
class InitTest(unittest.TestCase):
|
|
|
|
'''fdroidserver/init.py'''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
self.basedir = os.path.join(localmodule, 'tests')
|
2021-01-25 23:27:24 +01:00
|
|
|
fdroidserver.common.config = None
|
2020-10-22 23:34:47 +02:00
|
|
|
fdroidserver.init.config = None
|
2022-11-22 17:17:45 +01:00
|
|
|
self._td = mkdtemp()
|
|
|
|
self.testdir = self._td.name
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self._td.cleanup()
|
|
|
|
os.chdir(self.basedir)
|
2020-10-22 23:34:47 +02:00
|
|
|
|
|
|
|
def test_disable_in_config(self):
|
2022-11-22 17:17:45 +01:00
|
|
|
os.chdir(self.testdir)
|
2020-10-22 23:34:47 +02:00
|
|
|
with open('config.yml', 'w') as fp:
|
|
|
|
fp.write('keystore: NONE\n')
|
|
|
|
fp.write('keypass: mysupersecrets\n')
|
2021-01-25 23:27:24 +01:00
|
|
|
os.chmod('config.yml', 0o600)
|
2024-05-08 16:26:46 +02:00
|
|
|
config = fdroidserver.common.read_config()
|
2020-10-22 23:34:47 +02:00
|
|
|
self.assertEqual('NONE', config['keystore'])
|
|
|
|
self.assertEqual('mysupersecrets', config['keypass'])
|
|
|
|
fdroidserver.init.disable_in_config('keypass', 'comment')
|
|
|
|
with open(fp.name) as fp:
|
|
|
|
self.assertTrue('#keypass:' in fp.read())
|
|
|
|
fdroidserver.common.config = None
|
2024-05-08 16:26:46 +02:00
|
|
|
config = fdroidserver.common.read_config()
|
2020-10-22 23:34:47 +02:00
|
|
|
self.assertIsNone(config.get('keypass'))
|
|
|
|
|
2021-06-14 20:53:14 +02:00
|
|
|
@unittest.skipIf(os.name == 'nt', "calling main() like this hangs on Windows")
|
2021-01-25 23:27:24 +01:00
|
|
|
def test_main_in_empty_dir(self):
|
tests: make init apksigner test use existing test keystore
This test does not need to generate a keystore, so using the existing one
reduces the run time from 4 seconds to 0.040 seconds. Also, if makes this
mystery error on macOS go away:
```
Traceback (most recent call last):
File "/Users/travis/build/fdroidtravis/fdroidserver/tests/init.TestCase", line 66, in test_main_in_empty_dir
fdroidserver.init.main()
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/init.py", line 231, in main
common.genkeystore(c)
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/common.py", line 3434, in genkeystore
cmd = [config['keytool'], '-genkey',
KeyError: 'keytool'
```
2021-01-29 09:04:48 +01:00
|
|
|
"""Test that `fdroid init` will find apksigner and add it to the config"""
|
2022-11-22 17:17:45 +01:00
|
|
|
os.chdir(self.testdir)
|
2021-01-25 23:27:24 +01:00
|
|
|
|
2022-11-22 17:17:45 +01:00
|
|
|
shutil.copy(os.path.join(self.basedir, 'keystore.jks'), self.testdir)
|
tests: make init apksigner test use existing test keystore
This test does not need to generate a keystore, so using the existing one
reduces the run time from 4 seconds to 0.040 seconds. Also, if makes this
mystery error on macOS go away:
```
Traceback (most recent call last):
File "/Users/travis/build/fdroidtravis/fdroidserver/tests/init.TestCase", line 66, in test_main_in_empty_dir
fdroidserver.init.main()
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/init.py", line 231, in main
common.genkeystore(c)
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/common.py", line 3434, in genkeystore
cmd = [config['keytool'], '-genkey',
KeyError: 'keytool'
```
2021-01-29 09:04:48 +01:00
|
|
|
|
2021-01-25 23:27:24 +01:00
|
|
|
bindir = os.path.join(os.getcwd(), 'bin')
|
|
|
|
os.mkdir(bindir)
|
|
|
|
apksigner = os.path.join(bindir, 'apksigner')
|
|
|
|
open(apksigner, 'w').close()
|
|
|
|
os.chmod(apksigner, 0o755)
|
|
|
|
os.environ['PATH'] = bindir
|
|
|
|
|
tests: make init apksigner test use existing test keystore
This test does not need to generate a keystore, so using the existing one
reduces the run time from 4 seconds to 0.040 seconds. Also, if makes this
mystery error on macOS go away:
```
Traceback (most recent call last):
File "/Users/travis/build/fdroidtravis/fdroidserver/tests/init.TestCase", line 66, in test_main_in_empty_dir
fdroidserver.init.main()
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/init.py", line 231, in main
common.genkeystore(c)
File "/Users/travis/build/fdroidtravis/fdroidserver/fdroidserver/common.py", line 3434, in genkeystore
cmd = [config['keytool'], '-genkey',
KeyError: 'keytool'
```
2021-01-29 09:04:48 +01:00
|
|
|
sys.argv = ['fdroid init', '--keystore', 'keystore.jks', '--repo-keyalias=sova']
|
2021-01-25 23:27:24 +01:00
|
|
|
fdroidserver.init.main()
|
2021-03-22 14:51:59 +01:00
|
|
|
self.assertEqual(apksigner, fdroidserver.init.config.get('apksigner'))
|
2021-01-25 23:27:24 +01:00
|
|
|
|
2020-10-22 23:34:47 +02:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
os.chdir(os.path.dirname(__file__))
|
|
|
|
|
2024-05-02 14:08:28 +02:00
|
|
|
import argparse
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser()
|
|
|
|
parser.add_argument(
|
2021-06-07 11:49:21 +02:00
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2024-05-08 16:26:46 +02:00
|
|
|
fdroidserver.init.options = parse_args_for_test(parser, sys.argv)
|
2020-10-22 23:34:47 +02:00
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(InitTest))
|
|
|
|
unittest.main(failfast=False)
|