[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
This commit is contained in:
Larry Shaffer 2013-09-15 16:18:26 -06:00
parent 68d12b45b1
commit 8fb9816c67
2 changed files with 48 additions and 27 deletions

View File

@ -12,29 +12,41 @@ __copyright__ = 'Copyright 2013, The QGIS Project'
# This will get replaced with a git SHA1 when you do a git archive # This will get replaced with a git SHA1 when you do a git archive
__revision__ = '$Format:%H$' __revision__ = '$Format:%H$'
from PyQt4 import QtGui, QtCore from PyQt4 import QtCore
from qgis.core import QgsApplication
import sys import sys
import os import os
import time import time
import locale import locale
import shutil import shutil
import subprocess import subprocess
import tempfile
from utilities import unittest, getQgisTestApp, unitTestDataPath from utilities import unittest, unitTestDataPath
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
TEST_DATA_DIR = unitTestDataPath() TEST_DATA_DIR = unitTestDataPath()
class TestPyQgsAppStartup(unittest.TestCase): class TestPyQgsAppStartup(unittest.TestCase):
TMP_DIR = ''
@classmethod
def setUpClass(cls):
cls.TMP_DIR = tempfile.mkdtemp()
@classmethod
def tearDownClass(cls):
shutil.rmtree(cls.TMP_DIR, ignore_errors=True)
def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}): def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}):
"""Run QGIS with the given option. Wait for testFile to be created. If time runs out, fail. """Run QGIS with the given option. Wait for testFile to be created. If time runs out, fail.
""" """
# from unicode to local # from unicode to local
testDir = str(QtCore.QString( testDir ).toLocal8Bit()) testDir = str(QtCore.QString( testDir ).toLocal8Bit())
myTestFile = testDir + "/" + testFile if not os.path.exists(testDir):
os.mkdir(testDir)
myTestFile = os.path.join(testDir, testFile)
# print 'myTestFile: ', myTestFile
if os.path.exists( myTestFile ): if os.path.exists( myTestFile ):
os.remove( myTestFile ) os.remove( myTestFile )
@ -44,7 +56,7 @@ class TestPyQgsAppStartup(unittest.TestCase):
myenv.update( env ) myenv.update( env )
p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv ) p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv )
s = 0 s = 0
ok = True ok = True
while not os.path.exists( myTestFile ): while not os.path.exists( myTestFile ):
@ -55,50 +67,57 @@ class TestPyQgsAppStartup(unittest.TestCase):
break break
p.terminate() p.terminate()
# remove testDir
shutil.rmtree( testDir, ignore_errors = True )
return ok return ok
def testOptionsPath( self ): def testOptionsPath( self ):
for p in [ 'test_config', 'test config', 'test_configé€' ]: subdir = 'QGIS' # Linux
assert self.doTestOptionsPath( "--optionspath", (os.getcwd() + '/' + p).decode('utf-8'), "QGIS/QGIS2.ini", 5 ), "options path %s" % p if sys.platform[:3] == 'dar': # Mac
subdir = 'qgis.org'
ini = os.path.join(subdir, 'QGIS2.ini')
for p in [ 'test_opts', 'test opts', 'test_optsé€' ]:
assert self.doTestOptionsPath( "--optionspath", os.path.join(self.TMP_DIR, p), ini, 5 ), "options path %s" % p
def testConfigPath( self ): def testConfigPath( self ):
for p in [ 'test_config', 'test config', 'test_configé€' ]: for p in [ 'test_config', 'test config', 'test_configé€' ]:
assert self.doTestOptionsPath( "--configpath", (os.getcwd() + '/' + p).decode('utf-8'), "qgis.db", 30 ), "config path %s" % p assert self.doTestOptionsPath( "--configpath", os.path.join(self.TMP_DIR, p), "qgis.db", 30 ), "config path %s" % p
def testPluginPath( self ): def testPluginPath( self ):
for t in ['test_plugins', 'test plugins', 'test_pluginsé€' ]: for t in ['test_plugins', 'test plugins', 'test_pluginsé€' ]:
# get a unicode test dir # get a unicode test dir
testDir = (os.getcwd() + '/' + t).decode('utf-8') testDir = (os.path.join(self.TMP_DIR, t)).decode('utf-8')
# copy from testdata # copy from testdata
shutil.rmtree( testDir, ignore_errors = True ) shutil.rmtree( testDir, ignore_errors = True )
shutil.copytree( TEST_DATA_DIR + '/test_plugin_path', testDir ) shutil.copytree( os.path.join(TEST_DATA_DIR, 'test_plugin_path'), testDir )
# we use here a minimal plugin that writes to 'plugin_started.txt' when it is started # we use here a minimal plugin that writes to 'plugin_started.txt' when it is started
# if QGIS_PLUGINPATH is correctly parsed, this plugin is executed and the file is created # if QGIS_PLUGINPATH is correctly parsed, this plugin is executed and the file is created
assert self.doTestOptionsPath( "--optionspath", testDir, "plugin_started.txt", 10, { 'QGIS_PLUGINPATH' : str(QtCore.QString(testDir).toLocal8Bit()) } ) assert self.doTestOptionsPath( "--optionspath", testDir, "plugin_started.txt", 10,
{ 'QGIS_PLUGINPATH' : str(QtCore.QString(testDir).toLocal8Bit()) } )
if __name__ == '__main__': if __name__ == '__main__':
# look for qgis bin path # look for qgis bin path
QGIS_BIN = '' QGIS_BIN = ''
prefixPath = os.environ['QGIS_PREFIX_PATH'] prefixPath = os.environ['QGIS_PREFIX_PATH']
# see qgsapplication.cpp:98 # see qgsapplication.cpp:98
for f in [ '', '/..', '/bin', '/../../..' ]: for f in ['', '..', 'bin']:
testDir = prefixPath + f d = os.path.join(prefixPath, f)
if os.path.exists( testDir + '/qgis' ): b = os.path.abspath(os.path.join(d, 'qgis'))
QGIS_BIN = testDir + '/qgis' if os.path.exists(b):
QGIS_BIN = b
break break
if os.path.exists( testDir + '/qgis.exe' ): b = os.path.abspath(os.path.join(d, 'qgis.exe'))
QGIS_BIN = testDir + '/qgis.exe' if os.path.exists(b):
QGIS_BIN = b
break
b = os.path.abspath(os.path.join(d, 'QGIS.app/Contents/MacOS/QGIS'))
if os.path.exists(b):
QGIS_BIN = b
break break
print 'QGIS_BIN =', QGIS_BIN
print 'QGIS_BIN: ', QGIS_BIN
assert 'qgis' in QGIS_BIN.lower() and os.path.exists(QGIS_BIN), \
'QGIS binary not found, skipping test suite'
unittest.main() unittest.main()

View File

@ -0,0 +1,2 @@
[PythonPlugins]
PluginPathTest=true