diff --git a/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs b/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs
index 902ce18..eac1279 100644
--- a/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs
+++ b/IntelliTect.Multitool.Tests/RepositoryPaths.Tests.cs
@@ -43,4 +43,38 @@ public void TrySearchForGitContainingDirectory_ReturnsFalseWhenNotFound()
Assert.False(RepositoryPaths.TrySearchForGitContainingDirectory(new DirectoryInfo(path), out string gitParentDirectory));
Assert.Empty(gitParentDirectory);
}
+
+ [Fact]
+ public void BuildVariables_HandlesFileNotFound_Gracefully()
+ {
+ // Save current temp file if it exists
+ string tempFilePath = Path.Combine(Path.GetTempPath(), RepositoryPaths.BuildVariableFileName);
+ string? backupContent = null;
+ bool fileExisted = File.Exists(tempFilePath);
+ if (fileExisted)
+ {
+ backupContent = File.ReadAllText(tempFilePath);
+ File.Delete(tempFilePath);
+ }
+
+ try
+ {
+ // Reset the static field to force re-initialization
+ var field = typeof(RepositoryPaths).GetField("_buildVariables", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static);
+ field?.SetValue(null, null);
+
+ // Access BuildVariables when file doesn't exist - should not throw
+ var buildVars = RepositoryPaths.BuildVariables;
+ Assert.NotNull(buildVars);
+ Assert.Empty(buildVars);
+ }
+ finally
+ {
+ // Restore the file if it existed
+ if (fileExisted && backupContent != null)
+ {
+ File.WriteAllText(tempFilePath, backupContent);
+ }
+ }
+ }
}
\ No newline at end of file
diff --git a/IntelliTect.Multitool/RepositoryPaths.cs b/IntelliTect.Multitool/RepositoryPaths.cs
index 68627f2..470202c 100644
--- a/IntelliTect.Multitool/RepositoryPaths.cs
+++ b/IntelliTect.Multitool/RepositoryPaths.cs
@@ -15,10 +15,44 @@ public static class RepositoryPaths
///
/// Holds the key value pairs of the build variables that this class can use.
///
- public static ReadOnlyDictionary BuildVariables { get; } = new(File.ReadAllLines(Path.Combine(Path.GetTempPath(), BuildVariableFileName))
- .Select(line => line.Split("::"))
- .ToDictionary(split => split[0].Trim(),
- split => !string.IsNullOrEmpty(split[1]) ? split[1].Trim() : null));
+ public static ReadOnlyDictionary BuildVariables
+ {
+ get
+ {
+ if (_buildVariables == null)
+ {
+ _buildVariables = LoadBuildVariables();
+ }
+ return _buildVariables;
+ }
+ }
+
+ private static ReadOnlyDictionary? _buildVariables;
+
+ private static ReadOnlyDictionary LoadBuildVariables()
+ {
+ try
+ {
+ string filePath = Path.Combine(Path.GetTempPath(), BuildVariableFileName);
+ var lines = File.ReadAllLines(filePath);
+ var dictionary = lines
+ .Select(line => line.Split("::"))
+ .ToDictionary(split => split[0].Trim(),
+ split => !string.IsNullOrEmpty(split[1]) ? split[1].Trim() : null);
+ return new ReadOnlyDictionary(dictionary);
+ }
+ catch (FileNotFoundException)
+ {
+ // Return empty dictionary if the build variables file doesn't exist
+ // This can happen when the temp file is cleaned up by the OS
+ return new ReadOnlyDictionary(new Dictionary());
+ }
+ catch (DirectoryNotFoundException)
+ {
+ // Return empty dictionary if the temp directory doesn't exist
+ return new ReadOnlyDictionary(new Dictionary());
+ }
+ }
///
/// Finds the root of the repository by looking for the directory containing the .git directory.