From 507a9520bb251d19d20bddbaf5a6197f553561a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Fri, 2 Aug 2024 14:32:34 +0100 Subject: [PATCH] Add new unit tests for FileSystemInfo and hidden/system files (#109) ***NO_CI*** --- .../FileUnitTests.cs | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/System.IO.FileSystem.UnitTests/FileUnitTests.cs b/System.IO.FileSystem.UnitTests/FileUnitTests.cs index bc7018e..65f5cb1 100644 --- a/System.IO.FileSystem.UnitTests/FileUnitTests.cs +++ b/System.IO.FileSystem.UnitTests/FileUnitTests.cs @@ -835,5 +835,103 @@ public void WriteAllText_should_overwrite_existing_file() TextContent); }); } + + [TestMethod] + public void TestAccessingFileProperties() + { + string path = @$"{Root}temp\testdir\"; + string fileName = "file1.txt"; + + // make sure the directory doesn't exist + if (Directory.Exists(path)) + { + Directory.Delete(path, true); + } + + Directory.CreateDirectory(path); + + OutputHelper.WriteLine($"Creating {fileName}..."); + File.WriteAllBytes($@"{path}{fileName}", BinaryContent); + + // getting directory info + OutputHelper.WriteLine($"Getting directory info for {path}..."); + + var dirInfo = new DirectoryInfo(path); + + // getting file info + OutputHelper.WriteLine($"Getting file info for {fileName}..."); + + var fileList = dirInfo.GetFiles(); + var file = fileList[0]; + + // access file properties + OutputHelper.WriteLine($"Accessing file properties for {fileName}..."); + + var fileNameProperty = file.FullName; + Assert.AreEqual($@"{path}{fileName}", fileNameProperty); + + var fileSizePropert = file.Length; + Assert.AreEqual(BinaryContent.Length, fileSizePropert, "Reported file length is different than expected."); + + Assert.AreEqual(FileAttributes.Normal, (file.Attributes & FileAttributes.Normal), "File attribute Normal is not set."); + + Assert.AreEqual(fileName, file.Name); + } + + [TestMethod] + public void TestListingSysAndHiddenFiles_00() + { + string path = @$"{Root}temp\testdir\"; + string fileName = "this_is_a_system_file.sys"; + string filePath = Path.Combine(path, fileName); + + // make sure the directory doesn't exist + if (Directory.Exists(path)) + { + Directory.Delete(path, true); + } + + // make sure the file doesn't exist + if (File.Exists(filePath)) + { + File.Delete(filePath); + } + + Directory.CreateDirectory(path); + + CreateFile(filePath, EmptyContent); + + // list directory + var dirInfo = new DirectoryInfo(path); + + var fileList = dirInfo.GetFiles(); + + Assert.AreEqual(0, fileList.Length, "System file is listed when it shouldn't."); + } + + [TestMethod] + public void TestListingSysAndHiddenFiles_01() + { + // this is looking for config files stored in ESP32 devices internal storage + + // check if this test run is using the internal storage + if (!Root.StartsWith(@"I:\")) + { + // this is not the internal storage, skip the test + OutputHelper.WriteLine("Skipping test because it's not running on the internal storage."); + + return; + } + + // list directory + var dirInfo = new DirectoryInfo(Root); + + var fileList = dirInfo.GetFiles(); + + foreach (var file in fileList) + { + Assert.IsFalse(file.Name.Contains(".sys"), "System file is listed when it shouldn't."); + } + } } }