Skip to content

Commit 8fb9816

Browse files
committedSep 15, 2013
[tests] Make app startup test work on Mac
- Handle bundled app path on Mac - Move test writes to temporary, instead of source, directory - Add Mac code for options.ini parent directory - Add Mac options.ini file for plugin path test - Clean up imports
1 parent 68d12b4 commit 8fb9816

File tree

2 files changed

+48
-27
lines changed

2 files changed

+48
-27
lines changed
 

‎tests/src/python/test_qgsappstartup.py

Lines changed: 46 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,41 @@
1212
# This will get replaced with a git SHA1 when you do a git archive
1313
__revision__ = '$Format:%H$'
1414

15-
from PyQt4 import QtGui, QtCore
16-
from qgis.core import QgsApplication
15+
from PyQt4 import QtCore
1716
import sys
1817
import os
1918
import time
2019
import locale
2120
import shutil
2221
import subprocess
22+
import tempfile
2323

24-
from utilities import unittest, getQgisTestApp, unitTestDataPath
25-
26-
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
24+
from utilities import unittest, unitTestDataPath
2725

2826
TEST_DATA_DIR = unitTestDataPath()
2927

28+
3029
class TestPyQgsAppStartup(unittest.TestCase):
3130

31+
TMP_DIR = ''
32+
33+
@classmethod
34+
def setUpClass(cls):
35+
cls.TMP_DIR = tempfile.mkdtemp()
36+
37+
@classmethod
38+
def tearDownClass(cls):
39+
shutil.rmtree(cls.TMP_DIR, ignore_errors=True)
40+
3241
def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}):
3342
"""Run QGIS with the given option. Wait for testFile to be created. If time runs out, fail.
3443
"""
3544
# from unicode to local
3645
testDir = str(QtCore.QString( testDir ).toLocal8Bit())
37-
myTestFile = testDir + "/" + testFile
46+
if not os.path.exists(testDir):
47+
os.mkdir(testDir)
48+
myTestFile = os.path.join(testDir, testFile)
49+
# print 'myTestFile: ', myTestFile
3850

3951
if os.path.exists( myTestFile ):
4052
os.remove( myTestFile )
@@ -44,7 +56,7 @@ def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}):
4456
myenv.update( env )
4557

4658
p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv )
47-
59+
4860
s = 0
4961
ok = True
5062
while not os.path.exists( myTestFile ):
@@ -55,50 +67,57 @@ def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}):
5567
break
5668

5769
p.terminate()
58-
59-
# remove testDir
60-
shutil.rmtree( testDir, ignore_errors = True )
6170
return ok
6271

6372
def testOptionsPath( self ):
64-
for p in [ 'test_config', 'test config', 'test_configé€' ]:
65-
assert self.doTestOptionsPath( "--optionspath", (os.getcwd() + '/' + p).decode('utf-8'), "QGIS/QGIS2.ini", 5 ), "options path %s" % p
73+
subdir = 'QGIS' # Linux
74+
if sys.platform[:3] == 'dar': # Mac
75+
subdir = 'qgis.org'
76+
ini = os.path.join(subdir, 'QGIS2.ini')
77+
for p in [ 'test_opts', 'test opts', 'test_optsé€' ]:
78+
assert self.doTestOptionsPath( "--optionspath", os.path.join(self.TMP_DIR, p), ini, 5 ), "options path %s" % p
6679

6780
def testConfigPath( self ):
6881
for p in [ 'test_config', 'test config', 'test_configé€' ]:
69-
assert self.doTestOptionsPath( "--configpath", (os.getcwd() + '/' + p).decode('utf-8'), "qgis.db", 30 ), "config path %s" % p
82+
assert self.doTestOptionsPath( "--configpath", os.path.join(self.TMP_DIR, p), "qgis.db", 30 ), "config path %s" % p
7083

7184
def testPluginPath( self ):
7285
for t in ['test_plugins', 'test plugins', 'test_pluginsé€' ]:
73-
86+
7487
# get a unicode test dir
75-
testDir = (os.getcwd() + '/' + t).decode('utf-8')
88+
testDir = (os.path.join(self.TMP_DIR, t)).decode('utf-8')
7689

7790
# copy from testdata
7891
shutil.rmtree( testDir, ignore_errors = True )
79-
shutil.copytree( TEST_DATA_DIR + '/test_plugin_path', testDir )
92+
shutil.copytree( os.path.join(TEST_DATA_DIR, 'test_plugin_path'), testDir )
8093

8194
# we use here a minimal plugin that writes to 'plugin_started.txt' when it is started
8295
# if QGIS_PLUGINPATH is correctly parsed, this plugin is executed and the file is created
83-
assert self.doTestOptionsPath( "--optionspath", testDir, "plugin_started.txt", 10, { 'QGIS_PLUGINPATH' : str(QtCore.QString(testDir).toLocal8Bit()) } )
96+
assert self.doTestOptionsPath( "--optionspath", testDir, "plugin_started.txt", 10,
97+
{ 'QGIS_PLUGINPATH' : str(QtCore.QString(testDir).toLocal8Bit()) } )
8498

8599

86100
if __name__ == '__main__':
87-
88101
# look for qgis bin path
89102
QGIS_BIN = ''
90103
prefixPath = os.environ['QGIS_PREFIX_PATH']
91104
# see qgsapplication.cpp:98
92-
for f in [ '', '/..', '/bin', '/../../..' ]:
93-
testDir = prefixPath + f
94-
if os.path.exists( testDir + '/qgis' ):
95-
QGIS_BIN = testDir + '/qgis'
105+
for f in ['', '..', 'bin']:
106+
d = os.path.join(prefixPath, f)
107+
b = os.path.abspath(os.path.join(d, 'qgis'))
108+
if os.path.exists(b):
109+
QGIS_BIN = b
110+
break
111+
b = os.path.abspath(os.path.join(d, 'qgis.exe'))
112+
if os.path.exists(b):
113+
QGIS_BIN = b
96114
break
97-
if os.path.exists( testDir + '/qgis.exe' ):
98-
QGIS_BIN = testDir + '/qgis.exe'
115+
b = os.path.abspath(os.path.join(d, 'QGIS.app/Contents/MacOS/QGIS'))
116+
if os.path.exists(b):
117+
QGIS_BIN = b
99118
break
100-
101-
print 'QGIS_BIN =', QGIS_BIN
102119

120+
print 'QGIS_BIN: ', QGIS_BIN
121+
assert 'qgis' in QGIS_BIN.lower() and os.path.exists(QGIS_BIN), \
122+
'QGIS binary not found, skipping test suite'
103123
unittest.main()
104-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
[PythonPlugins]
2+
PluginPathTest=true

0 commit comments

Comments
 (0)
Please sign in to comment.