using AIStudio.Tools; using Microsoft.Extensions.Logging.Abstractions; namespace AIStudio.Tests.Tools; /// /// 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. /// [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")); } } }