Skip to content

Commit

Permalink
Merge pull request #47 from marcransome/unit-tests
Browse files Browse the repository at this point in the history
Add unit tests and update build configuration
  • Loading branch information
marcransome authored Feb 4, 2023
2 parents b24f991 + 9c5d042 commit 5f7e4ed
Show file tree
Hide file tree
Showing 16 changed files with 459 additions and 85 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ jobs:
with:
languages: cpp
queries: security-and-quality
- name: Build project
- name: Install dependencies and build project
run: |
brew install popt
cmake -S . -B build
cmake --build build
- name: Perform CodeQL analysis
Expand Down
12 changes: 11 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@ cmake_minimum_required(VERSION 3.22)
project(flog C)

set(CMAKE_C_STANDARD 17)
set(CMAKE_MODULE_PATH ${CMAKE_SOURCE_DIR}/cmake)

add_subdirectory(src)
find_package(PkgConfig REQUIRED)
pkg_check_modules(POPT REQUIRED popt>=1.18)

if (NOT UNIT_TESTING)
message(STATUS "Building program target")
add_subdirectory(src)
else()
message(STATUS "Building unit test targets")
enable_testing()
add_subdirectory(test)
endif()
23 changes: 21 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,12 @@ And here's a similar log stream viewed with Apple's `log(1)` command:

### Requirements

* macOS (`11.x` or later)
* macOS `11.x` (Big Sur) or later
* A C17 compiler
* CMake (`3.22` or later)
* CMake version `>=3.22`
* `pkg-config` version `>=0.29.2`
* `libpopt` version `>=1.18`
* `libcmocka` version `>=1.1.5` (if building unit test targets)

### Building from source

Expand All @@ -94,6 +97,22 @@ cmake -S . -B build
cmake --build build
```

### Building unit test targets

To perform an out-of-source build for unit test targets only:

```bash
cmake -S . -B build -DUNIT_TESTING=ON
cmake --build build
```

To execute all unit test targets:

```bash
cd build/test
ctest -V
```

## Acknowledgements

* Trunk icon made by [Freepik](https://www.flaticon.com/authors/freepik) from [www.flaticon.com](https://www.flaticon.com/)
Expand Down
15 changes: 15 additions & 0 deletions cmake/add_cmocka_test.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function(add_cmocka_test unit)
set(test_target test_${unit})

add_executable(${test_target} test_${unit}.c ${CMAKE_SOURCE_DIR}/src/${unit}.c)

target_link_libraries(${test_target} PRIVATE ${CMOCKA_LINK_LIBRARIES} PRIVATE ${POPT_LINK_LIBRARIES})
target_include_directories(${test_target} PRIVATE ${CMOCKA_INCLUDE_DIRS} PRIVATE ${POPT_INCLUDE_DIRS})

target_include_directories(${test_target} PRIVATE ${CMAKE_SOURCE_DIR}/src)

target_compile_options(${test_target} PRIVATE ${CMOCKA_CFLAGS} PRIVATE ${POPT_CFLAGS})
target_compile_definitions(${test_target} PRIVATE UNIT_TESTING)

add_test(NAME ${test_target} COMMAND ${test_target})
endfunction()
6 changes: 3 additions & 3 deletions man/flog.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.\" Automatically generated by Pandoc 2.19.2
.\" Automatically generated by Pandoc 3.0.1
.\"
.\" Define V font for inline verbatim, using C font in formats
.\" that render this, and otherwise B font.
Expand All @@ -14,7 +14,7 @@
. ftr VB CB
. ftr VBI CBI
.\}
.TH "flog" "1" "" "Version 1.3.0" "Flog User\[cq]s Guide"
.TH "flog" "1" "" "Version 1.4.0" "Flog User\[cq]s Guide"
.hy
.SH NAME
.PP
Expand All @@ -35,7 +35,7 @@ Wrap the \f[I]message\f[R] string in quotes to preserve spacing.
.SS Options
.TP
\f[B]-h,\f[R] \f[B]--help\f[R]
Print brief usage information
Print brief help information
.TP
\f[B]-v,\f[R] \f[B]--version\f[R]
Print the current version string
Expand Down
4 changes: 2 additions & 2 deletions man/flog.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% flog(1) Version 1.3.0 | Flog User's Guide
% flog(1) Version 1.4.0 | Flog User's Guide

NAME
====
Expand All @@ -20,7 +20,7 @@ Options

**-h,** **\--help**

: Print brief usage information
: Print brief help information

**-v,** **\--version**

Expand Down
6 changes: 6 additions & 0 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
set(target flog)

add_executable(flog main.c flog.c flog.h config.c config.h defs.h utils.c utils.h)

target_link_libraries(${target} PRIVATE ${POPT_LINK_LIBRARIES})
target_include_directories(${target} PRIVATE ${POPT_INCLUDE_DIRS})
target_compile_options(${target} PRIVATE ${POPT_CFLAGS})

install(TARGETS flog DESTINATION bin)
Loading

0 comments on commit 5f7e4ed

Please sign in to comment.