|
| 1 | +using System.Diagnostics; |
| 2 | +using UniGetUI.Core.Data; |
| 3 | +using UniGetUI.PackageEngine.Enums; |
| 4 | +using UniGetUI.PackageEngine.Interfaces; |
| 5 | +using UniGetUI.PackageEngine.Managers.AurManager; |
| 6 | +using UniGetUI.PackageEngine.Managers.PacmanManager; |
| 7 | +using UniGetUI.PackageEngine.Operations; |
| 8 | +using UniGetUI.PackageEngine.PackageClasses; |
| 9 | +using UniGetUI.PackageEngine.PackageLoader; |
| 10 | +using UniGetUI.PackageEngine.Serializable; |
| 11 | +using UniGetUI.PackageOperations; |
| 12 | + |
| 13 | +namespace UniGetUI.PackageEngine.Tests; |
| 14 | + |
| 15 | +public sealed class AurEndToEndFactAttribute : FactAttribute |
| 16 | +{ |
| 17 | + public const string Variable = "UNIGETUI_AUR_E2E"; |
| 18 | + |
| 19 | + public AurEndToEndFactAttribute() |
| 20 | + { |
| 21 | + if (Environment.GetEnvironmentVariable(Variable) != "1") |
| 22 | + Skip = $"Builds and installs a real AUR package through paru or yay; set {Variable}=1 to run on Arch Linux."; |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +[CollectionDefinition("AUR end to end", DisableParallelization = true)] |
| 27 | +public sealed class AurEndToEndTestCollection; |
| 28 | + |
| 29 | +[Collection("AUR end to end")] |
| 30 | +public sealed class AurEndToEndTests |
| 31 | +{ |
| 32 | + private const string TestPackage = "pfetch"; |
| 33 | + |
| 34 | + private static AurHelperKind ExpectedHelper() => |
| 35 | + Environment.GetEnvironmentVariable("UNIGETUI_AUR_E2E_HELPER") switch |
| 36 | + { |
| 37 | + "paru" => AurHelperKind.Paru, |
| 38 | + "yay" => AurHelperKind.Yay, |
| 39 | + var other => throw new InvalidOperationException($"UNIGETUI_AUR_E2E_HELPER must be paru or yay, not '{other}'"), |
| 40 | + }; |
| 41 | + |
| 42 | + private static Aur ReadyAur() |
| 43 | + { |
| 44 | + CoreData.ElevatorPath = "/usr/bin/sudo"; |
| 45 | + CoreData.ElevatorArgs = "-A"; |
| 46 | + Environment.SetEnvironmentVariable("SUDO_ASKPASS", "/usr/bin/false"); |
| 47 | + |
| 48 | + var aur = new Aur(); |
| 49 | + aur.Initialize(); |
| 50 | + Assert.True(aur.IsReady(), $"AUR manager not ready: {aur.Status.Version} ({aur.Status.ExecutablePath})"); |
| 51 | + Assert.Equal(ExpectedHelper(), aur.HelperKind); |
| 52 | + Assert.Equal(Environment.GetEnvironmentVariable("UNIGETUI_AUR_E2E_HELPER"), aur.Properties.ExecutableFriendlyName); |
| 53 | + return aur; |
| 54 | + } |
| 55 | + |
| 56 | + private static int CountInstalled(IPackageManager manager) => |
| 57 | + manager.GetInstalledPackages().Count(p => p.Id == TestPackage); |
| 58 | + |
| 59 | + private static async Task Completes(AbstractOperation operation) |
| 60 | + { |
| 61 | + await operation.MainThread().WaitAsync(TimeSpan.FromMinutes(10)); |
| 62 | + string output = string.Join(Environment.NewLine, operation.GetOutput().Select(l => l.Item1)); |
| 63 | + Console.WriteLine(output); |
| 64 | + Assert.True(operation.Status == OperationStatus.Succeeded, output); |
| 65 | + } |
| 66 | + |
| 67 | + private static bool PacmanHas(string package) |
| 68 | + { |
| 69 | + using var p = Process.Start(new ProcessStartInfo("pacman", ["-Q", package]) { RedirectStandardOutput = true, RedirectStandardError = true })!; |
| 70 | + p.WaitForExit(); |
| 71 | + return p.ExitCode == 0; |
| 72 | + } |
| 73 | + |
| 74 | + [AurEndToEndFact] |
| 75 | + public async Task SearchesShowsDetailsInstallsListsAndRemovesARealAurPackage() |
| 76 | + { |
| 77 | + _ = new DiscoverablePackagesLoader([]); |
| 78 | + _ = new UpgradablePackagesLoader([]); |
| 79 | + _ = new InstalledPackagesLoader([]); |
| 80 | + |
| 81 | + var aur = ReadyAur(); |
| 82 | + Assert.False(string.IsNullOrWhiteSpace(aur.Status.Version)); |
| 83 | + Assert.False(PacmanHas(TestPackage), $"{TestPackage} is already installed; the runner must start clean"); |
| 84 | + |
| 85 | + var found = aur.FindPackages(TestPackage, out bool searchFailed); |
| 86 | + Assert.False(searchFailed); |
| 87 | + var offered = Assert.Single(found, p => p.Id == TestPackage); |
| 88 | + Assert.Matches(@"^\d", offered.VersionString); |
| 89 | + |
| 90 | + var details = new PackageDetails(offered); |
| 91 | + await details.Load(); |
| 92 | + Assert.True(details.IsPopulated); |
| 93 | + Assert.False(string.IsNullOrWhiteSpace(details.Description)); |
| 94 | + Assert.Equal(new Uri($"https://aur.archlinux.org/packages/{TestPackage}"), details.ManifestUrl); |
| 95 | + Assert.False(string.IsNullOrWhiteSpace(details.Publisher)); |
| 96 | + |
| 97 | + using (var install = new InstallPackageOperation(offered, new InstallOptions { RunAsAdministrator = true }, IgnoreParallelInstalls: true)) |
| 98 | + await Completes(install); |
| 99 | + Assert.True(PacmanHas(TestPackage), $"{TestPackage} is not installed after the install operation"); |
| 100 | + |
| 101 | + Assert.Equal(1, CountInstalled(aur)); |
| 102 | + |
| 103 | + var pacman = new Pacman(); |
| 104 | + pacman.Initialize(); |
| 105 | + Assert.True(pacman.IsReady()); |
| 106 | + Assert.Equal(1, CountInstalled(pacman)); |
| 107 | + pacman.ForeignPackagesListedElsewhere = aur.IsReady; |
| 108 | + Assert.Equal(0, CountInstalled(pacman)); |
| 109 | + |
| 110 | + Assert.DoesNotContain(aur.GetAvailableUpdates(), p => p.Id == TestPackage); |
| 111 | + |
| 112 | + var installed = aur.GetInstalledPackages().Single(p => p.Id == TestPackage); |
| 113 | + using (var uninstall = new UninstallPackageOperation(installed, new InstallOptions(), IgnoreParallelInstalls: true)) |
| 114 | + await Completes(uninstall); |
| 115 | + Assert.False(PacmanHas(TestPackage), $"{TestPackage} is still installed after the uninstall operation"); |
| 116 | + Assert.Equal(0, CountInstalled(aur)); |
| 117 | + } |
| 118 | +} |
0 commit comments