Add the WORKING_DIRECTORY property to tests

This commit is contained in:
Rolf Eike Beer 2010-10-26 12:06:15 +02:00 committed by Ben Boeckel
parent 72ebd4ee12
commit d95f817f77
5 changed files with 108 additions and 1 deletions

View File

@ -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;
}
}
}
}

View File

@ -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.");
}

View File

@ -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!")

View File

@ -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}"
)

View File

@ -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;
}