Skip to content

Commit

Permalink
feat(tests): add a new project to catch missing includes
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrechette committed Jan 6, 2024
1 parent 223c516 commit b78ec05
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ elseif(PLATFORM_EMSCRIPTEN)
add_subdirectory("${PROJECT_SOURCE_DIR}/main_emscripten")
else()
add_subdirectory("${PROJECT_SOURCE_DIR}/main_generic")
add_subdirectory("${PROJECT_SOURCE_DIR}/validate_includes")
endif()
41 changes: 41 additions & 0 deletions tests/validate_includes/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required (VERSION 3.2)
project(sjson-cpp_validate_includes CXX)

# The goal of this project is to generate a single cpp file for every public header
# This will allow us to detect if we are missing an include file during development

include_directories("${PROJECT_SOURCE_DIR}/../../includes")

# Grab all of our public header files
file(GLOB ALL_PUBLIC_HEADER_FILES LIST_DIRECTORIES false
${PROJECT_SOURCE_DIR}/../../includes/sjson/*.h)

# Generate the single include cpp files
foreach(HEADER_FILE ${ALL_PUBLIC_HEADER_FILES})
# Find the root include directory position
string(FIND ${HEADER_FILE} "sjson" HEADER_FILE_SJSON_CPP_POS REVERSE)

# Strip the root of the include path
string(SUBSTRING ${HEADER_FILE} ${HEADER_FILE_SJSON_CPP_POS} -1 HEADER_INCLUDE_PATH)

# Configure our cpp file content
set(SJSON_CPP_SINGLE_INCLUDE_NAME ${HEADER_INCLUDE_PATH})

# Sanitize our filename so we can generate a unique cpp file for it
string(REPLACE "/" "_" HEADER_SANITIZED_FILENAME ${HEADER_INCLUDE_PATH})
string(REPLACE "\\" "_" HEADER_SANITIZED_FILENAME ${HEADER_SANITIZED_FILENAME})

# Generate our single include cpp file
configure_file(${PROJECT_SOURCE_DIR}/single_include.cpp.in single_include_${HEADER_SANITIZED_FILENAME}.cpp @ONLY)
endforeach(HEADER_FILE)

# Grab all of our main source files
file(GLOB_RECURSE ALL_MAIN_SOURCE_FILES LIST_DIRECTORIES false
${PROJECT_SOURCE_DIR}/*.cpp
${PROJECT_BINARY_DIR}/*.cpp)

create_source_groups("${ALL_MAIN_SOURCE_FILES}" ${PROJECT_SOURCE_DIR})

add_library(${PROJECT_NAME} STATIC ${ALL_MAIN_SOURCE_FILES})

setup_default_compiler_flags(${PROJECT_NAME})
27 changes: 27 additions & 0 deletions tests/validate_includes/dummy.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2024 Nicholas Frechette, Cody Jones, and sjson-cpp contributors
//
// 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.
////////////////////////////////////////////////////////////////////////////////

// We define a single symbol here to avoid a warning when we build our static library
// to validate includes
void some_function() {}
25 changes: 25 additions & 0 deletions tests/validate_includes/single_include.cpp.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
////////////////////////////////////////////////////////////////////////////////
// The MIT License (MIT)
//
// Copyright (c) 2024 Nicholas Frechette, Cody Jones, and sjson-cpp contributors
//
// 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.
////////////////////////////////////////////////////////////////////////////////

#include <@SJSON_CPP_SINGLE_INCLUDE_NAME@>

0 comments on commit b78ec05

Please sign in to comment.