|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +"""QGIS Unit tests for Processing algorithm runner(s). |
| 3 | +
|
| 4 | +.. note:: This program is free software; you can redistribute it and/or modify |
| 5 | +it under the terms of the GNU General Public License as published by |
| 6 | +the Free Software Foundation; either version 2 of the License, or |
| 7 | +(at your option) any later version. |
| 8 | +""" |
| 9 | +__author__ = 'Alessandro Pasotti' |
| 10 | +__date__ = '2019-02' |
| 11 | +__copyright__ = 'Copyright 2019, The QGIS Project' |
| 12 | +# This will get replaced with a git SHA1 when you do a git archive |
| 13 | +__revision__ = '$Format:%H$' |
| 14 | + |
| 15 | +import re |
| 16 | +from qgis.PyQt.QtCore import QCoreApplication |
| 17 | +from qgis.testing import start_app, unittest |
| 18 | +from qgis.core import QgsProcessingAlgRunnerTask |
| 19 | + |
| 20 | +from processing.core.Processing import Processing |
| 21 | +from processing.core.ProcessingConfig import ProcessingConfig |
| 22 | +from qgis.testing import start_app, unittest |
| 23 | +from qgis.analysis import QgsNativeAlgorithms |
| 24 | +from qgis.core import ( |
| 25 | + QgsApplication, |
| 26 | + QgsSettings, |
| 27 | + QgsProcessingContext, |
| 28 | + QgsProcessingAlgRunnerTask, |
| 29 | + QgsProcessingAlgorithm, |
| 30 | + QgsProject, |
| 31 | + QgsProcessingFeedback, |
| 32 | +) |
| 33 | + |
| 34 | +start_app() |
| 35 | + |
| 36 | + |
| 37 | +class ConsoleFeedBack(QgsProcessingFeedback): |
| 38 | + |
| 39 | + _error = '' |
| 40 | + |
| 41 | + def reportError(self, error, fatalError=False): |
| 42 | + self._error = error |
| 43 | + print(error) |
| 44 | + |
| 45 | + |
| 46 | +class CrashingProcessingAlgorithm(QgsProcessingAlgorithm): |
| 47 | + """ |
| 48 | + Wrong class in factory createInstance() |
| 49 | + """ |
| 50 | + |
| 51 | + INPUT = 'INPUT' |
| 52 | + OUTPUT = 'OUTPUT' |
| 53 | + |
| 54 | + def tr(self, string): |
| 55 | + return QCoreApplication.translate('Processing', string) |
| 56 | + |
| 57 | + def createInstance(self): |
| 58 | + """Wrong!""" |
| 59 | + return ExampleProcessingAlgorithm() |
| 60 | + |
| 61 | + def name(self): |
| 62 | + return 'mycrashingscript' |
| 63 | + |
| 64 | + def displayName(self): |
| 65 | + return self.tr('My Crashing Script') |
| 66 | + |
| 67 | + def group(self): |
| 68 | + return self.tr('Example scripts') |
| 69 | + |
| 70 | + def groupId(self): |
| 71 | + return 'examplescripts' |
| 72 | + |
| 73 | + def shortHelpString(self): |
| 74 | + return self.tr("Example algorithm short description") |
| 75 | + |
| 76 | + def initAlgorithm(self, config=None): |
| 77 | + pass |
| 78 | + |
| 79 | + def processAlgorithm(self, parameters, context, feedback): |
| 80 | + return {self.OUTPUT: 'an_id'} |
| 81 | + |
| 82 | + |
| 83 | +class TestQgsProcessingAlgRunner(unittest.TestCase): |
| 84 | + |
| 85 | + @classmethod |
| 86 | + def setUpClass(cls): |
| 87 | + """Run before all tests""" |
| 88 | + QCoreApplication.setOrganizationName("QGIS_Test") |
| 89 | + QCoreApplication.setOrganizationDomain( |
| 90 | + "QGIS_TestPyQgsProcessingInPlace.com") |
| 91 | + QCoreApplication.setApplicationName("QGIS_TestPyQgsProcessingInPlace") |
| 92 | + QgsSettings().clear() |
| 93 | + Processing.initialize() |
| 94 | + QgsApplication.processingRegistry().addProvider(QgsNativeAlgorithms()) |
| 95 | + cls.registry = QgsApplication.instance().processingRegistry() |
| 96 | + |
| 97 | + def test_bad_script_dont_crash(self): # spellok |
| 98 | + """Test regression #21270 (segfault)""" |
| 99 | + |
| 100 | + context = QgsProcessingContext() |
| 101 | + context.setProject(QgsProject.instance()) |
| 102 | + feedback = ConsoleFeedBack() |
| 103 | + |
| 104 | + task = QgsProcessingAlgRunnerTask(CrashingProcessingAlgorithm(), {}, context=context, feedback=feedback) |
| 105 | + self.assertTrue(task.isCanceled()) |
| 106 | + self.assertIn('name \'ExampleProcessingAlgorithm\' is not defined', feedback._error) |
| 107 | + |
| 108 | + |
| 109 | +if __name__ == '__main__': |
| 110 | + unittest.main() |
0 commit comments