summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Source/CTest/cmCTestTestHandler.cxx5
-rw-r--r--Source/cmTest.cxx6
-rw-r--r--Tests/CMakeLists.txt13
-rw-r--r--Tests/WorkingDirectory/CMakeLists.txt29
-rw-r--r--Tests/WorkingDirectory/main.cxx56
5 files changed, 108 insertions, 1 deletions
diff --git a/Source/CTest/cmCTestTestHandler.cxx b/Source/CTest/cmCTestTestHandler.cxx
index 6dd348d..b8e38fb 100644
--- a/Source/CTest/cmCTestTestHandler.cxx
+++ b/Source/CTest/cmCTestTestHandler.cxx
@@ -2190,7 +2190,6 @@ bool cmCTestTestHandler::SetTestsProperties(
{
rtit->Labels.push_back(*crit);
}
-
}
if ( key == "MEASUREMENT" )
{
@@ -2219,6 +2218,10 @@ bool cmCTestTestHandler::SetTestsProperties(
std::string(crit->c_str())));
}
}
+ if ( key == "WORKING_DIRECTORY" )
+ {
+ rtit->Directory = val;
+ }
}
}
}
diff --git a/Source/cmTest.cxx b/Source/cmTest.cxx
index 4e9b973..c25a8b6 100644
--- a/Source/cmTest.cxx
+++ b/Source/cmTest.cxx
@@ -196,4 +196,10 @@ void cmTest::DefineProperties(cmake *cm)
"If set to true, this will invert the pass/fail flag of the test.",
"This property can be used for tests that are expected to fail and "
"return a non zero return code.");
+
+ cm->DefineProperty
+ ("WORKING_DIRECTORY", cmProperty::TEST,
+ "The directory from which the test executable will be called.",
+ "If this is not set it is called from the directory the test executable "
+ "is located in.");
}
diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt
index 04f0774..8c89be5 100644
--- a/Tests/CMakeLists.txt
+++ b/Tests/CMakeLists.txt
@@ -1084,6 +1084,19 @@ ${CMake_BINARY_DIR}/bin/cmake -DVERSION=master -P ${CMake_SOURCE_DIR}/Utilities/
LIST(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/BundleGeneratorTest")
ENDIF(APPLE AND CTEST_TEST_CPACK)
+ ADD_TEST(WorkingDirectory ${CMAKE_CTEST_COMMAND}
+ --build-and-test
+ "${CMake_SOURCE_DIR}/Tests/WorkingDirectory"
+ "${CMake_BINARY_DIR}/Tests/WorkingDirectory"
+ --build-generator ${CMAKE_TEST_GENERATOR}
+ --build-project WorkingDirectoryProj
+ --build-makeprogram ${CMAKE_TEST_MAKEPROGRAM}
+ --build-exe-dir "${CMake_BINARY_DIR}/Tests/WorkingDirectory"
+ --force-new-ctest-process
+ --test-command ${CMAKE_CTEST_COMMAND} -V
+ )
+ LIST(APPEND TEST_BUILD_DIRS "${CMake_BINARY_DIR}/Tests/WorkingDirectory")
+
# Make sure CTest can handle a test with no newline in output.
ADD_TEST(CTest.NoNewline
${CMAKE_CMAKE_COMMAND} -E echo_append "This line has no newline!")
diff --git a/Tests/WorkingDirectory/CMakeLists.txt b/Tests/WorkingDirectory/CMakeLists.txt
new file mode 100644
index 0000000..5fbcd2a
--- /dev/null
+++ b/Tests/WorkingDirectory/CMakeLists.txt
@@ -0,0 +1,29 @@
+cmake_minimum_required(VERSION 2.6)
+project(WorkingDirectoryProj)
+
+add_executable(WorkingDirectory main.cxx)
+
+enable_testing()
+
+add_test(NAME WorkingDirectory1 COMMAND WorkingDirectory)
+add_test(NAME WorkingDirectory2 COMMAND WorkingDirectory)
+add_test(WorkingDirectory3 WorkingDirectory)
+
+set_tests_properties(WorkingDirectory1 PROPERTIES
+ WORKING_DIRECTORY "${CMAKE_BINARY_DIR}"
+ PASS_REGULAR_EXPRESSION "Working directory: ${CMAKE_BINARY_DIR}"
+)
+
+string(REGEX REPLACE "/[^/]*$" "" _parent_dir "${CMAKE_BINARY_DIR}")
+
+set_tests_properties(WorkingDirectory2 PROPERTIES
+ WORKING_DIRECTORY "${CMAKE_BINARY_DIR}/.."
+ PASS_REGULAR_EXPRESSION "Working directory: ${_parent_dir}"
+)
+
+string(REGEX REPLACE "/[^/]*$" "" _wd_exe "${CMAKE_BINARY_DIR}")
+get_filename_component(_default_cwd "${_wd_exe}" ABSOLUTE)
+
+set_tests_properties(WorkingDirectory3 PROPERTIES
+ PASS_REGULAR_EXPRESSION "Working directory: ${_default_cwd}"
+)
diff --git a/Tests/WorkingDirectory/main.cxx b/Tests/WorkingDirectory/main.cxx
new file mode 100644
index 0000000..6636da0
--- /dev/null
+++ b/Tests/WorkingDirectory/main.cxx
@@ -0,0 +1,56 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__))
+
+#include <io.h>
+#include <direct.h>
+
+#if defined(__WATCOMC__)
+#include <direct.h>
+#define _getcwd getcwd
+#endif
+
+inline const char* Getcwd(char* buf, unsigned int len)
+{
+ const char* ret = _getcwd(buf, len);
+ if(!ret)
+ {
+ fprintf(stderr, "No current working directory.\n");
+ abort();
+ }
+ // make sure the drive letter is capital
+ if(strlen(buf) > 1 && buf[1] == ':')
+ {
+ buf[0] = toupper(buf[0]);
+ }
+ return ret;
+}
+
+#else
+#include <sys/types.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+inline const char* Getcwd(char* buf, unsigned int len)
+{
+ const char* ret = getcwd(buf, len);
+ if(!ret)
+ {
+ fprintf(stderr, "No current working directory\n");
+ abort();
+ }
+ return ret;
+}
+
+#endif
+
+int main(int argc, char *argv[])
+{
+ char buf[2048];
+ const char *cwd = Getcwd(buf, sizeof(buf));
+
+ fprintf(stdout, "Working directory: %s\n", cwd);
+
+ return 0;
+}