Merge pull request #21995 from chennes/moveBackupPolicy

Extract BackupPolicy class into its own file to facilitate automated testing
This commit is contained in:
Benjamin Nauck
2025-07-12 17:58:14 +02:00
committed by GitHub
35 changed files with 683 additions and 398 deletions

View File

@@ -41,7 +41,7 @@ if(MSVC)
endif()
if(WIN32)
add_definitions(-DCOIN_DLL -D_USE_MATH_DEFINES)
add_definitions(-DCOIN_DLL)
endif(WIN32)
if(NOT BUILD_DYNAMIC_LINK_PYTHON)
@@ -78,69 +78,83 @@ function(setup_qt_test)
endforeach()
endfunction()
# Add test executables here
set(TestExecutables
Tests_run
App_tests_run
Base_tests_run
Misc_tests_run
Zipios_tests_run
)
# NOTE: The following tests don't yet run on Windows because they can't find the *.pyd files needed by each FreeCAD
# module.
if(BUILD_GUI)
list (APPEND TestExecutables Gui_tests_run)
endif()
if(BUILD_ASSEMBLY)
list (APPEND TestExecutables Assembly_tests_run)
list (APPEND TestExecutables Assembly_tests_run)
endif(BUILD_ASSEMBLY)
if(BUILD_MATERIAL)
list (APPEND TestExecutables Material_tests_run)
list (APPEND TestExecutables Material_tests_run)
endif(BUILD_MATERIAL)
if(BUILD_MEASURE)
list (APPEND TestExecutables Measure_tests_run)
list (APPEND TestExecutables Measure_tests_run)
endif(BUILD_MEASURE)
if(BUILD_MESH)
list (APPEND TestExecutables Mesh_tests_run)
list (APPEND TestExecutables Mesh_tests_run)
endif(BUILD_MESH)
if(BUILD_MESH_PART)
list (APPEND TestExecutables MeshPart_tests_run)
list (APPEND TestExecutables MeshPart_tests_run)
endif(BUILD_MESH_PART)
if(BUILD_PART)
list (APPEND TestExecutables Part_tests_run)
list (APPEND TestExecutables Part_tests_run)
endif(BUILD_PART)
if(BUILD_PART_DESIGN)
list (APPEND TestExecutables PartDesign_tests_run)
endif(BUILD_PART_DESIGN)
if(BUILD_POINTS)
list (APPEND TestExecutables Points_tests_run)
list (APPEND TestExecutables Points_tests_run)
endif(BUILD_POINTS)
if(BUILD_SKETCHER)
list (APPEND TestExecutables Sketcher_tests_run)
list (APPEND TestExecutables Sketcher_tests_run)
endif(BUILD_SKETCHER)
if(BUILD_SPREADSHEET)
list (APPEND TestExecutables Spreadsheet_tests_run)
list (APPEND TestExecutables Spreadsheet_tests_run)
endif()
if(BUILD_START)
list (APPEND TestExecutables Start_tests_run)
list (APPEND TestExecutables Start_tests_run)
endif()
# -------------------------
foreach (exe ${TestExecutables})
add_executable(${exe})
endforeach()
if ( NOT FREECAD_USE_EXTERNAL_GTEST )
if(WIN32)
set(BUILD_SHARED_LIBS OFF)
endif()
add_subdirectory(lib)
endif()
add_subdirectory(src)
target_link_libraries(Tests_run
gtest_main
gmock_main
${Google_Tests_LIBS}
FreeCADApp
FreeCADGui
)
include(GoogleTest)
# discovers tests by asking the compiled test executable to enumerate its tests
set(CMAKE_GTEST_DISCOVER_TESTS_DISCOVERY_MODE PRE_TEST)
foreach (exe ${TestExecutables})
if(WIN32)
# On Windows the test executables need to be in the same place as all the other DLLs that are getting built
if(CMAKE_CONFIGURATION_TYPES)
# Visual Studio solution file, supports switching configs on the fly in the IDE
set(OUTPUT_DIR ${CMAKE_BINARY_DIR}/bin)
foreach(OUTPUT_CONFIG Debug Release RelWithDebInfo MinSizeRel)
string(TOUPPER "${OUTPUT_CONFIG}" UPPER_CONFIG)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY_${UPPER_CONFIG} ${OUTPUT_DIR}/${OUTPUT_CONFIG})
endforeach()
else()
# Ninja (usually), e.g. when using CLion with MSVC toolchain, etc. is actually single-config
set_target_properties(${exe} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set_target_properties(${exe} PROPERTIES LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
endif()
else()
set_target_properties(${exe} PROPERTIES RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/tests)
endif()
gtest_discover_tests(${exe})
endforeach()

View File

@@ -0,0 +1,114 @@
// SPDX-License-Identifier: LGPL-2.1-or-later
/****************************************************************************
* Copyright (c) 2025 The FreeCAD project association AISBL *
* *
* This file is part of FreeCAD. *
* *
* FreeCAD is free software: you can redistribute it and/or modify it *
* under the terms of the GNU Lesser General Public License as *
* published by the Free Software Foundation, either version 2.1 of the *
* License, or (at your option) any later version. *
* *
* FreeCAD 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 *
* Lesser General Public License for more details. *
* *
* You should have received a copy of the GNU Lesser General Public *
* License along with FreeCAD. If not, see *
* <https://www.gnu.org/licenses/>. *
* *
***************************************************************************/
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "InitApplication.h"
#include <App/BackupPolicy.h>
#include <filesystem>
#include <fstream>
#include <random>
#include <string>
class BackupPolicyTest: public ::testing::Test
{
protected:
static void SetUpTestSuite()
{
tests::initApplication();
}
void SetUp() override
{
_tempDir =
std::filesystem::temp_directory_path() / ("fc_backup_policy-" + randomString(16));
std::filesystem::create_directory(_tempDir);
}
void TearDown() override
{
std::filesystem::remove_all(_tempDir);
}
void apply(const std::string& sourcename, const std::string& targetname)
{
_policy.apply(sourcename, targetname);
}
void setPolicyTerms(App::BackupPolicy::Policy p, int count, bool useExt, const std::string& fmt)
{
_policy.setPolicy(p);
_policy.setNumberOfFiles(count);
_policy.useBackupExtension(useExt);
_policy.setDateFormat(fmt);
}
// Create a named temporary file: returns the full path to the new file. Deleted by the TearDown
// method at the end of the test.
std::filesystem::path createTempFile(const std::string& filename)
{
std::filesystem::path p = _tempDir / filename;
std::ofstream fileStream(p.string());
fileStream << "Test data";
fileStream.close();
return p;
}
private:
std::string randomString(size_t length)
{
static constexpr std::string_view chars = "0123456789"
"abcdefghijklmnopqrstuvwxyz"
"ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<> dis(0, static_cast<int>(chars.size()) - 1);
std::string result;
result.reserve(length);
std::ranges::generate_n(std::back_inserter(result), length, [&]() {
return chars[dis(gen)];
});
return result;
}
App::BackupPolicy _policy;
std::filesystem::path _tempDir;
};
TEST_F(BackupPolicyTest, StandardSourceDoesNotExist)
{
// Arrange
setPolicyTerms(App::BackupPolicy::Policy::Standard, 1, true, "%Y-%m-%d_%H-%M-%S");
// Act & Assert
EXPECT_THROW(apply("nonexistent.fcstd", "backup.fcstd"), Base::FileException);
}

View File

@@ -1,7 +1,6 @@
target_compile_definitions(Tests_run PRIVATE DATADIR="${CMAKE_SOURCE_DIR}/data")
target_sources(Tests_run PRIVATE
add_executable(App_tests_run
Application.cpp
BackupPolicy.cpp
Branding.cpp
ComplexGeoData.cpp
Document.cpp
@@ -24,3 +23,12 @@ target_sources(Tests_run PRIVATE
VarSet.cpp
VRMLObject.cpp
)
target_compile_definitions(App_tests_run PRIVATE DATADIR="${CMAKE_SOURCE_DIR}/data")
target_link_libraries(App_tests_run PRIVATE
GTest::gtest_main
GTest::gmock_main
${Google_Tests_LIBS}
FreeCADApp
)

View File

@@ -1,4 +1,4 @@
target_sources(Tests_run PRIVATE
add_executable(Base_tests_run
Axis.cpp
Base64.cpp
Bitmask.cpp
@@ -31,3 +31,10 @@ target_sources(Tests_run PRIVATE
)
setup_qt_test(InventorBuilder)
target_link_libraries(Base_tests_run PRIVATE
GTest::gtest_main
GTest::gmock_main
${Google_Tests_LIBS}
FreeCADApp
)

View File

@@ -1,5 +1,5 @@
# Standard C++ GTest tests
target_sources(Tests_run PRIVATE
add_executable(Gui_tests_run
Assistant.cpp
Camera.cpp
StyleParameters/StyleParametersApplicationTest.cpp
@@ -9,3 +9,12 @@ target_sources(Tests_run PRIVATE
# Qt tests
setup_qt_test(QuantitySpinBox)
target_link_libraries(Gui_tests_run PRIVATE
GTest::gtest_main
GTest::gmock_main
${Google_Tests_LIBS}
FreeCADApp
FreeCADGui
)

View File

@@ -1,3 +1,10 @@
target_sources(Tests_run PRIVATE
add_executable(Misc_tests_run
fmt.cpp
)
target_link_libraries(Misc_tests_run PRIVATE
GTest::gtest_main
GTest::gmock_main
${Google_Tests_LIBS}
fmt::fmt
)

View File

@@ -1,5 +1,5 @@
#include "fmt/format.h"
#include "fmt/printf.h"
#include <fmt/format.h>
#include <fmt/printf.h>
#include <gtest/gtest.h>
#include <stdexcept>

View File

@@ -1,3 +1,3 @@
target_sources(Assembly_tests_run PRIVATE
add_executable(Assembly_tests_run
AssemblyObject.cpp
)

View File

@@ -1,3 +1,4 @@
add_subdirectory(App)
if (NOT FREECAD_USE_EXTERNAL_ONDSELSOLVER)
target_include_directories(Assembly_tests_run PUBLIC
@@ -10,5 +11,3 @@ target_link_libraries(Assembly_tests_run
${Google_Tests_LIBS}
Assembly
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Material_tests_run PRIVATE
add_executable(Material_tests_run
TestMaterialCards.cpp
TestMaterialFilter.cpp
TestMaterialProperties.cpp

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Material_tests_run
gtest_main
${Google_Tests_LIBS}
Materials
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Measure_tests_run PRIVATE
add_executable(Measure_tests_run
MeasureDistance.cpp
)

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Measure_tests_run
gtest_main
${Google_Tests_LIBS}
Measure
)
add_subdirectory(App)

View File

@@ -1,9 +1,9 @@
target_compile_definitions(Mesh_tests_run PRIVATE DATADIR="${CMAKE_SOURCE_DIR}/data")
target_sources(Mesh_tests_run PRIVATE
add_executable(Mesh_tests_run
Core/KDTree.cpp
Exporter.cpp
Importer.cpp
Mesh.cpp
MeshFeature.cpp
)
target_compile_definitions(Mesh_tests_run PRIVATE DATADIR="${CMAKE_SOURCE_DIR}/data")

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Mesh_tests_run
gtest_main
${Google_Tests_LIBS}
Mesh
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(MeshPart_tests_run PRIVATE
add_executable(MeshPart_tests_run
MeshPart.cpp
)

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(MeshPart_tests_run
gtest_main
${Google_Tests_LIBS}
MeshPart
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Part_tests_run PRIVATE
add_executable(Part_tests_run
Attacher.cpp
AttachExtension.cpp
BRepMesh.cpp

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Part_tests_run
gtest_main
${Google_Tests_LIBS}
Part
)
add_subdirectory(App)

View File

@@ -1,5 +1,5 @@
target_sources(PartDesign_tests_run PRIVATE
add_executable(PartDesign_tests_run
BackwardCompatibility.cpp
DatumPlane.cpp
ShapeBinder.cpp

View File

@@ -1,8 +1,8 @@
add_subdirectory(App)
target_link_libraries(PartDesign_tests_run
gtest_main
${Google_Tests_LIBS}
PartDesign
Sketcher
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Points_tests_run PRIVATE
add_executable(Points_tests_run
Points.cpp
PointsFeature.cpp
)

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Points_tests_run
gtest_main
${Google_Tests_LIBS}
Points
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Sketcher_tests_run PRIVATE
add_executable(Sketcher_tests_run
SketcherTestHelpers.cpp
SketchObject.cpp
SketchObjectChanges.cpp

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Sketcher_tests_run
gtest_main
${Google_Tests_LIBS}
Sketcher
)
add_subdirectory(App)

View File

@@ -1,4 +1,4 @@
target_sources(Spreadsheet_tests_run PRIVATE
add_executable(Spreadsheet_tests_run
PropertySheet.cpp
RenameProperty.cpp
)

View File

@@ -1,7 +1,7 @@
add_subdirectory(App)
target_link_libraries(Spreadsheet_tests_run
gtest_main
${Google_Tests_LIBS}
Spreadsheet
)
add_subdirectory(App)

View File

@@ -1,8 +1,6 @@
target_sources(
Start_tests_run
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/FileUtilities.cpp
${CMAKE_CURRENT_SOURCE_DIR}/ThumbnailSource.cpp
add_executable(Start_tests_run
FileUtilities.cpp
ThumbnailSource.cpp
)
target_include_directories(

View File

@@ -1,3 +1,4 @@
add_subdirectory(App)
target_include_directories(Start_tests_run PUBLIC
${Python3_INCLUDE_DIRS}
@@ -8,5 +9,3 @@ target_link_libraries(Start_tests_run
${Google_Tests_LIBS}
Start
)
add_subdirectory(App)

View File

@@ -1,6 +1,18 @@
target_sources(
Tests_run
PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/collectioncollection.cpp
${CMAKE_CURRENT_SOURCE_DIR}/zipfile.cpp
add_executable(Zipios_tests_run
collectioncollection.cpp
zipfile.cpp
)
target_link_libraries(Zipios_tests_run PRIVATE
GTest::gtest_main
GTest::gmock_main
${Google_Tests_LIBS}
FreeCADApp
)
target_include_directories(
Zipios_tests_run PRIVATE
${ZIPIOS_INCLUDES}
${ZLIB_INCLUDE_DIR}
)