【问题标题】:CTest.exe on Windows splits PATH environment property on semicolonsWindows 上的 CTest.exe 将 PATH 环境属性拆分为分号
【发布时间】:2022-02-11 00:03:11
【问题描述】:

我有一个 Jenkins 管道,它使用 bat 脚本中的 ctest.exe 在 Windows VM 上运行测试,如下所示:

// Run all tests in parallel, timeout after 1 hour, exclude any tests needing opengl
bat ''' "C:/Program Files/CMake/bin/ctest.exe" --timeout 3600 --verbose -C Release -j4 -E "(_gl)|(GL)" '''

在我的 CTest 设置中,我使用下面的代码设置环境。我已经环顾四周,发现我需要转义 PATH 中的反斜杠以避免 CMake 将其解释为列表,但这似乎并没有一直到最后:

set(test_dll_path)
set(PATH_STRING "")

# Add the paths I want to a list
list(APPEND test_dll_path "${PROJECT_SOURCE_DIR}/../bin64")
list(APPEND test_dll_path "${PROJECT_SOURCE_DIR}/../bin64/plugins")

# Concatenate that list and convert it to a string
cmake_path(APPEND_STRING test_dll_path ";$ENV{PATH}")
cmake_path(CONVERT "${test_dll_path}" TO_NATIVE_PATH_LIST test_dll_path NORMALIZE)

# Escape the backslashes to avoid CTest interpreting it as a list
string(REPLACE ";" "\\;" test_dll_path "${test_dll_path}")

# Join it to the existing path using more escaped backslashes
list(JOIN test_dll_path "\\;" PATH_STRING)

# Assign the environment to blank first ("append_string" only works if the property existed already)
set_property( TEST ${TEST_TARGET} PROPERTY ENVIRONMENT "" )
set_property( TEST ${TEST_TARGET} APPEND_STRING PROPERTY ENVIRONMENT "PATH=${PATH_STRING}" )
    
# This comes through as one full string when printed as a message, but still interpreted as a list when CTest uses it later :(
message(DEBUG "Test ${TEST_TARGET} PATH = \"${PATH_STRING}\"")

然后,当我运行 jenkins 管道时, --verbose 设置会打印测试环境。 发生这种情况时,PATH 会在每个分号上拆分,并且测试的行为就像只有第一项在 PATH 中一样:

1: Test command: C:\jenkins\workspace\ows-IncrementalBuild-Test_PR-856\source\Tests\tut.exe "-cppunitreport"
1: Environment variables: 
1:  PATH=C:\jenkins\workspace\ows-IncrementalBuild-Test_PR-856\source\bin64
1:  C:\jenkins\workspace\ows-IncrementalBuild-Test_PR-856\source\bin64\plugins
1:  C:\Program Files (x86)\Microsoft Visual Studio 14.0\Common7\IDE\CommonExtensions\Microsoft\TestWindow
1:  C:\Program Files (x86)\MSBuild\14.0\bin\amd64
1:  C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\amd64
1:  C:\Windows\Microsoft.NET\Framework64\v4.0.30319

...etc

【问题讨论】:

    标签: windows jenkins cmake jenkins-pipeline ctest


    【解决方案1】:

    从一周的搜索中我没有看到这个,但是一位同事刚刚给我发了这个答案: How do I make a list in CMake with the semicolon value?

    所以答案是,如果有疑问,请继续添加更多反斜杠。加入路径时我需要三个,如下所示:

    # Escape the backslashes to avoid CTest interpreting it as a list
    string(REPLACE ";" "\\\;" test_dll_path "${test_dll_path}")
    
    # Join it to the existing path using more escaped backslashes
    list(JOIN test_dll_path "\\\;" PATH_STRING)
    

    【讨论】:

    • 发布这三个反斜杠的位置可能对其他人有用。
    • 您还可以使用$<SEMICOLON> 生成器表达式或在CMake 3.22+ 上使用新的ENVIRONMENT_MODIFICATION 测试属性。
    猜你喜欢
    • 1970-01-01
    • 2012-05-27
    • 2019-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多