Skip to content

Commit 2ceeb94

Browse files
committed
Fix crash on close of access control test
1 parent 497f270 commit 2ceeb94

File tree

1 file changed

+41
-34
lines changed

1 file changed

+41
-34
lines changed

tests/src/python/test_qgsserver_accesscontrol.py

+41-34
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
print 'CTEST_FULL_OUTPUT'
1616

17-
from os import path, environ
17+
import os
1818
from shutil import copyfile
1919
from math import sqrt
2020
from subprocess import check_output
@@ -160,20 +160,20 @@ class TestQgsServerAccessControl(TestCase):
160160
def setUp(self):
161161
self.testdata_path = unitTestDataPath("qgis_server_accesscontrol")
162162

163-
dataFile = path.join(self.testdata_path, "helloworld.db")
164-
self.assertTrue(path.isfile(dataFile), 'Could not find data file "{}"'.format(dataFile))
165-
copyfile(dataFile, path.join(self.testdata_path, "_helloworld.db"))
163+
dataFile = os.path.join(self.testdata_path, "helloworld.db")
164+
self.assertTrue(os.path.isfile(dataFile), 'Could not find data file "{}"'.format(dataFile))
165+
copyfile(dataFile, os.path.join(self.testdata_path, "_helloworld.db"))
166166

167167
for k in ["QUERY_STRING", "QGIS_PROJECT_FILE"]:
168-
if k in environ:
169-
del environ[k]
168+
if k in os.environ:
169+
del os.environ[k]
170170

171-
self.projectPath = path.join(self.testdata_path, "project.qgs")
172-
self.assertTrue(path.isfile(self.projectPath), 'Could not find project file "{}"'.format(self.projectPath))
171+
self.projectPath = os.path.join(self.testdata_path, "project.qgs")
172+
self.assertTrue(os.path.isfile(self.projectPath), 'Could not find project file "{}"'.format(self.projectPath))
173173
self.projectPath = urllib.quote(self.projectPath)
174174

175175
def tearDown(self):
176-
copyfile(path.join(self.testdata_path, "_helloworld.db"), path.join(self.testdata_path, "helloworld.db"))
176+
copyfile(os.path.join(self.testdata_path, "_helloworld.db"), os.path.join(self.testdata_path, "helloworld.db"))
177177

178178
# # WMS # # WMS # # WMS # #
179179

@@ -997,39 +997,39 @@ def _result(self, data):
997997
return data[1], headers
998998

999999
def _get_fullaccess(self, query_string):
1000-
environ["REQUEST_METHOD"] = "GET"
1000+
os.environ["REQUEST_METHOD"] = "GET"
10011001
result = self._handle_request(False, query_string)
1002-
del environ["REQUEST_METHOD"]
1002+
del os.environ["REQUEST_METHOD"]
10031003
return result
10041004

10051005
def _get_restricted(self, query_string):
1006-
environ["REQUEST_METHOD"] = "GET"
1006+
os.environ["REQUEST_METHOD"] = "GET"
10071007
result = self._handle_request(True, query_string)
1008-
del environ["REQUEST_METHOD"]
1008+
del os.environ["REQUEST_METHOD"]
10091009
return result
10101010

10111011
def _post_fullaccess(self, data, query_string=None):
1012-
environ["REQUEST_METHOD"] = "POST"
1013-
environ["REQUEST_BODY"] = data
1014-
environ["QGIS_PROJECT_FILE"] = self.projectPath
1012+
os.environ["REQUEST_METHOD"] = "POST"
1013+
os.environ["REQUEST_BODY"] = data
1014+
os.environ["QGIS_PROJECT_FILE"] = self.projectPath
10151015
result = self._handle_request(False, query_string)
1016-
del environ["REQUEST_METHOD"]
1017-
del environ["REQUEST_BODY"]
1018-
del environ["QGIS_PROJECT_FILE"]
1016+
del os.environ["REQUEST_METHOD"]
1017+
del os.environ["REQUEST_BODY"]
1018+
del os.environ["QGIS_PROJECT_FILE"]
10191019
return result
10201020

10211021
def _post_restricted(self, data, query_string=None):
1022-
environ["REQUEST_METHOD"] = "POST"
1023-
environ["REQUEST_BODY"] = data
1024-
environ["QGIS_PROJECT_FILE"] = self.projectPath
1022+
os.environ["REQUEST_METHOD"] = "POST"
1023+
os.environ["REQUEST_BODY"] = data
1024+
os.environ["QGIS_PROJECT_FILE"] = self.projectPath
10251025
result = self._handle_request(True, query_string)
1026-
del environ["REQUEST_METHOD"]
1027-
del environ["REQUEST_BODY"]
1028-
del environ["QGIS_PROJECT_FILE"]
1026+
del os.environ["REQUEST_METHOD"]
1027+
del os.environ["REQUEST_BODY"]
1028+
del os.environ["QGIS_PROJECT_FILE"]
10291029
return result
10301030

10311031
def _img_diff(self, image, control_image, max_diff, max_size_diff=QSize()):
1032-
temp_image = path.join(tempfile.gettempdir(), "%s_result.png" % control_image)
1032+
temp_image = os.path.join(tempfile.gettempdir(), "%s_result.png" % control_image)
10331033

10341034
with open(temp_image, "wb") as f:
10351035
f.write(image)
@@ -1048,13 +1048,13 @@ def _img_diff_error(self, response, headers, image, max_diff=10, max_size_diff=Q
10481048
"Content type is wrong: %s" % headers.get("Content-Type"))
10491049
test, report = self._img_diff(response, image, max_diff, max_size_diff)
10501050

1051-
with open(path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file:
1051+
with open(os.path.join(tempfile.gettempdir(), image + "_result.png"), "rb") as rendered_file:
10521052
encoded_rendered_file = base64.b64encode(rendered_file.read())
10531053
message = "Image is wrong\n%s\nImage:\necho '%s' | base64 -d >%s/%s_result.png" % (
10541054
report, encoded_rendered_file.strip(), tempfile.gettempdir(), image
10551055
)
10561056

1057-
with open(path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file:
1057+
with open(os.path.join(tempfile.gettempdir(), image + "_result_diff.png"), "rb") as diff_file:
10581058
encoded_diff_file = base64.b64encode(diff_file.read())
10591059
message += "\nDiff:\necho '%s' | base64 -d > %s/%s_result_diff.png" % (
10601060
encoded_diff_file.strip(), tempfile.gettempdir(), image
@@ -1063,24 +1063,31 @@ def _img_diff_error(self, response, headers, image, max_diff=10, max_size_diff=Q
10631063
self.assertTrue(test, message)
10641064

10651065
def _geo_img_diff(self, image_1, image_2):
1066-
with open(path.join(tempfile.gettempdir(), image_2), "wb") as f:
1066+
if os.name == 'nt':
1067+
# Not supported on Windows due to #13061
1068+
return 0
1069+
1070+
with open(os.path.join(tempfile.gettempdir(), image_2), "wb") as f:
10671071
f.write(image_1)
1068-
image_1 = gdal.Open(path.join(tempfile.gettempdir(), image_2), GA_ReadOnly)
1072+
image_1 = gdal.Open(os.path.join(tempfile.gettempdir(), image_2), GA_ReadOnly)
10691073
assert image_1, "No output image written: " + image_2
10701074

1071-
image_2 = gdal.Open(path.join(self.testdata_path, "results", image_2), GA_ReadOnly)
1075+
image_2 = gdal.Open(os.path.join(self.testdata_path, "results", image_2), GA_ReadOnly)
10721076
assert image_1, "No expected image found:" + image_2
10731077

1074-
if image_1.RasterXSize != image_2.RasterXSize:
1075-
return 1000 # wrong size
1076-
if image_1.RasterYSize != image_2.RasterYSize:
1078+
if image_1.RasterXSize != image_2.RasterXSize or image_1.RasterYSize != image_2.RasterYSize:
1079+
image_1 = None
1080+
image_2 = None
10771081
return 1000 # wrong size
10781082

10791083
square_sum = 0
10801084
for x in range(image_1.RasterXSize):
10811085
for y in range(image_1.RasterYSize):
10821086
square_sum += (image_1.ReadAsArray()[x][y] - image_2.ReadAsArray()[x][y]) ** 2
10831087

1088+
# Explicitly close GDAL datasets
1089+
image_1 = None
1090+
image_2 = None
10841091
return sqrt(square_sum)
10851092

10861093
def _test_colors(self, colors):

0 commit comments

Comments
 (0)