File: GetFilenameComponentRealpathTest.cmake.in

package info (click to toggle)
cmake 4.2.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152,344 kB
  • sloc: ansic: 403,894; cpp: 303,807; sh: 4,097; python: 3,582; yacc: 3,106; lex: 1,279; f90: 538; asm: 471; lisp: 375; cs: 270; java: 266; fortran: 239; objc: 215; perl: 213; xml: 198; makefile: 108; javascript: 83; pascal: 63; tcl: 55; php: 25; ruby: 22
file content (88 lines) | stat: -rw-r--r-- 2,783 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
set(bindir ${CMAKE_CURRENT_BINARY_DIR})

#
# Test nonexistent REALPATH & ABSOLUTE resolution
#
get_filename_component(nonexistent1 ${bindir}/THIS_IS_A_NONEXISTENT_FILE REALPATH)
get_filename_component(nonexistent2 ${bindir}/THIS_IS_A_NONEXISTENT_FILE ABSOLUTE)
if(NOT nonexistent1 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
    message(FATAL_ERROR "REALPATH is not preserving nonexistent files")
endif()
if(NOT nonexistent2 STREQUAL "${bindir}/THIS_IS_A_NONEXISTENT_FILE")
    message(FATAL_ERROR "ABSOLUTE is not preserving nonexistent files")
endif()

#
# Test treatment of .. after file name
#
foreach(c REALPATH ABSOLUTE)
  get_filename_component(dir "${CMAKE_CURRENT_LIST_DIR}" ${c})
  get_filename_component(fileDotDot "${CMAKE_CURRENT_LIST_FILE}/.." ${c})
  if(NOT "${fileDotDot}" STREQUAL "${dir}")
    message(FATAL_ERROR
      "${c} did not resolve\n"
      "  ${CMAKE_CURRENT_LIST_FILE}/..\n"
      "lexically:\n"
      "  ${fileDotDot}"
    )
  endif()
endforeach()

#
# Test treatment of relative paths
#
foreach(c REALPATH ABSOLUTE)
  get_filename_component(dir "subdir/THIS_IS_A_NONEXISTENT_FILE" ${c})
  if(NOT "${dir}" STREQUAL "${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE")
    message(FATAL_ERROR
      "${c} does not handle relative paths.  Expected:\n"
      "  ${bindir}/subdir/THIS_IS_A_NONEXISTENT_FILE\n"
      "but got:\n"
      "  ${nonexistent1}\n"
      )
  endif()
endforeach()

#
# Test symbolic link resolution
#
if(UNIX)
    # file1 => file2 => file3 (real)
    file(WRITE ${bindir}/file3 "test file")

    find_program(LN NAMES "ln")
    if(LN)
        # Create symlinks using "ln -s"
        if(NOT EXISTS ${bindir}/file2)
            execute_process(COMMAND ${LN} "-s" "${bindir}/file3" "${bindir}/file2")
        endif()
        if(NOT EXISTS ${bindir}/file1)
            execute_process(COMMAND ${LN} "-s" "${bindir}/file2" "${bindir}/file1")
        endif()

        get_filename_component(file1 ${bindir}/file1 REALPATH)
        get_filename_component(file2 ${bindir}/file2 REALPATH)
        get_filename_component(file3 ${bindir}/file3 REALPATH)

        if(NOT file3 STREQUAL "${bindir}/file3")
            message(FATAL_ERROR "CMake fails resolving REALPATH file file3")
        endif()

        if(NOT file2 STREQUAL "${bindir}/file3")
            message(FATAL_ERROR "CMake fails resolving simple symlink")
        endif()

        if(NOT file1 STREQUAL "${bindir}/file3")
            message(FATAL_ERROR "CMake fails resolving double symlink")
        endif()

        # cleanup
        file(REMOVE ${bindir}/file1)
        file(REMOVE ${bindir}/file2)
        if(EXISTS file1 OR EXISTS file2)
           message(FATAL_ERROR "removal of file1 or file2 failed")
        endif()
    endif()

    file(REMOVE ${bindir}/file3)
endif()