Moved temporary files into a folder of their own below the data directory (#1245)

This commit is contained in:
Thorsten Sommer authored and GitHub committed 2026-11-29 18:45:34 +01:00
1 parent ddfc632b63
commit 12d3e1ec6c
52 files changed
+1219 -396

No files matched your search

@@ -56,7 +56,7 @@ public sealed class DataBackupWriterTests
this.WriteDataFile("visualBriefings/briefing/manifest.json", "{}");
this.WriteDataFile("beyondFactsLab/preparation/project.json", "{}");
this.WriteDataFile("modelComparisons/presets/preset.json", "{}");
this.WriteDataFile("media-staging/operation/transcript.md", "temporary");
this.WriteDataFile("tmp/operation/transcript.md", "temporary");
this.WriteDataFile("databases/sqlite/rag-index.sqlite3", "the index while AI Studio writes to it");
this.WriteDataFile("databases/provenance/provenance.sqlite3", "the provenance store while AI Studio writes to it");
this.WriteDataFile("databases/provenance/provenance.sqlite3-wal", "the write-ahead log of the provenance store");
+3 -4
View File
@@ -1,9 +1,8 @@
using AIStudio.Tools.OpenXml;
namespace AIStudio.Tests.Tools.OpenXml;
/// <summary>
/// The folder in which StagedDocument puts its files. Tests have no data directory, so it lies in the temporary folder.
/// The folder in which StagedDocument puts its files: the temporary folder of AI Studio, which lies below the data
/// directory of the tests, see TestHost.
/// </summary>
internal static class StagingFolder
{
@@ -12,7 +11,7 @@ internal static class StagingFolder
/// </summary>
public static HashSet<string> Files()
{
var directory = Path.Join(Path.GetTempPath(), StagedDocument.DIRECTORY);
var directory = Path.Join(TestHost.DATA_DIRECTORY, "tmp");
return Directory.Exists(directory) ? Directory.EnumerateFiles(directory).ToHashSet() : [];
}
}
@@ -0,0 +1,51 @@
using System.ComponentModel;
using System.Diagnostics;
using AIStudio.Tools;
namespace AIStudio.Tests.Tools;
/// <summary>
/// Checks which failures to start Pandoc mean that it is not installed.
/// </summary>
/// <remarks>
/// Most users have no Pandoc until they install it through AI Studio. The log notes that once, without a stack trace,
/// and keeps errors with their stack traces for what nobody expects. The exception comes from a real Process.Start, since
/// only it shows what each OS reports for a program which does not exist.
/// </remarks>
[TestFixture]
public sealed class PandocMissingExecutableTests
{
[Test]
public void AProgramWhichDoesNotExistCounts()
{
var startInfo = new ProcessStartInfo($"ai-studio-missing-program-{Guid.NewGuid():N}")
{
ArgumentList = { "--version" },
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = true
};
var failure = Assert.Catch(() => Process.Start(startInfo)?.Dispose());
Assert.That(Pandoc.IsMissingExecutable(failure!), Is.True, failure?.ToString());
}
[Test]
public void AProgramWhichCannotBeStartedDoesNotCount()
{
// Windows reports a denied access as ERROR_ACCESS_DENIED (5), macOS and Linux as EACCES (13):
var failure = new Win32Exception(OperatingSystem.IsWindows() ? 5 : 13);
Assert.That(Pandoc.IsMissingExecutable(failure), Is.False);
}
[Test]
public void AnotherExceptionDoesNotCount()
{
var failure = new InvalidOperationException("No process is associated with this object.");
Assert.That(Pandoc.IsMissingExecutable(failure), Is.False);
}
}
@@ -0,0 +1,76 @@
using AIStudio.Tools.PluginSystem;
namespace AIStudio.Tests.Tools.PluginSystem;
/// <summary>
/// Checks how a deployment which AI Studio downloaded from the configuration server of an organization reaches its place.
/// </summary>
/// <remarks>
/// AI Studio unpacks the deployment in its temporary folder and moves it to plugins/.config afterward. That folder
/// is missing on a new installation until the first deployment arrives, and once somebody deleted it.
/// </remarks>
[TestFixture]
public sealed class ConfigPluginDeploymentTests
{
private string dataDirectory = string.Empty;
[SetUp]
public void CreateDataDirectory()
{
// A user name beyond ASCII, as in the path of many data directories:
this.dataDirectory = Path.Combine(Path.GetTempPath(), $"ai-studio-deployment-ä-ж-日本-{Guid.NewGuid():N}");
Directory.CreateDirectory(Path.Combine(this.dataDirectory, "plugins"));
}
[TearDown]
public void DeleteDataDirectory()
{
if (Directory.Exists(this.dataDirectory))
Directory.Delete(this.dataDirectory, true);
}
[Test]
public void TheFirstDeploymentArrivesAlthoughItsFolderIsMissing()
{
var stagedDirectory = this.StageDeployment("ID = \"first\"");
var deploymentDirectory = Path.Combine(this.dataDirectory, "plugins", ".config", Guid.NewGuid().ToString());
PluginFactory.MoveDeploymentIntoPlace(stagedDirectory, deploymentDirectory);
using (Assert.EnterMultipleScope())
{
Assert.That(File.ReadAllText(Path.Combine(deploymentDirectory, "plugin.lua")), Is.EqualTo("ID = \"first\""));
Assert.That(Directory.Exists(stagedDirectory), Is.False);
}
}
[Test]
public void ADeploymentArrivesNextToTheOthers()
{
var otherDeploymentDirectory = Path.Combine(this.dataDirectory, "plugins", ".config", Guid.NewGuid().ToString());
Directory.CreateDirectory(otherDeploymentDirectory);
File.WriteAllText(Path.Combine(otherDeploymentDirectory, "plugin.lua"), "ID = \"other\"");
var stagedDirectory = this.StageDeployment("ID = \"next\"");
var deploymentDirectory = Path.Combine(this.dataDirectory, "plugins", ".config", Guid.NewGuid().ToString());
PluginFactory.MoveDeploymentIntoPlace(stagedDirectory, deploymentDirectory);
using (Assert.EnterMultipleScope())
{
Assert.That(File.ReadAllText(Path.Combine(deploymentDirectory, "plugin.lua")), Is.EqualTo("ID = \"next\""));
Assert.That(File.ReadAllText(Path.Combine(otherDeploymentDirectory, "plugin.lua")), Is.EqualTo("ID = \"other\""));
}
}
/// <summary>
/// Places an unpacked deployment in the temporary folder below the data directory, as the download does.
/// </summary>
private string StageDeployment(string pluginCode)
{
var stagedDirectory = Path.Combine(this.dataDirectory, "tmp", Guid.NewGuid().ToString("N"), "deployment");
Directory.CreateDirectory(stagedDirectory);
File.WriteAllText(Path.Combine(stagedDirectory, "plugin.lua"), pluginCode);
return stagedDirectory;
}
}
+193
View File
@@ -0,0 +1,193 @@
using AIStudio.Tools;
using Microsoft.Extensions.Logging.Abstractions;
namespace AIStudio.Tests.Tools;
/// <summary>
/// Checks the temporary folder of AI Studio below its data directory: where its files go, that starting and
/// stopping empty it, and that emptying it never deletes anything outside.
/// </summary>
[TestFixture]
[NonParallelizable]
public sealed class TemporaryFilesTests
{
private string dataDirectory = string.Empty;
private string outsideDirectory = string.Empty;
private string TemporaryFolder => Path.Join(this.dataDirectory, "tmp");
[SetUp]
public void CreateDirectories()
{
this.dataDirectory = Path.Join(Path.GetTempPath(), $"ai-studio-temporary-files-{Guid.NewGuid():N}");
this.outsideDirectory = Path.Join(Path.GetTempPath(), $"ai-studio-temporary-files-outside-{Guid.NewGuid():N}");
Directory.CreateDirectory(this.dataDirectory);
Directory.CreateDirectory(this.outsideDirectory);
}
[TearDown]
public void RestoreTheFolderOfTheTests()
{
TemporaryFiles.Initialize(TestHost.DATA_DIRECTORY, NullLogger.Instance);
foreach (var directory in new[] { this.dataDirectory, this.outsideDirectory })
{
if (Directory.Exists(directory))
Directory.Delete(directory, true);
}
}
[Test]
public void ANewFileLiesInTheTemporaryFolderAndDoesNotExistYet()
{
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
var first = TemporaryFiles.GetFilePath(".webm");
var second = TemporaryFiles.GetFilePath(".webm");
using (Assert.EnterMultipleScope())
{
Assert.That(Path.GetDirectoryName(first), Is.EqualTo(this.TemporaryFolder));
Assert.That(Path.GetExtension(first), Is.EqualTo(".webm"));
Assert.That(File.Exists(first), Is.False);
Assert.That(Directory.Exists(this.TemporaryFolder), Is.True);
Assert.That(second, Is.Not.EqualTo(first));
}
}
[Test]
public void ANewFolderLiesInTheTemporaryFolderAndIsEmpty()
{
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
var first = TemporaryFiles.CreateDirectory();
var second = TemporaryFiles.CreateDirectory();
using (Assert.EnterMultipleScope())
{
Assert.That(Path.GetDirectoryName(first), Is.EqualTo(this.TemporaryFolder));
Assert.That(Directory.Exists(first), Is.True);
Assert.That(Directory.EnumerateFileSystemEntries(first), Is.Empty);
Assert.That(second, Is.Not.EqualTo(first));
}
}
[Test]
public async Task StartingEmptiesTheTemporaryFolderAndTheFoldersOfEarlierVersions()
{
var leftover = Path.Join(this.TemporaryFolder, "recording.webm");
var nestedLeftover = Path.Join(this.TemporaryFolder, "briefing", "picture.png");
var attachmentOfAnEarlierVersion = Path.Join(this.dataDirectory, "mailAttachments", "invoice.pdf");
var pandocOfAnInterruptedInstallation = Path.Join(this.dataDirectory, "pandoc-install-0123456789abcdef", "pandoc");
var stagedDocument = Path.Join(this.dataDirectory, ".document-staging", "0123456789abcdef.docx");
var stagedPlugin = Path.Join(this.dataDirectory, ".plugin-staging", "plugin-import.staging-0123456789abcdef", "plugin.lua");
var stagedTranscript = Path.Join(this.dataDirectory, "media-staging", "0123456789abcdef", "transcript.md");
var chat = Path.Join(this.dataDirectory, "workspaces", "chat.json");
var pandoc = Path.Join(this.dataDirectory, "pandoc", "pandoc");
foreach (var path in new[] { leftover, nestedLeftover, attachmentOfAnEarlierVersion, pandocOfAnInterruptedInstallation, stagedDocument, stagedPlugin, stagedTranscript, chat, pandoc })
{
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
await File.WriteAllTextAsync(path, "content");
}
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
using (Assert.EnterMultipleScope())
{
Assert.That(Directory.Exists(this.TemporaryFolder), Is.False);
Assert.That(Directory.Exists(Path.Join(this.dataDirectory, "mailAttachments")), Is.False);
Assert.That(Directory.Exists(Path.GetDirectoryName(pandocOfAnInterruptedInstallation)), Is.False);
Assert.That(Directory.Exists(Path.Join(this.dataDirectory, ".document-staging")), Is.False, "A folder whose name begins with a dot counts as hidden on Linux and macOS.");
Assert.That(Directory.Exists(Path.Join(this.dataDirectory, ".plugin-staging")), Is.False);
Assert.That(Directory.Exists(Path.Join(this.dataDirectory, "media-staging")), Is.False);
Assert.That(File.ReadAllText(chat), Is.EqualTo("content"));
Assert.That(File.ReadAllText(pandoc), Is.EqualTo("content"));
}
}
[Test]
public async Task ClearingEmptiesTheTemporaryFolder()
{
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
var file = TemporaryFiles.GetFilePath(".md");
await File.WriteAllTextAsync(file, "content");
var folder = TemporaryFiles.CreateDirectory();
TemporaryFiles.Clear(NullLogger.Instance);
using (Assert.EnterMultipleScope())
{
Assert.That(File.Exists(file), Is.False);
Assert.That(Directory.Exists(folder), Is.False);
Assert.That(Path.GetDirectoryName(TemporaryFiles.GetFilePath(".md")), Is.EqualTo(this.TemporaryFolder), "The folder is created again on demand.");
}
}
[TestCase("")]
[TestCase("relative/data")]
public async Task ADataDirectoryWithoutAnAbsolutePathDeletesNothing(string dataDirectoryOfTheRuntime)
{
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
var file = TemporaryFiles.GetFilePath(".md");
await File.WriteAllTextAsync(file, "content");
TemporaryFiles.Initialize(dataDirectoryOfTheRuntime, NullLogger.Instance);
using (Assert.EnterMultipleScope())
{
Assert.That(File.Exists(file), Is.True);
Assert.That(Path.GetDirectoryName(TemporaryFiles.GetFilePath(".md")), Is.EqualTo(this.TemporaryFolder));
}
}
[Test]
public async Task ALinkInTheTemporaryFolderIsRemovedButNotFollowed()
{
var precious = Path.Join(this.outsideDirectory, "precious.md");
await File.WriteAllTextAsync(precious, "Precious");
Directory.CreateDirectory(this.TemporaryFolder);
try
{
Directory.CreateSymbolicLink(Path.Join(this.TemporaryFolder, "folder-link"), this.outsideDirectory);
File.CreateSymbolicLink(Path.Join(this.TemporaryFolder, "file-link.md"), precious);
}
catch (Exception exception) when (exception is UnauthorizedAccessException or IOException)
{
Assert.Ignore("Creating symbolic links needs a privilege this system does not grant.");
}
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
using (Assert.EnterMultipleScope())
{
Assert.That(Directory.Exists(this.TemporaryFolder), Is.False);
Assert.That(File.ReadAllText(precious), Is.EqualTo("Precious"));
}
}
[Test]
public async Task ATemporaryFolderWhichIsALinkIsRemovedButNotFollowed()
{
var precious = Path.Join(this.outsideDirectory, "precious.md");
await File.WriteAllTextAsync(precious, "Precious");
try
{
Directory.CreateSymbolicLink(this.TemporaryFolder, this.outsideDirectory);
}
catch (Exception exception) when (exception is UnauthorizedAccessException or IOException)
{
Assert.Ignore("Creating symbolic links needs a privilege this system does not grant.");
}
TemporaryFiles.Initialize(this.dataDirectory, NullLogger.Instance);
using (Assert.EnterMultipleScope())
{
Assert.That(new DirectoryInfo(this.TemporaryFolder).LinkTarget, Is.Null);
Assert.That(File.ReadAllText(precious), Is.EqualTo("Precious"));
}
}
}