From 8fb9816c67f49b160ade18ea7c133abb44b8f5fe Mon Sep 17 00:00:00 2001 From: Larry Shaffer Date: Sun, 15 Sep 2013 16:18:26 -0600 Subject: [PATCH] [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 --- tests/src/python/test_qgsappstartup.py | 73 ++++++++++++------- .../test_plugin_path/qgis.org/QGIS2.ini | 2 + 2 files changed, 48 insertions(+), 27 deletions(-) create mode 100644 tests/testdata/test_plugin_path/qgis.org/QGIS2.ini diff --git a/tests/src/python/test_qgsappstartup.py b/tests/src/python/test_qgsappstartup.py index e8d2702e67bd..49914d1c8b7c 100644 --- a/tests/src/python/test_qgsappstartup.py +++ b/tests/src/python/test_qgsappstartup.py @@ -12,29 +12,41 @@ # 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 @@ def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}): 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 @@ def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}): 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 + b = os.path.abspath(os.path.join(d, 'qgis.exe')) + 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.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() - diff --git a/tests/testdata/test_plugin_path/qgis.org/QGIS2.ini b/tests/testdata/test_plugin_path/qgis.org/QGIS2.ini new file mode 100644 index 000000000000..d7f926bf075a --- /dev/null +++ b/tests/testdata/test_plugin_path/qgis.org/QGIS2.ini @@ -0,0 +1,2 @@ +[PythonPlugins] +PluginPathTest=true