Skip to content
Permalink
Browse files

Avoid temporary destination filename has "." inside. Fixes #36353

  • Loading branch information
github-actions authored and nyalldawson committed May 12, 2020
1 parent c6f1add commit b3d168ffbbdbc33b4dd8a9744601282ab3f21d1b
Showing with 53 additions and 2 deletions.
  1. +8 −2 src/core/processing/qgsprocessingparameters.cpp
  2. +45 −0 tests/src/analysis/testqgsprocessing.cpp
@@ -5016,13 +5016,19 @@ QString QgsProcessingDestinationParameter::asPythonString( const QgsProcessing::

QString QgsProcessingDestinationParameter::generateTemporaryDestination() const
{
// sanitize name to avoid multiple . in the filename. E.g. when name() contain
// backend command name having a "." inside as in case of grass commands
QRegularExpression rx( QStringLiteral( "[.]" ) );
QString sanitizedName = name();
sanitizedName.replace( rx, QStringLiteral( "_" ) );

if ( defaultFileExtension().isEmpty() )
{
return QgsProcessingUtils::generateTempFilename( name() );
return QgsProcessingUtils::generateTempFilename( sanitizedName );
}
else
{
return QgsProcessingUtils::generateTempFilename( name() + '.' + defaultFileExtension() );
return QgsProcessingUtils::generateTempFilename( sanitizedName + '.' + defaultFileExtension() );
}
}

@@ -26,6 +26,7 @@
#include <QObject>
#include <QtTest/QSignalSpy>
#include <QList>
#include <QFileInfo>
#include "qgis.h"
#include "qgstest.h"
#include "qgsrasterlayer.h"
@@ -527,6 +528,7 @@ class TestQgsProcessing: public QObject
void features();
void uniqueValues();
void createIndex();
void generateTemporaryDestination();
void parseDestinationString();
void createFeatureSink();
void parameters();
@@ -1520,6 +1522,49 @@ void TestQgsProcessing::createIndex()
QCOMPARE( ids, QList<QgsFeatureId>() << 2 );
}

void TestQgsProcessing::generateTemporaryDestination()
{
// setup a context
QgsProject p;
p.setCrs( QgsCoordinateReferenceSystem::fromEpsgId( 28353 ) );
QgsProcessingContext context;
context.setProject( &p );

// destination vector with "." in it's name
std::unique_ptr< QgsProcessingParameterVectorDestination > def( new QgsProcessingParameterVectorDestination( "with.inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "gpkg"
QFileInfo f = QFileInfo( def->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );

// destination raster with "." in it's name
std::unique_ptr< QgsProcessingParameterRasterDestination > def2( new QgsProcessingParameterRasterDestination( "with.inside", QString(), QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def2->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def2->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "tif"
f = QFileInfo( def2->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "tif" ) );

// destination vector without "." in it's name
std::unique_ptr< QgsProcessingParameterVectorDestination > def3( new QgsProcessingParameterVectorDestination( "without_inside", QString(), QgsProcessing::TypeVectorAnyGeometry, QString(), false ) );

// check that temporary destination does not have dot at the end when there is no extension
QVERIFY( !def3->generateTemporaryDestination().endsWith( QLatin1String( "." ) ) );
// check that temporary destination starts with tempFolder
QVERIFY( def3->generateTemporaryDestination().startsWith( QgsProcessingUtils::tempFolder() ) );
// check that extension with QFileInfo::completeSuffix is "gpkg"
f = QFileInfo( def3->generateTemporaryDestination() );
QCOMPARE( f.completeSuffix(), QString( "gpkg" ) );

}

void TestQgsProcessing::parseDestinationString()
{
QString providerKey;

0 comments on commit b3d168f

Please sign in to comment.
You can’t perform that action at this time.