From 99c68d19cd234cc85ce055782baf0c8d7a642df6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Sim=C3=B5es?= Date: Wed, 31 Jul 2024 10:10:01 +0100 Subject: [PATCH] Improve unit tests (#108) ***NO_CI*** --- .../DirectoryUnitTests.cs | 4 +- .../FileSystemUnitTestsBase.cs | 44 ++++++++----------- .../FileUnitTests.cs | 22 ++++++---- .../PathUnitTests.cs | 4 +- .../nano.runsettings | 6 +-- azure-pipelines.yml | 2 + 6 files changed, 43 insertions(+), 39 deletions(-) diff --git a/System.IO.FileSystem.UnitTests/DirectoryUnitTests.cs b/System.IO.FileSystem.UnitTests/DirectoryUnitTests.cs index 4b9382f..fa9667b 100644 --- a/System.IO.FileSystem.UnitTests/DirectoryUnitTests.cs +++ b/System.IO.FileSystem.UnitTests/DirectoryUnitTests.cs @@ -13,7 +13,9 @@ public class DirectoryUnitTests : FileSystemUnitTestsBase [Setup] public void Setup() { - Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware."); + // setting these to false assuming that the SD card is already mounted + WaitForRemovableDrive = false; + ConfigAndMountSdCard = false; RemovableDrivesHelper(); } diff --git a/System.IO.FileSystem.UnitTests/FileSystemUnitTestsBase.cs b/System.IO.FileSystem.UnitTests/FileSystemUnitTestsBase.cs index 5c09c7b..ecd90ea 100644 --- a/System.IO.FileSystem.UnitTests/FileSystemUnitTestsBase.cs +++ b/System.IO.FileSystem.UnitTests/FileSystemUnitTestsBase.cs @@ -25,31 +25,12 @@ public abstract class FileSystemUnitTestsBase // I: and J: internal flash internal const string Root = @"I:\"; - // set to true to wait for removable drive(s) to be mounted - internal const bool _waitForRemovableDrive = false; + public static bool WaitForRemovableDrive { get; set; } = false; - // set to true to have SPI SD card mounted - internal const bool _configAndMountSdCard = false; + public static bool ConfigAndMountSdCard { get; set; } = false; ////////////////////////////////////////////////// - private SDCard _mycardBacking; - - internal SDCard MyCard - { - set - { - _mycardBacking = value; - } - - get - { - _mycardBacking ??= InitializeSDCard(); - - return _mycardBacking; - } - } - /// /// Initializes the SD card. Can be overridden in the derived class to provide specific initialization. /// @@ -72,13 +53,14 @@ protected SDCard InitializeSDCard() /// internal void RemovableDrivesHelper() { - if (_configAndMountSdCard) + if (ConfigAndMountSdCard) { TryToMountAgain: try { - MyCard.Mount(); + SDCard card = InitializeSDCard(); + card.Mount(); } catch (Exception ex) { @@ -86,13 +68,17 @@ internal void RemovableDrivesHelper() Thread.Sleep(TimeSpan.FromSeconds(2)); - MyCard = null; - goto TryToMountAgain; } } + else + { + OutputHelper.WriteLine("***************************************"); + OutputHelper.WriteLine("*** Skipping SD card initialization ***"); + OutputHelper.WriteLine("***************************************"); + } - if (_waitForRemovableDrive) + if (WaitForRemovableDrive) { // wait until all removable drives are mounted while (DriveInfo.GetDrives().Length < _numberOfDrives) @@ -100,6 +86,12 @@ internal void RemovableDrivesHelper() Thread.Sleep(1000); } } + else + { + OutputHelper.WriteLine("******************************************"); + OutputHelper.WriteLine("*** Skipping wait for removable drives ***"); + OutputHelper.WriteLine("******************************************"); + } } } } diff --git a/System.IO.FileSystem.UnitTests/FileUnitTests.cs b/System.IO.FileSystem.UnitTests/FileUnitTests.cs index 89d6904..bc7018e 100644 --- a/System.IO.FileSystem.UnitTests/FileUnitTests.cs +++ b/System.IO.FileSystem.UnitTests/FileUnitTests.cs @@ -21,7 +21,9 @@ public class FileUnitTests : FileSystemUnitTestsBase [Setup] public void Setup() { - Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware."); + // setting these to false assuming that the SD card is already mounted + WaitForRemovableDrive = false; + ConfigAndMountSdCard = false; RemovableDrivesHelper(); } @@ -112,9 +114,9 @@ private static void CreateFile(string path, string content) { OutputHelper.WriteLine($"Creating file: {path}..."); - CreateFile( + File.AppendAllText( path, - Encoding.UTF8.GetBytes(content)); + content); } private static void DeleteFile(string path) @@ -359,15 +361,17 @@ public void Create_creates_file() ExecuteTestAndTearDown(() => { OutputHelper.WriteLine($"Creating file {Destination} WITHOUT content..."); - using var stream = File.Create(Destination); + File.AppendAllText( + Destination, + string.Empty); Console.WriteLine("Checking it file exists..."); AssertFileExists(Destination); Console.WriteLine("Checking file content..."); AssertContentEquals( - stream, - EmptyContent); + Destination, + string.Empty); }); ExecuteTestAndTearDown(() => @@ -378,14 +382,16 @@ public void Create_creates_file() new byte[100]); Console.WriteLine("Creating file and truncating it..."); - using var stream = File.Create(Destination); + File.WriteAllBytes( + Destination, + EmptyContent); Console.WriteLine("Checking it file exists..."); AssertFileExists(Destination); Console.WriteLine("Checking file content..."); AssertContentEquals( - stream, + Destination, EmptyContent); }); } diff --git a/System.IO.FileSystem.UnitTests/PathUnitTests.cs b/System.IO.FileSystem.UnitTests/PathUnitTests.cs index 63403b4..98e8742 100644 --- a/System.IO.FileSystem.UnitTests/PathUnitTests.cs +++ b/System.IO.FileSystem.UnitTests/PathUnitTests.cs @@ -13,7 +13,9 @@ public class PathUnitTests : FileSystemUnitTestsBase [Setup] public void Setup() { - Assert.SkipTest("These test will only run on real hardware. Comment out this line if you are testing on real hardware."); + // setting these to false assuming that the SD card is already mounted + WaitForRemovableDrive = false; + ConfigAndMountSdCard = false; RemovableDrivesHelper(); } diff --git a/System.IO.FileSystem.UnitTests/nano.runsettings b/System.IO.FileSystem.UnitTests/nano.runsettings index 438c21c..9bcb71f 100644 --- a/System.IO.FileSystem.UnitTests/nano.runsettings +++ b/System.IO.FileSystem.UnitTests/nano.runsettings @@ -3,14 +3,14 @@ .\TestResults - 120000 + 60000 net48 x64 - None - True COM3 + Verbose + False diff --git a/azure-pipelines.yml b/azure-pipelines.yml index 9b54a8b..a6e364e 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -59,6 +59,8 @@ steps: - template: azure-pipelines-templates/class-lib-build.yml@templates parameters: sonarCloudProject: 'nanoframework_lib-System.IO.FileSystem' + runUnitTests: true + unitTestRunsettings: '$(System.DefaultWorkingDirectory)\System.IO.FileSystem.UnitTests\nano.runsettings' # update dependents - template: azure-pipelines-templates/update-dependents.yml@templates