diff --git a/docs/ARCHITECTURE.md b/docs/ARCHITECTURE.md index cfdc756a..343b9ef8 100644 --- a/docs/ARCHITECTURE.md +++ b/docs/ARCHITECTURE.md @@ -92,7 +92,7 @@ Dependencies point one way: `Core` ← `Core.Windows`, `Data`, `Protocol` ← `S | Host | Composition root | What it runs | |---|---|---| -| Titano (desktop) | `App/Infrastructure/AppServices` | SQLite index in `%LOCALAPPDATA%\Titano` (or `.\data` when portable); `ArchiveService` with WIC first, then managed decoders; `ProcessingService` with Media Foundation, then managed encoders; one `JobEngine` with the handlers of both; thumbnails; `ServerSession`; `SyncHost` while signed in (sync engine and agent, upload and download handlers, metered hold, editors of server sequences); the render node while opted in and signed in; update check and download (`About/UpdateCheck`, `About/UpdateDownload`, `About/UpdateFlow`: the setup, or the portable zip for a portable copy, verified against `SHA256SUMS`, resumable, cancellable, failing after 60 s without data); diagnostics bundle; daily index backups and the restore staged in Settings › Advanced (`DatabaseBackups`) | +| Titano (desktop) | `App/Infrastructure/AppServices` | SQLite index in `%LOCALAPPDATA%\Titano` (or `.\data` when portable, unless the copy is in a OneDrive folder); `ArchiveService` with WIC first, then managed decoders; `ProcessingService` with Media Foundation, then managed encoders; one `JobEngine` with the handlers of both; thumbnails; `ServerSession`; `SyncHost` while signed in (sync engine and agent, upload and download handlers, metered hold, editors of server sequences); the render node while opted in and signed in; update check and download (`About/UpdateCheck`, `About/UpdateDownload`, `About/UpdateFlow`: the setup, or the portable zip for a portable copy, verified against `SHA256SUMS`, resumable, cancellable, failing after 60 s without data); diagnostics bundle; daily index backups and the restore staged in Settings › Advanced (`DatabaseBackups`) | | Titano Server | `Server/Hosting/ServerHost.Build` | Options from `server.json` + `TITANO_*`; PostgreSQL (or SQLite on Windows); `ArchiveModule` then `ProcessingModule` register job handler providers; `JobEngineHost` (renders only managed formats and MP4 jobs no render node can take, BRIEF §14.7); `RenderNodeService`/`RenderNodeMonitor`; `UploadService`; `EventHub` (WebSocket); embedded admin panel; LAN discovery beacon; Windows service host; daily backups of an SQLite index (`DatabaseBackupService`) | | Titano Uploader | `Uploader/App.xaml.cs` | `TitanoClient`, persisted queue, watch folders; no database | | Titano Setup | `Setup/Program` | TPK1 payload extraction, registry, shortcuts, file types named in setup's language, silent mode, uninstaller; only what starts a program its log or payload lists is changed: Windows services of those programs stopped once the programs have closed, started again (also after a failure) or deleted through the Service Control Manager (an elevated `/SERVICES … /NAMES` step when needed), sign-in entries of those programs removed, shared shortcuts handed to the product that keeps the program; the Server only for all users inside Program Files; "Remove my data" limited to the programs no other installed product uses | diff --git a/src/Titano.App/App.xaml.cs b/src/Titano.App/App.xaml.cs index 52a7fee7..9da130e6 100644 --- a/src/Titano.App/App.xaml.cs +++ b/src/Titano.App/App.xaml.cs @@ -45,6 +45,8 @@ public partial class App : Application // Capture runs in CI: mirror the log to stderr so failures are visible in the job output. Log.SetSink(capture ? new TeeLogSink(_log, new StderrLogSink()) : _log); Log.Info("app", $"Titano {AppServices.VersionText} starting; data at {Environment.DataDirectory}"); + // Settings tells the user once the window is up. + if (Environment.SyncedFolder is { } synced) Log.Warning("app", $"This portable copy runs in the OneDrive folder {synced}: its data is kept outside it"); var projectFile = capture ? null : ProjectFileArgument(e.Args); if (!capture) diff --git a/src/Titano.App/Infrastructure/AppEnvironment.cs b/src/Titano.App/Infrastructure/AppEnvironment.cs index 349f91bd..06c60555 100644 --- a/src/Titano.App/Infrastructure/AppEnvironment.cs +++ b/src/Titano.App/Infrastructure/AppEnvironment.cs @@ -1,18 +1,22 @@ using System.IO; +using Titano.Core.Windows.Shell; namespace Titano.App.Infrastructure; /// -/// Where the client keeps its data (BRIEF §6.2): %LOCALAPPDATA%\Titano, or .\data next to the -/// executable when portable.txt exists. Never inside Documents/OneDrive (SQLite corrupts in synced folders). +/// Where the client keeps its data (BRIEF §6.2, ): %LOCALAPPDATA%\Titano, or .\data +/// next to the executable when portable.txt exists. Never inside a folder OneDrive synchronises (SQLite corrupts +/// there): a portable copy in one keeps its data in %LOCALAPPDATA%\Titano, and Settings tells the user so +/// (). /// public sealed class AppEnvironment { - private AppEnvironment(string dataDirectory, bool portable, bool isCapture) + private AppEnvironment(string dataDirectory, bool portable, bool isCapture, string? syncedFolder = null) { DataDirectory = dataDirectory; IsPortable = portable; IsCapture = isCapture; + SyncedFolder = syncedFolder; Directory.CreateDirectory(DataDirectory); Directory.CreateDirectory(LogDirectory); Directory.CreateDirectory(CacheDirectory); @@ -24,20 +28,20 @@ public sealed class AppEnvironment /// A throw-away environment for --capture: nothing of the user's data is read or written. public bool IsCapture { get; } + /// The OneDrive folder this portable copy runs from; its data is in %LOCALAPPDATA%\Titano instead of .\data. Null otherwise. + public string? SyncedFolder { get; } + public string LogDirectory => Path.Combine(DataDirectory, "logs"); public string CacheDirectory => Path.Combine(DataDirectory, "cache"); public string BackupDirectory => Path.Combine(DataDirectory, "backups"); public string DatabasePath => Path.Combine(DataDirectory, "titano.db"); public string SettingsPath => Path.Combine(DataDirectory, "settings.json"); - public static AppEnvironment Detect() - { - var exeDir = AppContext.BaseDirectory; - if (File.Exists(Path.Combine(exeDir, "portable.txt"))) - return new AppEnvironment(Path.Combine(exeDir, "data"), portable: true, isCapture: false); - var local = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData); - return new AppEnvironment(Path.Combine(local, "Titano"), portable: false, isCapture: false); - } + /// The environment of this process: its executable's folder and the user's OneDrive folders decide. + public static AppEnvironment Detect() => From(AppPaths.Resolve()); + + /// The environment at (its folders are created). + public static AppEnvironment From(AppPaths paths) => new(paths.DataRoot, paths.IsPortable, isCapture: false, paths.PortableSyncRoot); /// A fresh temporary data folder for screenshots and smoke tests; delete it with . public static AppEnvironment ForCapture() => diff --git a/src/Titano.Core.Windows/Shell/AppPaths.cs b/src/Titano.Core.Windows/Shell/AppPaths.cs index e2cfd70e..7bd02b74 100644 --- a/src/Titano.Core.Windows/Shell/AppPaths.cs +++ b/src/Titano.Core.Windows/Shell/AppPaths.cs @@ -1,22 +1,28 @@ +using Titano.Core.IO; + namespace Titano.Core.Windows.Shell; /// -/// Where the desktop programs keep their data (BRIEF §6.2): %LOCALAPPDATA%\Titano, or .\data next to the -/// executable in portable mode. SQLite corrupts inside synchronised folders, so a data root under OneDrive is -/// reported by and refused by . +/// Where the desktop programs keep their data (BRIEF §6.2, ): %LOCALAPPDATA%\Titano, or +/// .\data next to the executable in portable mode. SQLite corrupts inside synchronised folders, so a portable +/// copy in a OneDrive folder keeps its data in %LOCALAPPDATA%\Titano instead ( +/// names that folder to tell the user), and a data root still under OneDrive is reported by +/// and refused by . /// public sealed class AppPaths { - public const string ProductFolderName = "Titano"; + public const string ProductFolderName = DataLocation.ProductFolderName; public const string DatabaseFileName = "titano.db"; - private AppPaths(string dataRoot, bool isPortable, string? syncedFolder) + private AppPaths(DataLocation location) { - DataRoot = dataRoot; - IsPortable = isPortable; - SyncedFolder = syncedFolder; + DataRoot = location.Root; + IsPortable = location.IsPortable; + SyncedFolder = location.SyncedFolder; + PortableSyncRoot = location.PortableSyncRoot; } + /// A portable copy, also when its data had to go to %LOCALAPPDATA%\Titano. public bool IsPortable { get; } public string DataRoot { get; } @@ -34,6 +40,9 @@ public sealed class AppPaths public bool IsInSyncedFolder => SyncedFolder is not null; + /// The OneDrive sync root a portable copy runs from, whose data is in %LOCALAPPDATA%\Titano instead; null otherwise. + public string? PortableSyncRoot { get; } + /// Paths for this process: the executable's folder decides portable mode. public static AppPaths Resolve() => Resolve(AppContext.BaseDirectory, KnownFolders.LocalAppData); @@ -41,14 +50,9 @@ public sealed class AppPaths public static AppPaths Resolve(string applicationDirectory, string localAppData) => Resolve(applicationDirectory, localAppData, OneDriveFolders.SyncRoots()); - internal static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyList syncRoots) - { - var portable = PortableMode.IsEnabled(applicationDirectory); - var root = Path.GetFullPath(portable - ? Path.Combine(applicationDirectory, PortableMode.DataFolderName) - : Path.Combine(localAppData, ProductFolderName)); - return new AppPaths(root, portable, OneDriveFolders.Containing(root, syncRoots)); - } + /// Paths for an executable in , checked against . + public static AppPaths Resolve(string applicationDirectory, string localAppData, IReadOnlyCollection syncRoots) => + new(DataLocation.Choose(applicationDirectory, PortableMode.IsEnabled(applicationDirectory), localAppData, syncRoots)); /// Creates the data, logs, cache and thumbnails folders. /// The data root lies inside a OneDrive-synced folder. diff --git a/src/Titano.Core.Windows/Shell/OneDriveFolders.cs b/src/Titano.Core.Windows/Shell/OneDriveFolders.cs index b37a5a67..3b9ce98a 100644 --- a/src/Titano.Core.Windows/Shell/OneDriveFolders.cs +++ b/src/Titano.Core.Windows/Shell/OneDriveFolders.cs @@ -1,4 +1,6 @@ +using System.Security; using Microsoft.Win32; +using Titano.Core.IO; namespace Titano.Core.Windows.Shell; @@ -10,34 +12,34 @@ public static class OneDriveFolders /// /// Sync roots from the OneDrive environment variables and HKCU\Software\Microsoft\OneDrive\Accounts\*\UserFolder, - /// as full paths without a trailing separator, distinct (case-insensitive). + /// as full paths without a trailing separator, distinct (case-insensitive). An account key that cannot be read is + /// skipped: the programs start all the same. /// public static IReadOnlyList SyncRoots() { var roots = new List(); foreach (var variable in EnvironmentVariables) Add(roots, Environment.GetEnvironmentVariable(variable)); - using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey); - if (accounts is not null) + try { - foreach (var name in accounts.GetSubKeyNames()) + using var accounts = Registry.CurrentUser.OpenSubKey(AccountsKey); + if (accounts is not null) { - using var account = accounts.OpenSubKey(name); - Add(roots, account?.GetValue("UserFolder") as string); + foreach (var name in accounts.GetSubKeyNames()) + { + using var account = accounts.OpenSubKey(name); + Add(roots, account?.GetValue("UserFolder") as string); + } } } + catch (Exception ex) when (ex is SecurityException or IOException or UnauthorizedAccessException) + { + // The environment variables name the usual roots. + } return roots; } /// The sync root that contains , or null. Both are expected to be full paths. - public static string? Containing(string path, IEnumerable syncRoots) => syncRoots.FirstOrDefault(root => IsUnder(path, root)); - - /// True when equals or lies below it (whole path segments, case-insensitive). - internal static bool IsUnder(string path, string root) - { - var trimmedRoot = root.TrimEnd('\\', '/'); - if (trimmedRoot.Length == 0 || !path.StartsWith(trimmedRoot, StringComparison.OrdinalIgnoreCase)) return false; - return path.Length == trimmedRoot.Length || path[trimmedRoot.Length] is '\\' or '/'; - } + public static string? Containing(string path, IEnumerable syncRoots) => DataLocation.SyncRootOf(path, syncRoots); private static void Add(List roots, string? candidate) { diff --git a/src/Titano.Core.Windows/Shell/PortableMode.cs b/src/Titano.Core.Windows/Shell/PortableMode.cs index 2c438a37..c8f48f1c 100644 --- a/src/Titano.Core.Windows/Shell/PortableMode.cs +++ b/src/Titano.Core.Windows/Shell/PortableMode.cs @@ -1,10 +1,12 @@ +using Titano.Core.IO; + namespace Titano.Core.Windows.Shell; /// Portable mode (BRIEF §6.2): a portable.txt next to the executable keeps all data under .\data\. public static class PortableMode { public const string MarkerFileName = "portable.txt"; - public const string DataFolderName = "data"; + public const string DataFolderName = DataLocation.PortableFolderName; public static bool IsEnabled(string applicationDirectory) => File.Exists(Path.Combine(applicationDirectory, MarkerFileName)); } diff --git a/src/Titano.Core/IO/DataLocation.cs b/src/Titano.Core/IO/DataLocation.cs new file mode 100644 index 00000000..d097d59f --- /dev/null +++ b/src/Titano.Core/IO/DataLocation.cs @@ -0,0 +1,52 @@ +namespace Titano.Core.IO; + +/// +/// Where the desktop client keeps its data (BRIEF §6.2): the per-user folder (%LOCALAPPDATA%\Titano), or +/// .\data next to the executable of a portable copy (a portable.txt beside it). SQLite corrupts inside +/// synchronised folders, so a portable copy in a folder OneDrive synchronises (a OneDrive folder, or a Desktop or +/// Documents folder moved into one) keeps its data in the per-user folder instead, and +/// says so, to tell the user. Paths are full paths; a folder lies in a sync root when its whole leading segments are +/// that root's, ignoring case as Windows does. +/// +/// The data folder. +/// The copy is portable, also when its data had to go to the per-user folder. +/// The sync root that contains itself; null when the data is safe there. +/// The sync root a portable copy runs from, whose data went to the per-user folder instead; null otherwise. +public sealed record DataLocation(string Root, bool IsPortable, string? SyncedFolder, string? PortableSyncRoot) +{ + /// The data folder's name under the user's local application data. + public const string ProductFolderName = "Titano"; + + /// The data folder's name next to a portable copy's executable. + public const string PortableFolderName = "data"; + + /// + /// The data folder of a copy in ( when it has a + /// portable.txt), given the user's local application data and the OneDrive sync roots. + /// + public static DataLocation Choose(string applicationDirectory, bool portable, string localAppData, IReadOnlyCollection syncRoots) + { + ArgumentNullException.ThrowIfNull(applicationDirectory); + ArgumentNullException.ThrowIfNull(localAppData); + ArgumentNullException.ThrowIfNull(syncRoots); + var perUser = Path.GetFullPath(Path.Combine(localAppData, ProductFolderName)); + if (!portable) return new DataLocation(perUser, false, SyncRootOf(perUser, syncRoots), null); + var beside = Path.GetFullPath(Path.Combine(applicationDirectory, PortableFolderName)); + return SyncRootOf(beside, syncRoots) is { } synced + ? new DataLocation(perUser, true, SyncRootOf(perUser, syncRoots), synced) + : new DataLocation(beside, true, null, null); + } + + /// The sync root that contains , or null. + public static string? SyncRootOf(string path, IEnumerable syncRoots) => syncRoots.FirstOrDefault(root => IsUnder(path, root)); + + /// True when equals or lies below it (whole path segments, case-insensitive). + public static bool IsUnder(string path, string root) + { + ArgumentNullException.ThrowIfNull(path); + ArgumentNullException.ThrowIfNull(root); + var trimmedRoot = root.TrimEnd('\\', '/'); + if (trimmedRoot.Length == 0 || !path.StartsWith(trimmedRoot, StringComparison.OrdinalIgnoreCase)) return false; + return path.Length == trimmedRoot.Length || path[trimmedRoot.Length] is '\\' or '/'; + } +} diff --git a/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs b/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs index 993a0fdb..46ec0cb6 100644 --- a/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs +++ b/src/Titano.Uploader/Infrastructure/UploaderEnvironment.cs @@ -4,7 +4,8 @@ namespace Titano.Uploader.Infrastructure; /// /// Where the uploader keeps its files: %LOCALAPPDATA%\Titano (shared with the client), or .\data next -/// to the executable when portable.txt exists — the same rule as the desktop client. +/// to the executable when portable.txt exists — the desktop client's rule, except that the client never keeps +/// its SQLite index in a folder OneDrive synchronises; the Uploader's JSON files, written atomically, stay there. /// public sealed class UploaderEnvironment { diff --git a/tests/Titano.App.Tests/Infrastructure/AppEnvironmentTests.cs b/tests/Titano.App.Tests/Infrastructure/AppEnvironmentTests.cs new file mode 100644 index 00000000..771e2d94 --- /dev/null +++ b/tests/Titano.App.Tests/Infrastructure/AppEnvironmentTests.cs @@ -0,0 +1,34 @@ +using System.IO; +using Titano.App.Infrastructure; +using Titano.Core.Windows.Shell; +using Titano.Testing; + +namespace Titano.App.Tests.Infrastructure; + +/// BRIEF §6.2: the client's data folder, which is never inside a folder OneDrive synchronises. +public sealed class AppEnvironmentTests(TestContext context) +{ + [Test] + public void APortableCopyInOneDriveKeepsItsDataInTheUserFolderAndSaysSo() + { + var oneDrive = context.CreateTempDirectory("onedrive"); + var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Desktop", "Titano")).FullName; + File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), ""); + var local = context.CreateTempDirectory("local"); + + var environment = AppEnvironment.From(AppPaths.Resolve(app, local, [oneDrive])); + + Assert.Equal(Path.Combine(local, "Titano", "titano.db"), environment.DatabasePath); + Assert.True(environment.IsPortable, "still a portable copy: updates fetch the portable zip, no sign-in entry"); + Assert.Equal(oneDrive, environment.SyncedFolder); + Assert.True(Directory.Exists(environment.LogDirectory)); + Assert.False(Directory.Exists(Path.Combine(app, "data")), "nothing is written into the synchronised folder"); + + // Elsewhere (a USB stick) a portable copy keeps its data beside it. + var stick = context.CreateTempDirectory("stick"); + File.WriteAllText(Path.Combine(stick, PortableMode.MarkerFileName), ""); + var beside = AppEnvironment.From(AppPaths.Resolve(stick, local, [oneDrive])); + Assert.Equal(Path.Combine(stick, "data"), beside.DataDirectory); + Assert.Null(beside.SyncedFolder); + } +} diff --git a/tests/Titano.Core.Tests/Foundation/DataLocationTests.cs b/tests/Titano.Core.Tests/Foundation/DataLocationTests.cs new file mode 100644 index 00000000..958858dd --- /dev/null +++ b/tests/Titano.Core.Tests/Foundation/DataLocationTests.cs @@ -0,0 +1,68 @@ +using Titano.Core.IO; +using Titano.Testing; + +namespace Titano.Core.Tests.Foundation; + +/// BRIEF §6.2: where the client keeps its index, and never inside a folder OneDrive synchronises. +public sealed class DataLocationTests(TestContext context) +{ + [Test] + public void AnInstalledCopyUsesThePerUserFolderAndAPortableOneItsOwn() + { + var app = context.CreateTempDirectory("app"); + var local = context.CreateTempDirectory("local"); + + var installed = DataLocation.Choose(app, portable: false, local, []); + Assert.Equal(new DataLocation(Path.Combine(local, "Titano"), false, null, null), installed); + + var portable = DataLocation.Choose(app, portable: true, local, []); + Assert.Equal(new DataLocation(Path.Combine(app, "data"), true, null, null), portable); + } + + [Test] + public void APortableCopyInASynchronisedFolderKeepsItsDataInThePerUserFolder() + { + // A portable copy unpacked on a Desktop that OneDrive's folder backup moved into the sync root. + var oneDrive = context.CreateTempDirectory("onedrive"); + var app = Path.Combine(oneDrive, "Desktop", "Titano"); + var local = context.CreateTempDirectory("local"); + + var location = DataLocation.Choose(app, portable: true, local, [Path.Combine(context.CreateTempDirectory("other"), "OneDrive"), oneDrive]); + + Assert.Equal(Path.Combine(local, "Titano"), location.Root); + Assert.True(location.IsPortable, "still a portable copy (updates, no sign-in entry)"); + Assert.Equal(oneDrive, location.PortableSyncRoot); + Assert.Null(location.SyncedFolder); + + // A sync root that only shares the start of the name is another folder. + var beside = DataLocation.Choose(oneDrive + "Backup", portable: true, local, [oneDrive]); + Assert.Equal(new DataLocation(Path.Combine(oneDrive + "Backup", "data"), true, null, null), beside); + } + + [Test] + public void APerUserFolderInsideASyncRootIsReported() + { + var oneDrive = context.CreateTempDirectory("onedrive"); + var local = Path.Combine(oneDrive, "AppData", "Local"); + + var installed = DataLocation.Choose(context.CreateTempDirectory("app"), portable: false, local, [oneDrive]); + Assert.Equal(oneDrive, installed.SyncedFolder); + + var portable = DataLocation.Choose(Path.Combine(oneDrive, "Titano"), portable: true, local, [oneDrive]); + Assert.Equal<(string, string?, string?)>((Path.Combine(local, "Titano"), oneDrive, oneDrive), (portable.Root, portable.SyncedFolder, portable.PortableSyncRoot)); + } + + [Test] + public void SyncRootMatchingIsSegmentWiseAndIgnoresCase() + { + var root = Path.Combine(context.CreateTempDirectory(), "OneDrive"); + Assert.True(DataLocation.IsUnder(root, root)); + Assert.True(DataLocation.IsUnder(Path.Combine(root, "Pictures"), root + Path.DirectorySeparatorChar)); + Assert.True(DataLocation.IsUnder(Path.Combine(root, "x").ToUpperInvariant(), root)); + Assert.True(DataLocation.IsUnder(@"C:\Users\ada\OneDrive\Desktop\Titano\data", @"c:\users\ada\onedrive"), "Windows paths on any platform"); + Assert.False(DataLocation.IsUnder(root + "Backup", root)); + Assert.False(DataLocation.IsUnder(root, string.Empty)); + Assert.Null(DataLocation.SyncRootOf(root + "Backup", [root])); + Assert.Equal(root, DataLocation.SyncRootOf(Path.Combine(root, "Documents"), ["", root])); + } +} diff --git a/tests/Titano.Core.Windows.Tests/Shell/AppPathsTests.cs b/tests/Titano.Core.Windows.Tests/Shell/AppPathsTests.cs index 3be36314..d70366f8 100644 --- a/tests/Titano.Core.Windows.Tests/Shell/AppPathsTests.cs +++ b/tests/Titano.Core.Windows.Tests/Shell/AppPathsTests.cs @@ -17,6 +17,7 @@ public sealed class AppPathsTests(TestContext context) Assert.Equal(Path.Combine(app, "data"), paths.DataRoot); Assert.Equal(Path.Combine(app, "data", "titano.db"), paths.DatabasePath); Assert.False(paths.IsInSyncedFolder); + Assert.Null(paths.PortableSyncRoot); paths.CreateDirectories(); Assert.True(Directory.Exists(paths.LogsDirectory)); @@ -37,13 +38,31 @@ public sealed class AppPathsTests(TestContext context) } [Test] - public void DataInsideOneDriveIsReportedAndRefused() + public void APortableCopyInOneDriveKeepsItsDataInLocalAppData() { var oneDrive = context.CreateTempDirectory("onedrive"); var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Tools", "Titano")).FullName; File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), ""); + var local = context.CreateTempDirectory("local"); - var paths = AppPaths.Resolve(app, context.CreateTempDirectory("local"), [oneDrive]); + var paths = AppPaths.Resolve(app, local, [oneDrive]); + Assert.True(paths.IsPortable); + Assert.Equal(Path.Combine(local, "Titano"), paths.DataRoot); + Assert.Equal(oneDrive, paths.PortableSyncRoot); + Assert.False(paths.IsInSyncedFolder); + paths.CreateDirectories(); + Assert.True(Directory.Exists(paths.LogsDirectory)); + Assert.False(Directory.Exists(Path.Combine(app, "data")), "nothing is written into the synchronised folder"); + } + + [Test] + public void DataInsideOneDriveIsReportedAndRefused() + { + // Local application data inside a sync root: nowhere is safe, so nothing is created. + var oneDrive = context.CreateTempDirectory("onedrive"); + var local = Directory.CreateDirectory(Path.Combine(oneDrive, "AppData", "Local")).FullName; + + var paths = AppPaths.Resolve(context.CreateTempDirectory("app"), local, [oneDrive]); Assert.True(paths.IsInSyncedFolder); Assert.Equal(oneDrive, paths.SyncedFolder); Assert.Throws(paths.CreateDirectories); @@ -54,10 +73,7 @@ public sealed class AppPathsTests(TestContext context) public void SyncRootMatchingIsSegmentWise() { var root = Path.Combine(context.CreateTempDirectory(), "OneDrive"); - Assert.True(OneDriveFolders.IsUnder(root, root)); - Assert.True(OneDriveFolders.IsUnder(Path.Combine(root, "Pictures"), root + Path.DirectorySeparatorChar)); - Assert.True(OneDriveFolders.IsUnder(Path.Combine(root, "x").ToUpperInvariant(), root)); - Assert.False(OneDriveFolders.IsUnder(root + "Backup", root)); + Assert.Equal(root, OneDriveFolders.Containing(Path.Combine(root, "Pictures"), [root])); Assert.Null(OneDriveFolders.Containing(root + "Backup", [root])); } @@ -68,8 +84,10 @@ public sealed class AppPathsTests(TestContext context) var app = context.CreateTempDirectory("app"); File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), ""); var paths = AppPaths.Resolve(app, KnownFolders.LocalAppData); + var roots = OneDriveFolders.SyncRoots(); Assert.True(paths.IsPortable); - Assert.Equal(OneDriveFolders.Containing(paths.DataRoot, OneDriveFolders.SyncRoots()), paths.SyncedFolder); - Assert.True(OneDriveFolders.SyncRoots().All(Path.IsPathFullyQualified)); + Assert.Equal(OneDriveFolders.Containing(Path.Combine(app, PortableMode.DataFolderName), roots), paths.PortableSyncRoot); + Assert.Equal(OneDriveFolders.Containing(paths.DataRoot, roots), paths.SyncedFolder); + Assert.True(roots.All(Path.IsPathFullyQualified)); } } diff --git a/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs b/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs new file mode 100644 index 00000000..cd0c9bd6 --- /dev/null +++ b/tests/Titano.Core.Windows.Tests/Shell/OneDriveFoldersTests.cs @@ -0,0 +1,39 @@ +using Titano.Core.Windows.Shell; +using Titano.Testing; + +namespace Titano.Core.Windows.Tests.Shell; + +/// The OneDrive folders of the user as Windows reports them. Serial: a test sets a process environment variable. +[Serial] +public sealed class OneDriveFoldersTests(TestContext context) +{ + private const string Variable = "OneDriveCommercial"; + + [Test] + [WindowsOnly] + public void APortableCopyInTheUsersOneDriveKeepsItsDataInLocalAppData() + { + var oneDrive = context.CreateTempDirectory("onedrive"); + var previous = Environment.GetEnvironmentVariable(Variable); + Environment.SetEnvironmentVariable(Variable, oneDrive); + try + { + Assert.Contains(OneDriveFolders.SyncRoots(), root => string.Equals(root, Path.TrimEndingDirectorySeparator(oneDrive), StringComparison.OrdinalIgnoreCase)); + // A Desktop that OneDrive's folder backup moved into the sync root, with a portable copy unpacked on it. + var app = Directory.CreateDirectory(Path.Combine(oneDrive, "Desktop", "Titano")).FullName; + File.WriteAllText(Path.Combine(app, PortableMode.MarkerFileName), ""); + var local = context.CreateTempDirectory("local"); + + var paths = AppPaths.Resolve(app, local); + + Assert.True(paths.IsPortable); + Assert.Equal(Path.Combine(local, AppPaths.ProductFolderName), paths.DataRoot); + Assert.NotNull(paths.PortableSyncRoot); + Assert.False(paths.IsInSyncedFolder); + } + finally + { + Environment.SetEnvironmentVariable(Variable, previous); + } + } +}