Showing with 103 additions and 30 deletions.
  1. +12 −0 CMakeLists.txt
  2. +8 −0 src/python/qgspythonutilsimpl.cpp
  3. +83 −30 tests/src/python/test_qgsappstartup.py
12 changes: 12 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,18 @@ IF (PEDANTIC)
IF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-return-type-c-linkage -Wno-overloaded-virtual")
ENDIF ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")

# add any extra CXXFLAGS flags set by user. can be -D CXX_EXTRA_FLAGS or environment variable
# command line -D option overrides environment variable
# e.g. useful for suppressing transient upstream warnings in dependencies, like Qt
SET(CXX_EXTRA_FLAGS "" CACHE STRING "Additional appended CXXFLAGS")
IF ("${CXX_EXTRA_FLAGS}" STREQUAL "" AND DEFINED $ENV{CXX_EXTRA_FLAGS})
SET(CXX_EXTRA_FLAGS "$ENV{CXX_EXTRA_FLAGS}")
ENDIF ("${CXX_EXTRA_FLAGS}" STREQUAL "" AND DEFINED $ENV{CXX_EXTRA_FLAGS})
IF (NOT "${CXX_EXTRA_FLAGS}" STREQUAL "")
MESSAGE (STATUS "Appending CXX_EXTRA_FLAGS")
SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CXX_EXTRA_FLAGS}")
ENDIF (NOT "${CXX_EXTRA_FLAGS}" STREQUAL "")
ENDIF (MSVC)

ENDIF (PEDANTIC)
Expand Down
8 changes: 8 additions & 0 deletions src/python/qgspythonutilsimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ void QgsPythonUtilsImpl::initPython( QgisInterface* interface )
runString( "import sys" ); // import sys module (for display / exception hooks)
runString( "import os" ); // import os module (for user paths)

// support for PYTHONSTARTUP-like environment variable: PYQGIS_STARTUP
// (unlike PYTHONHOME and PYTHONPATH, PYTHONSTARTUP is not supported for embedded interpreter by default)
// this is different than user's 'startup.py' (below), since it is loaded just after Py_Initialize
// it is very useful for cleaning sys.path, which may have undesireable paths, or for
// isolating/loading the initial environ without requiring a virt env, e.g. homebrew or MacPorts installs on Mac
runString( "pyqgstart = os.getenv('PYQGIS_STARTUP')\n" );
runString( "if pyqgstart is not None and os.path.exists(pyqgstart): execfile(pyqgstart)\n" );

#ifdef Q_OS_WIN
runString( "oldhome=None" );
runString( "if os.environ.has_key('HOME'): oldhome=os.environ['HOME']\n" );
Expand Down
113 changes: 83 additions & 30 deletions tests/src/python/test_qgsappstartup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
"""QGIS Unit tests for QgsApplication.
From build dir: ctest -R PyQgsAppStartup -V
.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
Expand All @@ -16,7 +18,7 @@
import sys
import os
import time
import locale
# import locale
import shutil
import subprocess
import tempfile
Expand All @@ -38,63 +40,113 @@ def setUpClass(cls):
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.
# TODO: refactor parameters to **kwargs to handle all startup combinations
def doTestStartup(self, option='', testDir='', testFile='',
loadPlugins=False, customization=False,
timeOut=10, env=None):
"""Run QGIS with the given option. Wait for testFile to be created.
If time runs out, fail.
"""
myTestFile = testFile

# from unicode to local
testDir = str(QtCore.QString( testDir ).toLocal8Bit())
if not os.path.exists(testDir):
os.mkdir(testDir)
myTestFile = os.path.join(testDir, testFile)
if testDir:
testDir = str(QtCore.QString(testDir).toLocal8Bit())
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 )
if os.path.exists(myTestFile):
os.remove(myTestFile)

# whether to load plugins
plugins = '' if loadPlugins else '--noplugins'

# whether to enable GUI customization
customize = '' if customization else '--nocustomization'

# environnement variables = system variables + provided 'env'
myenv = os.environ.copy()
myenv.update( env )
if env is not None:
myenv.update(env)

p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv )
p = subprocess.Popen(
[QGIS_BIN, "--nologo", plugins, customize, option, testDir],
env=myenv)

s = 0
ok = True
while not os.path.exists( myTestFile ):
while not os.path.exists(myTestFile):
time.sleep(1)
s = s+1
s += 1
if s > timeOut:
ok = False
break

p.terminate()
return ok

def testOptionsPath( self ):
def testOptionsPath(self):
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.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é€' ]:
for p in ['test_opts', 'test opts', 'test_optsé€']:
assert self.doTestStartup(option="--optionspath",
testDir=os.path.join(self.TMP_DIR, p),
testFile=ini,
timeOut=5), "options path %s" % p

def testConfigPath(self):
for p in ['test_config', 'test config', 'test_configé€']:
assert self.doTestStartup(option="--configpath",
testDir=os.path.join(self.TMP_DIR, p),
testFile="qgis.db",
timeOut=30), "config path %s" % p

def testPluginPath(self):
for t in ['test_plugins', 'test plugins', 'test_pluginsé€']:

# get a unicode test dir
testDir = (os.path.join(self.TMP_DIR, t)).decode('utf-8')

# copy from testdata
shutil.rmtree( testDir, ignore_errors = True )
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()) } )
shutil.rmtree(testDir, ignore_errors=True)
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.doTestStartup(
option="--optionspath",
testDir=testDir,
testFile="plugin_started.txt",
timeOut=10,
env={'QGIS_PLUGINPATH':
str(QtCore.QString(testDir).toLocal8Bit())})

def testPyQgisStartupEnvVar(self):
# verify PYQGIS_STARTUP env variable file is run by embedded interpreter
# create a temp python module that writes out test file
testfile = 'pyqgis_startup.txt'
testfilepath = os.path.join(self.TMP_DIR, testfile)
testcode = [
"f = open('{0}', 'w')\n".format(testfilepath),
"f.write('This is a test')\n",
"f.close()\n"
]
testmod = os.path.join(self.TMP_DIR, 'pyqgis_startup.py')
f = open(testmod, 'w')
f.writelines(testcode)
f.close()
msg = 'Failed to create test file from executing PYQGIS_STARTUP file'
assert self.doTestStartup(
testFile=testfilepath,
timeOut=10,
env={'PYQGIS_STARTUP': testmod}), msg


if __name__ == '__main__':
Expand All @@ -117,6 +169,7 @@ def testPluginPath( self ):
QGIS_BIN = b
break

print ''
print 'QGIS_BIN: ', QGIS_BIN
assert 'qgis' in QGIS_BIN.lower() and os.path.exists(QGIS_BIN), \
'QGIS binary not found, skipping test suite'
Expand Down