2019-07-25 02:26:43 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
|
|
|
|
import logging
|
|
|
|
import optparse
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import unittest
|
|
|
|
import tempfile
|
|
|
|
import textwrap
|
|
|
|
from unittest import mock
|
2021-06-08 15:31:55 +02:00
|
|
|
from pathlib import Path
|
|
|
|
|
|
|
|
|
2019-07-25 02:26:43 +02:00
|
|
|
from testcommon import TmpCwd
|
|
|
|
|
2021-06-08 15:31:55 +02:00
|
|
|
localmodule = Path(__file__).resolve().parent.parent
|
|
|
|
print('localmodule: ' + str(localmodule))
|
2019-07-25 02:26:43 +02:00
|
|
|
if localmodule not in sys.path:
|
2021-06-08 15:31:55 +02:00
|
|
|
sys.path.insert(0, str(localmodule))
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
from fdroidserver import common
|
|
|
|
from fdroidserver import rewritemeta
|
|
|
|
from fdroidserver.exception import FDroidException
|
|
|
|
|
|
|
|
|
|
|
|
class RewriteMetaTest(unittest.TestCase):
|
|
|
|
'''fdroidserver/publish.py'''
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
2021-06-08 15:31:55 +02:00
|
|
|
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))
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
def test_rewrite_scenario_trivial(self):
|
|
|
|
|
2019-07-26 14:07:22 +02:00
|
|
|
sys.argv = ['rewritemeta', 'a', 'b']
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
2021-06-08 15:31:55 +02:00
|
|
|
Path('metadata').mkdir()
|
|
|
|
with Path('metadata/a.yml').open('w') as f:
|
2020-07-02 00:48:03 +02:00
|
|
|
f.write('AutoName: a')
|
2021-06-08 15:31:55 +02:00
|
|
|
with Path('metadata/b.yml').open('w') as f:
|
2019-07-25 02:26:43 +02:00
|
|
|
f.write('AutoName: b')
|
|
|
|
|
|
|
|
rewritemeta.main()
|
|
|
|
|
2021-06-08 15:31:55 +02:00
|
|
|
self.assertEqual(
|
|
|
|
Path('metadata/a.yml').read_text(),
|
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
2019-07-25 02:26:43 +02:00
|
|
|
License: Unknown
|
|
|
|
|
2020-07-02 00:48:03 +02:00
|
|
|
AutoName: a
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
2021-06-07 11:49:21 +02:00
|
|
|
'''
|
2021-06-08 15:31:55 +02:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
self.assertEqual(
|
|
|
|
Path('metadata/b.yml').read_text(),
|
|
|
|
textwrap.dedent(
|
|
|
|
'''\
|
2019-07-25 02:26:43 +02:00
|
|
|
License: Unknown
|
|
|
|
|
2020-07-02 00:48:03 +02:00
|
|
|
AutoName: b
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
AutoUpdateMode: None
|
|
|
|
UpdateCheckMode: None
|
2021-06-07 11:49:21 +02:00
|
|
|
'''
|
2021-06-08 15:31:55 +02:00
|
|
|
),
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
|
2019-07-26 14:07:22 +02:00
|
|
|
def test_rewrite_scenario_yml_no_ruamel(self):
|
|
|
|
sys.argv = ['rewritemeta', 'a']
|
|
|
|
with tempfile.TemporaryDirectory() as tmpdir, TmpCwd(tmpdir):
|
2021-06-08 15:31:55 +02:00
|
|
|
Path('metadata').mkdir()
|
|
|
|
with Path('metadata/a.yml').open('w') as f:
|
2019-07-26 14:07:22 +02:00
|
|
|
f.write('AutoName: a')
|
|
|
|
|
2019-07-29 17:39:51 +02:00
|
|
|
def boom(*args):
|
|
|
|
raise FDroidException(' '.join((str(x) for x in args)))
|
2019-07-26 14:07:22 +02:00
|
|
|
|
2019-07-29 17:39:51 +02:00
|
|
|
with mock.patch('importlib.util.find_spec', boom):
|
2019-07-26 14:07:22 +02:00
|
|
|
with self.assertRaises(FDroidException):
|
|
|
|
rewritemeta.main()
|
|
|
|
|
2021-06-08 15:31:55 +02:00
|
|
|
self.assertEqual(
|
|
|
|
Path('metadata/a.yml').read_text(), 'AutoName: a'
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
parser = optparse.OptionParser()
|
2021-06-07 11:49:21 +02:00
|
|
|
parser.add_option(
|
|
|
|
"-v",
|
|
|
|
"--verbose",
|
|
|
|
action="store_true",
|
|
|
|
default=False,
|
|
|
|
help="Spew out even more information than normal",
|
|
|
|
)
|
2019-07-25 02:26:43 +02:00
|
|
|
(common.options, args) = parser.parse_args(['--verbose'])
|
|
|
|
|
|
|
|
newSuite = unittest.TestSuite()
|
|
|
|
newSuite.addTest(unittest.makeSuite(RewriteMetaTest))
|
|
|
|
unittest.main(failfast=False)
|