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
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,15 @@
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

#include <gtest/gtest.h>

Check warning on line 7 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detection_config_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "common/tempdir.h"

Check warning on line 9 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detection_config_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "driver_detection_config.h" not found.
#include "driver_detection_config.h"

Check warning on line 11 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detection_config_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

namespace {
std::filesystem::path GetTempFilePath()
{
return std::filesystem::temp_directory_path() / ("test_config.json");
}
} // namespace

class DriverDetectionConfigTest : public ::testing::Test
{
protected:
void SetUp() override
{
configFilePath = GetTempFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(configFilePath);
ASSERT_FALSE(configFilePath.empty()) << "Failed to create temporary directory";
}

void TearDown() override { std::filesystem::remove(configFilePath); }

std::filesystem::path configFilePath;
};
Comment on lines 14 to 16

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

为了避免在多个测试用例中重复创建 TempDirconfigFilePath,建议将这个逻辑移回 DriverDetectionConfigTest 测试夹具中。这样可以减少代码重复,并使测试代码更简洁。 您可以像下面这样更新测试夹具:

class DriverDetectionConfigTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path configFilePath = temp_dir.path() / "test_config.json";
};
class DriverDetectionConfigTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path configFilePath = temp_dir.path() / "test_config.json";
};


TEST_F(DriverDetectionConfigTest, DefaultConstruction)
Expand All @@ -44,6 +26,8 @@

TEST_F(DriverDetectionConfigTest, ConfigManagerConstruction)
{
TempDir temp_dir;
auto configFilePath = temp_dir.path() / "test_config.json";
Comment on lines +29 to +30

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将 TempDirconfigFilePath 移至测试夹具后,可以删除此处的重复代码。

using namespace linglong::driver::detect;

// Test that construction works
Expand All @@ -52,6 +36,8 @@

TEST_F(DriverDetectionConfigTest, LoadNonExistentConfig)
{
TempDir temp_dir;
auto configFilePath = temp_dir.path() / "test_config.json";
Comment on lines +39 to +40

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将 TempDirconfigFilePath 移至测试夹具后,可以删除此处的重复代码。

using namespace linglong::driver::detect;

DriverDetectionConfigManager manager(configFilePath.string());
Expand All @@ -62,6 +48,8 @@

TEST_F(DriverDetectionConfigTest, SaveAndLoadConfig)
{
TempDir temp_dir;
auto configFilePath = temp_dir.path() / "test_config.json";
Comment on lines +51 to +52

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将 TempDirconfigFilePath 移至测试夹具后,可以删除此处的重复代码。

using namespace linglong::driver::detect;

// Create manager and modify config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,18 @@
* SPDX-License-Identifier: LGPL-3.0-or-later
*/

#include <gtest/gtest.h>

Check warning on line 7 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detector_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "common/tempdir.h"

Check warning on line 9 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detector_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "driver_detection_config.h" not found.
#include "driver_detection_config.h"

Check warning on line 10 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detector_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "driver_detector.h" not found.
#include "driver_detector.h"

Check warning on line 11 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detector_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "nvidia_driver_detector.h" not found.
#include "nvidia_driver_detector.h"

#include <filesystem>

Check warning on line 14 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/driver_detector_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

namespace {
std::filesystem::path GetTempFilePath()
{
return std::filesystem::temp_directory_path() / ("test_version");
}
} // namespace

class DriverDetectorTest : public ::testing::Test
{
protected:
void SetUp() override
{
versionFilePath = GetTempFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(versionFilePath);
ASSERT_FALSE(versionFilePath.empty()) << "Failed to create temporary directory";
}

void TearDown() override { std::filesystem::remove(versionFilePath); }

std::filesystem::path versionFilePath;
};
Comment on lines 17 to 19

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

此文件中的一些测试需要临时文件,而另一些则不需要。为了避免在不需要的测试中创建不必要的 TempDir,同时减少需要它的测试中的代码重复,建议创建一个新的测试夹具,例如 DriverDetectorTempFileTest。 然后,需要临时文件的测试可以从这个新的夹具继承。

class DriverDetectorTest : public ::testing::Test
{
};

class DriverDetectorTempFileTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path versionFilePath = temp_dir.path() / "test_version";
};


TEST_F(DriverDetectorTest, NvidiaDriverDetectorInitialization)
Expand Down Expand Up @@ -77,6 +59,8 @@

TEST_F(DriverDetectorTest, DriverDetectionConfigManagerSave)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

请使用新建议的 DriverDetectorTempFileTest 夹具。

Suggested change
TEST_F(DriverDetectorTest, DriverDetectionConfigManagerSave)
TEST_F(DriverDetectorTempFileTest, DriverDetectionConfigManagerSave)

{
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "test_version";
Comment on lines +62 to +63

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在使用 DriverDetectorTempFileTest 夹具后,这些行就不再需要了。

linglong::driver::detect::DriverDetectionConfigManager configManager(versionFilePath.string());

// Load default config
Expand All @@ -93,6 +77,8 @@

TEST_F(DriverDetectorTest, DriverDetectionConfigManagerRoundTrip)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

请使用新建议的 DriverDetectorTempFileTest 夹具。

Suggested change
TEST_F(DriverDetectorTest, DriverDetectionConfigManagerRoundTrip)
TEST_F(DriverDetectorTempFileTest, DriverDetectionConfigManagerRoundTrip)

{
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "test_version";
Comment on lines +80 to +81

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在使用 DriverDetectorTempFileTest 夹具后,这些行就不再需要了。

// Create and configure first manager
linglong::driver::detect::DriverDetectionConfigManager configManager1(versionFilePath.string());
ASSERT_TRUE(configManager1.loadConfig());
Expand Down Expand Up @@ -133,6 +119,8 @@

TEST_F(DriverDetectorTest, ConfigManagerUserChoice)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

请使用新建议的 DriverDetectorTempFileTest 夹具。

Suggested change
TEST_F(DriverDetectorTest, ConfigManagerUserChoice)
TEST_F(DriverDetectorTempFileTest, ConfigManagerUserChoice)

{
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "test_version";
Comment on lines +122 to +123

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在使用 DriverDetectorTempFileTest 夹具后,这些行就不再需要了。

linglong::driver::detect::DriverDetectionConfigManager configManager(versionFilePath.string());
ASSERT_TRUE(configManager.loadConfig());

Expand Down
60 changes: 41 additions & 19 deletions libs/linglong/tests/ll-tests/src/linglong/utils/filelock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gmock/gmock.h>

Check warning on line 5 in libs/linglong/tests/ll-tests/src/linglong/utils/filelock_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: <gmock/gmock.h> not found. Please note: Cppcheck does not need standard library headers to get proper results.
#include <gtest/gtest.h>

Check warning on line 6 in libs/linglong/tests/ll-tests/src/linglong/utils/filelock_test.cpp

View workflow job for this annotation

GitHub Actions / cppcheck

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

#include "common/tempdir.h"
#include "linglong/utils/error/error.h"
#include "linglong/utils/filelock.h"

Expand All @@ -18,33 +19,16 @@
using namespace linglong::utils::filelock;
using namespace linglong::utils::error;

namespace {
// Helper function to create a temporary file path
std::filesystem::path GetTempFilePath()
{
return std::filesystem::temp_directory_path() / ("test_filelock.lock");
}
} // namespace

// Test fixture for FileLock tests
class FileLockTest : public ::testing::Test
{
protected:
std::filesystem::path temp_path;

void SetUp() override
{
temp_path = GetTempFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(temp_path);
}

void TearDown() override { std::filesystem::remove(temp_path); }
};
Comment on lines 23 to 25

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

此文件中的所有测试用例都重复了 TempDirtemp_path 的创建逻辑。为了消除这种重复并提高代码的可维护性,建议将此逻辑移回 FileLockTest 夹具中。

class FileLockTest : public ::testing::Test
{
protected:
    TempDir temp_dir;
    std::filesystem::path temp_path = temp_dir.path() / "test_filelock.lock";
};


// Test creating FileLock with create_if_missing = true
TEST_F(FileLockTest, CreateWithMissingFile)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +30 to +31

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
EXPECT_TRUE(std::filesystem::exists(temp_path));
Expand All @@ -57,13 +41,17 @@
// Test creating FileLock without create_if_missing when file missing
TEST_F(FileLockTest, CreateWithoutMissingFileFails)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +44 to +45

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, false);
ASSERT_FALSE(result);
}

// Test creating FileLock on existing file
TEST_F(FileLockTest, CreateOnExistingFile)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +53 to +54

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

std::ofstream file(temp_path);
file.close();

Expand All @@ -77,6 +65,8 @@
// Test lock and unlock for Read lock
TEST_F(FileLockTest, LockUnlockRead)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +68 to +69

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -94,6 +84,8 @@
// Test lock and unlock for Write lock
TEST_F(FileLockTest, LockUnlockWrite)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +87 to +88

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -111,6 +103,8 @@
// Test tryLock success
TEST_F(FileLockTest, TryLockSuccess)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +106 to +107

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -126,6 +120,8 @@
// Test tryLock failure when already locked by another process
TEST_F(FileLockTest, TryLockFailureInterProcess)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +123 to +124

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock1 = std::move(result).value();
Expand Down Expand Up @@ -159,6 +155,8 @@
// Test tryLockFor with timeout success
TEST_F(FileLockTest, TryLockForSuccess)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +158 to +159

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -172,6 +170,8 @@
// Test tryLockFor with timeout failure
TEST_F(FileLockTest, TryLockForFailure)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +173 to +174

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock1 = std::move(result).value();
Expand Down Expand Up @@ -205,6 +205,8 @@
// Test relock from Read to Write
TEST_F(FileLockTest, RelockReadToWrite)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +208 to +209

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -221,6 +223,8 @@
// Test relock from Write to Read
TEST_F(FileLockTest, RelockWriteToRead)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +226 to +227

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -237,6 +241,8 @@
// Test relock when not locked fails
TEST_F(FileLockTest, RelockNotLocked)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +244 to +245

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -260,6 +266,8 @@
// Test multiple read locks possible (shared)
TEST_F(FileLockTest, MultipleReadLocks)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +269 to +270

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result1 = FileLock::create(temp_path);
ASSERT_TRUE(result1);
auto lock1 = std::move(result1).value();
Expand Down Expand Up @@ -290,6 +298,8 @@
// Test write lock exclusive
TEST_F(FileLockTest, WriteLockExclusive)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +301 to +302

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result1 = FileLock::create(temp_path);
ASSERT_TRUE(result1);
auto lock1 = std::move(result1).value();
Expand Down Expand Up @@ -326,6 +336,8 @@
// Test behavior after fork
TEST_F(FileLockTest, LockAfterFork)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +339 to +340

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand Down Expand Up @@ -357,6 +369,8 @@
// Test cannot create duplicate lock in same process
TEST_F(FileLockTest, NoDuplicateLockSameProcess)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +372 to +373

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result1 = FileLock::create(temp_path, true);
ASSERT_TRUE(result1);

Expand All @@ -369,6 +383,8 @@
// Test move constructor
TEST_F(FileLockTest, MoveConstructor)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +386 to +387

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock1 = std::move(result).value();
Expand All @@ -382,6 +398,8 @@
// Test move assignment
TEST_F(FileLockTest, MoveAssignment)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +401 to +402

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result1 = FileLock::create(temp_path, true);
ASSERT_TRUE(result1);
auto lock1 = std::move(result1).value();
Expand All @@ -402,6 +420,8 @@
// Test tryLock when already locked same type
TEST_F(FileLockTest, TryLockAlreadyLockedSameType)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +423 to +424

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand All @@ -414,6 +434,8 @@
// Test lock when already locked different type
TEST_F(FileLockTest, LockAlreadyLockedDiffType)
{
TempDir temp_dir;
auto temp_path = temp_dir.path() / "test_filelock.lock";
Comment on lines +437 to +438

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

在将临时文件逻辑移至夹具后,可以删除这些行。

auto result = FileLock::create(temp_path, true);
ASSERT_TRUE(result);
auto lock = std::move(result).value();
Expand Down