From 415324769d810ade8de481429a1ded510425678e Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Sat, 22 Apr 2017 18:07:47 -0400 Subject: [PATCH 1/7] Make factory samples relative Fixes #3491 Related #1719 --- src/core/SampleBuffer.cpp | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index f5ec42cdbac..0c66f706653 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -34,6 +34,9 @@ #include +// FIXME TODO +#include + #define OV_EXCLUDE_STATIC_CALLBACKS #ifdef LMMS_HAVE_OGGVORBIS #include @@ -1411,25 +1414,33 @@ void SampleBuffer::setReversed( bool _on ) -QString SampleBuffer::tryToMakeRelative( const QString & _file ) +QString SampleBuffer::tryToMakeRelative( const QString & file ) { - if( QFileInfo( _file ).isRelative() == false ) + if( QFileInfo( file ).isRelative() == false ) { - QString f = QString( _file ).replace( QDir::separator(), '/' ); - QString fsd = ConfigManager::inst()->factorySamplesDir(); + QString f = QString( file ).replace( QDir::separator(), '/' ); + + // First, look in factory samples + // Isolate "samples/" from "data:/samples/" + QString samplesSuffix = ConfigManager::inst()->factorySamplesDir().mid( ConfigManager::inst()->dataDir().length() ); + + // Iterate over all valid "data:/" searchPaths + foreach ( const QString& path, QDir::searchPaths( "data" ) ) { + QString samplesPath = QString( path + samplesSuffix ).replace( QDir::separator(), '/' ); + if ( f.startsWith( samplesPath ) ) { + return QString( f ).mid( samplesPath.length() ); + } + } + + // Next, look in user samples QString usd = ConfigManager::inst()->userSamplesDir(); - fsd.replace( QDir::separator(), '/' ); usd.replace( QDir::separator(), '/' ); - if( f.startsWith( fsd ) ) - { - return QString( f ).mid( fsd.length() ); - } - else if( f.startsWith( usd ) ) + if( f.startsWith( usd ) ) { return QString( f ).mid( usd.length() ); } } - return _file; + return file; } From 0c292f01b334d11e9a3e811618795e997c1e1ad4 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Sat, 22 Apr 2017 18:11:48 -0400 Subject: [PATCH 2/7] Remove debug --- src/core/SampleBuffer.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index 0c66f706653..fcd9974fb7c 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -34,9 +34,6 @@ #include -// FIXME TODO -#include - #define OV_EXCLUDE_STATIC_CALLBACKS #ifdef LMMS_HAVE_OGGVORBIS #include From b4250be3f1ef557704e60b54eb1c89cea7303eb1 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Sat, 22 Apr 2017 18:17:08 -0400 Subject: [PATCH 3/7] Consistent formatting --- src/core/SampleBuffer.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index fcd9974fb7c..9b799f8df04 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -1422,9 +1422,11 @@ QString SampleBuffer::tryToMakeRelative( const QString & file ) QString samplesSuffix = ConfigManager::inst()->factorySamplesDir().mid( ConfigManager::inst()->dataDir().length() ); // Iterate over all valid "data:/" searchPaths - foreach ( const QString& path, QDir::searchPaths( "data" ) ) { + foreach ( const QString& path, QDir::searchPaths( "data" ) ) + { QString samplesPath = QString( path + samplesSuffix ).replace( QDir::separator(), '/' ); - if ( f.startsWith( samplesPath ) ) { + if ( f.startsWith( samplesPath ) ) + { return QString( f ).mid( samplesPath.length() ); } } From 9e42e6b01d8998f4df811e3c7c75c354ae1426ce Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 2 May 2017 23:08:50 -0400 Subject: [PATCH 4/7] Remove deprecated loop, add unit test --- src/core/SampleBuffer.cpp | 2 +- tests/CMakeLists.txt | 1 + tests/src/core/RelativePathsTest.cpp | 42 ++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 tests/src/core/RelativePathsTest.cpp diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index 9b799f8df04..cb930b08789 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -1422,7 +1422,7 @@ QString SampleBuffer::tryToMakeRelative( const QString & file ) QString samplesSuffix = ConfigManager::inst()->factorySamplesDir().mid( ConfigManager::inst()->dataDir().length() ); // Iterate over all valid "data:/" searchPaths - foreach ( const QString& path, QDir::searchPaths( "data" ) ) + for ( const QString & path : QDir::searchPaths( "data" ) ) { QString samplesPath = QString( path + samplesSuffix ).replace( QDir::separator(), '/' ); if ( f.startsWith( samplesPath ) ) diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2367ea4bbc2..8cbe23858bf 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -19,6 +19,7 @@ ADD_EXECUTABLE(tests $ src/core/ProjectVersionTest.cpp + src/core/RelativePathsTest.cpp src/tracks/AutomationTrackTest.cpp ) diff --git a/tests/src/core/RelativePathsTest.cpp b/tests/src/core/RelativePathsTest.cpp new file mode 100644 index 00000000000..538778ba6b6 --- /dev/null +++ b/tests/src/core/RelativePathsTest.cpp @@ -0,0 +1,42 @@ +/* + * ProjectVersionTest.cpp + * + * Copyright (c) 2015 Lukas W + * + * This file is part of LMMS - https://lmms.io + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this program (see COPYING); if not, write to the + * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, + * Boston, MA 02110-1301 USA. + * + */ + +#include "QTestSuite.h" + +#include "SampleBuffer.h" + +#include + +class RelativePathsTest : QTestSuite +{ + Q_OBJECT +private slots: + void RelativePathComparisonTests() + { + QVERIFY(SampleBuffer::tryToMakeRelative(QDir::currentPath() + "/data/samples/drums/kick01.ogg") == "drums/kick01.ogg"); + QVERIFY(SampleBuffer::tryToMakeAbsolute("drums/kick01.ogg") == QDir::currentPath() + "/data/samples/drums/kick01.ogg"); + } +} RelativePathTests; + +#include "RelativePathsTest.moc" From e15a6212fa1765b75b61e85fd2bb43655869cbdc Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Tue, 2 May 2017 23:11:23 -0400 Subject: [PATCH 5/7] Change copyright --- tests/src/core/RelativePathsTest.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/src/core/RelativePathsTest.cpp b/tests/src/core/RelativePathsTest.cpp index 538778ba6b6..2ae8c1af707 100644 --- a/tests/src/core/RelativePathsTest.cpp +++ b/tests/src/core/RelativePathsTest.cpp @@ -1,7 +1,7 @@ /* - * ProjectVersionTest.cpp + * RelativePathsTest.cpp * - * Copyright (c) 2015 Lukas W + * Copyright (c) 2017 Tres Finocchiaro * * This file is part of LMMS - https://lmms.io * From ccd369bf9f247e8e17a692dca626db952a4a7674 Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Wed, 3 May 2017 00:23:42 -0400 Subject: [PATCH 6/7] Fix import --- tests/src/core/RelativePathsTest.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/src/core/RelativePathsTest.cpp b/tests/src/core/RelativePathsTest.cpp index 2ae8c1af707..544a02d7acc 100644 --- a/tests/src/core/RelativePathsTest.cpp +++ b/tests/src/core/RelativePathsTest.cpp @@ -26,7 +26,7 @@ #include "SampleBuffer.h" -#include +#include class RelativePathsTest : QTestSuite { From 9564d8176337cd31d2edb1c6070608065574cdbe Mon Sep 17 00:00:00 2001 From: Tres Finocchiaro Date: Fri, 5 May 2017 20:59:12 -0400 Subject: [PATCH 7/7] Unit test improvements --- src/core/ConfigManager.cpp | 7 ++++++- tests/src/core/RelativePathsTest.cpp | 10 ++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/src/core/ConfigManager.cpp b/src/core/ConfigManager.cpp index 5cdfe3d97a4..13c6d9a85b3 100644 --- a/src/core/ConfigManager.cpp +++ b/src/core/ConfigManager.cpp @@ -63,7 +63,12 @@ ConfigManager::ConfigManager() : // If we're in development (lmms is not installed) let's get the source and // binary directories by reading the CMake Cache - QFile cmakeCache(qApp->applicationDirPath() + "/CMakeCache.txt"); + QDir appPath = qApp->applicationDirPath(); + // If in tests, get parent directory + if (appPath.dirName() == "tests") { + appPath.cdUp(); + } + QFile cmakeCache(appPath.absoluteFilePath("CMakeCache.txt")); if (cmakeCache.exists()) { cmakeCache.open(QFile::ReadOnly); QTextStream stream(&cmakeCache); diff --git a/tests/src/core/RelativePathsTest.cpp b/tests/src/core/RelativePathsTest.cpp index 544a02d7acc..555fa39b56a 100644 --- a/tests/src/core/RelativePathsTest.cpp +++ b/tests/src/core/RelativePathsTest.cpp @@ -24,6 +24,7 @@ #include "QTestSuite.h" +#include "ConfigManager.h" #include "SampleBuffer.h" #include @@ -34,8 +35,13 @@ class RelativePathsTest : QTestSuite private slots: void RelativePathComparisonTests() { - QVERIFY(SampleBuffer::tryToMakeRelative(QDir::currentPath() + "/data/samples/drums/kick01.ogg") == "drums/kick01.ogg"); - QVERIFY(SampleBuffer::tryToMakeAbsolute("drums/kick01.ogg") == QDir::currentPath() + "/data/samples/drums/kick01.ogg"); + QFileInfo fi(ConfigManager::inst()->factorySamplesDir() + "/drums/kick01.ogg"); + QVERIFY(fi.exists()); + + QString absPath = fi.absoluteFilePath(); + QString relPath = "drums/kick01.ogg"; + QCOMPARE(SampleBuffer::tryToMakeRelative(absPath), relPath); + QCOMPARE(SampleBuffer::tryToMakeAbsolute(relPath), absPath); } } RelativePathTests;