Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Stellar.FileSystem.stellar.module
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"$schema": "https://quantumdevteam.github.io/StellarEngine-Core/schemas/stellar.module.schema.json",
"Id": "Stellar.FileSystem",
"Version": "0.9.0",
"Version": "0.11.0",
"Level": "CoreModule",
"Description": "File system of the engine",
"Dependencies": []
Expand Down
63 changes: 63 additions & 0 deletions src/Managed/Stellar.FileSystem/FileSystemEntryPoint.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
using Stellar.Kernel.Configuration;
using Stellar.Kernel.EntryPoint;
using Stellar.Kernel.FileSystem;
using Stellar.Kernel.Services;

namespace Stellar.FileSystem;

/// <summary>
/// Publishes the file system of the engine:
/// app:// is the folder of the application, read-only,
/// user:// the folder of the user.
/// </summary>
[StellarEntryPoint]
public sealed class FileSystemEntryPoint(RuntimeConfiguration configuration)
: StellarEntryPoint(FileSystemModule.Id, configuration)
{
private IServiceCatalog? _services;
private VirtualFileSystem? _fileSystem;

/// <inheritdoc />
public override int Run<TContext>(ref readonly TContext context)
{
if (context.Services is not { } services) return 1;

_services = services;
_fileSystem = VirtualFileSystem.CreateDefault();

_fileSystem.Mount(new DomainMount(
"app", DomainScheme.Directory, AppContext.BaseDirectory,
new Dictionary<string, string> { ["readonly"] = "true" }
));

if (!string.IsNullOrWhiteSpace(RuntimeConfiguration.ProjectName))
{
string company = RuntimeConfiguration.CompanyName;
if (string.IsNullOrWhiteSpace(company) || company == "Unknown") company = "Starlet";
_fileSystem.Mount(new DomainMount(
"user", DomainScheme.Directory, Path.Combine(Environment.GetFolderPath(
Environment.SpecialFolder.ApplicationData),
company, RuntimeConfiguration.ProjectName)
));
}

services.Set<IFileSystem>(_fileSystem);

return 0;
}

/// <inheritdoc />
public override void RequestStop<TContext>(ref readonly TContext context)
{
if (_services is not { } services) return;
_services = null;

if (ReferenceEquals(services.Find<IFileSystem>(), _fileSystem)) services.Remove<IFileSystem>();
}

/// <inheritdoc />
public override void Dispose()
{
_fileSystem?.Dispose();
}
}
188 changes: 188 additions & 0 deletions tests/Stellar.FileSystem.Tests/EntryPointTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,188 @@
using Stellar.Core.EntryPoints;
using Stellar.Core.Services;
using Stellar.Kernel.Configuration;
using Stellar.Kernel.EntryPoint;
using Stellar.Kernel.FileSystem;
using Stellar.Kernel.Services;

namespace Stellar.FileSystem.Tests;

public sealed class EntryPointTests
: IDisposable
{
private static readonly string AppData =
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);

// Names nobody else has: what the tests leave in the real folder of the user is deleted whole
private readonly string _company = "StellarTests-" + Guid.NewGuid().ToString("N");
private readonly string _project = "Project-" + Guid.NewGuid().ToString("N");
private readonly ServiceCatalog _services = new();

public void Dispose()
{
Delete(Path.Combine(AppData, _company));
Delete(Path.Combine(AppData, _project));
Delete(Path.Combine(AppData, "Starlet", _project));
Delete(Path.Combine(AppData, "Unknown", _project));

// Only if it is ours: a folder of somebody else is not empty
string shared = Path.Combine(AppData, "Starlet");
if (Directory.Exists(shared) && !Directory.EnumerateFileSystemEntries(shared).Any()) Directory.Delete(shared);
}

private static void Delete(string path)
{
if (Directory.Exists(path)) Directory.Delete(path, true);
}

private static FileSystemEntryPoint Make(string company, string project)
{
return new FileSystemEntryPoint(new RuntimeConfiguration { CompanyName = company, ProjectName = project });
}

private static int Run(FileSystemEntryPoint entryPoint, IServiceCatalog? services)
{
var context = new RunContext(null, services: services);
return entryPoint.Run(in context);
}

private static void RequestStop(FileSystemEntryPoint entryPoint)
{
var context = new StopContext(null, StopReason.Regular);
entryPoint.RequestStop(in context);
}

private static void Stop(FileSystemEntryPoint entryPoint)
{
RequestStop(entryPoint);
entryPoint.Dispose();
}

[Fact]
public void Run_PublishesTheFileSystem_AndStopTakesItBack()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
Run(entryPoint, _services).Should().Be(0);

var files = _services.Find<IFileSystem>()!;
files.Should().BeOfType<VirtualFileSystem>();
files.Domains.Keys.Should().BeEquivalentTo("app", "user");

Stop(entryPoint);
_services.Find<IFileSystem>().Should().BeNull();
files.Domains.Keys.Should().BeEmpty();
}

[Fact]
public void User_IsTheFolderOfTheProject_OfTheCompany()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
Run(entryPoint, _services);

_services.Find<IFileSystem>()!.WriteAllText(FilePath.Parse("user://a.txt"), "text");
File.ReadAllText(Path.Combine(AppData, _company, _project, "a.txt")).Should().Be("text");

Stop(entryPoint);
File.Exists(Path.Combine(AppData, _company, _project, "a.txt")).Should().BeTrue("the files outlive the engine");
}

[Theory]
[InlineData("Unknown")]
[InlineData("")]
[InlineData(null)]
public void User_OfNoCompany_IsInStarlet(string? company)
{
FileSystemEntryPoint entryPoint = Make(company!, _project);
Run(entryPoint, _services);
Stop(entryPoint);

Directory.Exists(Path.Combine(AppData, "Starlet", _project)).Should().BeTrue();
}

[Theory]
[InlineData("")]
[InlineData(null)]
public void User_OfNoProjectName_IsNotMounted(string? project)
{
FileSystemEntryPoint entryPoint = Make(_company, project!);
Run(entryPoint, _services);
Stop(entryPoint);

Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse();
}

[Fact]
public void User_OfNoProject_IsNotMounted()
{
FileSystemEntryPoint entryPoint = Make(_company, "");
Run(entryPoint, _services).Should().Be(0);

_services.Find<IFileSystem>()!.Domains.Keys.Should().BeEquivalentTo("app");

Stop(entryPoint);
Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse();
}

[Fact]
public void App_CannotBeWritten()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
Run(entryPoint, _services);

var files = _services.Find<IFileSystem>()!;
files.Invoking(it => it.WriteAllText(FilePath.Parse("app://a.txt"), "text"))
.Should().Throw<NotSupportedException>();
File.Exists(Path.Combine(AppContext.BaseDirectory, "a.txt")).Should().BeFalse();

Stop(entryPoint);
}

[Fact]
public void WithoutServices_DoesNotStart()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
Run(entryPoint, null).Should().NotBe(0);

entryPoint.Invoking(Stop).Should().NotThrow();
Directory.Exists(Path.Combine(AppData, _company)).Should().BeFalse();
}

[Fact]
public void Stop_LeavesTheFileSystemOfAnotherModule()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
Run(entryPoint, _services);

using var other = new VirtualFileSystem();
_services.Set<IFileSystem>(other);

Stop(entryPoint);
_services.Find<IFileSystem>().Should().BeSameAs(other);
}

[Fact]
public void TwoEngines_HaveTheirOwnFileSystems()
{
var otherServices = new ServiceCatalog();
FileSystemEntryPoint first = Make(_company, _project);
FileSystemEntryPoint second = Make(_company, _project);
Run(first, _services);
Run(second, otherServices);

Stop(first);
otherServices.Find<IFileSystem>()!.Domains.Keys.Should().BeEquivalentTo("app", "user");

Stop(second);
}

[Fact]
public void Dispose_MayBeRepeated_AndCalledWithoutRun()
{
FileSystemEntryPoint entryPoint = Make(_company, _project);
entryPoint.Invoking(it => it.Dispose()).Should().NotThrow();

Run(entryPoint, _services);
Stop(entryPoint);
entryPoint.Invoking(Stop).Should().NotThrow();
}
}