-
Notifications
You must be signed in to change notification settings - Fork 374
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
feat: cmake option to skip building mock libraries #13673
Conversation
e6d4247
to
c5b414f
Compare
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #13673 +/- ##
==========================================
- Coverage 93.28% 93.28% -0.01%
==========================================
Files 2228 2228
Lines 193491 193491
==========================================
- Hits 180501 180497 -4
- Misses 12990 12994 +4 ☔ View full report in Codecov by Sentry. |
I'm not a maintainer, but this is looking reasonable to me. |
cmake/GoogleCloudCppCommon.cmake
Outdated
@@ -164,6 +164,17 @@ endfunction () | |||
function (google_cloud_cpp_install_mocks library display_name) | |||
set(library_target "google_cloud_cpp_${library}") | |||
set(mocks_target "google_cloud_cpp_${library}_mocks") | |||
|
|||
# Always install the mock headers. These are harmless, in the sense that | |||
# they will not cause build failures in environments without GoogleTest. The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If GMock is not installed, as soon as you #include
one of them the build will stop working 🤷
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I assumed they were stubs from the comment, but if they're not, that would be problematic. I'm assuming other build issues creeped up when this wasn't done?
Ignore the above, I believe @coryan's comment was from the library's perspective and not the user of the library's perspective?
if (GOOGLE_CLOUD_CPP_WITH_MOCKS) | ||
# Create a header-only library for the mocks. We use a CMake `INTERFACE` | ||
# library for these, a regular library would not work on macOS (where | ||
# the library needs at least one .o file). Unfortunately INTERFACE |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No action needed: I wonder if this is still true now that we use CMake >= 3.13 ..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ack, I don't know the answer and I don't have easy access to a mac.
cmake/GoogleCloudCppCommon.cmake
Outdated
@@ -164,6 +164,7 @@ endfunction () | |||
function (google_cloud_cpp_install_mocks library display_name) | |||
set(library_target "google_cloud_cpp_${library}") | |||
set(mocks_target "google_cloud_cpp_${library}_mocks") | |||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reverted.
cmake_dependent_option( | ||
GOOGLE_CLOUD_CPP_WITH_MOCKS | ||
[==[Build the google-cloud-cpp mocking libraries. | ||
|
||
google-cloud-cpp offers mocking libraries with mock classes, to facilitate unit | ||
testing of Cloud C++ clients. Consumers of this library that do not use the | ||
provided mocks to test code involving the Cloud C++ clients may wish to turn | ||
this flag off.]==] | ||
ON | ||
"NOT BUILD_TESTING" | ||
ON) | ||
mark_as_advanced(GOOGLE_CLOUD_CPP_WITH_MOCKS) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TIL, you can use NOT condition
in cmake_dependent_option
. Using ON ... ON
looks weird. I think you want something like this:
cmake_dependent_option( | |
GOOGLE_CLOUD_CPP_WITH_MOCKS | |
[==[Build the google-cloud-cpp mocking libraries. | |
google-cloud-cpp offers mocking libraries with mock classes, to facilitate unit | |
testing of Cloud C++ clients. Consumers of this library that do not use the | |
provided mocks to test code involving the Cloud C++ clients may wish to turn | |
this flag off.]==] | |
ON | |
"NOT BUILD_TESTING" | |
ON) | |
mark_as_advanced(GOOGLE_CLOUD_CPP_WITH_MOCKS) | |
option( | |
GOOGLE_CLOUD_CPP_WITH_MOCKS | |
[==[Build the google-cloud-cpp mocking libraries. | |
google-cloud-cpp offers mocking libraries with mock classes, to facilitate unit | |
testing of Cloud C++ clients. Consumers of this library that do not use the | |
provided mocks to test code involving the Cloud C++ clients may wish to turn | |
this flag off.]==] | |
$<IF:$<BOOL:${BUILD_TESTING}>:OFF:ON>) | |
mark_as_advanced(GOOGLE_CLOUD_CPP_WITH_MOCKS) |
That is, the option always exists, but its default value is based on a generator expression.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think I want that.
The default value should always be ON
, regardless of BUILD_TESTING
. But if BUILD_TESTING
is ON
, then GOOGLE_CLOUD_CPP_WITH_MOCKS
must be ON
(because our unit tests depend on the mock libraries). That is what I am enforcing with the ON
... ON
.
I think this is doing what I want. Adding a debug print statement after the option is defined:
$ cmake -DBUILD_TESTING=ON -DGOOGLE_CLOUD_CPP_WITH_MOCKS=OFF
GOOGLE_CLOUD_CPP_WITH_MOCKS=ON
$ cmake -DBUILD_TESTING=OFF -DGOOGLE_CLOUD_CPP_WITH_MOCKS=OFF
GOOGLE_CLOUD_CPP_WITH_MOCKS=OFF
$ cmake -LAH
...
// Build the google-cloud-cpp mocking libraries.
google-cloud-cpp offers mocking libraries with mock classes, to facilitate unit
testing of Cloud C++ clients. Consumers of this library that do not use the
provided mocks to test code involving the Cloud C++ clients may wish to turn
this flag off.
GOOGLE_CLOUD_CPP_WITH_MOCKS:BOOL=ON
32d7a6c
to
db7582d
Compare
Thanks for the merge! |
Motivated by #13621
Consumers of
google-cloud-cpp
who want to test their code can use this flag to install mocks, without needing to build all of our unit tests, internal testing infrastructure.Background
We create mocking libraries (which are all header only) and install them by default (even when
BUILD_TESTING=OFF
). For many months, we only did this in Pub/Sub and GCS+gRPC, but in the latest release, we started installing mocks for all GAPICs. (And in this release, for all libraries except compute).These libraries link to GoogleTest, which can be hard to find. This can break some (atypical?) builds. (See the linked issue, which uses
FetchContent()
.Things to Scrutinize
GOOGLE_CLOUD_CPP_INSTALL_MOCKS
.This change is