Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 0 additions & 10 deletions .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ Files: misc/dfm-*/dfm-*Config.cmake.in
Copyright: UnionTech Software Technology Co., Ltd.
License: GPL-3.0-or-later

# cpp-stub
Files: tests/3rdparty/utils/cpp-stub/*
Copyright: 2019 coolxv
License: MIT

# stub-ext
Files: tests/3rdparty/utils/stub-ext/*
Copyright: 2020 Zhang Yu
License: MIT

# udfclient
Files: src/dfm-burn/3rdparty/udfclient/*
Copyright: Reinoud Zandijk <reinoud@netbsd.org>
Expand Down
7 changes: 7 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ endif ()
include(GNUInstallDirs)

add_subdirectory(${PROJECT_SOURCE_DIR}/src)

# Unit tests (requires Qt Test, enabled by default)
# option(BUILD_UNIT_TESTS is defined in autotests/CMakeLists.txt)
add_subdirectory(${PROJECT_SOURCE_DIR}/autotests)

# Legacy tests (temporarily disabled)
# add_subdirectory(${PROJECT_SOURCE_DIR}/tests)
9 changes: 0 additions & 9 deletions LICENSES/MIT.txt

This file was deleted.

40 changes: 40 additions & 0 deletions autotests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Unit tests for util-dfm using Qt Test
cmake_minimum_required(VERSION 3.10)

project(autotests)

# Enable/disable building of unit tests (default: ON)
option(BUILD_UNIT_TESTS "Build unit tests" ON)

if(NOT BUILD_UNIT_TESTS)
return()
endif()

message(STATUS "Building unit tests")

# Find Qt Test component based on Qt version
if(QT_VERSION_MAJOR EQUAL 6)
find_package(Qt6 REQUIRED COMPONENTS Test Core)
else()
find_package(Qt5 REQUIRED COMPONENTS Test Core)
endif()

# Common test configuration
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DQT_DEBUG")

# Enable testing
enable_testing()

# Add test subdirs for each module
add_subdirectory(dfm-io-tests)
add_subdirectory(dfm-mount-tests)
add_subdirectory(dfm-burn-tests)
add_subdirectory(dfm-search-tests)

# Add top-level test target (run all tests)
add_custom_target(test-all
COMMAND ${CMAKE_CTEST_COMMAND} --output-on-failure
DEPENDS dfm-io-test dfm-mount-test dfm-burn-test dfm-search-test
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
COMMENT "Running all unit tests..."
)
129 changes: 129 additions & 0 deletions autotests/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Unit Tests for util-dfm

This directory contains unit tests for the util-dfm libraries using the Qt Test framework.

## Directory Structure

```
autotests/
├── CMakeLists.txt # Main test configuration
├── README.md # This file
├── dfm-io-tests/ # Tests for dfm-io library
│ ├── CMakeLists.txt
│ └── tst_dfm_io.cpp
├── dfm-mount-tests/ # Tests for dfm-mount library
│ ├── CMakeLists.txt
│ └── tst_dfm_mount.cpp
├── dfm-burn-tests/ # Tests for dfm-burn library
│ ├── CMakeLists.txt
│ └── tst_dfm_burn.cpp
└── dfm-search-tests/ # Tests for dfm-search library
├── CMakeLists.txt
└── tst_dfm_search.cpp
```

## Building with Tests

By default, unit tests are enabled. To build with tests:

```bash
mkdir build && cd build
cmake -DCMAKE_INSTALL_PREFIX=/usr -j$(nproc) ..
cmake --build .
```

To disable building of tests:

```bash
cmake -DCMAKE_INSTALL_PREFIX=/usr -DBUILD_UNIT_TESTS=OFF -j$(nproc) ..
```

## Running Tests

### Run all tests

```bash
# From build directory
ctest --output-on-failure

# Or use the custom target
make test-all
```

### Run individual module tests

```bash
# From build directory
./bin/dfm-io-test
./bin/dfm-mount-test
./bin/dfm-burn-test
./bin/dfm-search-test
```

### Run with verbose output

```bash
ctest --verbose

# Or for individual tests
./bin/dfm-io-test -vs2
```

## Writing New Tests

1. Choose the appropriate test subdirectory for the module you're testing
2. Create a new test file following the naming convention `tst_*.cpp`
3. Include `<QTest>` header
4. Create a test class inheriting from `QObject` with `Q_OBJECT` macro
5. Use `QTEST_MAIN()` macro at the end of your test file
6. Update the corresponding CMakeLists.txt to include your new file

Example test:

```cpp
#include <QTest>

class tst_MyTest : public QObject
{
Q_OBJECT

private Q_SLOTS:
void initTestCase();
void cleanupTestCase();
void myTestFunction();
};

void tst_MyTest::initTestCase()
{
// Setup that runs once before all tests
}

void tst_MyTest::cleanupTestCase()
{
// Cleanup that runs once after all tests
}

void tst_MyTest::myTestFunction()
{
QVERIFY(true); // Verifies condition is true
QCOMPARE(1 + 1, 2); // Verifies equality
QVERIFY_EXCEPTION_THROWN(
throw std::runtime_error("Test"),
std::runtime_error
);
}

QTEST_MAIN(tst_MyTest)
#include "tst_mytest.moc"
```

## Qt Version Compatibility

The test system automatically detects Qt versions (Qt5 or Qt6) and links against the appropriate test library:

- Qt6: `Qt6::Test`
- Qt5: `Qt5::Test`

Library names are also adjusted based on Qt version:
- Qt6: `dfm6-io`, `dfm6-mount`, `dfm6-burn`, `dfm6-search`
- Qt5: `dfm-io`, `dfm-mount`, `dfm-burn`, `dfm-search`
36 changes: 36 additions & 0 deletions autotests/dfm-burn-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Unit tests for dfm-burn
if(DFM_BUILD_WITH_QT6)
set(BURN_TEST_LIB dfm6-burn)
set(QT_TEST_LIB Qt6::Test)
else()
set(BURN_TEST_LIB dfm-burn)
set(QT_TEST_LIB Qt5::Test)
endif()

message(STATUS "Adding unit tests for ${BURN_TEST_LIB}")

# Collect test source files
file(GLOB_RECURSE TEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.h
)

# Create the test executable
add_executable(dfm-burn-test
${TEST_SRCS}
)

# Link against the dfm-burn library and Qt Test
target_link_libraries(dfm-burn-test
${BURN_TEST_LIB}
${QT_TEST_LIB}
)

# Add include directories
target_include_directories(dfm-burn-test
PRIVATE
${CMAKE_SOURCE_DIR}/src/dfm-burn
)

# Register the test with CTest
add_test(NAME dfm-burn-test COMMAND dfm-burn-test)
34 changes: 34 additions & 0 deletions autotests/dfm-burn-tests/tst_dfm_burn.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QTest>

Check warning on line 5 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 5 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class tst_DfmBurn : public QObject
{
Q_OBJECT

private Q_SLOTS:

Check warning on line 11 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.

Check warning on line 11 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void initTestCase();
void cleanupTestCase();
void initialization_test();
};

void tst_DfmBurn::initTestCase()
{
// Setup that runs once before all tests
}

void tst_DfmBurn::cleanupTestCase()
{
// Cleanup that runs once after all tests
}

void tst_DfmBurn::initialization_test()
{
// A basic test to verify the test framework is working
QVERIFY(true);
}

QTEST_MAIN(tst_DfmBurn)
#include "tst_dfm_burn.moc"

Check warning on line 34 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "tst_dfm_burn.moc" not found.

Check warning on line 34 in autotests/dfm-burn-tests/tst_dfm_burn.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "tst_dfm_burn.moc" not found.
43 changes: 43 additions & 0 deletions autotests/dfm-io-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Unit tests for dfm-io
if(DFM_BUILD_WITH_QT6)
set(IO_TEST_LIB dfm6-io)
set(QT_TEST_LIB Qt6::Test)
else()
set(IO_TEST_LIB dfm-io)
set(QT_TEST_LIB Qt5::Test)
endif()

message(STATUS "Adding unit tests for ${IO_TEST_LIB}")

# Collect test source files
file(GLOB_RECURSE TEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.h
)

# Create the test executable
add_executable(dfm-io-test
${TEST_SRCS}
)

# Link against the dfm-io library and Qt Test
target_link_libraries(dfm-io-test
${IO_TEST_LIB}
${QT_TEST_LIB}
)

# Add include directories
if(DFM_BUILD_WITH_QT6)
target_include_directories(dfm-io-test
PRIVATE
${CMAKE_SOURCE_DIR}/src/dfm-io/dfm-io
)
else()
target_include_directories(dfm-io-test
PRIVATE
${CMAKE_SOURCE_DIR}/src/dfm-io/dfm-io
)
endif()

# Register the test with CTest
add_test(NAME dfm-io-test COMMAND dfm-io-test)
34 changes: 34 additions & 0 deletions autotests/dfm-io-tests/tst_dfm_io.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// SPDX-FileCopyrightText: 2026 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include <QTest>

Check warning on line 5 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.

Check warning on line 5 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: <QTest> not found. Please note: Cppcheck does not need standard library headers to get proper results.

class tst_DfmIO : public QObject
{
Q_OBJECT

private Q_SLOTS:

Check warning on line 11 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.

Check warning on line 11 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

There is an unknown macro here somewhere. Configuration is required. If Q_SLOTS is a macro then please configure it.
void initTestCase();
void cleanupTestCase();
void initialization_test();
};

void tst_DfmIO::initTestCase()
{
// Setup that runs once before all tests
}

void tst_DfmIO::cleanupTestCase()
{
// Cleanup that runs once after all tests
}

void tst_DfmIO::initialization_test()
{
// A basic test to verify the test framework is working
QVERIFY(true);
}

QTEST_MAIN(tst_DfmIO)
#include "tst_dfm_io.moc"

Check warning on line 34 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "tst_dfm_io.moc" not found.

Check warning on line 34 in autotests/dfm-io-tests/tst_dfm_io.cpp

View workflow job for this annotation

GitHub Actions / static-check / static-check

Include file: "tst_dfm_io.moc" not found.
36 changes: 36 additions & 0 deletions autotests/dfm-mount-tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Unit tests for dfm-mount
if(DFM_BUILD_WITH_QT6)
set(MOUNT_TEST_LIB dfm6-mount)
set(QT_TEST_LIB Qt6::Test)
else()
set(MOUNT_TEST_LIB dfm-mount)
set(QT_TEST_LIB Qt5::Test)
endif()

message(STATUS "Adding unit tests for ${MOUNT_TEST_LIB}")

# Collect test source files
file(GLOB_RECURSE TEST_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/*.cpp
${CMAKE_CURRENT_SOURCE_DIR}/*.h
)

# Create the test executable
add_executable(dfm-mount-test
${TEST_SRCS}
)

# Link against the dfm-mount library and Qt Test
target_link_libraries(dfm-mount-test
${MOUNT_TEST_LIB}
${QT_TEST_LIB}
)

# Add include directories
target_include_directories(dfm-mount-test
PRIVATE
${CMAKE_SOURCE_DIR}/src/dfm-mount
)

# Register the test with CTest
add_test(NAME dfm-mount-test COMMAND dfm-mount-test)
Loading
Loading