Skip to content

Commit

Permalink
Add code coverage reporting (trustwallet#267)
Browse files Browse the repository at this point in the history
  • Loading branch information
alejandro-isaza authored Apr 5, 2019
1 parent 2542c5c commit 98b31b8
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 13 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ lib/protobuf
jni/cpp/generated
jni/java
swift/Sources/Generated

# Code coverage files
coverage.info
coverage/
7 changes: 6 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ include(ExternalProject)
# Dependencies
include(cmake/Protobuf.cmake)

option(CODE_COVERAGE "Enable coverage reporting" OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fprofile-arcs -ftest-coverage")
set(CMAKE_EXE_LINKER_FLAGS "-fprofile-arcs -ftest-coverage")
endif()

# Source files
if(${ANDROID})
message("Configuring for JNI")
Expand All @@ -56,7 +62,6 @@ set_target_properties(TrustWalletCore
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
COMPILE_FLAGS "${WARNING_FLAGS}"
)

# Define headers for this library. PUBLIC headers are used for compiling the
Expand Down
14 changes: 7 additions & 7 deletions include/TrustWalletCore/TWBitcoin.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,17 @@ static const uint32_t TWSignatureHashTypeSingle = 0x03;
static const uint32_t TWSignatureHashTypeFork = 0x40;
static const uint32_t TWSignatureHashTypeAnyoneCanPay = 0x80;

enum TWBitcoinSignatureVersion {
BASE,
WITNESS_V0
};

TW_EXTERN_C_END

inline bool TWSignatureHashTypeIsSingle(uint32_t type) {
return (type & 0x1f) == TWSignatureHashTypeSingle;
}

inline bool TWSignatureHashTypeIsNone(uint32_t type) {
return (type & 0x1f) == TWSignatureHashTypeNone;
}

enum TWBitcoinSignatureVersion {
BASE,
WITNESS_V0
};

TW_EXTERN_C_END
8 changes: 7 additions & 1 deletion src/PublicKey.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#include "Hash.h"

#include <array>
#include <cassert>
#include <stdexcept>
#include <vector>

namespace TW {
Expand Down Expand Up @@ -76,9 +78,13 @@ class PublicKey {
}

/// Initializes a public key with a collection of bytes.
///
/// @throws std::invalid_argument if the data is not a valid public key.
template <typename T>
explicit PublicKey(const T& data) {
assert(isValid(data));
if (!isValid(data)) {
throw std::invalid_argument("Invalid public key data");
}
bytes.reserve(data.size());
std::copy(std::begin(data), std::end(data), std::back_inserter(bytes));
}
Expand Down
5 changes: 3 additions & 2 deletions src/uint256.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#pragma once

#include "Data.h"

#include <boost/multiprecision/cpp_int.hpp>

namespace TW {
Expand All @@ -17,7 +18,7 @@ using uint256_t = boost::multiprecision::uint256_t;
/// Loads a `uint256_t` from a collection of bytes.
inline uint256_t load(const Data& data) {
using boost::multiprecision::cpp_int;
if (std::empty(data)) {
if (data.empty()) {
return uint256_t(0);
}
uint256_t result;
Expand All @@ -29,7 +30,7 @@ inline uint256_t load(const Data& data) {
/// std::string).
inline uint256_t load(const std::string& data) {
using boost::multiprecision::cpp_int;
if (std::empty(data)) {
if (data.empty()) {
return uint256_t(0);
}
uint256_t result;
Expand Down
25 changes: 23 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,29 @@ include_directories(${Protobuf_INCLUDE_DIRS})
# Test executable
file(GLOB_RECURSE test_sources *.cpp **/*.cpp)
add_executable(tests ${test_sources})
target_compile_features(tests PRIVATE cxx_std_17)
target_link_libraries(tests gtest_main TrustWalletCore protobuf Boost::boost)
target_link_libraries(tests gtest_main TrezorCrypto TrustWalletCore protobuf Boost::boost)
target_include_directories(tests PRIVATE ${CMAKE_SOURCE_DIR}/src)
target_compile_options(tests PRIVATE "-Wall")

set_target_properties(tests
PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)

option(CODE_COVERAGE "Enable coverage reporting" OFF)
if(CODE_COVERAGE AND CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
# Add required flags (GCC & LLVM/Clang)
target_compile_options(tests INTERFACE
-O0 # no optimization
-g # generate debug info
--coverage # sets all required flags
)
if(CMAKE_VERSION VERSION_GREATER_EQUAL 3.13)
target_link_options(tests INTERFACE --coverage)
else()
target_link_libraries(tests INTERFACE --coverage)
endif()
endif()

add_test(NAME example_test COMMAND tests)
23 changes: 23 additions & 0 deletions tools/coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash
#
# This script captures test code coverage information.

set -e

lcov --capture --directory . --output-file coverage.info
lcov --remove coverage.info '/usr/*' --output-file coverage.info
lcov --remove coverage.info '/Applications/*' --output-file coverage.info
lcov --remove coverage.info '*/build/*' --output-file coverage.info
lcov --remove coverage.info '*.pb.cc' --output-file coverage.info
lcov --remove coverage.info '*.pb.h' --output-file coverage.info
lcov --remove coverage.info '*/tests/*' --output-file coverage.info
bash <(curl -s https://codecov.io/bash) -f coverage.info -t 1faef852-b196-4697-885b-0ff22a625181 || echo "Codecov did not collect coverage reports"

# Generate HTML report
if [[ "$1" == "html" ]]; then
genhtml --output-directory coverage \
--demangle-cpp --num-spaces 4 --sort \
--title "Trust Wallet Core Test Coverage" \
--function-coverage --branch-coverage --legend \
coverage.info
fi

0 comments on commit 98b31b8

Please sign in to comment.