Extracted files land in a staging folder inside the target and are moved into place with backups of replaced files; a failed move rolls everything back. Removal deletes only logged payload paths and prunes folders left empty. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
104 lines
4.3 KiB
C#
104 lines
4.3 KiB
C#
using Titano.Core.Packaging;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.Core.Tests.Packaging;
|
|
|
|
public sealed class InstallTransactionTests(TestContext context)
|
|
{
|
|
[Test]
|
|
public void CommitReplacesFilesAndCleansUp()
|
|
{
|
|
var target = context.CreateTempDirectory();
|
|
File.WriteAllText(Path.Combine(target, "Titano.exe"), "old");
|
|
File.WriteAllText(Path.Combine(target, "user-notes.txt"), "keep");
|
|
|
|
using (var tx = new InstallTransaction(target))
|
|
{
|
|
Write(tx.StagingDirectory, "Titano.exe", "new");
|
|
Write(tx.StagingDirectory, "lib/new.dll", "dll");
|
|
tx.Commit(["Titano.exe", "lib/new.dll"]);
|
|
}
|
|
|
|
Assert.Equal("new", File.ReadAllText(Path.Combine(target, "Titano.exe")));
|
|
Assert.Equal("dll", File.ReadAllText(Path.Combine(target, "lib", "new.dll")));
|
|
Assert.Equal("keep", File.ReadAllText(Path.Combine(target, "user-notes.txt")));
|
|
Assert.Empty(Directory.GetDirectories(target, ".setup-*"));
|
|
}
|
|
|
|
[Test]
|
|
public void FailedCommitRestoresThePreviousInstall()
|
|
{
|
|
var target = context.CreateTempDirectory();
|
|
File.WriteAllText(Path.Combine(target, "Titano.exe"), "old");
|
|
// A folder where the new version has a file makes the second move fail.
|
|
Directory.CreateDirectory(Path.Combine(target, "blocked"));
|
|
File.WriteAllText(Path.Combine(target, "blocked", "inside.txt"), "x");
|
|
|
|
using (var tx = new InstallTransaction(target))
|
|
{
|
|
Write(tx.StagingDirectory, "Titano.exe", "new");
|
|
Write(tx.StagingDirectory, "added.dll", "added");
|
|
Write(tx.StagingDirectory, "blocked", "file");
|
|
// Linux reports IOException, Windows UnauthorizedAccessException for a file moved onto a folder.
|
|
var ex = Assert.Throws<Exception>(() => tx.Commit(["Titano.exe", "added.dll", "blocked"]));
|
|
Assert.True(ex is IOException or UnauthorizedAccessException, ex.GetType().Name);
|
|
}
|
|
|
|
Assert.Equal("old", File.ReadAllText(Path.Combine(target, "Titano.exe")));
|
|
Assert.FileMissing(Path.Combine(target, "added.dll"));
|
|
Assert.Equal("x", File.ReadAllText(Path.Combine(target, "blocked", "inside.txt")));
|
|
Assert.Empty(Directory.GetDirectories(target, ".setup-*"));
|
|
}
|
|
|
|
[Test]
|
|
public void StaleStagingFoldersFromAnInterruptedRunAreRemoved()
|
|
{
|
|
var target = context.CreateTempDirectory();
|
|
Directory.CreateDirectory(Path.Combine(target, ".setup-staging-deadbeef", "sub"));
|
|
Directory.CreateDirectory(Path.Combine(target, ".setup-other"));
|
|
using (new InstallTransaction(target))
|
|
{
|
|
}
|
|
Assert.False(Directory.Exists(Path.Combine(target, ".setup-staging-deadbeef")));
|
|
Assert.True(Directory.Exists(Path.Combine(target, ".setup-other")));
|
|
}
|
|
|
|
[Test]
|
|
public void RemoveDeletesOnlyLoggedFilesAndPrunesEmptyFolders()
|
|
{
|
|
var root = context.CreateTempDirectory();
|
|
Write(root, "Titano.exe", "a");
|
|
Write(root, "lib/x/y.dll", "b");
|
|
Write(root, "lib/keep.txt", "user");
|
|
|
|
var failed = InstalledFiles.Remove(root, ["Titano.exe", "lib/x/y.dll", "already-gone.dll"], removeEmptyRoot: true);
|
|
|
|
Assert.Empty(failed);
|
|
Assert.FileMissing(Path.Combine(root, "Titano.exe"));
|
|
Assert.False(Directory.Exists(Path.Combine(root, "lib", "x")));
|
|
Assert.Equal("user", File.ReadAllText(Path.Combine(root, "lib", "keep.txt")));
|
|
Assert.True(Directory.Exists(root), "root keeps unlisted files");
|
|
|
|
InstalledFiles.Remove(root, ["lib/keep.txt"], removeEmptyRoot: true);
|
|
Assert.False(Directory.Exists(root));
|
|
}
|
|
|
|
[Test]
|
|
public void RemoveRefusesPathsOutsideTheRoot()
|
|
{
|
|
var parent = context.CreateTempDirectory();
|
|
var root = Path.Combine(parent, "install");
|
|
Directory.CreateDirectory(root);
|
|
Write(parent, "outside.txt", "precious");
|
|
Assert.Throws<PayloadException>(() => InstalledFiles.Remove(root, ["../outside.txt"]));
|
|
Assert.FileExists(Path.Combine(parent, "outside.txt"));
|
|
}
|
|
|
|
private static void Write(string root, string relative, string content)
|
|
{
|
|
var path = Path.Combine(root, relative.Replace('/', Path.DirectorySeparatorChar));
|
|
Directory.CreateDirectory(Path.GetDirectoryName(path)!);
|
|
File.WriteAllText(path, content);
|
|
}
|
|
}
|