Skip to content

Commit a28965b

Browse files
authored
Allow building host tests with code coverage (project-chip#33141)
* Fix access for == operator defined for AccessControl * Fix nested if warning * Fix access to ==operator and fix mismatched types * Allow building host tests with code coverage * Ignore virtual-device-app build directory * Fix initialization of CHIPoBLEStackMgr with google test * Fix find -exec syntax * Exclude tests from the coverage report * Revert "Ignore virtual-device-app build directory" This reverts commit 2215235.
1 parent d342d9b commit a28965b

File tree

10 files changed

+30
-60
lines changed

10 files changed

+30
-60
lines changed

scripts/build/build/target.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -406,7 +406,7 @@ def Create(self, name: str, runner, repository_path: str, output_prefix: str,
406406
builder.target = self
407407
builder.identifier = name
408408
builder.output_dir = os.path.join(output_prefix, name)
409-
builder.chip_dir = repository_path
409+
builder.chip_dir = os.path.abspath(repository_path)
410410
builder.options = builder_options
411411

412412
return builder

scripts/build/build/targets.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ def BuildHostTarget():
177177
target.AppendModifier("ossfuzz", fuzzing_type=HostFuzzingType.OSS_FUZZ).OnlyIfRe(
178178
"-clang").ExceptIfRe('-libfuzzer')
179179
target.AppendModifier('coverage', use_coverage=True).OnlyIfRe(
180-
'-(chip-tool|all-clusters)')
180+
'-(chip-tool|all-clusters|tests)')
181181
target.AppendModifier('dmalloc', use_dmalloc=True)
182182
target.AppendModifier('clang', use_clang=True)
183183
target.AppendModifier('test', extra_tests=True)

scripts/build/builders/host.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -512,19 +512,21 @@ def generate(self):
512512
def PreBuildCommand(self):
513513
if self.app == HostApp.TESTS and self.use_coverage:
514514
self._Execute(['ninja', '-C', self.output_dir, 'default'], title="Build-only")
515-
self._Execute(['find', os.path.join(self.output_dir, 'obj/src/'), '-depth',
516-
'-name', 'tests', '-exec', 'rm -rf {} \\;'], title="Cleanup unit tests")
517515
self._Execute(['lcov', '--initial', '--capture', '--directory', os.path.join(self.output_dir, 'obj'),
516+
'--exclude', os.path.join(self.chip_dir, '**/tests/*'),
518517
'--exclude', os.path.join(self.chip_dir, 'zzz_generated/*'),
519518
'--exclude', os.path.join(self.chip_dir, 'third_party/*'),
519+
'--exclude', os.path.join(self.chip_dir, 'out/*'),
520520
'--exclude', '/usr/include/*',
521521
'--output-file', os.path.join(self.coverage_dir, 'lcov_base.info')], title="Initial coverage baseline")
522522

523523
def PostBuildCommand(self):
524524
if self.app == HostApp.TESTS and self.use_coverage:
525525
self._Execute(['lcov', '--capture', '--directory', os.path.join(self.output_dir, 'obj'),
526+
'--exclude', os.path.join(self.chip_dir, '**/tests/*'),
526527
'--exclude', os.path.join(self.chip_dir, 'zzz_generated/*'),
527528
'--exclude', os.path.join(self.chip_dir, 'third_party/*'),
529+
'--exclude', os.path.join(self.chip_dir, 'out/*'),
528530
'--exclude', '/usr/include/*',
529531
'--output-file', os.path.join(self.coverage_dir, 'lcov_test.info')], title="Update coverage")
530532
self._Execute(['lcov', '--add-tracefile', os.path.join(self.coverage_dir, 'lcov_base.info'),

src/access/tests/TestAccessControl.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@
2323

2424
#include <gtest/gtest.h>
2525

26-
namespace {
26+
namespace chip {
27+
namespace Access {
2728

2829
using namespace chip;
2930
using namespace chip::Access;
@@ -2189,4 +2190,5 @@ TEST_F(TestAccessControl, TestUpdateEntry)
21892190
}
21902191
}
21912192

2192-
} // namespace
2193+
} // namespace Access
2194+
} // namespace chip

src/lib/core/tests/TestTLV.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -859,8 +859,7 @@ void WriteEncoding3(TLVWriter & writer)
859859
TLVWriter writer1;
860860

861861
err = writer.OpenContainer(ProfileTag(TestProfile_1, 1), kTLVType_Structure, writer1);
862-
if (err != CHIP_NO_ERROR)
863-
EXPECT_EQ(err, CHIP_NO_ERROR);
862+
EXPECT_EQ(err, CHIP_NO_ERROR);
864863

865864
err = writer1.PutBoolean(ProfileTag(TestProfile_2, 2), false);
866865
EXPECT_EQ(err, CHIP_NO_ERROR);

src/platform/tests/BUILD.gn

-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ if (chip_device_platform != "none" && chip_device_platform != "fake") {
7878
#
7979
sources = [
8080
"TestCHIPoBLEStackMgr.cpp",
81-
"TestCHIPoBLEStackMgr.h",
8281
"TestCHIPoBLEStackMgrDriver.cpp",
8382
]
8483
tests = [ "TestCHIPoBLEStackMgr" ]

src/platform/tests/TestCHIPoBLEStackMgr.h

-30
This file was deleted.

src/platform/tests/TestCHIPoBLEStackMgrDriver.cpp

+1-4
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,16 @@
1515
* limitations under the License.
1616
*/
1717

18-
#include "TestCHIPoBLEStackMgr.h"
1918
#include <gtest/gtest.h>
2019
#include <platform/CHIPDeviceConfig.h>
2120
#include <stdlib.h>
2221

2322
int main(int argc, char * argv[])
2423
{
25-
#if CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
24+
testing::InitGoogleTest(&argc, argv);
2625
if (argc == 2 && atoi(argv[1]) == 1)
2726
{
28-
testing::InitGoogleTest(nullptr, nullptr);
2927
return RUN_ALL_TESTS();
3028
}
3129
return 0;
32-
#endif // CHIP_DEVICE_CONFIG_ENABLE_CHIPOBLE
3330
}

src/system/SystemPacketBuffer.h

+3-2
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,8 @@ class DLL_EXPORT PacketBufferHandle
657657
#endif
658658
}
659659

660+
bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }
661+
660662
protected:
661663
#if CHIP_SYSTEM_CONFIG_USE_LWIP
662664
// For use via LwIPPacketBufferView only.
@@ -684,8 +686,7 @@ class DLL_EXPORT PacketBufferHandle
684686
}
685687

686688
PacketBuffer * Get() const { return mBuffer; }
687-
688-
bool operator==(const PacketBufferHandle & aOther) const { return mBuffer == aOther.mBuffer; }
689+
PacketBuffer * GetNext() const { return static_cast<PacketBuffer *>(mBuffer->next); }
689690

690691
#if CHIP_SYSTEM_PACKETBUFFER_HAS_RIGHTSIZE
691692
void InternalRightSize();

src/system/tests/TestSystemPacketBuffer.cpp

+15-15
Original file line numberDiff line numberDiff line change
@@ -469,13 +469,13 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
469469
{
470470
EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr));
471471
EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr));
472-
EXPECT_EQ(config_2.handle->next, nullptr);
472+
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
473473
}
474474
else
475475
{
476476
EXPECT_EQ(config_2.handle->len, length);
477477
EXPECT_EQ(config_2.handle->tot_len, length);
478-
EXPECT_EQ(config_2.handle->next, nullptr);
478+
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
479479
}
480480
}
481481
else
@@ -487,7 +487,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
487487
{
488488
EXPECT_EQ(config_2.handle->len, (config_2.end_buffer - config_2.payload_ptr));
489489
EXPECT_EQ(config_2.handle->tot_len, (config_2.end_buffer - config_2.payload_ptr));
490-
EXPECT_EQ(config_2.handle->next, nullptr);
490+
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
491491

492492
EXPECT_EQ(config_1.handle->tot_len,
493493
(config_1.init_len + static_cast<int32_t>(config_2.end_buffer - config_2.payload_ptr) -
@@ -497,7 +497,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckSetDataLength)
497497
{
498498
EXPECT_EQ(config_2.handle->len, length);
499499
EXPECT_EQ(config_2.handle->tot_len, length);
500-
EXPECT_EQ(config_2.handle->next, nullptr);
500+
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
501501

502502
EXPECT_EQ(config_1.handle->tot_len,
503503
(config_1.init_len + static_cast<int32_t>(length) - static_cast<int32_t>(config_2.init_len)));
@@ -632,19 +632,19 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckAddToEnd)
632632
EXPECT_EQ(config_3.handle->ref, 1); // config_3.handle
633633

634634
EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len));
635-
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
636-
EXPECT_EQ(config_2.handle->next, nullptr);
637-
EXPECT_EQ(config_3.handle->next, nullptr);
635+
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
636+
EXPECT_EQ(config_2.handle.GetNext(), nullptr);
637+
EXPECT_EQ(config_3.handle.GetNext(), nullptr);
638638

639639
config_1.handle->AddToEnd(config_3.handle.Retain());
640640
EXPECT_EQ(config_1.handle->ref, 1); // config_1.handle
641641
EXPECT_EQ(config_2.handle->ref, 2); // config_2.handle and config_1.handle->next
642642
EXPECT_EQ(config_3.handle->ref, 2); // config_3.handle and config_2.handle->next
643643

644644
EXPECT_EQ(config_1.handle->tot_len, (config_1.init_len + config_2.init_len + config_3.init_len));
645-
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
646-
EXPECT_EQ(config_2.handle->next, config_3.handle.Get());
647-
EXPECT_EQ(config_3.handle->next, nullptr);
645+
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
646+
EXPECT_EQ(config_2.handle.GetNext(), config_3.handle.Get());
647+
EXPECT_EQ(config_3.handle.GetNext(), nullptr);
648648

649649
config_1.handle = nullptr;
650650
config_2.handle = nullptr;
@@ -703,7 +703,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckPopHead)
703703
const PacketBufferHandle popped = config_1.handle.PopHead();
704704

705705
EXPECT_EQ(config_1.handle, config_2.handle);
706-
EXPECT_EQ(config_1.handle->next, nullptr);
706+
EXPECT_EQ(config_1.handle.GetNext(), nullptr);
707707
EXPECT_EQ(config_1.handle->tot_len, config_1.init_len);
708708
}
709709
}
@@ -786,7 +786,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckCompactHead)
786786
{
787787
EXPECT_EQ(config_1.handle->len, config_1.handle->MaxDataLength());
788788
EXPECT_EQ(buffer_2->len, config_1.handle->tot_len - config_1.handle->MaxDataLength());
789-
EXPECT_EQ(config_1.handle->next, buffer_2);
789+
EXPECT_EQ(config_1.handle.GetNext(), buffer_2);
790790
EXPECT_EQ(config_1.handle->ref, 2);
791791
EXPECT_EQ(buffer_2->ref, 1);
792792
}
@@ -796,13 +796,13 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckCompactHead)
796796
if (data_length_1 >= config_1.handle->MaxDataLength() && data_length_2 == 0)
797797
{
798798
/* make sure the second buffer is not freed */
799-
EXPECT_EQ(config_1.handle->next, buffer_2);
799+
EXPECT_EQ(config_1.handle.GetNext(), buffer_2);
800800
EXPECT_EQ(buffer_2->ref, 1);
801801
}
802802
else
803803
{
804804
/* make sure the second buffer is freed */
805-
EXPECT_EQ(config_1.handle->next, nullptr);
805+
EXPECT_EQ(config_1.handle.GetNext(), nullptr);
806806
buffer_2 = nullptr;
807807
}
808808
}
@@ -1263,7 +1263,7 @@ TEST_F_FROM_FIXTURE(TestSystemPacketBuffer, CheckFree)
12631263
// Verify that head ref count is decremented.
12641264
EXPECT_EQ(config_1.handle->ref, initial_refs_1 - 1);
12651265
// Verify that chain is maintained.
1266-
EXPECT_EQ(config_1.handle->next, config_2.handle.Get());
1266+
EXPECT_EQ(config_1.handle.GetNext(), config_2.handle.Get());
12671267
// Verify that chained buffer ref count has not changed.
12681268
EXPECT_EQ(config_2.handle->ref, initial_refs_2);
12691269
}

0 commit comments

Comments
 (0)