Skip to content

Commit 8c42896

Browse files
Copilotagocke
andcommitted
Refactor DnvmConfigFile to use instance methods
Changed DnvmConfigFile from a static class to a regular class with instance methods. This provides better testability and follows standard OOP patterns. Usage changes: - Before: var config = DnvmConfigFile.Read(); - After: var configFile = new DnvmConfigFile(); var config = configFile.Read(); Co-authored-by: agocke <515774+agocke@users.noreply.github.com>
1 parent 8f70456 commit 8c42896

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

src/dnvm/DnvmConfig.cs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,26 @@ public sealed partial record DnvmConfig
2525
}
2626

2727
/// <summary>
28-
/// Static methods for config file operations.
28+
/// Provides access to the dnvm configuration file.
2929
/// </summary>
30-
public static class DnvmConfigFile
30+
public sealed class DnvmConfigFile
3131
{
3232
private const string ConfigFileName = "dnvmConfig.json";
3333

3434
// Allow tests to override the config directory
3535
public static string? TestConfigDirectory { get; set; }
3636

37+
private readonly string _configDirectory;
38+
39+
public DnvmConfigFile() : this(GetConfigDirectory())
40+
{
41+
}
42+
43+
internal DnvmConfigFile(string configDirectory)
44+
{
45+
_configDirectory = configDirectory;
46+
}
47+
3748
/// <summary>
3849
/// Get the platform-specific config directory path.
3950
/// - Linux: ~/.config/dnvm/ (XDG_CONFIG_HOME/dnvm)
@@ -73,13 +84,12 @@ private static string GetConfigDirectory()
7384
}
7485
}
7586

76-
private static IFileSystem GetConfigFileSystem()
87+
private IFileSystem GetConfigFileSystem()
7788
{
78-
var configDir = GetConfigDirectory();
79-
Directory.CreateDirectory(configDir);
89+
Directory.CreateDirectory(_configDirectory);
8090
return new SubFileSystem(
8191
DnvmEnv.PhysicalFs,
82-
DnvmEnv.PhysicalFs.ConvertPathFromInternal(configDir));
92+
DnvmEnv.PhysicalFs.ConvertPathFromInternal(_configDirectory));
8393
}
8494

8595
private static UPath ConfigPath => UPath.Root / ConfigFileName;
@@ -88,7 +98,7 @@ private static IFileSystem GetConfigFileSystem()
8898
/// Reads the config file from the platform-specific config directory.
8999
/// Returns the default config if the file does not exist.
90100
/// </summary>
91-
public static DnvmConfig Read()
101+
public DnvmConfig Read()
92102
{
93103
try
94104
{
@@ -111,7 +121,7 @@ public static DnvmConfig Read()
111121
/// <summary>
112122
/// Writes the config file to the platform-specific config directory.
113123
/// </summary>
114-
public static void Write(DnvmConfig config)
124+
public void Write(DnvmConfig config)
115125
{
116126
var fs = GetConfigFileSystem();
117127
var tempFileName = $"{ConfigFileName}.{Path.GetRandomFileName()}.tmp";

src/dnvm/UpdateCommand.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ public static async Task<Result> UpdateSdks(
126126
{
127127
env.Console.WriteLine("Looking for available updates");
128128
// Check for dnvm updates
129-
var config = DnvmConfigFile.Read();
129+
var configFile = new DnvmConfigFile();
130+
var config = configFile.Read();
130131
if (await CheckForSelfUpdates(env.HttpClient, env.Console, logger, releasesUrl, config.PreviewsEnabled) is (true, _))
131132
{
132133
env.Console.WriteLine("dnvm is out of date. Run 'dnvm update --self' to update dnvm.");
@@ -244,7 +245,8 @@ public async Task<Result> UpdateSelf(Manifest manifest)
244245
return Result.NotASingleFile;
245246
}
246247

247-
var config = DnvmConfigFile.Read();
248+
var configFile = new DnvmConfigFile();
249+
var config = configFile.Read();
248250
DnvmReleases.Release release;
249251
switch (await CheckForSelfUpdates(_env.HttpClient, _env.Console, _logger, _releasesUrl, config.PreviewsEnabled))
250252
{

test/IntegrationTests/SelfInstallTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,8 @@ public Task UpdateSelfPreview() => RunWithServer(async (mockServer, env) =>
307307

308308
// Manually create config file with previews enabled
309309
var config = new DnvmConfig { PreviewsEnabled = true };
310-
DnvmConfigFile.Write(config);
310+
var configFile = new DnvmConfigFile();
311+
configFile.Write(config);
311312

312313
result = await DnvmRunner.RunAndRestoreEnv(
313314
env,

test/IntegrationTests/UpdateTests.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,8 @@ public async Task EnablePreviewsAndDownload() => await TestWithServer(async (moc
5353

5454
// Manually create config file with previews enabled
5555
var config = new DnvmConfig { PreviewsEnabled = true };
56-
DnvmConfigFile.Write(config);
56+
var configFile = new DnvmConfigFile();
57+
configFile.Write(config);
5758

5859
proc = await DnvmRunner.RunAndRestoreEnv(env, SelfInstallTests.DnvmExe,
5960
$"update --self -v --dnvm-url {mockServer.DnvmReleasesUrl}");

0 commit comments

Comments
 (0)