Skip to content

Commit d2f97fd

Browse files
committed
fix: avoid startup crash when saved appdata path is unavailable
1 parent 33a59db commit d2f97fd

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using WheelWizard.Services;
2+
3+
namespace WheelWizard.Test.Features.Settings;
4+
5+
[Collection("SettingsFeature")]
6+
public class PathManagerTests
7+
{
8+
[Fact]
9+
public void TrySetWheelWizardAppdataPath_ReturnsFalse_WhenTargetPathIsUnavailable()
10+
{
11+
if (!OperatingSystem.IsWindows())
12+
return;
13+
14+
var unavailablePath = GetUnavailableWindowsPath();
15+
var result = PathManager.TrySetWheelWizardAppdataPath(unavailablePath, out var errorMessage, out _);
16+
17+
Assert.False(result);
18+
Assert.False(string.IsNullOrWhiteSpace(errorMessage));
19+
}
20+
21+
private static string GetUnavailableWindowsPath()
22+
{
23+
return $@"\\nonexistent-host\WheelWizardTests\{Guid.NewGuid():N}";
24+
}
25+
}

WheelWizard/Services/PathManager.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,35 @@ public static bool IsUsingCustomWheelWizardAppdataPath
9595
return null;
9696

9797
var normalized = FileHelper.NormalizePath(storedPath);
98-
return FileHelper.PathsEqual(normalized, DefaultWheelWizardAppdataPath) ? null : normalized;
98+
if (FileHelper.PathsEqual(normalized, DefaultWheelWizardAppdataPath))
99+
return null;
100+
101+
// If a previously selected custom location is no longer available (for example,
102+
// when an external drive letter changes), fall back to the default path.
103+
if (!TryEnsureWheelWizardAppdataPathAccessible(normalized))
104+
return null;
105+
106+
return normalized;
99107
}
100108
catch
101109
{
102110
return null;
103111
}
104112
}
105113

114+
private static bool TryEnsureWheelWizardAppdataPathAccessible(string normalizedPath)
115+
{
116+
try
117+
{
118+
FileHelper.EnsureDirectory(normalizedPath);
119+
return true;
120+
}
121+
catch
122+
{
123+
return false;
124+
}
125+
}
126+
106127
private static string? LoadPersistedWheelWizardAppdataOverride()
107128
{
108129
#if WINDOWS

0 commit comments

Comments
 (0)