- Windows services whose program lies in the install folder (Titano.Server --install-service) are found in the registry and, through the Service Control Manager, stopped before files are replaced (they join the "programs running" prompt), started again after an upgrade that stopped them, deleted when an upgrade drops their program and on uninstall. An unelevated setup runs that step in an elevated copy (/SERVICES=...). - The Server component installs for all users only (manifest allUsersOnly): choosing it moves a new install to Program Files and disables "Just me" with the reason; a per-user install cannot take it. - Uninstall removes every HKCU Run value that starts a program in the folder (the Uploader's "Start with Windows"). - A shortcut that already starts an existing program elsewhere is neither retargeted nor logged, and a logged shortcut is deleted only while it still starts a program of this folder, so the two products no longer remove each other's "Titano Uploader" shortcuts. - "Remove my data" lists and deletes only the data of this product's programs that no other installed Titano product uses (the desktop app and the Uploader share %LOCALAPPDATA%\Titano and its logs folder). - The .titano file type is named in setup's language (Setup.FileType.*). Setup tests run in Titano.App.Tests on Windows CI. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_012iLMBNwBE2GnphAsu4cjaC
157 lines
8.2 KiB
C#
157 lines
8.2 KiB
C#
using System.Globalization;
|
|
using System.IO;
|
|
using Microsoft.Win32;
|
|
using Titano.Core.Packaging;
|
|
using Titano.Setup.Cli;
|
|
using Titano.Setup.Engine;
|
|
using Titano.Setup.Interop;
|
|
using Titano.Testing;
|
|
|
|
namespace Titano.App.Tests.Setup;
|
|
|
|
/// <summary>
|
|
/// What setup leaves behind and what it registers: services and startup entries of the install folder (SETUP-1,
|
|
/// SETUP-2), shortcuts another product owns (SRS-09), the Server's all-users scope (SRS-06) and the file type name
|
|
/// in setup's language (SRS-04).
|
|
/// </summary>
|
|
public sealed class SetupEngineTests(TestContext context)
|
|
{
|
|
[Theory]
|
|
[Case(@"""C:\Program Files\Titano\Titano.Server.exe"" --service", @"C:\Program Files\Titano\Titano.Server.exe")]
|
|
[Case(@"C:\Program Files\Titano\Titano.Server.exe --service", @"C:\Program Files\Titano\Titano.Server.exe")]
|
|
[Case(@"""C:\Program Files\dotnet\dotnet.exe"" ""C:\Apps\Titano\Titano.Server.dll"" --service", @"C:\Apps\Titano\Titano.Server.dll")]
|
|
[Case(@"C:\Windows\system32\svchost.exe -k netsvcs", @"C:\Windows\system32\svchost.exe")]
|
|
[Case(@"Titano.Uploader.exe --tray", null)]
|
|
[Case("", null)]
|
|
public void TheProgramOfACommandLineIsItsFirstFullPath(string commandLine, string? program) =>
|
|
Assert.Equal(program, CommandProgram.Of(commandLine));
|
|
|
|
[Test]
|
|
public void ServiceStepsTravelToTheElevatedCopy()
|
|
{
|
|
var arguments = new SetupArguments { Services = ServiceActions.Prune | ServiceActions.Start, Directory = @"C:\Program Files\Titano", Elevated = true };
|
|
var switches = arguments.ToSwitches().ToList();
|
|
Assert.Contains("/SERVICES=prune,start", switches);
|
|
Assert.Equal(arguments, SetupArguments.Parse(switches));
|
|
|
|
Assert.Equal(ServiceActions.Remove, SetupArguments.Parse(["/SERVICES=Remove", "/DIR=C:\\Apps\\Titano"]).Services);
|
|
Assert.Throws<ArgumentException>(() => SetupArguments.Parse(["/SERVICES=stop"]));
|
|
Assert.Throws<ArgumentException>(() => SetupArguments.Parse(["/SERVICES=kill", "/DIR=C:\\Apps\\Titano"]));
|
|
Assert.Throws<ArgumentException>(() => SetupArguments.Parse(["/SERVICES=none", "/DIR=C:\\Apps\\Titano"]));
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void AServiceWhoseProgramIsGoneIsPrunedAndNothingStartsIt()
|
|
{
|
|
var folder = context.CreateTempDirectory("services");
|
|
// No service is registered from a fresh folder, and nothing is asked of the Service Control Manager.
|
|
Assert.Empty(InstalledServices.Find(folder));
|
|
Assert.Equal(SetupExitCode.Success, InstalledServices.Apply(folder, ServiceActions.Remove));
|
|
|
|
var gone = new InstalledService("TitanoTest" + Guid.NewGuid().ToString("N"), "Titano Server", Path.Combine(folder, "Titano.Server.exe"), AutoStart: true);
|
|
Assert.False(InstalledServices.IsRunning(gone));
|
|
Assert.True(InstalledServices.NeedsWork([gone], ServiceActions.Prune));
|
|
Assert.True(InstalledServices.NeedsWork([gone], ServiceActions.Remove));
|
|
Assert.False(InstalledServices.NeedsWork([gone], ServiceActions.Stop));
|
|
Assert.False(InstalledServices.NeedsWork([gone], ServiceActions.Start));
|
|
Assert.False(InstalledServices.NeedsWork([], ServiceActions.Remove));
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void UninstallRemovesOnlyTheStartupEntriesOfItsFolder()
|
|
{
|
|
var folder = Path.Combine(context.CreateTempDirectory("startup"), "Titano Uploader");
|
|
const string testsKey = @"Software\Titano.Tests";
|
|
var keyPath = testsKey + @"\" + Guid.NewGuid().ToString("N");
|
|
try
|
|
{
|
|
using (var run = Registry.CurrentUser.CreateSubKey(keyPath, writable: true))
|
|
{
|
|
run.SetValue("Titano Uploader", $"\"{Path.Combine(folder, "Titano.Uploader.exe")}\" --tray");
|
|
run.SetValue("Unquoted", Path.Combine(folder, "tools", "Helper.exe") + " /quiet");
|
|
run.SetValue("Other", @"""C:\Program Files\Other\Other.exe"" --background");
|
|
run.SetValue("Sibling", $"\"{folder} 2\\Titano.Uploader.exe\"");
|
|
|
|
Assert.SequenceEqual(["Titano Uploader", "Unquoted"], StartupEntries.RemoveInside(run, folder).Order(StringComparer.Ordinal));
|
|
Assert.SequenceEqual(["Other", "Sibling"], run.GetValueNames().Order(StringComparer.Ordinal));
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
Registry.CurrentUser.DeleteSubKeyTree(keyPath, throwOnMissingSubKey: false);
|
|
bool empty;
|
|
using (var tests = Registry.CurrentUser.OpenSubKey(testsKey)) empty = tests is { SubKeyCount: 0, ValueCount: 0 };
|
|
if (empty) Registry.CurrentUser.DeleteSubKey(testsKey, throwOnMissingSubKey: false);
|
|
}
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void AShortcutTellsWhichProgramItStarts()
|
|
{
|
|
var folder = context.CreateTempDirectory("links");
|
|
var target = Path.Combine(folder, "Product A", "Titano.Uploader.exe");
|
|
Directory.CreateDirectory(Path.GetDirectoryName(target)!);
|
|
File.WriteAllBytes(target, [0x4D, 0x5A]);
|
|
var link = Path.Combine(folder, "Titano Uploader.lnk");
|
|
ShellLink.Create(link, target, "Titano Uploader");
|
|
|
|
// The shell may store the long form of a short (8.3) temporary path: the same file either way.
|
|
var stored = Assert.NotNull(ShellLink.TargetOf(link));
|
|
Assert.Equal(Path.GetFileName(target), Path.GetFileName(stored));
|
|
Assert.FileExists(stored);
|
|
var productA = Path.GetDirectoryName(stored)!;
|
|
Assert.True(CommandProgram.IsPathInside(stored, productA));
|
|
Assert.False(CommandProgram.IsPathInside(stored, Path.Combine(Path.GetDirectoryName(productA)!, "Product B")));
|
|
Assert.False(CommandProgram.IsPathInside(stored, productA + " 2"));
|
|
Assert.Null(ShellLink.TargetOf(Path.Combine(folder, "missing.lnk")));
|
|
var notALink = Path.Combine(folder, "not a link.lnk");
|
|
File.WriteAllText(notALink, "plain text");
|
|
Assert.Null(ShellLink.TargetOf(notALink));
|
|
}
|
|
|
|
[Test]
|
|
[WindowsOnly]
|
|
public void TheServerMakesANewInstallationAnAllUsersOne()
|
|
{
|
|
var manifest = Manifest();
|
|
var plan = InstallPlan.Resolve(manifest, new SetupArguments { Components = ["Titano", "Server"] });
|
|
Assert.Equal(InstallScope.AllUsers, plan.Scope);
|
|
Assert.Equal(SetupPaths.DefaultDirectory(InstallScope.AllUsers, manifest.Product), plan.Directory);
|
|
|
|
var desktop = InstallPlan.Resolve(manifest, new SetupArguments { Components = ["Titano"] });
|
|
Assert.Equal(InstallScope.CurrentUser, desktop.Scope);
|
|
Assert.True(desktop.CanInstall(manifest.FindComponent("Server")!));
|
|
|
|
// A per-user installation upgraded in place keeps its scope, so it cannot take the Server.
|
|
var perUser = desktop with { Existing = new ExistingInstall(InstallScope.CurrentUser, desktop.Directory, "0.9.0") };
|
|
Assert.False(perUser.CanInstall(manifest.FindComponent("Server")!));
|
|
Assert.True(perUser.CanInstall(manifest.FindComponent("Titano")!));
|
|
}
|
|
|
|
[Test]
|
|
public void TheProjectFileTypeIsNamedInSetupsLanguage()
|
|
{
|
|
var association = new PayloadAssociation { Component = "Titano", Extension = ".titano", ProgId = "Titano.Project", Description = "Titano project", Target = "Titano.exe" };
|
|
Assert.Equal("Titano project", InstallEngine.FileTypeName(association, CultureInfo.GetCultureInfo("en")));
|
|
Assert.Equal("Progetto Titano", InstallEngine.FileTypeName(association, CultureInfo.GetCultureInfo("it")));
|
|
|
|
var unknown = new PayloadAssociation { Component = "Titano", Extension = ".tlx", ProgId = "Titano.Unknown", Description = "Something else", Target = "Titano.exe" };
|
|
Assert.Equal("Something else", InstallEngine.FileTypeName(unknown, CultureInfo.GetCultureInfo("it")));
|
|
}
|
|
|
|
/// <summary>A manifest under a product name no machine has installed, so the registry holds no installation of it.</summary>
|
|
private static PayloadManifest Manifest() => new()
|
|
{
|
|
Product = "TitanoTest" + Guid.NewGuid().ToString("N")[..12],
|
|
Version = "1.0.0",
|
|
Components =
|
|
[
|
|
new PayloadComponent { Id = "Titano", DisplayName = "Titano", DefaultSelected = true },
|
|
new PayloadComponent { Id = "Server", DisplayName = "Titano Server", AllUsersOnly = true },
|
|
],
|
|
};
|
|
}
|