[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
__revision__ = '$Format:%H$'
from PyQt4 import QtGui, QtCore
from qgis.core import QgsApplication
from PyQt4 import QtCore
import sys
import os
import time
import locale
import shutil
import subprocess
import tempfile
from utilities import unittest, getQgisTestApp, unitTestDataPath
QGISAPP, CANVAS, IFACE, PARENT = getQgisTestApp()
from utilities import unittest, unitTestDataPath
TEST_DATA_DIR = unitTestDataPath()
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 = {}):
"""Run QGIS with the given option. Wait for testFile to be created. If time runs out, fail.
"""
# from unicode to local
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 ):
os.remove( myTestFile )
@ -44,7 +56,7 @@ class TestPyQgsAppStartup(unittest.TestCase):
myenv.update( env )
p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv )
s = 0
ok = True
while not os.path.exists( myTestFile ):
@ -55,50 +67,57 @@ class TestPyQgsAppStartup(unittest.TestCase):
break
p.terminate()
# remove testDir
shutil.rmtree( testDir, ignore_errors = True )
return ok
def testOptionsPath( self ):
for p in [ 'test_config', 'test config', 'test_configé€' ]:
assert self.doTestOptionsPath( "--optionspath", (os.getcwd() + '/' + p).decode('utf-8'), "QGIS/QGIS2.ini", 5 ), "options path %s" % p
subdir = 'QGIS' # Linux
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 ):
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 ):
for t in ['test_plugins', 'test plugins', 'test_pluginsé€' ]:
# 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
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
# 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__':
# look for qgis bin path
QGIS_BIN = ''
prefixPath = os.environ['QGIS_PREFIX_PATH']
# see qgsapplication.cpp:98
for f in [ '', '/..', '/bin', '/../../..' ]:
testDir = prefixPath + f
if os.path.exists( testDir + '/qgis' ):
QGIS_BIN = testDir + '/qgis'
for f in ['', '..', 'bin']:
d = os.path.join(prefixPath, f)
b = os.path.abspath(os.path.join(d, 'qgis'))
if os.path.exists(b):
QGIS_BIN = b
break
if os.path.exists( testDir + '/qgis.exe' ):
QGIS_BIN = testDir + '/qgis.exe'
b = os.path.abspath(os.path.join(d, '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
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()

View File

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