Skip to content

Commit

Permalink
Add initial project
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesDias committed Dec 3, 2022
0 parents commit dc474a5
Show file tree
Hide file tree
Showing 116 changed files with 141,429 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/unit-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Test

on: [push]

jobs:
unit-tests:
runs-on: ubuntu-latest
container:
image: charlesdias/gcc-arm-cmake:latest

steps:
- uses: actions/checkout@v3

- name: Configure and build
run: |
make build
- name: Run Google Test
run: |
make test
- name: Upload coverage reports to Codecov with GitHub Action
uses: codecov/codecov-action@v3
with:
gcov_ignore:
./build
./docs
./tests
103 changes: 103 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
### Eclipse ###
Debug/
Release/
### CMake ###
build/

# Created by https://www.toptal.com/developers/gitignore/api/eclipse,vscode,cmake
# Edit at https://www.toptal.com/developers/gitignore?templates=eclipse,vscode,cmake

### CMake ###
CMakeLists.txt.user
CMakeCache.txt
CMakeFiles
CMakeScripts
Testing
cmake_install.cmake
install_manifest.txt
compile_commands.json
CTestTestfile.cmake
CPackConfig.cmake
_deps
CMakeUserPresets.json

### CMake Patch ###
# External projects
*-prefix/

### Eclipse ###
.metadata
bin/
tmp/
*.tmp
*.bak
*.swp
*~.nib
local.properties
.settings/
.loadpath
.recommenders

# External tool builders
.externalToolBuilders/

# Locally stored "Eclipse launch configurations"
*.launch

# PyDev specific (Python IDE for Eclipse)
*.pydevproject

# CDT-specific (C/C++ Development Tooling)
.cproject

# CDT- autotools
.autotools

# Java annotation processor (APT)
.factorypath

# PDT-specific (PHP Development Tools)
.buildpath

# sbteclipse plugin
.target

# Tern plugin
.tern-project

# TeXlipse plugin
.texlipse

# STS (Spring Tool Suite)
.springBeans

# Code Recommenders
.recommenders/

# Annotation Processing
.apt_generated/
.apt_generated_test/

# Scala IDE specific (Scala & Java development for Eclipse)
.cache-main
.scala_dependencies
.worksheet

# Uncomment this line if you wish to ignore the project description file.
# Typically, this file would be tracked if it contains build/dependency configurations:
#.project

### Eclipse Patch ###
# Spring Boot Tooling
.sts4-cache/

### vscode ###
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
*.code-workspace


# End of https://www.toptal.com/developers/gitignore/api/eclipse,vscode,cmake
41 changes: 41 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 3.21.0)

project(stm32_gtest)

set(CMAKE_C_STANDARD 17)
set(CMAKE_C_STANDARD_REQUIRED ON) # Define explicitly to use of the standard C17 declared in the previous line
set(CMAKE_C_EXTENSIONS OFF) # Disabling vendor-specific extensions

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON) # Define explicitly to use of the standard C++17 declared in the previous line
set(CMAKE_CXX_EXTENSIONS OFF) # Disabling vendor-specific extensions

option(ENABLE_COVERAGE "Enable a Code Coverage build." ON)

### CMAKE MODULES
set(CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake/)
if (ENABLE_COVERAGE)
include(CodeCoverage)
append_coverage_compiler_flags()
endif()

message(STATUS "EXTERNAL: Clone Google Test Framework from Git repository...")
include(FetchContent)
FetchContent_Declare(
googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.12.1
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# It must to be declare after test folder
enable_testing()

# Variables name for target
set(LIBRARY_NAME library_gpio)

add_subdirectory(stm32-cube-ide)
add_subdirectory(tests)
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2022 Charles Dias

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
29 changes: 29 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
all: build
@echo ""
@echo "Done!"

config:
@echo "-------------------- Configure CMake ---------------------"
cmake -S . -B build
@echo ""

build: config
@echo "-------------------- Build CMake--------------------------"
cmake --build build --config Debug --target coverage -j4
@echo ""

test:
@echo "-------------------- Run CTest ---------------------------"
cd build && pwd && ctest
@echo ""

dependency:
@echo "-------------------- Create Graph Dependecy --------------"
cd build && cmake .. --graphviz=graph.dot && dot -Tpng graph.dot -o graph_image.png
@echo ""

clean:
@echo ""
@echo "-------------------- Clean build folder ------------------"
rm -rf build
@echo ""
113 changes: 113 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# STM32_GTest

![License](https://camo.githubusercontent.com/890acbdcb87868b382af9a4b1fac507b9659d9bf/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c75652e737667)
[![Test](https://github.com/CharlesDias/STM32_GTest/actions/workflows/unit-test.yml/badge.svg)](https://github.com/CharlesDias/STM32_GTest/actions/workflows/unit-test.yml)
[![codecov](https://codecov.io/gh/CharlesDias/STM32_GTest/branch/main/graph/badge.svg)](https://codecov.io/gh/CharlesDias/STM32_GTest)

This is a sample project for testing C code for STM32 microcontrollers using the Google Test Framework. Some topics covered:

* Sample project using the NUCLEO-F446ZE board.
* Use of STM32CubeIDE for building and compiling the application project.
* Use of CMake for building the test code.
* Testing C code via Google Test Framework.
* Use GMock for mocking the STM32 HAL functions.
* Code coverage with Github Actions and [Codecov](https://codecov.io).
* Use of Docker container.

## Project structure

``` text
./
├── CMakeLists.txt
├── cmake
│   └── cmake modules
├── docker
│   └── Dockerfile
├── .github
│   └── workflows
│   └── GitHub workflows
├── stm32-cube-ide
│   ├── CMakeLists.txt
│   ├── Core
│   │   ├── Inc
│   │   │   └── ...
│   │   ├── Src
│   │   │   ├── main.c (Sample code)
│   │   │   └── ...
│   │   └── Files generated by STM32CubeIDE
│   ├── Drivers
│   │   └── Files generated by STM32CubeIDE
│   └── Library
│      └── Header and source library files
└── tests
   ├── CMakeLists.txt
   ├── header-overrides
   │   └── Overrides some header files copied from the STM32CubeIDE project that are needed by the mock
   ├── integration
   │   └── integration_hello_test.cpp
   ├── mock
   │   ├── fixture.h
   │   └── hal_gpio_mock.cpp
   └── unit
   ├── gpio_test.cpp
   └── unit_hello_test.cpp
```

## Building tests project via CMake

Clone this repo for your local machine

```console
git clone https://github.com/CharlesDias/STM32_GTest.git
```

Access the project folder **STM32_GTest**

```console
cd STM32_GTest
```

Pull the latest docker image used to build the project.

```console
docker pull charlesdias/stm32_gtest
```

Run the image docker.

```console
docker run --rm -it -v $(pwd):/home/project -w /home/project charlesdias/stm32_gtest
```

Run the command below inside the Docker container

```console
make clean && make build && make test
```

Access the `build/coverage/index.html` file to see the coverage report.

### Dependency graph

Dependency graph for test project.

![Dependency graph](docs/images/dependency_graph.png "Dependency graph")

## Building application project via STM32CubeIDE

Clone this repo for your local machine

```console
git clone https://github.com/CharlesDias/STM32_GTest.git
```

Open the STM32CubeIDE and import this project.

Build and load the firmware on NUCLEO-F446ZE board. See the expected output.

![STM32 project](docs/images/stm32_gtest.gif "STM32 project")
Loading

0 comments on commit dc474a5

Please sign in to comment.