[gtest][listing] Enable gtest json listing for the fbcode/caffe2 project (#156816)

***SUMMARY***

The main function in this tests overrides that of the Gtest framework which contains it's `RUN_ALL_TESTS()` function. The main function in this test is called conditionally when conditions apply, in this case, when the C10_MOBILE directive is provided. This is wrong as we always want to call the `RUN_ALL_TEST()` function.

In this PR, we only make the test suite available for cases that apply, i.e if the C10_MOBILE directive exist which represents the caching allocator and is only exposed on mobile

***TEST PLAN***

This tests should run in modes where it applies which should be covered in the CI run.

Below shows a sample run in the dev-nosan mode which do not have the cache allocator

BEFORE
```
buck test fbcode//caffe2:cpu_caching_allocator_test
Discovered 0. Pass 0. Fail 0. Fatal 0. Skip 0. Timeout 0
⚠ Listing failed: caffe2:cpu_caching_allocator_test
Listing tests failed with error:
Failed to read from /data/users/ysuleiman/fbsource/buck-out/v2/test/buck-out/v2/test_discovery/fbcode/6dcc55a61c1b90b3/default/tpx_execution_dir/gtest_output_file.json. Listing process stdout: , stderr:
```

AFTER
```
buck test '@fbcode//mode/dev-nosan' fbcode//caffe2:cpu_caching_allocator_test
Analyzing targets. Remaining      0/46242                                                                                1871690 actions, 2251668 artifacts declared
Executing actions. Remaining      0/257870                                                                               83:28:24.4s exec time total
Command: test.     Finished 10 remote, 112314 cache (99% hit)                                                            83:22:43.5s exec time cached (99%)
Time elapsed: 2:57.7s
Tests finished: Pass 0. Fail 0. Fatal 0. Skip 0. Build failure 0
NO TESTS RAN
```

Rollback Plan:
steps:
  - manual.note:
      content: Revert this diff

Reviewed By: patskovn

Differential Revision: D77229077
Pull Request resolved: https://github.com/pytorch/pytorch/pull/156816
Approved by: https://github.com/kimishpatel
This commit is contained in:
Yahaya Suleiman 2025-07-07 14:16:39 +00:00 committed by PyTorch MergeBot
parent 54a4d34d10
commit 16c3b4143b

View File

@ -5,6 +5,9 @@
#include <c10/mobile/CPUCachingAllocator.h> #include <c10/mobile/CPUCachingAllocator.h>
// At the moment caching allocator is only exposed to mobile cpu allocator.
#ifdef C10_MOBILE
TEST(CPUCachingAllocatorTest, check_alloc_free) { TEST(CPUCachingAllocatorTest, check_alloc_free) {
c10::CPUCachingAllocator caching_allocator; c10::CPUCachingAllocator caching_allocator;
c10::WithCPUCachingAllocatorGuard cachine_allocator_guard( c10::WithCPUCachingAllocatorGuard cachine_allocator_guard(
@ -41,10 +44,9 @@ TEST(CPUCachingAllocatorTest, check_alloc_inside_free_outside) {
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// At the moment caching allocator is only exposed to mobile cpu allocator.
#ifdef C10_MOBILE
::testing::InitGoogleTest(&argc, argv); ::testing::InitGoogleTest(&argc, argv);
at::manual_seed(42); at::manual_seed(42);
return RUN_ALL_TESTS(); return RUN_ALL_TESTS();
#endif /* C10_Mobile */
} }
#endif /* C10_Mobile */