cpp-subprocess/test/CMakeLists.txt
Hennadii Stepanov ed313971c0
Do not escape double quotes for command line arguments on Windows (#113)
* refactor: Remove unused `util::is_ready()` function

Commit ffd4c731a2c7c09f34e7b81a16e04bdf91fa973d introduced the
`util::is_ready()` function, which has never been used internally and is
not part of the public API.

This change removes the function.

* refactor: Guard `util::quote_argument()` with `#ifdef __USING_WINDOWS__`

The `util::quote_argument()` function is specific to Windows and is used
in code already guarded by `#ifdef __USING_WINDOWS__`.

* Do not escape double quotes for command line arguments on Windows

This change fixes the handling of double quotes and aligns the behavior
with Python's `Popen` class. For example:
```
>py -3
>>> import subprocess
>>> p = subprocess.Popen("cmd.exe /c dir \"C:\\Program Files\"", stdout=subprocess.PIPE, text=True)
>>> print(f"Captured stdout:\n{stdout}")
```

Currently, the same command line processed by the `quote_argument()`
function looks like `cmd.exe /c dir "\"C:\Program" "Files\""`, which is
broken.

With this change, it looks correct: `cmd.exe /c dir "C:\Program Files"`.

* Add `test_double_quotes` test

The new test ensures that no regressions are introduced in the future.
2025-04-24 21:54:41 +05:30

36 lines
1.1 KiB
CMake

set(test_names test_subprocess test_cat test_double_quotes test_env test_err_redirection test_exception test_split test_main test_ret_code)
set(test_files env_script.sh write_err.sh write_err.txt)
foreach(test_name IN LISTS test_names)
add_executable(${test_name} ${test_name}.cc)
target_link_libraries(${test_name} PRIVATE subprocess)
add_test(
NAME ${test_name}
COMMAND $<TARGET_FILE:${test_name}>
)
endforeach()
foreach(test_file IN LISTS test_files)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/${test_file}
${CMAKE_CURRENT_BINARY_DIR}/${test_file}
COPYONLY
)
endforeach()
set(TEST_REDIRECTION_PYTHON_SCRIPT_PATH ${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.py)
configure_file(
${CMAKE_CURRENT_SOURCE_DIR}/test_redirection.cc.in
${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc
@ONLY
)
add_executable(test_redirection ${CMAKE_CURRENT_BINARY_DIR}/test_redirection.cc)
target_link_libraries(test_redirection PRIVATE subprocess)
add_test(
NAME test_redirection
COMMAND $<TARGET_FILE:test_redirection>
)