-
Notifications
You must be signed in to change notification settings - Fork 65
refactor: replace manual temp file handling with TempDir utility #1565
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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
|
||
|
|
||
| #include "common/tempdir.h" | ||
| #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
|
||
| #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; | ||
| }; | ||
|
|
||
| TEST_F(DriverDetectionConfigTest, DefaultConstruction) | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| using namespace linglong::driver::detect; | ||
|
|
||
| // Test that construction works | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| using namespace linglong::driver::detect; | ||
|
|
||
| DriverDetectionConfigManager manager(configFilePath.string()); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| using namespace linglong::driver::detect; | ||
|
|
||
| // Create manager and modify config | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,36 +4,18 @@ | |
| * SPDX-License-Identifier: LGPL-3.0-or-later | ||
| */ | ||
|
|
||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "common/tempdir.h" | ||
| #include "driver_detection_config.h" | ||
| #include "driver_detector.h" | ||
| #include "nvidia_driver_detector.h" | ||
|
|
||
| #include <filesystem> | ||
| #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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 此文件中的一些测试需要临时文件,而另一些则不需要。为了避免在不需要的测试中创建不必要的 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) | ||
|
|
@@ -77,6 +59,8 @@ | |
|
|
||
| TEST_F(DriverDetectorTest, DriverDetectionConfigManagerSave) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| TempDir temp_dir; | ||
| auto versionFilePath = temp_dir.path() / "test_version"; | ||
|
Comment on lines
+62
to
+63
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| linglong::driver::detect::DriverDetectionConfigManager configManager(versionFilePath.string()); | ||
|
|
||
| // Load default config | ||
|
|
@@ -93,6 +77,8 @@ | |
|
|
||
| TEST_F(DriverDetectorTest, DriverDetectionConfigManagerRoundTrip) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| TempDir temp_dir; | ||
| auto versionFilePath = temp_dir.path() / "test_version"; | ||
|
Comment on lines
+80
to
+81
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| // Create and configure first manager | ||
| linglong::driver::detect::DriverDetectionConfigManager configManager1(versionFilePath.string()); | ||
| ASSERT_TRUE(configManager1.loadConfig()); | ||
|
|
@@ -133,6 +119,8 @@ | |
|
|
||
| TEST_F(DriverDetectorTest, ConfigManagerUserChoice) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| { | ||
| TempDir temp_dir; | ||
| auto versionFilePath = temp_dir.path() / "test_version"; | ||
|
Comment on lines
+122
to
+123
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| linglong::driver::detect::DriverDetectionConfigManager configManager(versionFilePath.string()); | ||
| ASSERT_TRUE(configManager.loadConfig()); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,10 @@ | |
| // | ||
| // SPDX-License-Identifier: LGPL-3.0-or-later | ||
|
|
||
| #include <gmock/gmock.h> | ||
| #include <gtest/gtest.h> | ||
|
|
||
| #include "common/tempdir.h" | ||
| #include "linglong/utils/error/error.h" | ||
| #include "linglong/utils/filelock.h" | ||
|
|
||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| // 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| EXPECT_TRUE(std::filesystem::exists(temp_path)); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| 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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| std::ofstream file(temp_path); | ||
| file.close(); | ||
|
|
||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock1 = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock1 = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result1 = FileLock::create(temp_path); | ||
| ASSERT_TRUE(result1); | ||
| auto lock1 = std::move(result1).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result1 = FileLock::create(temp_path); | ||
| ASSERT_TRUE(result1); | ||
| auto lock1 = std::move(result1).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result1 = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result1); | ||
|
|
||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock1 = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result1 = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result1); | ||
| auto lock1 = std::move(result1).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
@@ -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
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| auto result = FileLock::create(temp_path, true); | ||
| ASSERT_TRUE(result); | ||
| auto lock = std::move(result).value(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
为了避免在多个测试用例中重复创建
TempDir和configFilePath,建议将这个逻辑移回DriverDetectionConfigTest测试夹具中。这样可以减少代码重复,并使测试代码更简洁。 您可以像下面这样更新测试夹具: