1
1
# -*- coding: utf-8 -*-
2
2
"""QGIS Unit tests for QgsApplication.
3
3
4
+ From build dir: ctest -R PyQgsAppStartup -V
5
+
4
6
.. note:: This program is free software; you can redistribute it and/or modify
5
7
it under the terms of the GNU General Public License as published by
6
8
the Free Software Foundation; either version 2 of the License, or
16
18
import sys
17
19
import os
18
20
import time
19
- import locale
21
+ # import locale
20
22
import shutil
21
23
import subprocess
22
24
import tempfile
@@ -38,63 +40,113 @@ def setUpClass(cls):
38
40
def tearDownClass (cls ):
39
41
shutil .rmtree (cls .TMP_DIR , ignore_errors = True )
40
42
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.
43
49
"""
50
+ myTestFile = testFile
51
+
44
52
# 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
+
49
59
# print 'myTestFile: ', myTestFile
50
60
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'
53
69
54
70
# environnement variables = system variables + provided 'env'
55
71
myenv = os .environ .copy ()
56
- myenv .update ( env )
72
+ if env is not None :
73
+ myenv .update (env )
57
74
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 )
59
78
60
79
s = 0
61
80
ok = True
62
- while not os .path .exists ( myTestFile ):
81
+ while not os .path .exists (myTestFile ):
63
82
time .sleep (1 )
64
- s = s + 1
83
+ s += 1
65
84
if s > timeOut :
66
85
ok = False
67
86
break
68
87
69
88
p .terminate ()
70
89
return ok
71
90
72
- def testOptionsPath ( self ):
91
+ def testOptionsPath (self ):
73
92
subdir = 'QGIS' # Linux
74
93
if sys .platform [:3 ] == 'dar' : # Mac
75
94
subdir = 'qgis.org'
76
95
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é€' ]:
86
111
87
112
# get a unicode test dir
88
113
testDir = (os .path .join (self .TMP_DIR , t )).decode ('utf-8' )
89
114
90
115
# 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
98
150
99
151
100
152
if __name__ == '__main__' :
@@ -117,6 +169,7 @@ def testPluginPath( self ):
117
169
QGIS_BIN = b
118
170
break
119
171
172
+ print ''
120
173
print 'QGIS_BIN: ' , QGIS_BIN
121
174
assert 'qgis' in QGIS_BIN .lower () and os .path .exists (QGIS_BIN ), \
122
175
'QGIS binary not found, skipping test suite'
0 commit comments