Skip to content

Commit

Permalink
Ensure calling isSupportedOutputValue doesn't add layer to project
Browse files Browse the repository at this point in the history
This is an advance test only, and shouldn't result in layers
attempting to add to the project.

Refs qgis#57698
  • Loading branch information
nyalldawson committed Jun 7, 2024
1 parent e6baabe commit dcf88dd
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/Datasources2Vrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def supportedOutputRasterLayerExtensions(self):
return ['vrt']

def isSupportedOutputValue(self, value, context):
output_path = QgsProcessingParameters.parameterAsOutputLayer(self, value, context)
output_path = QgsProcessingParameters.parameterAsOutputLayer(self, value, context, testOnly=True)
if pathlib.Path(output_path).suffix.lower() != '.vrt':
return False, QCoreApplication.translate("GdalAlgorithm", 'Output filename must use a .vrt extension')
return True, ''
Expand Down
2 changes: 1 addition & 1 deletion python/plugins/processing/algs/gdal/buildvrt.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def parameterAsOutputLayer(self, definition, value, context):
return super(QgsProcessingParameterRasterDestination, self).parameterAsOutputLayer(definition, value, context)

def isSupportedOutputValue(self, value, context):
output_path = QgsProcessingParameters.parameterAsOutputLayer(self, value, context)
output_path = QgsProcessingParameters.parameterAsOutputLayer(self, value, context, testOnly=True)
if pathlib.Path(output_path).suffix.lower() != '.vrt':
return False, QCoreApplication.translate("GdalAlgorithm", 'Output filename must use a .vrt extension')
return True, ''
Expand Down
2 changes: 1 addition & 1 deletion src/core/processing/qgsprocessingprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ QStringList QgsProcessingProvider::supportedOutputTableExtensions() const
bool QgsProcessingProvider::isSupportedOutputValue( const QVariant &outputValue, const QgsProcessingDestinationParameter *parameter, QgsProcessingContext &context, QString &error ) const
{
error.clear();
QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context ).trimmed();
QString outputPath = QgsProcessingParameters::parameterAsOutputLayer( parameter, outputValue, context, true ).trimmed();

if ( outputPath.isEmpty() )
{
Expand Down
6 changes: 4 additions & 2 deletions tests/src/analysis/testqgsprocessing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8122,19 +8122,21 @@ void TestQgsProcessing::parameterVectorOut()
def.reset( new QgsProcessingParameterVectorDestination( "with_geom", QString(), Qgis::ProcessingSourceType::VectorAnyGeometry, QString(), true ) );
DummyProvider3 provider;
QString error;
context.setLayersToLoadOnCompletion( {} );
QVERIFY( provider.isSupportedOutputValue( QVariant(), def.get(), context, error ) ); // optional
QVERIFY( provider.isSupportedOutputValue( QString(), def.get(), context, error ) ); // optional
QVERIFY( !provider.isSupportedOutputValue( "d:/test.shp", def.get(), context, error ) );
QVERIFY( !provider.isSupportedOutputValue( "d:/test.SHP", def.get(), context, error ) );
QVERIFY( !provider.isSupportedOutputValue( "ogr:d:/test.shp", def.get(), context, error ) );
QVERIFY( !provider.isSupportedOutputValue( QgsProcessingOutputLayerDefinition( "d:/test.SHP" ), def.get(), context, error ) );
QVERIFY( !provider.isSupportedOutputValue( QgsProcessingOutputLayerDefinition( "d:/test.SHP", &p ), def.get(), context, error ) );
QVERIFY( provider.isSupportedOutputValue( "d:/test.mif", def.get(), context, error ) );
QVERIFY( provider.isSupportedOutputValue( "d:/test.MIF", def.get(), context, error ) );
QVERIFY( provider.isSupportedOutputValue( "ogr:d:/test.MIF", def.get(), context, error ) );
QVERIFY( provider.isSupportedOutputValue( QgsProcessingOutputLayerDefinition( "d:/test.MIF" ), def.get(), context, error ) );
QVERIFY( provider.isSupportedOutputValue( QgsProcessingOutputLayerDefinition( "d:/test.MIF", &p ), def.get(), context, error ) );
def.reset( new QgsProcessingParameterVectorDestination( "with_geom", QString(), Qgis::ProcessingSourceType::VectorAnyGeometry, QString(), false ) );
QVERIFY( !provider.isSupportedOutputValue( QVariant(), def.get(), context, error ) ); // non-optional
QVERIFY( !provider.isSupportedOutputValue( QString(), def.get(), context, error ) ); // non-optional
QVERIFY( context.layersToLoadOnCompletion().isEmpty() );

provider.loadAlgorithms();
def->mOriginalProvider = &provider;
Expand Down

0 comments on commit dcf88dd

Please sign in to comment.