From 94c6e222e8d439f120397815bfaaabae30963e29 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 13 Jan 2026 20:11:02 +1100 Subject: [PATCH 1/2] Create FileExTests.cs --- src/Tests/FileExTests.cs | 69 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/Tests/FileExTests.cs diff --git a/src/Tests/FileExTests.cs b/src/Tests/FileExTests.cs new file mode 100644 index 00000000..e4d56602 --- /dev/null +++ b/src/Tests/FileExTests.cs @@ -0,0 +1,69 @@ +public class FileExTests +{ + [Fact] + public void MakeReadOnly_SetsReadOnlyAttribute() + { + var tempFile = Path.GetTempFileName(); + try + { + FileEx.MakeReadOnly(tempFile); + + var attributes = File.GetAttributes(tempFile); + Assert.True((attributes & FileAttributes.ReadOnly) != 0); + } + finally + { + File.SetAttributes(tempFile, FileAttributes.Normal); + File.Delete(tempFile); + } + } + + [Fact] + public void ClearReadOnly_RemovesReadOnlyAttribute() + { + var tempFile = Path.GetTempFileName(); + try + { + File.SetAttributes(tempFile, File.GetAttributes(tempFile) | FileAttributes.ReadOnly); + + FileEx.ClearReadOnly(tempFile); + + var attributes = File.GetAttributes(tempFile); + Assert.False((attributes & FileAttributes.ReadOnly) != 0); + } + finally + { + File.SetAttributes(tempFile, FileAttributes.Normal); + File.Delete(tempFile); + } + } + + [Fact] + public void ClearReadOnly_DoesNothingIfFileDoesNotExist() + { + var nonExistentFile = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); + + FileEx.ClearReadOnly(nonExistentFile); + + Assert.False(File.Exists(nonExistentFile)); + } + + [Fact] + public void MakeReadOnly_ThenClearReadOnly_RoundTrip() + { + var tempFile = Path.GetTempFileName(); + try + { + FileEx.MakeReadOnly(tempFile); + Assert.True((File.GetAttributes(tempFile) & FileAttributes.ReadOnly) != 0); + + FileEx.ClearReadOnly(tempFile); + Assert.False((File.GetAttributes(tempFile) & FileAttributes.ReadOnly) != 0); + } + finally + { + File.SetAttributes(tempFile, FileAttributes.Normal); + File.Delete(tempFile); + } + } +} From 1fe3f404e91b6a4e2210566d3616dffaf43e382e Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Tue, 13 Jan 2026 20:11:47 +1100 Subject: [PATCH 2/2] Update FileEx.cs --- src/MarkdownSnippets/FileEx.cs | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/MarkdownSnippets/FileEx.cs b/src/MarkdownSnippets/FileEx.cs index 3e3e1d74..3d96bb88 100644 --- a/src/MarkdownSnippets/FileEx.cs +++ b/src/MarkdownSnippets/FileEx.cs @@ -38,15 +38,16 @@ public static void ClearReadOnly(string path) return; } - new FileInfo(path) + var attributes = File.GetAttributes(path); + if ((attributes & FileAttributes.ReadOnly) != 0) { - IsReadOnly = false - }; + File.SetAttributes(path, attributes & ~FileAttributes.ReadOnly); + } } - public static void MakeReadOnly(string path) => - new FileInfo(path) - { - IsReadOnly = true - }; + public static void MakeReadOnly(string path) + { + var attributes = File.GetAttributes(path); + File.SetAttributes(path, attributes | FileAttributes.ReadOnly); + } } \ No newline at end of file