Clean up CMock makefiles and add coverage filtering (#523)

* Cleanup Makefiles

* Add lcovrc configuration file

* Add CMock test build directory to .gitignore

* Add callgraph.py and filtercov.py scripts

* Cleanup list Makefile and update list_utest.c with coverage tags

* Add information about coverage filtering and running single test cases

* Remove -fprofile-exclude-files for compatibility with older versions of gcc.
Fix line endings (change to unix style)

* Lint callgraph.py and filtercov.py. Print and error when no target functions are defined.

* Indent with spaces when possible

* Replace tabs with spaces and enable .RECIPEPREFIX

* Add fake_port.h and related portmacro.h changes

* Fix list makefile when bin directory is not available

* Clean up grouped rules

* Update makesfile.. Add "two_tests" example dir

* Fix memory checker error

* Move common makefile items to subdir.mk and testdir.mk includes

* Update core_checker.py exclusions

* Remove line from portmacro.h that doesn't match core_checker.py
This commit is contained in:
Paul Bartell 2021-03-15 17:01:29 -07:00 committed by GitHub
parent c8fa483b68
commit d7e5f40885
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
20 changed files with 1110 additions and 329 deletions

View file

@ -1,73 +1,14 @@
# Change according to what your unit test directory is.
# For example if testing queue.c your directory should be called queue
# and the project name should be queue
# if testing list.c your directory should be called list
# and the project name should be list
PROJECT := queue
# Indent with spaces
.RECIPEPREFIX := $(.RECIPEPREFIX) $(.RECIPEPREFIX)
# Do not move this line below the include
MAKEFILE_ABSPATH := $(abspath $(lastword $(MAKEFILE_LIST)))
include ../makefile.in
# List the dependency files you wish to mock
MOCK_FILES_FP := $(KERNEL_DIR)/include/task.h
MOCK_FILES_FP += $(KERNEL_DIR)/include/list.h
MOCK_FILES_FP += $(UT_ROOT_DIR)/config/fake_assert.h
# SUITES lists the suites contained in subdirectories of this directory
SUITES += two_tests
# List the options the compilation would need
CPPFLAGS += -DportUSING_MPU_WRAPPERS=0
# PROJECT and SUITE variables are determined based on path like so:
# $(UT_ROOT_DIR)/$(PROJECT)/$(SUITE)
PROJECT := $(lastword $(subst /, ,$(dir $(abspath $(MAKEFILE_ABSPATH)))))
# Try not to edit beyond this line
MOCK_FILES := $(notdir $(MOCK_FILES_FP))
MOCK_OBJ := $(addprefix mock_,$(MOCK_FILES:.h=.o))
MOCK_SRC := $(addprefix mock_,$(MOCK_FILES:.h=.c))
EXEC := $(PROJECT)_utest
PROJECT_DIR := $(abspath .)
SCRATCH_DIR := $(GENERATED_DIR)/$(PROJECT)
PROJ_LIB_DIR := $(SCRATCH_DIR)/lib
MOCK_OBJ_LIST := $(addprefix $(PROJ_LIB_DIR)/,$(MOCK_OBJ))
MOCKS_DIR := $(SCRATCH_DIR)/mocks
MOCK_SRC_LIST := $(addprefix $(MOCKS_DIR)/,$(MOCK_SRC))
CFLAGS += -I$(MOCKS_DIR)
COVERAGE_OPTS := -fprofile-arcs -ftest-coverage -fprofile-generate
ifeq ($(MOCK_FILES_FP),)
$(shell mkdir -p $(MOCKS_DIR))
$(shell touch -a $(MOCKS_DIR)/mock_dummy.c)
endif
$(MOCKS_DIR)/mock_%.c : Makefile $(PROJECT_DIR)/$(PROJECT).yml | directories
cd $(SCRATCH_DIR) && \
ruby $(CMOCK_EXEC_DIR)/cmock.rb -o$(PROJECT_DIR)/$(PROJECT).yml \
$(MOCK_FILES_FP)
$(PROJ_LIB_DIR)/mock_%.o : $(MOCKS_DIR)/mock_%.c
$(CC) -c $< -fPIC $(CFLAGS) -o $@
$(BIN_DIR)/$(EXEC) : $(SCRATCH_DIR)/test_runner.o \
$(SCRATCH_DIR)/$(PROJECT).o \
$(SCRATCH_DIR)/$(PROJECT)_utest.o \
$(MOCK_OBJ_LIST)
$(CC) $+ $(LDFLAGS) -o $@
$(SCRATCH_DIR)/test_runner.o : $(SCRATCH_DIR)/test_runner.c \
$(SCRATCH_DIR)/$(PROJECT).o
$(CC) -c $< $(CPPFLAGS) $(CFLAGS) -o $@
$(SCRATCH_DIR)/$(PROJECT)_utest.o : $(PROJECT_DIR)/$(PROJECT)_utest.c \
$(MOCKS_DIR)/mock_*.c \
| directories
$(CC) -c $< $(CPPFLAGS) $(CFLAGS) -o $@
$(SCRATCH_DIR)/$(PROJECT).o : $(KERNEL_DIR)/$(PROJECT).c
$(CC) -c $< $(CPPFLAGS) $(CFLAGS) $(COVERAGE_OPTS) -o $@
$(SCRATCH_DIR)/test_runner.c : $(SCRATCH_DIR)/$(PROJECT)_utest.o \
Makefile | directories
ruby $(UNITY_BIN_DIR)/generate_test_runner.rb $(EXEC).c \
$(PROJECT_DIR)/$(PROJECT).yml $@
.PHONY: directories
directories :
-mkdir $(SCRATCH_DIR)
-mkdir $(MOCKS_DIR)
-mkdir $(PROJ_LIB_DIR)
# prevent deletion by chain of implicit rules
NO_DELETE: $(MOCK_SRC_LIST)
include ../subdir.mk

View file

@ -42,6 +42,7 @@
#include "mock_task.h"
#include "mock_list.h"
#include "mock_fake_assert.h"
#include "mock_fake_port.h"
/* ============================ GLOBAL VARIABLES =========================== */
@ -61,7 +62,12 @@ void vPortFree( void * pv )
******************************************************************************/
void setUp( void )
{
mock_task_Init();
mock_fake_assert_Init();
mock_fake_port_Init();
vFakeAssert_Ignore();
vFakePortEnterCriticalSection_Ignore();
vFakePortExitCriticalSection_Ignore();
/* Track calls to malloc / free */
UnityMalloc_StartTest();
@ -71,6 +77,15 @@ void setUp( void )
void tearDown( void )
{
UnityMalloc_EndTest();
mock_task_Verify();
mock_task_Destroy();
mock_fake_assert_Verify();
mock_fake_assert_Destroy();
mock_fake_port_Verify();
mock_fake_port_Destroy();
}
/*! called at the beginning of the whole suite */
@ -86,7 +101,7 @@ int suiteTearDown( int numFailures )
/*!
* @brief xQueueCreate happy path.
*
* @coverage xQueueGenericCreate
*/
void test_xQueueCreate_Success( void )
{

View file

@ -0,0 +1,47 @@
# Indent with spaces
.RECIPEPREFIX := $(.RECIPEPREFIX) $(.RECIPEPREFIX)
# Do not move this line below the include
MAKEFILE_ABSPATH := $(abspath $(lastword $(MAKEFILE_LIST)))
include ../../makefile.in
# PROJECT_SRC lists the .c files under test
PROJECT_SRC += queue.c
# PROJECT_DEPS_SRC list the .c file that are dependencies of PROJECT_SRC files
# Files in PROJECT_DEPS_SRC are excluded from coverage measurements
PROJECT_DEPS_SRC +=
# PROJECT_HEADER_DEPS: headers that should be excluded from coverage measurements.
PROJECT_HEADER_DEPS += FreeRTOS.h
# SUITE_UT_SRC: .c files that contain test cases (must end in _utest.c)
SUITE_UT_SRC += queue_2_utest.c
SUITE_UT_SRC += queue_1_utest.c
# SUITE_SUPPORT_SRC: .c files used for testing that do not contain test cases.
# Paths are relative to PROJECT_DIR
SUITE_SUPPORT_SRC +=
# List the headers used by PROJECT_SRC that you would like to mock
MOCK_FILES_FP += $(KERNEL_DIR)/include/task.h
MOCK_FILES_FP += $(KERNEL_DIR)/include/list.h
MOCK_FILES_FP += $(UT_ROOT_DIR)/config/fake_assert.h
MOCK_FILES_FP += $(UT_ROOT_DIR)/config/fake_port.h
# List any addiitonal flags needed by the preprocessor
CPPFLAGS += -DportUSING_MPU_WRAPPERS=0
# List any addiitonal flags needed by the compiler
CFLAGS +=
# Try not to edit beyond this line unless necessary.
# Project / Suite are determined based on path: $(UT_ROOT_DIR)/$(PROJECT)/$(SUITE)
PROJECT := $(lastword $(subst /, ,$(dir $(abspath $(MAKEFILE_ABSPATH)/../))))
SUITE := $(lastword $(subst /, ,$(dir $(MAKEFILE_ABSPATH))))
# Make variables available to included makefile
export
include ../../testdir.mk

View file

@ -0,0 +1 @@
../queue_utest.c

View file

@ -0,0 +1 @@
../queue_utest.c