Skip to content

Commit

Permalink
Fix: unit tests (#394)
Browse files Browse the repository at this point in the history
- fixed unit tests built without any libs
- removed extra warnings from the Release build
  • Loading branch information
dnzbk committed Sep 17, 2024
1 parent 2dc6755 commit e5a2837
Show file tree
Hide file tree
Showing 26 changed files with 344 additions and 187 deletions.
37 changes: 13 additions & 24 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,10 @@ if (PROJECT_SOURCE_DIR STREQUAL PROJECT_BINARY_DIR)
message(FATAL_ERROR "In-source builds are not allowed. You should create separate directory for build files.")
endif()

set_property(GLOBAL PROPERTY PACKAGE)
set_property(GLOBAL PROPERTY LIBS)
set_property(GLOBAL PROPERTY INCLUDES)

set(VERSION "24.4")
set(PACKAGE "nzbget")
set(PACKAGE "nzbget" CACHE INTERNAL "")
set(LIBS "" CACHE INTERNAL "")
set(INCLUDES "" CACHE INTERNAL "")
set(PACKAGE_BUGREPORT "https://github.com/nzbgetcom/nzbget/issues")
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 17)
Expand Down Expand Up @@ -72,11 +70,11 @@ if(CMAKE_BUILD_TYPE STREQUAL "Debug")
endif()
elseif(CMAKE_BUILD_TYPE STREQUAL "Release")
if(CMAKE_CXX_COMPILER_ID MATCHES "Clang|AppleClang")
set(CMAKE_CXX_FLAGS "-O2 -g0 -pthread -DNDEBUG -Weverything -Wno-c++98-compat" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-O2 -g0 -pthread -DNDEBUG -Wno-c++98-compat" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
set(CMAKE_CXX_FLAGS "-O2 -g0 -pthread -DNDEBUG -Wall -Wextra" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "-O2 -g0 -pthread -DNDEBUG" CACHE STRING "" FORCE)
elseif(CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
set(CMAKE_CXX_FLAGS "/O2 /MT /MP /W4 /EHs /DNDEBUG /DWIN32 /wd4800 /wd4267" CACHE STRING "" FORCE)
set(CMAKE_CXX_FLAGS "/O2 /MT /MP /EHs /DNDEBUG /DWIN32 /wd4800 /wd4267" CACHE STRING "" FORCE)
set(CMAKE_CXX_STANDARD_LIBRARIES "${CMAKE_CXX_STANDARD_LIBRARIES} winmm.lib" CACHE STRING "" FORCE)
endif()
endif()
Expand All @@ -89,8 +87,9 @@ set(CMAKE_CXX_FLAGS_RELEASE ${CMAKE_CXX_FLAGS} CACHE STRING "" FORCE)
include_directories(${CMAKE_BINARY_DIR})
include_directories(${CMAKE_SOURCE_DIR})

include(daemon/sources.cmake)

if(NOT BUILD_ONLY_TESTS)
include(daemon/sources.cmake)
add_executable(${PACKAGE} ${SRC})

if(CMAKE_BUILD_TYPE STREQUAL "Release")
Expand All @@ -105,6 +104,7 @@ endif()

if(WIN32)
include(cmake/windows.cmake)

if(NOT BUILD_ONLY_TESTS)
target_sources(${PACKAGE} PRIVATE ${CMAKE_SOURCE_DIR}/windows/resources/nzbget.rc)
configure_file(
Expand All @@ -125,33 +125,22 @@ if(WIN32)
endif()
else()
include(cmake/posix.cmake)

if(NOT BUILD_ONLY_TESTS)
include(${CMAKE_SOURCE_DIR}/cmake/install.cmake)
endif()
endif()

include(lib/sources.cmake)

configure_file(
${CMAKE_SOURCE_DIR}/cmake/config.h.in
${CMAKE_BINARY_DIR}/config.h
)

if(NOT BUILD_ONLY_TESTS)
target_link_libraries(${PACKAGE} PRIVATE ${LIBS})
target_include_directories(${PACKAGE} PRIVATE
${CMAKE_SOURCE_DIR}/daemon/connect
${CMAKE_SOURCE_DIR}/daemon/extension
${CMAKE_SOURCE_DIR}/daemon/feed
${CMAKE_SOURCE_DIR}/daemon/frontend
${CMAKE_SOURCE_DIR}/daemon/main
${CMAKE_SOURCE_DIR}/daemon/nntp
${CMAKE_SOURCE_DIR}/daemon/nserv
${CMAKE_SOURCE_DIR}/daemon/postprocess
${CMAKE_SOURCE_DIR}/daemon/queue
${CMAKE_SOURCE_DIR}/daemon/remote
${CMAKE_SOURCE_DIR}/daemon/util
${CMAKE_SOURCE_DIR}/daemon/system
${INCLUDES}
)
target_include_directories(${PACKAGE} PRIVATE ${INCLUDES})
endif()

if(ENABLE_TESTS OR BUILD_ONLY_TESTS)
Expand Down
9 changes: 2 additions & 7 deletions cmake/posix.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ if(ENABLE_STATIC)
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} -static" CACHE STRING "" FORCE)
endif()
set(BUILD_SHARED_LIBS OFF)
set(LIBS $ENV{LIBS})
set(INCLUDES $ENV{INCLUDES})
include(${CMAKE_SOURCE_DIR}/lib/sources.cmake)
set(LIBS ${LIBS} $ENV{LIBS})
set(INCLUDES ${INCLUDES} $ENV{INCLUDES})
else()
find_package(Threads REQUIRED)
find_package(LibXml2 REQUIRED)
Expand Down Expand Up @@ -86,17 +85,13 @@ else()

include(${CMAKE_SOURCE_DIR}/cmake/boost.cmake)

include(${CMAKE_SOURCE_DIR}/lib/sources.cmake)

add_dependencies(${PACKAGE} boost)
add_dependencies(yencode boost)
add_dependencies(par2 boost)
add_dependencies(regex boost)
else()
set(LIBS ${LIBS} Boost::json)
set(INCLUDES ${INCLUDES} ${Boost_INCLUDE_DIR})

include(${CMAKE_SOURCE_DIR}/lib/sources.cmake)
endif()
endif()

Expand Down
6 changes: 2 additions & 4 deletions cmake/windows.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ find_package(Threads REQUIRED)
find_package(LibXml2 REQUIRED)
find_package(Boost REQUIRED COMPONENTS json)

set(LIBS Threads::Threads Boost::json LibXml2::LibXml2)
set(INCLUDES ${Boost_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR})
set(LIBS ${LIBS} Threads::Threads Boost::json LibXml2::LibXml2)
set(INCLUDES ${INCLUDES} ${Boost_INCLUDE_DIR} ${LIBXML2_INCLUDE_DIR})

if(NOT DISABLE_TLS)
find_package(OpenSSL REQUIRED)
Expand All @@ -30,8 +30,6 @@ endif()
find_package(ZLIB REQUIRED)
set(LIBS ${LIBS} ZLIB::ZLIB)
set(INCLUDES ${INCLUDES} ${ZLIB_INCLUDE_DIRS})

include(${CMAKE_SOURCE_DIR}/lib/sources.cmake)
set(INCLUDES ${INCLUDES}
${CMAKE_SOURCE_DIR}/daemon/windows
${CMAKE_SOURCE_DIR}/windows/resources
Expand Down
16 changes: 16 additions & 0 deletions daemon/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,20 @@ set(WIN32_SRC

if(WIN32)
set(SRC ${SRC} ${WIN32_SRC})
set(INCLUDES ${INCLUDES} ${CMAKE_SOURCE_DIR}/windows)
endif()

set(INCLUDES ${INCLUDES}
${CMAKE_SOURCE_DIR}/daemon/connect
${CMAKE_SOURCE_DIR}/daemon/extension
${CMAKE_SOURCE_DIR}/daemon/feed
${CMAKE_SOURCE_DIR}/daemon/frontend
${CMAKE_SOURCE_DIR}/daemon/main
${CMAKE_SOURCE_DIR}/daemon/nntp
${CMAKE_SOURCE_DIR}/daemon/nserv
${CMAKE_SOURCE_DIR}/daemon/postprocess
${CMAKE_SOURCE_DIR}/daemon/queue
${CMAKE_SOURCE_DIR}/daemon/remote
${CMAKE_SOURCE_DIR}/daemon/system
${CMAKE_SOURCE_DIR}/daemon/util
)
1 change: 1 addition & 0 deletions lib/sources.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ endif()

set(LIBS ${LIBS} regex yencode)
set(INCLUDES ${INCLUDES} ${CMAKE_SOURCE_DIR}/lib/regex ${CMAKE_SOURCE_DIR}/lib/yencode)

if(NOT DISABLE_PARCHECK)
set(LIBS ${LIBS} par2)
set(INCLUDES ${INCLUDES} ${CMAKE_SOURCE_DIR}/lib/par2)
Expand Down
17 changes: 1 addition & 16 deletions tests/extension/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,7 @@ file(GLOB ExtensionSrc

add_executable(ExtensionTests ${ExtensionSrc})
target_link_libraries(ExtensionTests PRIVATE ${LIBS})

target_include_directories(ExtensionTests PRIVATE
${CMAKE_SOURCE_DIR}/daemon/main
${CMAKE_SOURCE_DIR}/daemon/util
${CMAKE_SOURCE_DIR}/daemon/extension
${CMAKE_SOURCE_DIR}/daemon/remote
${CMAKE_SOURCE_DIR}/daemon/queue
${CMAKE_SOURCE_DIR}/daemon/feed
${CMAKE_SOURCE_DIR}/daemon/nntp
${CMAKE_SOURCE_DIR}/daemon/postprocess
${CMAKE_SOURCE_DIR}/daemon/connect
${CMAKE_SOURCE_DIR}/daemon/system
${CMAKE_SOURCE_DIR}/lib/regex
${CMAKE_SOURCE_DIR}/lib/yencode
${INCLUDES}
)
target_include_directories(ExtensionTests PRIVATE ${INCLUDES})

file(COPY ../testdata/extension/manifest DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
file(COPY ../testdata/extension/V1 DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
Expand Down
3 changes: 0 additions & 3 deletions tests/extension/ExtensionLoaderTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@

#include "FileSystem.h"
#include "Extension.h"
#include "WorkState.h"
#include "ExtensionLoader.h"

WorkState* g_WorkState;

BOOST_AUTO_TEST_CASE(ExtensionV1LoaderTest)
{
Extension::Script extension;
Expand Down
15 changes: 4 additions & 11 deletions tests/extension/ExtensionManagerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,11 @@
#include "ExtensionManager.h"
#include "FileSystem.h"
#include "Options.h"
#include "Log.h"
#include "DiskState.h"

char* (*g_EnvironmentVariables)[] = nullptr;
Log* g_Log;
Options* g_Options;
DiskState* g_DiskState;

static std::string currentPath = FileSystem::GetCurrentDirectory().Str();
static std::string testDir = "ScriptDir=" + currentPath + "/scripts";
static std::string extensions = std::string("Extensions=") + "Extension2, Extension1, email; Extension1; Extension1";
static std::string scriptOrder = std::string("ScriptOrder=") + "Extension2, Extension1, email; Extension1; Extension1";
const std::string currentPath = FileSystem::GetCurrentDirectory().Str();
const std::string testDir = "ScriptDir=" + currentPath + "/scripts";
const std::string extensions = std::string("Extensions=") + "Extension2, Extension1, email; Extension1; Extension1";
const std::string scriptOrder = std::string("ScriptOrder=") + "Extension2, Extension1, email; Extension1; Extension1";

BOOST_AUTO_TEST_CASE(LoadExtesionsTest)
{
Expand Down
52 changes: 52 additions & 0 deletions tests/extension/main.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,55 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "nzbget.h"

#define BOOST_TEST_MODULE ExtensionTests
#include <boost/test/included/unit_test.hpp>
#include <boost/json/src.hpp>

#include "Log.h"
#include "Options.h"
#include "WorkState.h"
#include "DiskState.h"

char* (*g_EnvironmentVariables)[];
Log* g_Log;
WorkState* g_WorkState;
Options* g_Options;
DiskState* g_DiskState;

struct InitGlobals
{
InitGlobals()
{
g_EnvironmentVariables = nullptr;
g_Log = new Log();
g_WorkState = new WorkState();
g_DiskState = new DiskState();
}

~InitGlobals()
{
delete g_Log;
delete g_WorkState;
delete g_DiskState;
}
};

BOOST_GLOBAL_FIXTURE(InitGlobals);
17 changes: 4 additions & 13 deletions tests/feed/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
add_executable(FeedTest
add_executable(FeedTests
FeedFilterTest.cpp
FeedFileTest.cpp
main.cpp
Expand All @@ -14,17 +14,8 @@ add_executable(FeedTest
${CMAKE_SOURCE_DIR}/daemon/queue/DiskState.cpp
${CMAKE_SOURCE_DIR}/daemon/nntp/NewsServer.cpp
)
target_link_libraries(FeedTest PRIVATE ${LIBS})
target_include_directories(FeedTest PRIVATE
${CMAKE_SOURCE_DIR}/daemon/feed
${CMAKE_SOURCE_DIR}/daemon/main
${CMAKE_SOURCE_DIR}/daemon/util
${CMAKE_SOURCE_DIR}/daemon/queue
${CMAKE_SOURCE_DIR}/daemon/nntp
${CMAKE_SOURCE_DIR}/daemon/remote
${CMAKE_SOURCE_DIR}/lib/regex
${INCLUDES}
)
target_link_libraries(FeedTests PRIVATE ${LIBS})
target_include_directories(FeedTests PRIVATE ${INCLUDES})

file(COPY ../testdata/feed DESTINATION ${CMAKE_CURRENT_BINARY_DIR})
add_test(NAME FeedTest COMMAND $<TARGET_FILE:FeedTest> --log_level=message)
add_test(NAME FeedTests COMMAND $<TARGET_FILE:FeedTests> --log_level=message)
7 changes: 0 additions & 7 deletions tests/feed/FeedFilterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,6 @@
#include <boost/test/unit_test.hpp>

#include "FeedFilter.h"
#include "Log.h"
#include "Options.h"
#include "DiskState.h"

Log* g_Log;
Options* g_Options;
DiskState* g_DiskState;

void TestFilter(FeedItemInfo* feedItemInfo, const char* filterDef, FeedItemInfo::EMatchStatus expectedMatch)
{
Expand Down
52 changes: 52 additions & 0 deletions tests/feed/main.cpp
Original file line number Diff line number Diff line change
@@ -1,2 +1,54 @@
/*
* This file is part of nzbget. See <https://nzbget.com>.
*
* Copyright (C) 2024 Denis <denis@nzbget.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

#include "nzbget.h"

#define BOOST_TEST_MODULE "FeedTest"
#include <boost/test/included/unit_test.hpp>

#include "Log.h"
#include "Options.h"
#include "WorkState.h"
#include "DiskState.h"

char* (*g_EnvironmentVariables)[];
Log* g_Log;
WorkState* g_WorkState;
Options* g_Options;
DiskState* g_DiskState;

struct InitGlobals
{
InitGlobals()
{
g_EnvironmentVariables = nullptr;
g_Log = new Log();
g_WorkState = new WorkState();
g_DiskState = new DiskState();
}

~InitGlobals()
{
delete g_Log;
delete g_WorkState;
delete g_DiskState;
}
};

BOOST_GLOBAL_FIXTURE(InitGlobals);
12 changes: 1 addition & 11 deletions tests/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,6 @@ add_executable(MainTests
${CMAKE_SOURCE_DIR}/daemon/nntp/NewsServer.cpp
)
target_link_libraries(MainTests PRIVATE ${LIBS})
target_include_directories(MainTests PRIVATE
${CMAKE_SOURCE_DIR}/daemon/main
${CMAKE_SOURCE_DIR}/daemon/util
${CMAKE_SOURCE_DIR}/daemon/remote
${CMAKE_SOURCE_DIR}/daemon/queue
${CMAKE_SOURCE_DIR}/daemon/feed
${CMAKE_SOURCE_DIR}/daemon/nntp
${CMAKE_SOURCE_DIR}/lib/yencode
${CMAKE_SOURCE_DIR}/lib/regex
${INCLUDES}
)
target_include_directories(MainTests PRIVATE ${INCLUDES})

add_test(NAME MainTests COMMAND $<TARGET_FILE:MainTests> --log_level=message)
Loading

0 comments on commit e5a2837

Please sign in to comment.