CTest: Extract common implementation of add_subdirectory and subdirs

The two functions have basically the same functionality in a CTest
context, however the add_subdirectory implementation was missing code
for changing the current working directory similarly to how it was done
for subdirs. This commit extracts the common code and also fixes that
bug.

Issue: #22921
1 file changed