From 0ad707288af9afbc0aa3a2b776b250ea8377c3b3 Mon Sep 17 00:00:00 2001 From: henrygd Date: Wed, 26 Aug 2026 11:50:19 -0400 Subject: [PATCH] fix windows sensor mocks and data directory tests --- agent/data_dir_test.go | 16 ++++++++++++---- agent/sensors_test.go | 3 ++- agent/sensors_windows.go | 7 +++++-- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/agent/data_dir_test.go b/agent/data_dir_test.go index 1705aacc..e9003846 100644 --- a/agent/data_dir_test.go +++ b/agent/data_dir_test.go @@ -12,6 +12,14 @@ import ( "github.com/stretchr/testify/require" ) +func invalidDataDir(t *testing.T) string { + t.Helper() + + filePath := filepath.Join(t.TempDir(), "file") + require.NoError(t, os.WriteFile(filePath, nil, 0644)) + return filepath.Join(filePath, "data") +} + func TestGetDataDir(t *testing.T) { // Test with explicit dataDir parameter t.Run("explicit data dir", func(t *testing.T) { @@ -48,7 +56,7 @@ func TestGetDataDir(t *testing.T) { // Test with invalid explicit dataDir t.Run("invalid explicit data dir", func(t *testing.T) { - invalidPath := "/invalid/path/that/cannot/be/created" + invalidPath := invalidDataDir(t) _, err := GetDataDir(invalidPath) assert.Error(t, err) }) @@ -78,7 +86,7 @@ func TestTestDataDirs(t *testing.T) { // Test with multiple directories, first one valid t.Run("multiple dirs - first valid", func(t *testing.T) { tempDir := t.TempDir() - invalidDir := "/invalid/path" + invalidDir := invalidDataDir(t) result, err := testDataDirs([]string{tempDir, invalidDir}) require.NoError(t, err) assert.Equal(t, tempDir, result) @@ -87,7 +95,7 @@ func TestTestDataDirs(t *testing.T) { // Test with multiple directories, second one valid t.Run("multiple dirs - second valid", func(t *testing.T) { tempDir := t.TempDir() - invalidDir := "/invalid/path" + invalidDir := invalidDataDir(t) result, err := testDataDirs([]string{invalidDir, tempDir}) require.NoError(t, err) assert.Equal(t, tempDir, result) @@ -109,7 +117,7 @@ func TestTestDataDirs(t *testing.T) { // Test with no valid directories t.Run("no valid directories", func(t *testing.T) { - invalidPaths := []string{"/invalid/path1", "/invalid/path2"} + invalidPaths := []string{invalidDataDir(t), invalidDataDir(t)} _, err := testDataDirs(invalidPaths) assert.Error(t, err) assert.Contains(t, err.Error(), "data directory not found") diff --git a/agent/sensors_test.go b/agent/sensors_test.go index a56faec3..f46c5247 100644 --- a/agent/sensors_test.go +++ b/agent/sensors_test.go @@ -602,8 +602,9 @@ func TestUpdateTemperaturesSkipsOnTimeout(t *testing.T) { }, } + originalGetSensorTemps := getSensorTemps t.Cleanup(func() { - getSensorTemps = sensors.TemperaturesWithContext + getSensorTemps = originalGetSensorTemps }) getSensorTemps = func(ctx context.Context) ([]sensors.TemperatureStat, error) { time.Sleep(50 * time.Millisecond) diff --git a/agent/sensors_windows.go b/agent/sensors_windows.go index c4b2997a..bca568a2 100644 --- a/agent/sensors_windows.go +++ b/agent/sensors_windows.go @@ -214,9 +214,12 @@ func (lhm *lhmProcess) getTemps(ctx context.Context) (temps []sensors.Temperatur return temps, nil } -// getSensorTemps attempts to pull sensor temperatures from the embedded LHM process. +// getSensorTemps is a variable so tests can replace the platform sensor collector. +var getSensorTemps = getWindowsSensorTemps + +// getWindowsSensorTemps attempts to pull sensor temperatures from the embedded LHM process. // NB: LibreHardwareMonitorLib requires admin privileges to access all available sensors. -func getSensorTemps(ctx context.Context) (temps []sensors.TemperatureStat, err error) { +func getWindowsSensorTemps(ctx context.Context) (temps []sensors.TemperatureStat, err error) { defer func() { if err != nil { slog.Debug("Error reading sensors", "err", err)