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