ctest: De-duplicate add_subdirectory and subdirs
CTest re-implements the subdirs and add_subdirectory commands. De-duplicate their implementations and add test cases verifying both.
In particular, this fixes the add_subdirectory implementation that was missing code for changing the current working directory.
Factored out of !6734 (closed).
Co-Authored-By: @friendlyanon
Issue: #22921