chiark / gitweb /
common: check file existence before opening manifest
[fdroidserver.git] / tests / build.TestCase
1 #!/usr/bin/env python3
2
3 # http://www.drdobbs.com/testing/unit-testing-with-python/240165163
4
5 import inspect
6 import optparse
7 import os
8 import re
9 import shutil
10 import sys
11 import tempfile
12 import unittest
13
14 localmodule = os.path.realpath(
15     os.path.join(os.path.dirname(inspect.getfile(inspect.currentframe())), '..'))
16 print('localmodule: ' + localmodule)
17 if localmodule not in sys.path:
18     sys.path.insert(0, localmodule)
19
20 import fdroidserver.build
21 import fdroidserver.common
22
23
24 class BuildTest(unittest.TestCase):
25     '''fdroidserver/build.py'''
26
27     def _set_build_tools(self):
28         build_tools = os.path.join(fdroidserver.common.config['sdk_path'], 'build-tools')
29         if os.path.exists(build_tools):
30             fdroidserver.common.config['build_tools'] = ''
31             for f in sorted(os.listdir(build_tools), reverse=True):
32                 versioned = os.path.join(build_tools, f)
33                 if os.path.isdir(versioned) \
34                         and os.path.isfile(os.path.join(versioned, 'aapt')):
35                     fdroidserver.common.config['build_tools'] = versioned
36                     break
37             return True
38         else:
39             print('no build-tools found: ' + build_tools)
40             return False
41
42     def _find_all(self):
43         for cmd in ('aapt', 'adb', 'android', 'zipalign'):
44             path = fdroidserver.common.find_sdk_tools_cmd(cmd)
45             if path is not None:
46                 self.assertTrue(os.path.exists(path))
47                 self.assertTrue(os.path.isfile(path))
48
49     def test_force_gradle_build_tools(self):
50         testsbase = os.path.join(os.path.dirname(__file__), '..', '.testfiles')
51         if not os.path.exists(testsbase):
52             os.makedirs(testsbase)
53         testsdir = tempfile.mkdtemp(prefix='test_adapt_gradle', dir=testsbase)
54         shutil.copytree(os.path.join(os.path.dirname(__file__), 'source-files'),
55                         os.path.join(testsdir, 'source-files'))
56         teststring = 'FAKE_VERSION_FOR_TESTING'
57         fdroidserver.build.force_gradle_build_tools(testsdir, teststring)
58         pattern = re.compile(bytes("buildToolsVersion[\s=]+'%s'\s+" % teststring, 'utf8'))
59         for p in ('source-files/fdroid/fdroidclient/build.gradle',
60                   'source-files/Zillode/syncthing-silk/build.gradle',
61                   'source-files/open-keychain/open-keychain/build.gradle',
62                   'source-files/osmandapp/osmand/build.gradle',
63                   'source-files/open-keychain/open-keychain/OpenKeychain/build.gradle'):
64             with open(os.path.join(testsdir, p), 'rb') as f:
65                 filedata = f.read()
66             self.assertIsNotNone(pattern.search(filedata))
67
68
69 if __name__ == "__main__":
70     parser = optparse.OptionParser()
71     parser.add_option("-v", "--verbose", action="store_true", default=False,
72                       help="Spew out even more information than normal")
73     (fdroidserver.common.options, args) = parser.parse_args(['--verbose'])
74
75     newSuite = unittest.TestSuite()
76     newSuite.addTest(unittest.makeSuite(BuildTest))
77     unittest.main()