From e8ba5dafc63de65ed8a469353b808e391633f0fc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Petr=20Mr=C3=A1zek?= Date: Wed, 13 Apr 2016 01:24:55 +0200 Subject: [PATCH] NOISSUE remove dead unit tests and reorganize CMake code related to unit tests --- cmake/UnitTest.cmake | 49 ++++++++ {tests => cmake/UnitTest}/TestUtil.h | 3 +- cmake/UnitTest/generate_test_data.cmake | 26 ++++ {tests => cmake/UnitTest}/test.manifest | 0 {tests => cmake/UnitTest}/test.rc | 0 tests/CMakeLists.txt | 128 +++++++++++--------- tests/copy_tests.cmake | 13 -- tests/data/1.json | 6 +- tests/data/2.json | 4 +- tests/data/channels.json | 4 +- tests/test_config.h.in | 3 - tests/tst_BaseWonkoEntityLocalLoadTask.cpp | 15 --- tests/tst_BaseWonkoEntityRemoteLoadTask.cpp | 15 --- tests/tst_UpdateChecker.cpp | 5 +- tests/tst_WonkoVersionList.cpp | 15 --- 15 files changed, 159 insertions(+), 127 deletions(-) create mode 100644 cmake/UnitTest.cmake rename {tests => cmake/UnitTest}/TestUtil.h (89%) create mode 100644 cmake/UnitTest/generate_test_data.cmake rename {tests => cmake/UnitTest}/test.manifest (100%) rename {tests => cmake/UnitTest}/test.rc (100%) delete mode 100644 tests/copy_tests.cmake delete mode 100644 tests/test_config.h.in delete mode 100644 tests/tst_BaseWonkoEntityLocalLoadTask.cpp delete mode 100644 tests/tst_BaseWonkoEntityRemoteLoadTask.cpp delete mode 100644 tests/tst_WonkoVersionList.cpp diff --git a/cmake/UnitTest.cmake b/cmake/UnitTest.cmake new file mode 100644 index 00000000..1cbb2b60 --- /dev/null +++ b/cmake/UnitTest.cmake @@ -0,0 +1,49 @@ +find_package(Qt5Test REQUIRED) + +set(TEST_RESOURCE_PATH ${CMAKE_CURRENT_LIST_DIR}) + +message(${TEST_RESOURCE_PATH}) + +function(add_unit_test name) + set(options "") + set(oneValueArgs DATA) + set(multiValueArgs SOURCES LIBS QT) + + cmake_parse_arguments(OPT "${options}" "${oneValueArgs}" "${multiValueArgs}" ${ARGN} ) + + if(WIN32) + add_executable(tst_${name} ${OPT_SOURCES} ${TEST_RESOURCE_PATH}/UnitTest/test.rc) + else() + add_executable(tst_${name} ${OPT_SOURCES}) + endif() + + if(NOT "${OPT_DATA}" STREQUAL "") + set(TEST_DATA_PATH "${CMAKE_CURRENT_BINARY_DIR}/data") + set(TEST_DATA_PATH_SRC "${CMAKE_CURRENT_SOURCE_DIR}/${OPT_DATA}") + message("From ${TEST_DATA_PATH_SRC} to ${TEST_DATA_PATH}") + string(REGEX REPLACE "[/\\:]" "_" DATA_TARGET_NAME "${TEST_DATA_PATH_SRC}") + if(UNIX) + # on unix we get the third / from the filename + set(TEST_DATA_URL "file://${TEST_DATA_PATH}") + else() + # we don't on windows, so we have to add it ourselves + set(TEST_DATA_URL "file:///${TEST_DATA_PATH}") + endif() + if(NOT TARGET "${DATA_TARGET_NAME}") + add_custom_target(${DATA_TARGET_NAME}) + add_dependencies(tst_${name} ${DATA_TARGET_NAME}) + add_custom_command( + TARGET ${DATA_TARGET_NAME} + COMMAND ${CMAKE_COMMAND} "-DTEST_DATA_URL=${TEST_DATA_URL}" -DSOURCE=${TEST_DATA_PATH_SRC} -DDESTINATION=${TEST_DATA_PATH} -P ${TEST_RESOURCE_PATH}/UnitTest/generate_test_data.cmake + WORKING_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} + ) + endif() + endif() + + target_link_libraries(tst_${name} ${OPT_LIBS}) + qt5_use_modules(tst_${name} Test ${OPT_QT}) + + target_include_directories(tst_${name} PRIVATE "${TEST_RESOURCE_PATH}/UnitTest/") + + add_test(NAME ${name} COMMAND tst_${name}) +endfunction() diff --git a/tests/TestUtil.h b/cmake/UnitTest/TestUtil.h similarity index 89% rename from tests/TestUtil.h rename to cmake/UnitTest/TestUtil.h index 5b0741b8..84f18a2e 100644 --- a/tests/TestUtil.h +++ b/cmake/UnitTest/TestUtil.h @@ -5,7 +5,8 @@ #include #include -#include "test_config.h" +#define expandstr(s) expandstr2(s) +#define expandstr2(s) #s class TestsInternal { diff --git a/cmake/UnitTest/generate_test_data.cmake b/cmake/UnitTest/generate_test_data.cmake new file mode 100644 index 00000000..9de7410b --- /dev/null +++ b/cmake/UnitTest/generate_test_data.cmake @@ -0,0 +1,26 @@ +# Copy files from source directory to destination directory, substituting any +# variables. Create destination directory if it does not exist. + +function(configure_files srcDir destDir) + message(STATUS "Configuring directory ${destDir} from ${srcDir}") + make_directory(${destDir}) + + file(GLOB templateFiles RELATIVE ${srcDir} ${srcDir}/*) + foreach(templateFile ${templateFiles}) + set(srcTemplatePath ${srcDir}/${templateFile}) + if(NOT IS_DIRECTORY ${srcTemplatePath}) + message(STATUS "Configuring file ${templateFile}") + configure_file( + ${srcTemplatePath} + ${destDir}/${templateFile} + @ONLY + NEWLINE_STYLE LF + ) + else() + message(STATUS "Recursing? ${srcTemplatePath}") + configure_files("${srcTemplatePath}" "${destDir}/${templateFile}") + endif() + endforeach() +endfunction() + +configure_files(${SOURCE} ${DESTINATION}) \ No newline at end of file diff --git a/tests/test.manifest b/cmake/UnitTest/test.manifest similarity index 100% rename from tests/test.manifest rename to cmake/UnitTest/test.manifest diff --git a/tests/test.rc b/cmake/UnitTest/test.rc similarity index 100% rename from tests/test.rc rename to cmake/UnitTest/test.rc diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 12fa4108..667aecd1 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,66 +1,82 @@ -# run the unit tests with `make test` -find_package(Qt5Test) +include(UnitTest) -add_custom_target(test_data) +add_unit_test(gradlespecifier + SOURCES tst_gradlespecifier.cpp + LIBS MultiMC_logic + ) -unset(MultiMC_TESTS) -macro(add_unit_test name) - unset(srcs) - foreach(arg ${testname} ${ARGN}) - list(APPEND srcs ${CMAKE_CURRENT_SOURCE_DIR}/${arg}) - endforeach() - if(WIN32) - list(APPEND srcs ${CMAKE_CURRENT_SOURCE_DIR}/test.rc) - endif() - add_executable(tst_${name} ${srcs}) - add_dependencies(tst_${name} test_data) - target_link_libraries(tst_${name} MultiMC_logic) - qt5_use_modules(tst_${name} Test Core Network) - list(APPEND MultiMC_TESTS tst_${name}) - add_test(NAME ${name} COMMAND tst_${name}) -endmacro() +add_unit_test(userutils + SOURCES tst_userutils.cpp + LIBS MultiMC_logic + ) -# Tests BEGIN # +add_unit_test(modutils + SOURCES tst_modutils.cpp + LIBS MultiMC_logic + ) -add_unit_test(gradlespecifier tst_gradlespecifier.cpp) -add_unit_test(userutils tst_userutils.cpp) -add_unit_test(modutils tst_modutils.cpp) -add_unit_test(inifile tst_inifile.cpp) -add_unit_test(FileSystem tst_FileSystem.cpp) -add_unit_test(Library tst_Library.cpp) -add_unit_test(UpdateChecker tst_UpdateChecker.cpp) -add_unit_test(DownloadTask tst_DownloadTask.cpp) -add_unit_test(filematchers tst_filematchers.cpp) -add_unit_test(ModList tst_ModList.cpp) -# add_unit_test(Resource tst_Resource.cpp) -add_unit_test(GZip tst_GZip.cpp) -add_unit_test(JavaVersion tst_JavaVersion.cpp) -add_unit_test(ParseUtils tst_ParseUtils.cpp) -add_unit_test(MojangVersionFormat tst_MojangVersionFormat.cpp) -add_unit_test(BaseWonkoEntityLocalLoadTask tst_BaseWonkoEntityLocalLoadTask.cpp) -add_unit_test(BaseWonkoEntityRemoteLoadTask tst_BaseWonkoEntityRemoteLoadTask.cpp) -add_unit_test(WonkoVersionList tst_WonkoVersionList.cpp) -add_unit_test(WonkoIndex tst_WonkoIndex.cpp) +add_unit_test(inifile + SOURCES tst_inifile.cpp + LIBS MultiMC_logic + ) -# Tests END # +add_unit_test(FileSystem + SOURCES tst_FileSystem.cpp + LIBS MultiMC_logic + ) +add_unit_test(Library + SOURCES tst_Library.cpp + LIBS MultiMC_logic + ) -set(MultiMC_TEST_DATA_PATH "${CMAKE_CURRENT_BINARY_DIR}/data") -set(MultiMC_TEST_DATA_PATH_SOURCE "${CMAKE_CURRENT_SOURCE_DIR}/data") -set(MultiMC_TEST_DATA_PATH_SOURCE_RAW "${CMAKE_CURRENT_SOURCE_DIR}/data_raw") +add_unit_test(UpdateChecker + SOURCES tst_UpdateChecker.cpp + LIBS MultiMC_logic + DATA data + ) -if(UNIX) - # on unix we get the third / from the filename - set(MultiMC_TEST_DATA_URL "file://${MultiMC_TEST_DATA_PATH}") -else() - # we don't on windows, so we have to add it ourselves - set(MultiMC_TEST_DATA_URL "file:///${MultiMC_TEST_DATA_PATH}") -endif() +add_unit_test(DownloadTask + SOURCES tst_DownloadTask.cpp + LIBS MultiMC_logic + DATA data_raw + ) -# clean and replace test data -add_custom_command( - TARGET test_data - COMMAND ${CMAKE_COMMAND} -DMultiMC_TEST_DATA_URL=${MultiMC_TEST_DATA_URL} -DMultiMC_TEST_DATA_PATH=${MultiMC_TEST_DATA_PATH} -DMultiMC_TEST_DATA_PATH_SOURCE=${MultiMC_TEST_DATA_PATH_SOURCE} -DMultiMC_TEST_DATA_PATH_SOURCE_RAW=${MultiMC_TEST_DATA_PATH_SOURCE_RAW} -P ${CMAKE_CURRENT_SOURCE_DIR}/copy_tests.cmake -) +add_unit_test(filematchers + SOURCES tst_filematchers.cpp + LIBS MultiMC_logic + ) -configure_file(test_config.h.in test_config.h @ONLY) +add_unit_test(ModList + SOURCES tst_ModList.cpp + LIBS MultiMC_logic + ) + +# add_unit_test(Resource +# SOURCES tst_Resource.cpp +# ) + +add_unit_test(GZip + SOURCES tst_GZip.cpp + LIBS MultiMC_logic + ) + +add_unit_test(JavaVersion + SOURCES tst_JavaVersion.cpp + LIBS MultiMC_logic + ) + +add_unit_test(ParseUtils + SOURCES tst_ParseUtils.cpp + LIBS MultiMC_logic + ) + +add_unit_test(MojangVersionFormat + SOURCES tst_MojangVersionFormat.cpp + LIBS MultiMC_logic + ) + +add_unit_test(WonkoIndex + SOURCES tst_WonkoIndex.cpp + LIBS MultiMC_logic + ) diff --git a/tests/copy_tests.cmake b/tests/copy_tests.cmake deleted file mode 100644 index 0af7f730..00000000 --- a/tests/copy_tests.cmake +++ /dev/null @@ -1,13 +0,0 @@ -file(GLOB data_files "${MultiMC_TEST_DATA_PATH_SOURCE}/*") -foreach(data_file ${data_files}) - get_filename_component(filename ${data_file} NAME) - configure_file( - ${data_file} - ${MultiMC_TEST_DATA_PATH}/${filename} - @ONLY - NEWLINE_STYLE LF - ) -endforeach() - -file(GLOB raw_data_files "${MultiMC_TEST_DATA_PATH_SOURCE_RAW}/*") -file(COPY ${raw_data_files} DESTINATION ${MultiMC_TEST_DATA_PATH}/) diff --git a/tests/data/1.json b/tests/data/1.json index 7eda1618..3dd189e5 100644 --- a/tests/data/1.json +++ b/tests/data/1.json @@ -8,7 +8,7 @@ "Sources": [ { "SourceType": "http", - "Url": "@MultiMC_TEST_DATA_URL@/fileOneA" + "Url": "@TEST_DATA_URL@/fileOneA" } ], "Executable": true, @@ -20,7 +20,7 @@ "Sources": [ { "SourceType": "http", - "Url": "@MultiMC_TEST_DATA_URL@/fileTwo" + "Url": "@TEST_DATA_URL@/fileTwo" } ], "Executable": false, @@ -32,7 +32,7 @@ "Sources": [ { "SourceType": "http", - "Url": "@MultiMC_TEST_DATA_URL@/fileThree" + "Url": "@TEST_DATA_URL@/fileThree" } ], "Executable": false, diff --git a/tests/data/2.json b/tests/data/2.json index de7d0e07..a7ba7029 100644 --- a/tests/data/2.json +++ b/tests/data/2.json @@ -8,7 +8,7 @@ "Sources": [ { "SourceType": "http", - "Url": "@MultiMC_TEST_DATA_URL@/fileOneB" + "Url": "@TEST_DATA_URL@/fileOneB" } ], "Executable": true, @@ -20,7 +20,7 @@ "Sources": [ { "SourceType": "http", - "Url": "@MultiMC_TEST_DATA_URL@/fileTwo" + "Url": "@TEST_DATA_URL@/fileTwo" } ], "Executable": false, diff --git a/tests/data/channels.json b/tests/data/channels.json index 10eede82..b46c64c8 100644 --- a/tests/data/channels.json +++ b/tests/data/channels.json @@ -5,13 +5,13 @@ "id": "develop", "name": "Develop", "description": "The channel called \"develop\"", - "url": "@MultiMC_TEST_DATA_URL@" + "url": "@TEST_DATA_URL@" }, { "id": "stable", "name": "Stable", "description": "It's stable at least", - "url": "@MultiMC_TEST_DATA_URL@" + "url": "@TEST_DATA_URL@" }, { "id": "42", diff --git a/tests/test_config.h.in b/tests/test_config.h.in deleted file mode 100644 index cf59f3e2..00000000 --- a/tests/test_config.h.in +++ /dev/null @@ -1,3 +0,0 @@ -#pragma once - -#define MultiMC_TEST_DATA_URL "@MultiMC_TEST_DATA_URL@" diff --git a/tests/tst_BaseWonkoEntityLocalLoadTask.cpp b/tests/tst_BaseWonkoEntityLocalLoadTask.cpp deleted file mode 100644 index 74da222a..00000000 --- a/tests/tst_BaseWonkoEntityLocalLoadTask.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "TestUtil.h" - -#include "wonko/tasks/BaseWonkoEntityLocalLoadTask.h" - -class BaseWonkoEntityLocalLoadTaskTest : public QObject -{ - Q_OBJECT -private -slots: -}; - -QTEST_GUILESS_MAIN(BaseWonkoEntityLocalLoadTaskTest) - -#include "tst_BaseWonkoEntityLocalLoadTask.moc" diff --git a/tests/tst_BaseWonkoEntityRemoteLoadTask.cpp b/tests/tst_BaseWonkoEntityRemoteLoadTask.cpp deleted file mode 100644 index 3a12e04e..00000000 --- a/tests/tst_BaseWonkoEntityRemoteLoadTask.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "TestUtil.h" - -#include "wonko/tasks/BaseWonkoEntityRemoteLoadTask.h" - -class BaseWonkoEntityRemoteLoadTaskTest : public QObject -{ - Q_OBJECT -private -slots: -}; - -QTEST_GUILESS_MAIN(BaseWonkoEntityRemoteLoadTaskTest) - -#include "tst_BaseWonkoEntityRemoteLoadTask.moc" diff --git a/tests/tst_UpdateChecker.cpp b/tests/tst_UpdateChecker.cpp index de1ad718..3a0d3781 100644 --- a/tests/tst_UpdateChecker.cpp +++ b/tests/tst_UpdateChecker.cpp @@ -8,6 +8,7 @@ Q_DECLARE_METATYPE(UpdateChecker::ChannelListEntry) bool operator==(const UpdateChecker::ChannelListEntry &e1, const UpdateChecker::ChannelListEntry &e2) { + qDebug() << e1.url << "vs" << e2.url; return e1.id == e2.id && e1.name == e2.name && e1.description == e2.description && @@ -76,8 +77,8 @@ slots: << true << true << (QList() - << UpdateChecker::ChannelListEntry{"develop", "Develop", "The channel called \"develop\"", MultiMC_TEST_DATA_URL} - << UpdateChecker::ChannelListEntry{"stable", "Stable", "It's stable at least", MultiMC_TEST_DATA_URL} + << UpdateChecker::ChannelListEntry{"develop", "Develop", "The channel called \"develop\"", findTestDataUrl("tests/data")} + << UpdateChecker::ChannelListEntry{"stable", "Stable", "It's stable at least", findTestDataUrl("tests/data")} << UpdateChecker::ChannelListEntry{"42", "The Channel", "This is the channel that is going to answer all of your questions", "https://dent.me/tea"}); } void tst_ChannelListParsing() diff --git a/tests/tst_WonkoVersionList.cpp b/tests/tst_WonkoVersionList.cpp deleted file mode 100644 index 7cb21df7..00000000 --- a/tests/tst_WonkoVersionList.cpp +++ /dev/null @@ -1,15 +0,0 @@ -#include -#include "TestUtil.h" - -#include "wonko/WonkoVersionList.h" - -class WonkoVersionListTest : public QObject -{ - Q_OBJECT -private -slots: -}; - -QTEST_GUILESS_MAIN(WonkoVersionListTest) - -#include "tst_WonkoVersionList.moc"