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 @@ -2,39 +2,24 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gtest/gtest.h>

Check warning on line 5 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/application_singleton_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 "application_singleton.h"

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

View workflow job for this annotation

GitHub Actions / cppcheck

Include file: "application_singleton.h" not found.
#include "common/tempdir.h"

Check warning on line 9 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/application_singleton_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>

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

namespace {
std::filesystem::path GetTempLockFilePath()
{
return std::filesystem::temp_directory_path() / "ll_driver_detect_singleton_test.lock";
}
} // namespace

class ApplicationSingletonTest : public ::testing::Test
{
protected:
void SetUp() override
{
lockFilePath = GetTempLockFilePath();
// Ensure the file does not exist initially
std::filesystem::remove(lockFilePath);
}

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

std::filesystem::path lockFilePath;
};

TEST_F(ApplicationSingletonTest, CanAcquireLockWhenNoOneHasIt)
{
TempDir temp_dir;
auto lockFilePath = temp_dir.path() / "ll_driver_detect_singleton_test.lock";
using namespace linglong::driver::detect;
ApplicationSingleton singleton(lockFilePath.string());

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

View workflow job for this annotation

GitHub Actions / cppcheck

The function 'SetUp' is never used.

auto result = singleton.tryAcquireLock();
ASSERT_TRUE(result.has_value())
Expand All @@ -48,6 +33,8 @@
{
using namespace linglong::driver::detect;

TempDir temp_dir;
auto lockFilePath = temp_dir.path() / "ll_driver_detect_singleton_test.lock";
// First instance acquires the lock
ApplicationSingleton singleton1(lockFilePath.string());
auto result1 = singleton1.tryAcquireLock();
Expand All @@ -65,8 +52,9 @@

TEST_F(ApplicationSingletonTest, CanAcquireLockAfterItIsReleased)
{
TempDir temp_dir;
auto lockFilePath = temp_dir.path() / "ll_driver_detect_singleton_test.lock";
using namespace linglong::driver::detect;

// Create a scope for the first singleton
{
ApplicationSingleton singleton1(lockFilePath.string());
Expand All @@ -87,6 +75,8 @@

TEST_F(ApplicationSingletonTest, ReleasingLockManuallyWorks)
{
TempDir temp_dir;
auto lockFilePath = temp_dir.path() / "ll_driver_detect_singleton_test.lock";
using namespace linglong::driver::detect;
ApplicationSingleton singleton1(lockFilePath.string());
auto result1 = singleton1.tryAcquireLock();
Expand All @@ -108,6 +98,8 @@

TEST_F(ApplicationSingletonTest, CreatesDirectoryForLockFile)
{
TempDir temp_dir;
auto lockFilePath = temp_dir.path() / "ll_driver_detect_singleton_test.lock";
Comment on lines +101 to +102

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

The temp_dir and lockFilePath variables are unused in this test. The test logic continues to use std::filesystem::temp_directory_path() directly and performs manual cleanup, which goes against the goal of this refactoring for better test isolation. This test should be updated to use the temp_dir to construct its paths and rely on RAII for cleanup, making it consistent with other tests in this file.

using namespace linglong::driver::detect;
auto deepLockPath = std::filesystem::temp_directory_path() / "deep_dir_for_test" / "test.lock";
std::filesystem::remove(deepLockPath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,19 @@
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include <gmock/gmock.h>

Check warning on line 5 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/nvidia_driver_detector_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/apps/ll-driver-detect/nvidia_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 8 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/nvidia_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"

Check warning on line 10 in libs/linglong/tests/ll-tests/src/apps/ll-driver-detect/nvidia_driver_detector_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>

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

View workflow job for this annotation

GitHub Actions / cppcheck

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

using namespace linglong::driver::detect;
using ::testing::_;
using ::testing::Return;

namespace {
std::filesystem::path GetTempVersionFilePath()
{
return std::filesystem::temp_directory_path() / "nvidia_test_version_file.txt";
}
} // namespace

// A mock class that only mocks checkPackageInstalled, as getDriverVersion is no longer virtual
class TestableNVIDIADriverDetector : public NVIDIADriverDetector
{
Expand All @@ -43,29 +37,21 @@
class NvidiaDriverDetectorTest : public ::testing::Test
{
protected:
void SetUp() override
{
versionFilePath = GetTempVersionFilePath();
std::filesystem::remove(versionFilePath); // Ensure clean slate
}

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

// Helper to create a mock version file
void createMockVersionFile(const std::string &content)
void createMockVersionFile(std::filesystem::path path, const std::string &content)
{
std::ofstream file(versionFilePath);
ASSERT_TRUE(file.is_open()) << "Failed to create mock version file: " << versionFilePath;
std::ofstream file(path);
ASSERT_TRUE(file.is_open()) << "Failed to create mock version file: " << path;
file << content;
file.close();
}

std::filesystem::path versionFilePath;
};

TEST_F(NvidiaDriverDetectorTest, Detect_Success_PackageNotInstalled)
{
createMockVersionFile("510.85.02"); // Simplified version format now
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "nvidia_test_version_file.txt";
createMockVersionFile(versionFilePath, "510.85.02"); // Simplified version format now
TestableNVIDIADriverDetector detector(versionFilePath.string());
const std::string expectedVersion = "510-85-02"; // With dots replaced by dashes
const std::string expectedPackageName =
Expand All @@ -86,7 +72,9 @@

TEST_F(NvidiaDriverDetectorTest, Detect_Success_PackageAlreadyInstalled)
{
createMockVersionFile("510.85.02");
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "nvidia_test_version_file.txt";
createMockVersionFile(versionFilePath, "510.85.02");
TestableNVIDIADriverDetector detector(versionFilePath.string());
const std::string expectedVersion = "510-85-02";
const std::string expectedPackageName =
Expand All @@ -107,6 +95,8 @@

TEST_F(NvidiaDriverDetectorTest, Detect_Fails_When_VersionFileDoesNotExist)
{
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "nvidia_test_version_file.txt";
std::filesystem::remove(versionFilePath); // Ensure file does not exist
TestableNVIDIADriverDetector detector(versionFilePath.string());

Expand All @@ -120,7 +110,9 @@

TEST_F(NvidiaDriverDetectorTest, Detect_Fails_When_VersionFileIsEmpty)
{
createMockVersionFile(""); // Empty file
TempDir temp_dir;
auto versionFilePath = temp_dir.path() / "nvidia_test_version_file.txt";
createMockVersionFile(versionFilePath, ""); // Empty file
TestableNVIDIADriverDetector detector(versionFilePath.string());

EXPECT_CALL(detector, checkPackageInstalled(_)).Times(0); // Should not be called
Expand Down