Unit tests

- Renamed GlobalUsings.cs to Usings.cs
- Refractored SysFS
- Seperated building of the terminal and unit tests
- To save on resources, the unit tests job on runs on Ubuntu
This commit is contained in:
Tony Bark 2023-01-07 14:31:09 -05:00
parent 19f7483fbd
commit c10e753c39
11 changed files with 237 additions and 127 deletions

View file

@ -28,3 +28,22 @@ jobs:
run: dotnet restore src/Tomas.Terminal
- name: Build
run: dotnet build src/Tomas.Terminal -c Release --no-restore
test:
timeout-minutes: 15
continue-on-error: true
runs-on: ubuntu-latest
strategy:
matrix:
dotnet: ["6.0.x"]
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: ${{ matrix.dotnet }}
- name: Install test dependencies
run: dotnet restore src/Tomas.Tests
- name: Test
run: dotnet test src --no-restore --debug

View file

@ -22,6 +22,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tomas.Terminal", "Tomas.Ter
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Tomas.Kernel", "Tomas.Kernel\Tomas.Kernel.csproj", "{B70BDFD5-64BA-4FCE-A00F-DDD209C2C0FB}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tomas.Tests", "Tomas.Tests\Tomas.Tests.csproj", "{76AD2140-2975-43DA-89A9-0BEC70B2ECDD}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -44,6 +46,10 @@ Global
{B70BDFD5-64BA-4FCE-A00F-DDD209C2C0FB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B70BDFD5-64BA-4FCE-A00F-DDD209C2C0FB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B70BDFD5-64BA-4FCE-A00F-DDD209C2C0FB}.Release|Any CPU.Build.0 = Release|Any CPU
{76AD2140-2975-43DA-89A9-0BEC70B2ECDD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{76AD2140-2975-43DA-89A9-0BEC70B2ECDD}.Debug|Any CPU.Build.0 = Debug|Any CPU
{76AD2140-2975-43DA-89A9-0BEC70B2ECDD}.Release|Any CPU.ActiveCfg = Release|Any CPU
{76AD2140-2975-43DA-89A9-0BEC70B2ECDD}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE

View file

View file

@ -8,152 +8,138 @@ namespace Tomas.Kernel;
static class SysFS
{
// The root directory of the file system
public const string ROOT_DIR = "0:\\";
// The system directory, located in the root directory
static string SYSTEM_DIR = $"{ROOT_DIR}\\SYSTEM\\";
// The root directory of the file system
public const string ROOT_DIR = "0:\\";
// The system directory, located in the root directory
static string SYSTEM_DIR = $"{ROOT_DIR}\\SYSTEM\\";
static string LOG_FILE = $"{SYSTEM_DIR}system.log";
static string LOG_FILE = $"{SYSTEM_DIR}system.log";
public const string FS_ERROR = "File system disabled.";
public const string FS_ERROR = "File system disabled.";
/// <summary>
/// An instance of the CosmosVFS class, used for accessing the virtual file system
/// </summary>
static CosmosVFS fileSystem = new();
/// <summary>
/// An instance of the CosmosVFS class, used for accessing the virtual file system
/// </summary>
static CosmosVFS fileSystem = new();
/// <summary>
/// Initializes the file system by creating the system directory and sysinfo.txt file
/// and setting the IsFSActive property of the SysMeta class to true
/// </summary>
public static void Initialize()
{
try
{
var createSysFiles = "Creating system files.";
var setSysPref = "Writing system info.";
var fsSuccess = "File system succesfully initialized.";
var sysInfoFile = "sysinfo.txt";
/// <summary>
/// Initializes the file system by creating the system directory and sysinfo.txt file
/// and setting the IsFSActive property of the SysMeta class to true
/// </summary>
public static void Initialize()
{
try
{
// File to store system information
const string sysInfoFile = "sysinfo.txt";
// Register the CosmosVFS instance as the virtual file system
VFSManager.RegisterVFS(fileSystem);
// Create system directory if it doesn't exist
if (!Directory.Exists(SYSTEM_DIR))
fileSystem.CreateDirectory(SYSTEM_DIR);
// Create the system directory
if (!Directory.Exists(SYSTEM_DIR))
fileSystem.CreateDirectory(SYSTEM_DIR);
// Create system log file if it doesn't exist
if (!File.Exists($"{SYSTEM_DIR}{LOG_FILE}"))
fileSystem.CreateFile($"{SYSTEM_DIR}{LOG_FILE}");
// Create the system.log file in the system directory
if (!File.Exists($"{SYSTEM_DIR}{LOG_FILE}"))
fileSystem.CreateFile($"{SYSTEM_DIR}{LOG_FILE}");
// Create sysinfo.txt file if it doesn't exist
if (!File.Exists($"{SYSTEM_DIR}{sysInfoFile}"))
fileSystem.CreateFile($"{SYSTEM_DIR}{sysInfoFile}");
Console.WriteLine(createSysFiles);
File.AppendAllText(LOG_FILE, createSysFiles);
// Write system name, version, and build number to sysinfo.txt file
File.WriteAllText($"{SYSTEM_DIR}sysinfo.txt", $"{SysMeta.NAME} v{SysMeta.VERSION} ({SysMeta.BuildNumber})");
// Create the sysinfo.txt file in the system directory
if (!File.Exists($"{SYSTEM_DIR}{sysInfoFile}"))
fileSystem.CreateFile($"{SYSTEM_DIR}{sysInfoFile}");
// Set IsFSEnabled property of SysMeta class to true
SysMeta.IsFSEnabled = true;
Console.WriteLine(setSysPref);
// Read contents of sysinfo.txt file and print to console
var systemInfo = File.ReadAllText($"{SYSTEM_DIR}sysinfo.txt");
Console.WriteLine(systemInfo);
}
catch (Exception err)
{
// Print error message if an exception is caught
Console.WriteLine($"{err.Message}{Environment.NewLine}Warning: Error messages will not logged.");
}
}
File.AppendAllText(LOG_FILE, setSysPref);
// Write the system name, version, and build number to the sysinfo.txt file
File.WriteAllText($"{SYSTEM_DIR}sysinfo.txt", $"{SysMeta.NAME} v{SysMeta.VERSION} ({SysMeta.BuildNumber})");
Console.WriteLine(fsSuccess);
File.AppendAllText(LOG_FILE, fsSuccess);
/// <summary>
/// Creates a new directory at the specified path
/// </summary>
/// <param name="directory">directory</param>
public static void CreateDirectory(string directory)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
// Set the IsFSActive property of the SysMeta class to true
SysMeta.IsFSEnabled = true;
// Create the directory using the CosmosVFS instance
if (!Directory.Exists($"{ROOT_DIR}\\{directory}"))
fileSystem.CreateDirectory($"{ROOT_DIR}\\{directory}");
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
}
}
// Read the contents of the sysinfo.txt file and print it to the console
var systemInfo = File.ReadAllText($"{SYSTEM_DIR}sysinfo.txt");
/// <summary>
/// Creates a new file at the specified path
/// </summary>
/// <param name="path">file path</param>
/// <param name="file">file</param>
public static void CreateFile(string path, string file)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
Console.WriteLine(systemInfo);
}
catch (Exception err)
{
// If an exception is caught, print an error message indicating that the file system failed to load
Console.WriteLine($"{err.Message}{Environment.NewLine}Warning: Error messages will not logged.");
}
}
// Create the file using the CosmosVFS instance
if (!File.Exists($"{ROOT_DIR}\\{path}\\{file}"))
fileSystem.CreateFile($"{ROOT_DIR}\\{path}\\{file}");
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
}
}
/// <summary>
/// Creates a new directory at the specified path
/// </summary>
/// <param name="directory">directory</param>
public static void CreateDirectory(string directory)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
/// <summary>
/// Lists all directories in the specified path
/// </summary>
/// <param name="path">path to directory</param>
/// <returns>returns a list of directories</returns>
public static string[] ListDirectories(string path)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
// Create the directory using the CosmosVFS instance
if (!Directory.Exists($"{ROOT_DIR}\\{directory}"))
fileSystem.CreateDirectory($"{ROOT_DIR}\\{directory}");
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
}
}
// Get the directories in the specified path using the Directory.GetDirectories method
var dirs = Directory.GetDirectories(path);
/// <summary>
/// Creates a new file at the specified path
/// </summary>
/// <param name="path">file path</param>
/// <param name="file">file</param>
public static void CreateFile(string path, string file)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
// Return the directories
return dirs;
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
// Create the file using the CosmosVFS instance
if (!File.Exists($"{ROOT_DIR}\\{path}\\{file}"))
fileSystem.CreateFile($"{ROOT_DIR}\\{path}\\{file}");
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
}
}
/// <summary>
/// Lists all directories in the specified path
/// </summary>
/// <param name="path">path to directory</param>
/// <returns>returns a list of directories</returns>
public static string[] ListDirectories(string path)
{
try
{
// If file system isn't enabeld, throw exception
if (!SysMeta.IsFSEnabled)
throw new IOException(FS_ERROR);
// Get the directories in the specified path using the Directory.GetDirectories method
var dirs = Directory.GetDirectories(path);
// Return the directories
return dirs;
}
catch (IOException err)
{
// If an exception is caught, print an error message indicating the error
Console.WriteLine(err.Message);
File.AppendAllText(LOG_FILE, err.Message);
throw;
}
}
throw;
}
}
}

View file

@ -0,0 +1,16 @@
/*
In jurisdictions that recognize copyright waivers, I've waived all copyright
and related or neighboring rights for to this project. In areas where these
waivers are not recognized, BSD-3-Clause is enforced.
See the (UN)LICENSE file in the project root for more information.
*/
namespace Tomas.Tests.Shell;
internal class MockProgram : IProgram
{
public bool Run(IShell shell)
{
Debug.WriteLine("Test Program.");
return true;
}
}

View file

@ -0,0 +1,17 @@
/*
In jurisdictions that recognize copyright waivers, I've waived all copyright
and related or neighboring rights for to this project. In areas where these
waivers are not recognized, BSD-3-Clause is enforced.
See the (UN)LICENSE file in the project root for more information.
*/
namespace Tomas.Tests.Shell;
internal class MockShell : IShell
{
public string? ReadLine { get; }
public Dictionary<string, IProgram> Programs => new()
{
{ "test", new MockProgram() },
};
}

View file

@ -0,0 +1,26 @@
/*
In jurisdictions that recognize copyright waivers, I've waived all copyright
and related or neighboring rights for to this project. In areas where these
waivers are not recognized, BSD-3-Clause is enforced.
See the (UN)LICENSE file in the project root for more information.
*/
using Tomas.Tests.Shell;
namespace Tomas.Tests;
public class ShellTests
{
// Create a new instance of the mock shell
readonly MockShell _mockShell = new();
[Fact]
public void ProgramTest()
{
// Create a mock program
var program = new MockProgram();
// Assert that the Run method of the program and returns true when passed the shell object.
Assert.True(program.Run(_mockShell));
}
}

View file

@ -0,0 +1,29 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Tomas.Core\Tomas.Core.csproj" />
<ProjectReference Include="..\Tomas.Interface\Tomas.Interface.csproj" />
</ItemGroup>
</Project>

11
src/Tomas.Tests/Usings.cs Normal file
View file

@ -0,0 +1,11 @@
/*
In jurisdictions that recognize copyright waivers, I've waived all copyright
and related or neighboring rights for to this project. In areas where these
waivers are not recognized, BSD-3-Clause is enforced.
See the (UN)LICENSE file in the project root for more information.
*/
global using Xunit;
global using System.Diagnostics.CodeAnalysis;
global using System.Diagnostics;
global using Tomas.Core;
global using Tomas.Interface;