Explorar el Código

ctkCmdLineModuleDefaultPathBuilder now handles multiple diretories in CTK_MODULE_LOAD_PATH

MattClarkson hace 12 años
padre
commit
94ddf92da1

+ 37 - 16
Libs/CommandLineModules/Core/Testing/Cpp/ctkCmdLineModuleDefaultPathBuilderTest.cpp

@@ -101,38 +101,59 @@ int ctkCmdLineModuleDefaultPathBuilderTest(int argc, char* argv[])
     return EXIT_FAILURE;
   }
 
+  QString tmp1 = result[0];
+  QString tmp2 = result[1];
+  QString expected = tmp1 + QDir::separator() + "cli-modules";
+
+  if (tmp2 != expected)
+  {
+    qDebug() << "Expected " << expected << ", but got " << tmp2;
+    return EXIT_FAILURE;
+  }
+
   builder.clear();
 
-  builder.addCtkModuleLoadPathDir();
+  builder.addCtkModuleLoadPath();
   result = builder.getDirectoryList();
 
   qDebug() << "5. Built:" << result;
 
-  // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid directory.
-  // If it does not exist, then the list should not change.
+  // If the environment variable CTK_MODULE_LOAD_PATH exists, it should point to a valid list of directories.
   QProcessEnvironment env = QProcessEnvironment::systemEnvironment();
   qDebug() << "Environment is:" << env.toStringList();
 
+#ifdef Q_OS_WIN32
+    QString pathSeparator(";");
+#else
+    QString pathSeparator(":");
+#endif
+
   if (env.contains("CTK_MODULE_LOAD_PATH"))
   {
-    QDir loadDir(env.value("CTK_MODULE_LOAD_PATH"));
+    QString loadPath = env.value("CTK_MODULE_LOAD_PATH");
+    QStringList loadPaths = loadPath.split(pathSeparator, QString::SkipEmptyParts);
 
-    qDebug() << "CTK_MODULE_LOAD_PATH does exist, and is set to:" << env.value("CTK_MODULE_LOAD_PATH") << ", and isExists() returns " << loadDir.exists();
-
-    if (loadDir.exists() && result.size() != 1)
+    foreach (QString path, loadPaths)
     {
-      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist and is valid, so there should be 1 entries";
-      return EXIT_FAILURE;
-    }
-    else if (!loadDir.exists() && result.size() != 0)
-    {
-      qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did exist but is invalid, so there should be 0 entries";
-      return EXIT_FAILURE;
+      if (!loadPaths.contains(path))
+      {
+        qDebug() << "Expecte loadPaths to contain path=" << path;
+        return EXIT_FAILURE;
+      }
     }
   }
-  else if (result.size() != 0)
+
+  builder.clear();
+
+  builder.addApplicationDir("blah-blah-blah");
+  builder.setStrictMode(true);
+  result = builder.getDirectoryList();
+
+  qDebug() << "6. Built:" << result;
+
+  if (result.size() != 0)
   {
-    qDebug() << "Environment variable CTK_MODULE_LOAD_PATH did not exist, so there should still be 0 entries.";
+    qDebug() << "Calling addApplicationDir(blah-blah-blah) should return nothing unless we do have a sub-folder called blah-blah-blah.";
     return EXIT_FAILURE;
   }
 

+ 21 - 9
Libs/CommandLineModules/Core/ctkCmdLineModuleDefaultPathBuilder.cpp

@@ -29,6 +29,7 @@
 struct ctkCmdLineModuleDefaultPathBuilderPrivate
 {
 public:
+
   ctkCmdLineModuleDefaultPathBuilderPrivate();
   ~ctkCmdLineModuleDefaultPathBuilderPrivate();
   void clear();
@@ -37,7 +38,7 @@ public:
   void addHomeDir(const QString& subFolder = QString());
   void addCurrentDir(const QString& subFolder = QString());
   void addApplicationDir(const QString& subFolder = QString());
-  void addCtkModuleLoadPathDir(const QString& subFolder = QString());
+  void addCtkModuleLoadPath();
   QStringList getDirectoryList() const;
 
   bool isStrictMode;
@@ -131,15 +132,27 @@ void ctkCmdLineModuleDefaultPathBuilderPrivate::addApplicationDir(const QString&
 
 
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilderPrivate::addCtkModuleLoadPathDir(
-    const QString& subFolder)
+void ctkCmdLineModuleDefaultPathBuilderPrivate::addCtkModuleLoadPath()
 {
   char *ctkModuleLoadPath = getenv("CTK_MODULE_LOAD_PATH");
   if (ctkModuleLoadPath != NULL)
   {
-    QDir dir = QDir(QString(ctkModuleLoadPath));
-    QString result = addSubFolder(dir.canonicalPath(), subFolder);
-    directoryList << result;
+    // The load path may in fact be a semi-colon or colon separated list of directories, not just one.
+    QString paths(ctkModuleLoadPath);
+
+#ifdef Q_OS_WIN32
+    QString pathSeparator(";");
+#else
+    QString pathSeparator(":");
+#endif
+
+    QStringList splitPath = paths.split(pathSeparator, QString::SkipEmptyParts);
+
+    foreach (QString path, splitPath)
+    {
+      QDir dir = QDir(path);
+      directoryList << dir.absolutePath();
+    }
   }
 }
 
@@ -228,10 +241,9 @@ void ctkCmdLineModuleDefaultPathBuilder::addApplicationDir(const QString& subFol
 
 
 //-----------------------------------------------------------------------------
-void ctkCmdLineModuleDefaultPathBuilder::addCtkModuleLoadPathDir(
-    const QString& subFolder)
+void ctkCmdLineModuleDefaultPathBuilder::addCtkModuleLoadPath()
 {
-  d->addCtkModuleLoadPathDir(subFolder);
+  d->addCtkModuleLoadPath();
 }
 
 

+ 8 - 5
Libs/CommandLineModules/Core/ctkCmdLineModuleDefaultPathBuilder.h

@@ -35,11 +35,13 @@ struct ctkCmdLineModuleDefaultPathBuilderPrivate;
  *
  * Simple class to enable the user to easily add various directories
  * to a list of directories. You create this object, add a load of directories
- * and then call getDirectoryList() to get a StringList of directory locations.
+ * by repeatedly calling add..() functions, and then call getDirectoryList()
+ * to get the final StringList of directory locations.
  *
  * The choices are:
  * <pre>
- * 1. The directory defined by the CTK_MODULE_LOAD_PATH environment variable or any sub-directory under this.
+ * 1. The directory or list of directories defined by the CTK_MODULE_LOAD_PATH environment variable.
+ * Uses usual PATH semantics such as colon separator on *nix systems and semi-colon on Windows.
  * 2. The directory defined by the users HOME directory, or any sub-directory under this.
  * 3. The directory defined by the current working directory, or any sub-directory under this.
  * 4. The directory defined by the application installation directory or any sub-directory under this.
@@ -94,10 +96,11 @@ public:
   virtual void addApplicationDir(const QString& subFolder = QString());
 
   /**
-   * @brief Adds the directory denoted by the environment variable CTK_MODULE_LOAD_PATH,
-   * or if specified a sub-directory.
+   * @brief Adds the directories denoted by the environment variable CTK_MODULE_LOAD_PATH.
+   *
+   * Semi-colon separated lists of directories are allowed.
    */
-  virtual void addCtkModuleLoadPathDir(const QString& subFolder = QString());
+  virtual void addCtkModuleLoadPath();
 
   /**
    * @brief Returns the QStringList containing directories.