Skip to content

Commit

Permalink
Improve unit tests (#108)
Browse files Browse the repository at this point in the history
***NO_CI***
  • Loading branch information
josesimoes authored Jul 31, 2024
1 parent aaf1815 commit 99c68d1
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 39 deletions.
4 changes: 3 additions & 1 deletion System.IO.FileSystem.UnitTests/DirectoryUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
44 changes: 18 additions & 26 deletions System.IO.FileSystem.UnitTests/FileSystemUnitTestsBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/// <summary>
/// Initializes the SD card. Can be overridden in the derived class to provide specific initialization.
/// </summary>
Expand All @@ -72,34 +53,45 @@ protected SDCard InitializeSDCard()
/// </summary>
internal void RemovableDrivesHelper()
{
if (_configAndMountSdCard)
if (ConfigAndMountSdCard)
{
TryToMountAgain:

try
{
MyCard.Mount();
SDCard card = InitializeSDCard();
card.Mount();
}
catch (Exception ex)
{
OutputHelper.WriteLine($"SDCard mount failed: {ex.Message}");

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)
{
Thread.Sleep(1000);
}
}
else
{
OutputHelper.WriteLine("******************************************");
OutputHelper.WriteLine("*** Skipping wait for removable drives ***");
OutputHelper.WriteLine("******************************************");
}
}
}
}
22 changes: 14 additions & 8 deletions System.IO.FileSystem.UnitTests/FileUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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(() =>
Expand All @@ -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);
});
}
Expand Down
4 changes: 3 additions & 1 deletion System.IO.FileSystem.UnitTests/PathUnitTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
6 changes: 3 additions & 3 deletions System.IO.FileSystem.UnitTests/nano.runsettings
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
<!-- Configurations that affect the Test Framework -->
<RunConfiguration>
<ResultsDirectory>.\TestResults</ResultsDirectory><!-- Path relative to solution directory -->
<TestSessionTimeout>120000</TestSessionTimeout><!-- Milliseconds -->
<TestSessionTimeout>60000</TestSessionTimeout><!-- Milliseconds -->
<TargetFrameworkVersion>net48</TargetFrameworkVersion>
<TargetPlatform>x64</TargetPlatform>
</RunConfiguration>
<nanoFrameworkAdapter>
<Logging>None</Logging> <!--Set to the desired level of logging for Unit Test execution. Possible values are: None, Detailed, Verbose, Error. -->
<IsRealHardware>True</IsRealHardware><!--Set to true to run tests on real hardware. -->
<RealHardwarePort>COM3</RealHardwarePort><!--Specify the COM port to use to connect to a nanoDevice. If none is specified, a device detection is performed and the 1st available one will be used. -->
<Logging>Verbose</Logging> <!--Set to the desired level of logging for Unit Test execution. Possible values are: None, Detailed, Verbose, Error. -->
<IsRealHardware>False</IsRealHardware><!--Set to true to run tests on real hardware. -->
<CLRVersion></CLRVersion><!--Specify the nanoCLR version to use. If not specified, the latest available will be used. -->
<PathToLocalCLRInstance></PathToLocalCLRInstance><!--Specify the path to a local nanoCLR instance. If not specified, the default one installed with nanoclr CLR witll be used. -->
</nanoFrameworkAdapter>
Expand Down
2 changes: 2 additions & 0 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 99c68d1

Please sign in to comment.