From ec1ba9b0ae27a5731b98fecab648fa3edcfcffa2 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 16 Apr 2013 09:27:51 -0400 Subject: [PATCH 1/2] get_filename_component: Add explicit unit tests Add test RunCMake.get_filename_component to cover cases of the command. Remove redundant coverage of these cases from the "complex" tests. --- Tests/Complex/Executable/complex.cxx | 80 ------------------- Tests/Complex/VarTests.cmake | 14 ---- Tests/Complex/cmTestConfigure.h.in | 9 --- Tests/ComplexOneConfig/Executable/complex.cxx | 80 ------------------- Tests/ComplexOneConfig/VarTests.cmake | 14 ---- Tests/ComplexOneConfig/cmTestConfigure.h.in | 9 --- Tests/RunCMake/CMakeLists.txt | 1 + .../get_filename_component/CMakeLists.txt | 3 + .../KnownComponents.cmake | 32 ++++++++ .../get_filename_component/RunCMakeTest.cmake | 4 + .../UnknownComponent-result.txt | 1 + .../UnknownComponent-stderr.txt | 4 + .../UnknownComponent.cmake | 1 + 13 files changed, 46 insertions(+), 206 deletions(-) create mode 100644 Tests/RunCMake/get_filename_component/CMakeLists.txt create mode 100644 Tests/RunCMake/get_filename_component/KnownComponents.cmake create mode 100644 Tests/RunCMake/get_filename_component/RunCMakeTest.cmake create mode 100644 Tests/RunCMake/get_filename_component/UnknownComponent-result.txt create mode 100644 Tests/RunCMake/get_filename_component/UnknownComponent-stderr.txt create mode 100644 Tests/RunCMake/get_filename_component/UnknownComponent.cmake diff --git a/Tests/Complex/Executable/complex.cxx b/Tests/Complex/Executable/complex.cxx index 27d70adc81..e904f2897a 100644 --- a/Tests/Complex/Executable/complex.cxx +++ b/Tests/Complex/Executable/complex.cxx @@ -634,86 +634,6 @@ int main() cmPassed("WHILE command is working"); } - // ---------------------------------------------------------------------- - // Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations - -#ifndef FILENAME_VAR_PATH_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_PATH_NAME is not defined."); -#else - if((strcmp(FILENAME_VAR_PATH_NAME, "Complex") == 0) || - (strcmp(FILENAME_VAR_PATH_NAME, "ComplexOneConfig") == 0)) - { - cmPassed("FILENAME_VAR_PATH_NAME == ", FILENAME_VAR_PATH_NAME); - } - else - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_PATH_NAME == ", FILENAME_VAR_PATH_NAME); - } -#endif - -#ifndef FILENAME_VAR_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME is not defined."); -#else - if(strcmp(FILENAME_VAR_NAME, "VarTests.cmake") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME == ", FILENAME_VAR_NAME); - } - else - { - cmPassed("FILENAME_VAR_NAME == ", FILENAME_VAR_NAME); - } -#endif - -#ifndef FILENAME_VAR_EXT - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_EXT is not defined."); -#else - if(strcmp(FILENAME_VAR_EXT, ".cmake") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_EXT == ", FILENAME_VAR_EXT); - } - else - { - cmPassed("FILENAME_VAR_EXT == ", FILENAME_VAR_EXT); - } -#endif - -#ifndef FILENAME_VAR_NAME_WE - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME_WE is not defined."); -#else - if(strcmp(FILENAME_VAR_NAME_WE, "VarTests") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME_WE == ", FILENAME_VAR_NAME_WE); - } - else - { - cmPassed("FILENAME_VAR_NAME_WE == ", FILENAME_VAR_NAME_WE); - } -#endif - -#ifndef PATH_VAR_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "PATH_VAR_NAME is not defined."); -#else - if((strcmp(PATH_VAR_NAME, "Complex") == 0) || - (strcmp(PATH_VAR_NAME, "ComplexOneConfig") == 0)) - { - cmPassed("PATH_VAR_NAME == ", PATH_VAR_NAME); - } - else - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "PATH_VAR_NAME == ", PATH_VAR_NAME); - } -#endif - // ---------------------------------------------------------------------- // Test LOAD_CACHE diff --git a/Tests/Complex/VarTests.cmake b/Tests/Complex/VarTests.cmake index 52ec1fc8e1..70b803a1e5 100644 --- a/Tests/Complex/VarTests.cmake +++ b/Tests/Complex/VarTests.cmake @@ -182,17 +182,3 @@ set(FOREACH_CONCAT "") foreach (INDEX a;b;c;d;e;f;g) set(FOREACH_CONCAT "${FOREACH_CONCAT}${INDEX}") endforeach() - -# -# Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations -# -find_file(FILENAME_VAR "VarTests.cmake" ${Complex_SOURCE_DIR}) - -get_filename_component(FILENAME_VAR_PATH ${FILENAME_VAR} PATH) -get_filename_component(FILENAME_VAR_PATH_NAME ${FILENAME_VAR_PATH} NAME) -get_filename_component(FILENAME_VAR_NAME ${FILENAME_VAR} NAME) -get_filename_component(FILENAME_VAR_EXT ${FILENAME_VAR} EXT) -get_filename_component(FILENAME_VAR_NAME_WE ${FILENAME_VAR} NAME_WE CACHE) - -find_path(PATH_VAR "cmTestConfigure.h.in" ${Complex_SOURCE_DIR}) -get_filename_component(PATH_VAR_NAME ${PATH_VAR} NAME) diff --git a/Tests/Complex/cmTestConfigure.h.in b/Tests/Complex/cmTestConfigure.h.in index 7741b6fd58..d7424b1845 100644 --- a/Tests/Complex/cmTestConfigure.h.in +++ b/Tests/Complex/cmTestConfigure.h.in @@ -15,15 +15,6 @@ // Test WHILE #define WHILE_VALUE ${while_var} -// Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations - -#define FILENAME_VAR_PATH_NAME "${FILENAME_VAR_PATH_NAME}" -#define FILENAME_VAR_NAME "${FILENAME_VAR_NAME}" -#define FILENAME_VAR_EXT "${FILENAME_VAR_EXT}" -#define FILENAME_VAR_NAME_WE "${FILENAME_VAR_NAME_WE}" - -#define PATH_VAR_NAME "${PATH_VAR_NAME}" - // Test LOAD_CACHE #define CACHE_TEST_VAR1 "${CACHE_TEST_VAR1}" diff --git a/Tests/ComplexOneConfig/Executable/complex.cxx b/Tests/ComplexOneConfig/Executable/complex.cxx index 27d70adc81..e904f2897a 100644 --- a/Tests/ComplexOneConfig/Executable/complex.cxx +++ b/Tests/ComplexOneConfig/Executable/complex.cxx @@ -634,86 +634,6 @@ int main() cmPassed("WHILE command is working"); } - // ---------------------------------------------------------------------- - // Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations - -#ifndef FILENAME_VAR_PATH_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_PATH_NAME is not defined."); -#else - if((strcmp(FILENAME_VAR_PATH_NAME, "Complex") == 0) || - (strcmp(FILENAME_VAR_PATH_NAME, "ComplexOneConfig") == 0)) - { - cmPassed("FILENAME_VAR_PATH_NAME == ", FILENAME_VAR_PATH_NAME); - } - else - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_PATH_NAME == ", FILENAME_VAR_PATH_NAME); - } -#endif - -#ifndef FILENAME_VAR_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME is not defined."); -#else - if(strcmp(FILENAME_VAR_NAME, "VarTests.cmake") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME == ", FILENAME_VAR_NAME); - } - else - { - cmPassed("FILENAME_VAR_NAME == ", FILENAME_VAR_NAME); - } -#endif - -#ifndef FILENAME_VAR_EXT - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_EXT is not defined."); -#else - if(strcmp(FILENAME_VAR_EXT, ".cmake") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_EXT == ", FILENAME_VAR_EXT); - } - else - { - cmPassed("FILENAME_VAR_EXT == ", FILENAME_VAR_EXT); - } -#endif - -#ifndef FILENAME_VAR_NAME_WE - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME_WE is not defined."); -#else - if(strcmp(FILENAME_VAR_NAME_WE, "VarTests") != 0) - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "FILENAME_VAR_NAME_WE == ", FILENAME_VAR_NAME_WE); - } - else - { - cmPassed("FILENAME_VAR_NAME_WE == ", FILENAME_VAR_NAME_WE); - } -#endif - -#ifndef PATH_VAR_NAME - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "PATH_VAR_NAME is not defined."); -#else - if((strcmp(PATH_VAR_NAME, "Complex") == 0) || - (strcmp(PATH_VAR_NAME, "ComplexOneConfig") == 0)) - { - cmPassed("PATH_VAR_NAME == ", PATH_VAR_NAME); - } - else - { - cmFailed("the FIND_FILE or GET_FILENAME_COMPONENT command is broken, " - "PATH_VAR_NAME == ", PATH_VAR_NAME); - } -#endif - // ---------------------------------------------------------------------- // Test LOAD_CACHE diff --git a/Tests/ComplexOneConfig/VarTests.cmake b/Tests/ComplexOneConfig/VarTests.cmake index 52ec1fc8e1..70b803a1e5 100644 --- a/Tests/ComplexOneConfig/VarTests.cmake +++ b/Tests/ComplexOneConfig/VarTests.cmake @@ -182,17 +182,3 @@ set(FOREACH_CONCAT "") foreach (INDEX a;b;c;d;e;f;g) set(FOREACH_CONCAT "${FOREACH_CONCAT}${INDEX}") endforeach() - -# -# Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations -# -find_file(FILENAME_VAR "VarTests.cmake" ${Complex_SOURCE_DIR}) - -get_filename_component(FILENAME_VAR_PATH ${FILENAME_VAR} PATH) -get_filename_component(FILENAME_VAR_PATH_NAME ${FILENAME_VAR_PATH} NAME) -get_filename_component(FILENAME_VAR_NAME ${FILENAME_VAR} NAME) -get_filename_component(FILENAME_VAR_EXT ${FILENAME_VAR} EXT) -get_filename_component(FILENAME_VAR_NAME_WE ${FILENAME_VAR} NAME_WE CACHE) - -find_path(PATH_VAR "cmTestConfigure.h.in" ${Complex_SOURCE_DIR}) -get_filename_component(PATH_VAR_NAME ${PATH_VAR} NAME) diff --git a/Tests/ComplexOneConfig/cmTestConfigure.h.in b/Tests/ComplexOneConfig/cmTestConfigure.h.in index 7741b6fd58..d7424b1845 100644 --- a/Tests/ComplexOneConfig/cmTestConfigure.h.in +++ b/Tests/ComplexOneConfig/cmTestConfigure.h.in @@ -15,15 +15,6 @@ // Test WHILE #define WHILE_VALUE ${while_var} -// Test FIND_FILE, FIND_PATH and various GET_FILENAME_COMPONENT combinations - -#define FILENAME_VAR_PATH_NAME "${FILENAME_VAR_PATH_NAME}" -#define FILENAME_VAR_NAME "${FILENAME_VAR_NAME}" -#define FILENAME_VAR_EXT "${FILENAME_VAR_EXT}" -#define FILENAME_VAR_NAME_WE "${FILENAME_VAR_NAME_WE}" - -#define PATH_VAR_NAME "${PATH_VAR_NAME}" - // Test LOAD_CACHE #define CACHE_TEST_VAR1 "${CACHE_TEST_VAR1}" diff --git a/Tests/RunCMake/CMakeLists.txt b/Tests/RunCMake/CMakeLists.txt index 402c8a9806..f6761077d3 100644 --- a/Tests/RunCMake/CMakeLists.txt +++ b/Tests/RunCMake/CMakeLists.txt @@ -71,6 +71,7 @@ add_RunCMake_test(CompatibleInterface) add_RunCMake_test(add_dependencies) add_RunCMake_test(build_command) add_RunCMake_test(find_package) +add_RunCMake_test(get_filename_component) add_RunCMake_test(include) add_RunCMake_test(include_directories) add_RunCMake_test(list) diff --git a/Tests/RunCMake/get_filename_component/CMakeLists.txt b/Tests/RunCMake/get_filename_component/CMakeLists.txt new file mode 100644 index 0000000000..e8db6b05be --- /dev/null +++ b/Tests/RunCMake/get_filename_component/CMakeLists.txt @@ -0,0 +1,3 @@ +cmake_minimum_required(VERSION 2.8) +project(${RunCMake_TEST} NONE) +include(${RunCMake_TEST}.cmake) diff --git a/Tests/RunCMake/get_filename_component/KnownComponents.cmake b/Tests/RunCMake/get_filename_component/KnownComponents.cmake new file mode 100644 index 0000000000..d5becc09d4 --- /dev/null +++ b/Tests/RunCMake/get_filename_component/KnownComponents.cmake @@ -0,0 +1,32 @@ +macro(check desc actual expect) + if(NOT "x${actual}" STREQUAL "x${expect}") + message(SEND_ERROR "${desc}: got \"${actual}\", not \"${expect}\"") + endif() +endmacro() + +set(filename "/path/to/filename.ext.in") +set(expect_PATH "/path/to") +set(expect_NAME "filename.ext.in") +set(expect_EXT ".ext.in") +set(expect_NAME_WE "filename") +foreach(c PATH NAME EXT NAME_WE) + get_filename_component(actual_${c} "${filename}" ${c}) + check("${c}" "${actual_${c}}" "${expect_${c}}") +endforeach() + +get_filename_component(test_slashes "c:\\path\\to\\filename.ext.in" PATH) +check("PATH from backslashes" "${test_slashes}" "c:/path/to") + +get_filename_component(test_winroot "c:\\filename.ext.in" PATH) +check("PATH in windows root" "${test_winroot}" "c:/") + +get_filename_component(test_absolute "/path/to/a/../filename.ext.in" ABSOLUTE) +check("ABSOLUTE" "${test_absolute}" "/path/to/filename.ext.in") + +get_filename_component(test_cache "/path/to/filename.ext.in" PATH CACHE) +check("CACHE 1" "${test_cache}" "/path/to") +get_filename_component(test_cache "/path/to/other/filename.ext.in" PATH CACHE) +check("CACHE 2" "${test_cache}" "/path/to") +unset(test_cache CACHE) +get_filename_component(test_cache "/path/to/other/filename.ext.in" PATH CACHE) +check("CACHE 3" "${test_cache}" "/path/to/other") diff --git a/Tests/RunCMake/get_filename_component/RunCMakeTest.cmake b/Tests/RunCMake/get_filename_component/RunCMakeTest.cmake new file mode 100644 index 0000000000..156fc8f3cc --- /dev/null +++ b/Tests/RunCMake/get_filename_component/RunCMakeTest.cmake @@ -0,0 +1,4 @@ +include(RunCMake) + +run_cmake(KnownComponents) +run_cmake(UnknownComponent) diff --git a/Tests/RunCMake/get_filename_component/UnknownComponent-result.txt b/Tests/RunCMake/get_filename_component/UnknownComponent-result.txt new file mode 100644 index 0000000000..d00491fd7e --- /dev/null +++ b/Tests/RunCMake/get_filename_component/UnknownComponent-result.txt @@ -0,0 +1 @@ +1 diff --git a/Tests/RunCMake/get_filename_component/UnknownComponent-stderr.txt b/Tests/RunCMake/get_filename_component/UnknownComponent-stderr.txt new file mode 100644 index 0000000000..b146e5b4bf --- /dev/null +++ b/Tests/RunCMake/get_filename_component/UnknownComponent-stderr.txt @@ -0,0 +1,4 @@ +CMake Error at UnknownComponent.cmake:1 \(get_filename_component\): + get_filename_component unknown component BOGUS +Call Stack \(most recent call first\): + CMakeLists.txt:[0-9]+ \(include\) diff --git a/Tests/RunCMake/get_filename_component/UnknownComponent.cmake b/Tests/RunCMake/get_filename_component/UnknownComponent.cmake new file mode 100644 index 0000000000..06abc51e62 --- /dev/null +++ b/Tests/RunCMake/get_filename_component/UnknownComponent.cmake @@ -0,0 +1 @@ +get_filename_component(var "/path/to/filename.ext.in" BOGUS) From 7d1590e6f98a48d1d4f8f95413710e37f01b6026 Mon Sep 17 00:00:00 2001 From: Brad King Date: Tue, 16 Apr 2013 09:37:37 -0400 Subject: [PATCH 2/2] get_filename_component: Add DIRECTORY option (#14091) The name "PATH" is not precise so add equivalent option "DIRECTORY". Document the latter as preferred and the former as a legacy alias. --- Source/cmGetFilenameComponentCommand.cxx | 2 +- Source/cmGetFilenameComponentCommand.h | 3 ++- .../KnownComponents.cmake | 19 ++++++++++--------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/Source/cmGetFilenameComponentCommand.cxx b/Source/cmGetFilenameComponentCommand.cxx index fd1ad601f3..1d7fefcee4 100644 --- a/Source/cmGetFilenameComponentCommand.cxx +++ b/Source/cmGetFilenameComponentCommand.cxx @@ -58,7 +58,7 @@ bool cmGetFilenameComponentCommand } std::string storeArgs; std::string programArgs; - if (args[2] == "PATH") + if (args[2] == "DIRECTORY" || args[2] == "PATH") { result = cmSystemTools::GetFilenamePath(filename); } diff --git a/Source/cmGetFilenameComponentCommand.h b/Source/cmGetFilenameComponentCommand.h index f294daa0de..09af3325ab 100644 --- a/Source/cmGetFilenameComponentCommand.h +++ b/Source/cmGetFilenameComponentCommand.h @@ -64,12 +64,13 @@ public: return " get_filename_component( [CACHE])\n" "Set to a component of , where is one of:\n" - " PATH = Directory without file name\n" + " DIRECTORY = Directory without file name\n" " NAME = File name without directory\n" " EXT = File name longest extension (.b.c from d/a.b.c)\n" " NAME_WE = File name without directory or longest extension\n" " ABSOLUTE = Full path to file\n" " REALPATH = Full path to existing file with symlinks resolved\n" + " PATH = Legacy alias for DIRECTORY (use for CMake <= 2.8.11)\n" "Paths are returned with forward slashes and have no trailing slahes. " "The longest file extension is always considered. " "If the optional CACHE argument is specified, the result variable is " diff --git a/Tests/RunCMake/get_filename_component/KnownComponents.cmake b/Tests/RunCMake/get_filename_component/KnownComponents.cmake index d5becc09d4..a4ef8ff98f 100644 --- a/Tests/RunCMake/get_filename_component/KnownComponents.cmake +++ b/Tests/RunCMake/get_filename_component/KnownComponents.cmake @@ -5,28 +5,29 @@ macro(check desc actual expect) endmacro() set(filename "/path/to/filename.ext.in") -set(expect_PATH "/path/to") +set(expect_DIRECTORY "/path/to") set(expect_NAME "filename.ext.in") set(expect_EXT ".ext.in") set(expect_NAME_WE "filename") -foreach(c PATH NAME EXT NAME_WE) +set(expect_PATH "/path/to") +foreach(c DIRECTORY NAME EXT NAME_WE PATH) get_filename_component(actual_${c} "${filename}" ${c}) check("${c}" "${actual_${c}}" "${expect_${c}}") endforeach() -get_filename_component(test_slashes "c:\\path\\to\\filename.ext.in" PATH) -check("PATH from backslashes" "${test_slashes}" "c:/path/to") +get_filename_component(test_slashes "c:\\path\\to\\filename.ext.in" DIRECTORY) +check("DIRECTORY from backslashes" "${test_slashes}" "c:/path/to") -get_filename_component(test_winroot "c:\\filename.ext.in" PATH) -check("PATH in windows root" "${test_winroot}" "c:/") +get_filename_component(test_winroot "c:\\filename.ext.in" DIRECTORY) +check("DIRECTORY in windows root" "${test_winroot}" "c:/") get_filename_component(test_absolute "/path/to/a/../filename.ext.in" ABSOLUTE) check("ABSOLUTE" "${test_absolute}" "/path/to/filename.ext.in") -get_filename_component(test_cache "/path/to/filename.ext.in" PATH CACHE) +get_filename_component(test_cache "/path/to/filename.ext.in" DIRECTORY CACHE) check("CACHE 1" "${test_cache}" "/path/to") -get_filename_component(test_cache "/path/to/other/filename.ext.in" PATH CACHE) +get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE) check("CACHE 2" "${test_cache}" "/path/to") unset(test_cache CACHE) -get_filename_component(test_cache "/path/to/other/filename.ext.in" PATH CACHE) +get_filename_component(test_cache "/path/to/other/filename.ext.in" DIRECTORY CACHE) check("CACHE 3" "${test_cache}" "/path/to/other")