TZIVI-254: IVI needs a newer version of cmake
[profile/ivi/cmake.git] / Tests / CMakeTests / GetFilenameComponentRealpathTest.cmake.in
1 set(bindir ${CMAKE_CURRENT_BINARY_DIR})
2
3 #
4 # Test nonexistent REALPATH & ABSOLUTE resolution
5 #
6 get_filename_component(nonexistent1 ${bindir}/THIS_IS_A_NONEXISTENT_FILE REALPATH)
7 get_filename_component(nonexistent2 ${bindir}/THIS_IS_A_NONEXISTENT_FILE ABSOLUTE)
8 if(NOT nonexistent1 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
9     message(FATAL_ERROR "REALPATH is not preserving nonexistent files")
10 endif()
11 if(NOT nonexistent2 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
12     message(FATAL_ERROR "ABSOLUTE is not preserving nonexistent files")
13 endif()
14
15 #
16 # Test treatment of relative paths
17 #
18 foreach(c REALPATH ABSOLUTE)
19   get_filename_component(dir "subdir/THIS_IS_A_NONEXISTENT_FILE" ${c})
20   if(NOT "${dir}" STREQUAL "${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE")
21     message(FATAL_ERROR
22       "${c} does not handle relative paths.  Expected:\n"
23       "  ${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE\n"
24       "but got:\n"
25       "  ${nonexistent1}\n"
26       )
27   endif()
28 endforeach()
29
30 #
31 # Test symbolic link resolution
32 #
33 if(UNIX)
34     # file1 => file2 => file3 (real)
35     file(WRITE ${bindir}/file3 "test file")
36
37     find_program(LN NAMES "ln")
38     if(LN)
39         # Create symlinks using "ln -s"
40         if(NOT EXISTS ${bindir}/file2)
41             execute_process(COMMAND ${LN} "-s" "${bindir}/file3" "${bindir}/file2")
42         endif()
43         if(NOT EXISTS ${bindir}/file1)
44             execute_process(COMMAND ${LN} "-s" "${bindir}/file2" "${bindir}/file1")
45         endif()
46
47         get_filename_component(file1 ${bindir}/file1 REALPATH)
48         get_filename_component(file2 ${bindir}/file2 REALPATH)
49         get_filename_component(file3 ${bindir}/file3 REALPATH)
50
51         if(NOT file3 STREQUAL "${bindir}/file3")
52             message(FATAL_ERROR "CMake fails resolving REALPATH file file3")
53         endif()
54
55         if(NOT file2 STREQUAL "${bindir}/file3")
56             message(FATAL_ERROR "CMake fails resolving simple symlink")
57         endif()
58
59         if(NOT file1 STREQUAL "${bindir}/file3")
60             message(FATAL_ERROR "CMake fails resolving double symlink")
61         endif()
62
63         # cleanup
64         file(REMOVE ${bindir}/file1)
65         file(REMOVE ${bindir}/file2)
66         if(EXISTS file1 OR EXISTS file2)
67            message(FATAL_ERROR "removal of file1 or file2 failed")
68         endif()
69     endif(LN)
70
71     file(REMOVE ${bindir}/file3)
72 endif()