Skip to content

Skip building tests by default #993

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Aug 9, 2023

Conversation

kkloberdanz
Copy link
Contributor

@kkloberdanz kkloberdanz commented Jul 26, 2023

CXX-1792
CXX-2702

Tests will not be compiled by default.
To enable building tests, build with -DBUILD_TESTING=ON

@kkloberdanz kkloberdanz force-pushed the skip-tests-by-default branch 2 times, most recently from 18107c1 to 62b82e3 Compare August 3, 2023 19:42
@kkloberdanz kkloberdanz changed the title Skip tests by default Skip building tests by default Aug 3, 2023
@kkloberdanz kkloberdanz force-pushed the skip-tests-by-default branch from 62b82e3 to f40a8b4 Compare August 4, 2023 13:02
@kkloberdanz kkloberdanz marked this pull request as ready for review August 4, 2023 13:36
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CMake has an semi-built-in option for this feature, BUILD_TESTING. At the top CMakeLists.txt, add:

if(NOT DEFINED BUILD_TESTING)
  set(BUILD_TESTING OFF)  # Set this to OFF by default
endif()
include(CTest)

@@ -12,24 +12,28 @@
# See the License for the specific language governing permissions and
# limitations under the License.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm curious if this will work, but I haven't tested it:

if(NOT BUILD_TESTING)
  set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()

(and then the remainder of the file is reverted to as-it-was). IIUC, this should exclude every added target from the implicit all, but still defined them and are available for building by being named explicitly?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with the suggestion to exclude test targets from all, but still have them defined.

Example: Targets in the examples directory are currently excluded from the all target with:

add_custom_target(examples DEPENDS ${MONGOCXX_EXAMPLE_EXECUTABLES} ${BSONCXX_EXAMPLE_EXECUTABLES})

But the example targets are still defined. CMake targets can be listed with cmake --build cmake-build --target help. An example target can be built by including the --target argument: cmake --build cmake-build --target aggregation_examples

Copy link
Contributor Author

@kkloberdanz kkloberdanz Aug 7, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't seem to work when I try it. The test programs are built by default when I implement this like so:

CMakeLists.txt:

if(NOT DEFINED BUILD_TESTING)
  set(BUILD_TESTING OFF)  # Set this to OFF by default
endif()

src/bsoncxx/test/CMakeLists.txt and src/mongocxx/test/CMakeLists.txt:

if(NOT BUILD_TESTING)
  set_property(DIRECTORY PROPERTY EXCLUDE_FROM_ALL TRUE)
endif()

include_directories(
  ...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I can figure why the directory property doesn't work, so just discard that suggestion for now. On the other hand, the BUILD_TESTING variable should work? Are you sure to clear it from your cache before configuring?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I found the issue, and after applying the fix below, your suggestion then works. I pushed these changes and the tests are green.

-        set(ENABLE_TESTS OLD_ENABLE_TESTS)
-        set(BUILD_TESTING OLD_BUILD_TESTING)
+        set(ENABLE_TESTS ${OLD_ENABLE_TESTS})
+        set(BUILD_TESTING ${OLD_BUILD_TESTING})

@kevinAlbs kevinAlbs removed their request for review August 7, 2023 18:51
Copy link
Contributor

@vector-of-bool vector-of-bool left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. One more comment.

CMakeLists.txt Outdated
Comment on lines 17 to 19
if(NOT DEFINED BUILD_TESTING)
set(BUILD_TESTING OFF) # Set this to OFF by default
endif()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest: Move this below project(). Will also want to include(CTest).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based off our conversation in slack, we'll leave off the include(CTest) for now since it is currently causing the tests to be built unconditionally.

@kkloberdanz kkloberdanz merged commit 7b01ed2 into mongodb:master Aug 9, 2023
@kkloberdanz kkloberdanz deleted the skip-tests-by-default branch August 9, 2023 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants