Skip to content

Commit

Permalink
Add COVERAGE option, and add in prepare test and tear down functions …
Browse files Browse the repository at this point in the history
…needed code to save coverage per test
  • Loading branch information
Jovana Boskovic committed May 12, 2024
1 parent 9d373cf commit 8d7ec6b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ ifndef CXXFLAGS
endif

ifeq (g++, $(findstring g++,$(CXX)))
override CXXFLAGS += -std=gnu++0x -pipe
CXXFLAGS += -std=gnu++0x -pipe
else ifeq (clang++, $(findstring clang++,$(CXX)))
override CXXFLAGS += -std=c++0x
else ifeq ($(CXX), c++)
Expand All @@ -149,6 +149,11 @@ else ifeq ($(CXX), c++)
endif
endif

ifeq ($(COVERAGE), 1)
$(info Adding coverage flags to CXXFLAGS)
CXXFLAGS += -fprofile-arcs -ftest-coverage
endif

ifeq ($(HAVE_RULES),yes)
PCRE_CONFIG = $(shell which pcre-config)
ifeq ($(PCRE_CONFIG),)
Expand Down
29 changes: 28 additions & 1 deletion test/fixture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@

#include "xml.h"

#include <cstdlib> // for getenv and setenv
#include <sys/stat.h> // for mkdir
extern "C" void __gcov_flush();

/**
* TestRegistry
**/
Expand Down Expand Up @@ -88,6 +92,29 @@ TestFixture::TestFixture(const char * const _name)

bool TestFixture::prepareTest(const char testname[])
{
const char* coverage = std::getenv("COVERAGE");
if (coverage) {
std::string testDir = "./coverage_per_test/" + std::string(testname); // Adjust the path as needed
if (mkdir(testDir.c_str(), 0777) != 0) {
// If directory creation fails and it's not because the directory exists
if (errno != EEXIST) {
std::cerr << "Failed to create directory for test: " << testDir
<< ", Error: " << strerror(errno) << std::endl;
return false;
}
}

// Set the environment variables to point to the new directory
if (setenv("GCOV_PREFIX", testDir.c_str(), 1) != 0) {
std::cerr << "Failed to set GCOV_PREFIX environment variable." << std::endl;
return false;
}

if (setenv("GCOV_PREFIX_STRIP", "0", 1) != 0) {
std::cerr << "Failed to set GCOV_PREFIX_STRIP environment variable." << std::endl;
return false;
}
}
mVerbose = false;
mTemplateFormat.clear();
mTemplateLocation.clear();
Expand All @@ -114,7 +141,7 @@ bool TestFixture::prepareTest(const char testname[])
void TestFixture::teardownTest()
{
teardownTestInternal();

__gcov_flush();
{
const std::string s = errout_str();
if (!s.empty())
Expand Down

0 comments on commit 8d7ec6b

Please sign in to comment.