Skip to content

Commit 87163c6

Browse files
committedSep 24, 2013
Add unit test for PYQGIS_STARTUP environment variable
- Make PyQgsAppStartup test more general and PEP8 compliant
1 parent 4c1b95b commit 87163c6

File tree

1 file changed

+83
-30
lines changed

1 file changed

+83
-30
lines changed
 

‎tests/src/python/test_qgsappstartup.py

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# -*- coding: utf-8 -*-
22
"""QGIS Unit tests for QgsApplication.
33
4+
From build dir: ctest -R PyQgsAppStartup -V
5+
46
.. note:: This program is free software; you can redistribute it and/or modify
57
it under the terms of the GNU General Public License as published by
68
the Free Software Foundation; either version 2 of the License, or
@@ -16,7 +18,7 @@
1618
import sys
1719
import os
1820
import time
19-
import locale
21+
# import locale
2022
import shutil
2123
import subprocess
2224
import tempfile
@@ -38,63 +40,113 @@ def setUpClass(cls):
3840
def tearDownClass(cls):
3941
shutil.rmtree(cls.TMP_DIR, ignore_errors=True)
4042

41-
def doTestOptionsPath(self, option, testDir, testFile, timeOut, env = {}):
42-
"""Run QGIS with the given option. Wait for testFile to be created. If time runs out, fail.
43+
# TODO: refactor parameters to **kwargs to handle all startup combinations
44+
def doTestStartup(self, option='', testDir='', testFile='',
45+
loadPlugins=False, customization=False,
46+
timeOut=10, env=None):
47+
"""Run QGIS with the given option. Wait for testFile to be created.
48+
If time runs out, fail.
4349
"""
50+
myTestFile = testFile
51+
4452
# from unicode to local
45-
testDir = str(QtCore.QString( testDir ).toLocal8Bit())
46-
if not os.path.exists(testDir):
47-
os.mkdir(testDir)
48-
myTestFile = os.path.join(testDir, testFile)
53+
if testDir:
54+
testDir = str(QtCore.QString(testDir).toLocal8Bit())
55+
if not os.path.exists(testDir):
56+
os.mkdir(testDir)
57+
myTestFile = os.path.join(testDir, testFile)
58+
4959
# print 'myTestFile: ', myTestFile
5060

51-
if os.path.exists( myTestFile ):
52-
os.remove( myTestFile )
61+
if os.path.exists(myTestFile):
62+
os.remove(myTestFile)
63+
64+
# whether to load plugins
65+
plugins = '' if loadPlugins else '--noplugins'
66+
67+
# whether to enable GUI customization
68+
customize = '' if customization else '--nocustomization'
5369

5470
# environnement variables = system variables + provided 'env'
5571
myenv = os.environ.copy()
56-
myenv.update( env )
72+
if env is not None:
73+
myenv.update(env)
5774

58-
p = subprocess.Popen( [ QGIS_BIN, "--nologo", option, testDir ], env = myenv )
75+
p = subprocess.Popen(
76+
[QGIS_BIN, "--nologo", plugins, customize, option, testDir],
77+
env=myenv)
5978

6079
s = 0
6180
ok = True
62-
while not os.path.exists( myTestFile ):
81+
while not os.path.exists(myTestFile):
6382
time.sleep(1)
64-
s = s+1
83+
s += 1
6584
if s > timeOut:
6685
ok = False
6786
break
6887

6988
p.terminate()
7089
return ok
7190

72-
def testOptionsPath( self ):
91+
def testOptionsPath(self):
7392
subdir = 'QGIS' # Linux
7493
if sys.platform[:3] == 'dar': # Mac
7594
subdir = 'qgis.org'
7695
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
79-
80-
def testConfigPath( self ):
81-
for p in [ 'test_config', 'test config', 'test_configé€' ]:
82-
assert self.doTestOptionsPath( "--configpath", os.path.join(self.TMP_DIR, p), "qgis.db", 30 ), "config path %s" % p
83-
84-
def testPluginPath( self ):
85-
for t in ['test_plugins', 'test plugins', 'test_pluginsé€' ]:
96+
for p in ['test_opts', 'test opts', 'test_optsé€']:
97+
assert self.doTestStartup(option="--optionspath",
98+
testDir=os.path.join(self.TMP_DIR, p),
99+
testFile=ini,
100+
timeOut=5), "options path %s" % p
101+
102+
def testConfigPath(self):
103+
for p in ['test_config', 'test config', 'test_configé€']:
104+
assert self.doTestStartup(option="--configpath",
105+
testDir=os.path.join(self.TMP_DIR, p),
106+
testFile="qgis.db",
107+
timeOut=30), "config path %s" % p
108+
109+
def testPluginPath(self):
110+
for t in ['test_plugins', 'test plugins', 'test_pluginsé€']:
86111

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

90115
# copy from testdata
91-
shutil.rmtree( testDir, ignore_errors = True )
92-
shutil.copytree( os.path.join(TEST_DATA_DIR, 'test_plugin_path'), testDir )
93-
94-
# we use here a minimal plugin that writes to 'plugin_started.txt' when it is started
95-
# if QGIS_PLUGINPATH is correctly parsed, this plugin is executed and the file is created
96-
assert self.doTestOptionsPath( "--optionspath", testDir, "plugin_started.txt", 10,
97-
{ 'QGIS_PLUGINPATH' : str(QtCore.QString(testDir).toLocal8Bit()) } )
116+
shutil.rmtree(testDir, ignore_errors=True)
117+
shutil.copytree(os.path.join(TEST_DATA_DIR, 'test_plugin_path'),
118+
testDir)
119+
120+
# we use here a minimal plugin that writes to 'plugin_started.txt'
121+
# when it is started. if QGIS_PLUGINPATH is correctly parsed, this
122+
# plugin is executed and the file is created
123+
assert self.doTestStartup(
124+
option="--optionspath",
125+
testDir=testDir,
126+
testFile="plugin_started.txt",
127+
timeOut=10,
128+
env={'QGIS_PLUGINPATH':
129+
str(QtCore.QString(testDir).toLocal8Bit())})
130+
131+
def testPyQgisStartupEnvVar(self):
132+
# verify PYQGIS_STARTUP env variable file is run by embedded interpreter
133+
# create a temp python module that writes out test file
134+
testfile = 'pyqgis_startup.txt'
135+
testfilepath = os.path.join(self.TMP_DIR, testfile)
136+
testcode = [
137+
"f = open('{0}', 'w')\n".format(testfilepath),
138+
"f.write('This is a test')\n",
139+
"f.close()\n"
140+
]
141+
testmod = os.path.join(self.TMP_DIR, 'pyqgis_startup.py')
142+
f = open(testmod, 'w')
143+
f.writelines(testcode)
144+
f.close()
145+
msg = 'Failed to create test file from executing PYQGIS_STARTUP file'
146+
assert self.doTestStartup(
147+
testFile=testfilepath,
148+
timeOut=10,
149+
env={'PYQGIS_STARTUP': testmod}), msg
98150

99151

100152
if __name__ == '__main__':
@@ -117,6 +169,7 @@ def testPluginPath( self ):
117169
QGIS_BIN = b
118170
break
119171

172+
print ''
120173
print 'QGIS_BIN: ', QGIS_BIN
121174
assert 'qgis' in QGIS_BIN.lower() and os.path.exists(QGIS_BIN), \
122175
'QGIS binary not found, skipping test suite'

0 commit comments

Comments
 (0)