-
Notifications
You must be signed in to change notification settings - Fork 932
Relaunch UniGetUI through /bin/sh instead of powershell.exe off Windows #5418
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
awss (awss1i)
wants to merge
2
commits into
Devolutions:main
Choose a base branch
from
awss1i:fix/relaunch-off-windows
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+314
−12
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| namespace UniGetUI.Core.Tools.Tests; | ||
|
|
||
| // xUnit 2 has no runtime skip; setting Skip in the attribute makes a test for another platform | ||
| // report as skipped instead of passing without running. | ||
|
|
||
| public sealed class WindowsFactAttribute : FactAttribute | ||
| { | ||
| public WindowsFactAttribute() | ||
| { | ||
| if (!OperatingSystem.IsWindows()) | ||
| { | ||
| Skip = "Runs on Windows only"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public sealed class UnixFactAttribute : FactAttribute | ||
| { | ||
| public UnixFactAttribute() | ||
| { | ||
| if (OperatingSystem.IsWindows()) | ||
| { | ||
| Skip = "Runs on Linux and macOS only"; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public sealed class MacOSFactAttribute : FactAttribute | ||
| { | ||
| public MacOSFactAttribute() | ||
| { | ||
| if (!OperatingSystem.IsMacOS()) | ||
| { | ||
| Skip = "Runs on macOS only"; | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| using System.Diagnostics; | ||
| using System.Runtime.Versioning; | ||
| using UniGetUI.Core.Tools; | ||
|
|
||
| namespace UniGetUI.Core.Tools.Tests; | ||
|
|
||
| public class RelaunchTests | ||
| { | ||
| [WindowsFact] | ||
| public void CreateRelaunchStartInfo_OnWindows_WaitsAndStartsThroughPowerShell() | ||
| { | ||
| ProcessStartInfo startInfo = CoreTools.CreateRelaunchStartInfo( | ||
| 4242, | ||
| @"C:\Users\O'Brien\UniGetUI\UniGetUI.exe" | ||
| ); | ||
|
|
||
| Assert.Equal("powershell.exe", startInfo.FileName); | ||
| Assert.Equal( | ||
| "-NoProfile -WindowStyle Hidden -Command \"Wait-Process -Id 4242; " | ||
| + @"Start-Process -FilePath 'C:\Users\O''Brien\UniGetUI\UniGetUI.exe'""", | ||
| startInfo.Arguments | ||
| ); | ||
| Assert.Empty(startInfo.ArgumentList); | ||
| Assert.False(startInfo.UseShellExecute); | ||
| Assert.True(startInfo.CreateNoWindow); | ||
| } | ||
|
|
||
| [UnixFact] | ||
| public void CreateRelaunchStartInfo_OffWindows_WaitsForThePidInAShellHelper() | ||
| { | ||
| ProcessStartInfo startInfo = CoreTools.CreateRelaunchStartInfo(4242, "/opt/unigetui/UniGetUI"); | ||
|
|
||
| Assert.Equal("/bin/sh", startInfo.FileName); | ||
| Assert.Equal("", startInfo.Arguments); | ||
| Assert.Equal(6, startInfo.ArgumentList.Count); | ||
| Assert.Equal("-c", startInfo.ArgumentList[0]); | ||
| Assert.Contains("kill -0 \"$pid\"", startInfo.ArgumentList[1]); | ||
| Assert.Contains("\"$exe\" >/dev/null 2>&1 &", startInfo.ArgumentList[1]); | ||
| Assert.Contains("/usr/bin/open -na \"$bundle\" || \"$exe\" >/dev/null 2>&1 &", startInfo.ArgumentList[1]); | ||
| Assert.Equal("sh", startInfo.ArgumentList[2]); | ||
| Assert.Equal("4242", startInfo.ArgumentList[3]); | ||
| Assert.Equal("/opt/unigetui/UniGetUI", startInfo.ArgumentList[4]); | ||
| Assert.Equal("", startInfo.ArgumentList[5]); | ||
| Assert.False(startInfo.UseShellExecute); | ||
| } | ||
|
|
||
| [MacOSFact] | ||
| public void CreateRelaunchStartInfo_OnMacOs_PassesTheBundleToTheHelper() | ||
| { | ||
| ProcessStartInfo startInfo = CoreTools.CreateRelaunchStartInfo( | ||
| 4242, | ||
| "/Applications/UniGetUI.app/Contents/MacOS/UniGetUI" | ||
| ); | ||
|
|
||
| Assert.Equal("/Applications/UniGetUI.app", startInfo.ArgumentList[5]); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("/Applications/UniGetUI.app/Contents/MacOS/UniGetUI", "UniGetUI.app")] | ||
| [InlineData("/Users/me/My Apps/UniGetUI.app/Contents/MacOS/UniGetUI", "UniGetUI.app")] | ||
| public void FindAppBundle_ReturnsTheBundleThatContainsTheExecutable(string executable, string bundleName) | ||
| { | ||
| string? bundle = CoreTools.FindAppBundle(executable); | ||
|
|
||
| Assert.NotNull(bundle); | ||
| Assert.Equal(bundleName, Path.GetFileName(bundle)); | ||
| Assert.EndsWith(bundleName, bundle); | ||
| } | ||
|
|
||
| [Theory] | ||
| [InlineData("/opt/unigetui/UniGetUI")] | ||
| [InlineData("/home/me/UniGetUI.app.bak/bin/UniGetUI")] | ||
| [InlineData("UniGetUI")] | ||
| public void FindAppBundle_ReturnsNullOutsideABundle(string executable) | ||
| { | ||
| Assert.Null(CoreTools.FindAppBundle(executable)); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void TryStartRelaunchHelper_ReturnsFalseWhenTheHelperCannotStart() | ||
| { | ||
| var startInfo = new ProcessStartInfo( | ||
| Path.Combine(Path.GetTempPath(), "unigetui-missing-" + Path.GetRandomFileName()) | ||
| ) | ||
| { | ||
| UseShellExecute = false, | ||
| }; | ||
|
|
||
| Assert.False(CoreTools.TryStartRelaunchHelper(startInfo)); | ||
| } | ||
|
|
||
| // The real helper: a sleeping child stands in for the exiting UniGetUI, and the "executable" is | ||
| // a script that leaves a marker. The marker must not appear while the child is alive. | ||
| [UnixFact] | ||
| [UnsupportedOSPlatform("windows")] | ||
| public Task RelaunchHelper_StartsTheExecutableOnlyAfterTheProcessExits() => | ||
| AssertHelperRelaunchesAfterExitAsync(bundle: null); | ||
|
|
||
| // A bundle open refuses (here one that does not exist; on Linux there is no /usr/bin/open at | ||
| // all) must fall back to starting the executable instead of leaving nothing running. | ||
| [UnixFact] | ||
| [UnsupportedOSPlatform("windows")] | ||
| public Task RelaunchHelper_StartsTheExecutableWhenOpenRejectsTheBundle() => | ||
| AssertHelperRelaunchesAfterExitAsync( | ||
| bundle: Path.Combine(Path.GetTempPath(), "unigetui-missing-" + Path.GetRandomFileName(), "UniGetUI.app") | ||
| ); | ||
|
|
||
| [UnsupportedOSPlatform("windows")] | ||
| private static async Task AssertHelperRelaunchesAfterExitAsync(string? bundle) | ||
| { | ||
| string directory = Path.Combine(Path.GetTempPath(), "unigetui-relaunch-" + Path.GetRandomFileName()); | ||
| Directory.CreateDirectory(directory); | ||
| string marker = Path.Combine(directory, "relaunched"); | ||
| string target = Path.Combine(directory, "target.sh"); | ||
| File.WriteAllText(target, $"#!/bin/sh\ntouch \"{marker}\"\n"); | ||
| File.SetUnixFileMode(target, UnixFileMode.UserRead | UnixFileMode.UserWrite | UnixFileMode.UserExecute); | ||
|
|
||
| using var standIn = Process.Start(new ProcessStartInfo("sleep", "1.5") { UseShellExecute = false })!; | ||
| try | ||
| { | ||
| ProcessStartInfo startInfo = CoreTools.CreateRelaunchStartInfo(standIn.Id, target); | ||
| if (bundle is not null) | ||
| { | ||
| startInfo.ArgumentList[5] = bundle; | ||
| } | ||
|
|
||
| using var helper = Process.Start(startInfo); | ||
| Assert.NotNull(helper); | ||
|
|
||
| await Task.Delay(500); | ||
| Assert.False(standIn.HasExited); | ||
| Assert.False(File.Exists(marker), "the helper must not start the executable while the process is alive"); | ||
|
|
||
| await standIn.WaitForExitAsync(); | ||
| var deadline = DateTime.UtcNow.AddSeconds(5); | ||
| while (!File.Exists(marker) && DateTime.UtcNow < deadline) | ||
| { | ||
| await Task.Delay(100); | ||
| } | ||
|
|
||
| Assert.True(File.Exists(marker), "the helper did not start the executable after the process exited"); | ||
| await helper.WaitForExitAsync(); | ||
| Assert.Equal(0, helper.ExitCode); | ||
| } | ||
| finally | ||
| { | ||
| if (!standIn.HasExited) | ||
| { | ||
| standIn.Kill(); | ||
| } | ||
|
|
||
| Directory.Delete(directory, recursive: true); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| [assembly: InternalsVisibleTo("UniGetUI.Core.Tools.Tests")] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The macOS branch has no fallback and swallows failure, which can leave no app running at all.
FindAppBundlematches any ancestor directory whose name ends in.app(case-insensitive) — it does not verify that the directory is a real bundle. Ifopenrefuses the path (a tarball extracted into a directory that merely ends in.app, a bundle LaunchServices rejects as damaged or quarantined, an unregistered ad-hoc-signed build), the helper exits non-zero, nobody reads that status, and UniGetUI has already terminated. The user accepts "restart" after a dependency install and the app is simply gone — the bug this PR set out to remove, moved to macOS.Falling back to the direct-exec branch costs one token:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Addressed in 1342c47 with the line you suggested:
/usr/bin/open -na "$bundle" || "$exe" >/dev/null 2>&1 &.The new
RelaunchHelper_StartsTheExecutableWhenOpenRejectsTheBundleruns the real helper with a bundle path that does not exist (on Linux there is no/usr/bin/openat all; on macOSopenrefuses the path) and asserts the executable starts only after the stand-in process exits. 5 consecutive runs green on Fedora 44; removing the|| "$exe"fallback fails it and the script assertion. Still not run on a Mac.