Skip to content
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

Enable starting apps with command line arguments when run as admin is selected #6923

Merged
merged 5 commits into from
Oct 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public void WebApplicationShouldReturnContextMenuWithOpenInConsoleWhenContextMen
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _pinnedWebpage.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _pinnedWebpage.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(3, contextMenuResults.Count);
Expand All @@ -455,7 +455,7 @@ public void InternetShortcutApplicationShouldReturnContextMenuWithOpenInConsoleW
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyInternetShortcutApp.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _dummyInternetShortcutApp.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(2, contextMenuResults.Count);
Expand All @@ -470,7 +470,7 @@ public void Win32ApplicationShouldReturnContextMenuWithOpenInConsoleWhenContextM
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _chrome.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _chrome.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(3, contextMenuResults.Count);
Expand All @@ -486,7 +486,7 @@ public void RunCommandShouldReturnContextMenuWithOpenInConsoleWhenContextMenusIs
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _cmdRunCommand.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _cmdRunCommand.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(3, contextMenuResults.Count);
Expand All @@ -502,7 +502,7 @@ public void AppRefApplicationShouldReturnContextMenuWithOpenInConsoleWhenContext
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyAppRefApp.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _dummyAppRefApp.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(3, contextMenuResults.Count);
Expand All @@ -518,7 +518,7 @@ public void ShortcutApplicationShouldReturnContextMenuWithOpenInConsoleWhenConte
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyShortcutApp.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _dummyShortcutApp.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(3, contextMenuResults.Count);
Expand All @@ -534,7 +534,7 @@ public void FolderApplicationShouldReturnContextMenuWithOpenInConsoleWhenContext
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyFolderApp.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _dummyFolderApp.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(2, contextMenuResults.Count);
Expand All @@ -549,7 +549,7 @@ public void GenericFileApplicationShouldReturnContextMenuWithOpenInConsoleWhenCo
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyGenericFileApp.ContextMenus(mock.Object);
List<ContextMenuResult> contextMenuResults = _dummyGenericFileApp.ContextMenus(string.Empty, mock.Object);

// Assert
Assert.AreEqual(2, contextMenuResults.Count);
Expand Down Expand Up @@ -586,5 +586,20 @@ public Win32Program.ApplicationType GetAppTypeFromPathShouldReturnCorrectAppType
// Act
return Win32Program.GetAppTypeFromPath(path);
}

[TestCase(null)]
[TestCase("")]
[TestCase("ping 1.1.1.1")]
public void EmptyArgumentsShouldNotThrow(string argument)
{
// Arrange
var mock = new Mock<IPublicAPI>();

// Act
List<ContextMenuResult> contextMenuResults = _dummyInternetShortcutApp.ContextMenus(argument, mock.Object);

// Assert (Should always return if the above does not throw any exception)
Assert.True(true);
}
}
}
4 changes: 2 additions & 2 deletions src/modules/launcher/Plugins/Microsoft.Plugin.Program/Main.cs
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,9 @@ public List<ContextMenuResult> LoadContextMenus(Result selectedResult)
}

var menuOptions = new List<ContextMenuResult>();
if (selectedResult.ContextData is Programs.IProgram program)
if (selectedResult.ContextData is IProgram program)
{
menuOptions = program.ContextMenus(_context.API);
menuOptions = program.ContextMenus(selectedResult.ProgramArguments, _context.API);
}

return menuOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Microsoft.Plugin.Program.Programs
{
public interface IProgram
{
List<ContextMenuResult> ContextMenus(IPublicAPI api);
List<ContextMenuResult> ContextMenus(string queryArguments, IPublicAPI api);

Result Result(string query, string queryArguments, IPublicAPI api);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
Icon = Logo,
Score = score,
ContextData = this,
ProgramArguments = queryArguments,
Action = e =>
{
Launch(api, queryArguments);
Expand All @@ -117,7 +118,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Intentially keeping the process alive.")]
public List<ContextMenuResult> ContextMenus(IPublicAPI api)
public List<ContextMenuResult> ContextMenus(string queryArguments, IPublicAPI api)
{
if (api == null)
{
Expand All @@ -144,7 +145,7 @@ public List<ContextMenuResult> ContextMenus(IPublicAPI api)

var info = ShellCommand.SetProcessStartInfo(command, verb: "runas");
info.UseShellExecute = true;

info.Arguments = queryArguments;
Process.Start(info);
return true;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,15 +220,10 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
IcoPath = IcoPath,
Score = score,
ContextData = this,
ProgramArguments = queryArguments,
Action = e =>
{
var info = new ProcessStartInfo
{
FileName = LnkResolvedPath ?? FullPath,
WorkingDirectory = ParentDirectory,
UseShellExecute = true,
Arguments = queryArguments,
};
var info = GetProcessStartInfo(queryArguments);

Main.StartProcess(Process.Start, info);

Expand All @@ -248,7 +243,7 @@ public Result Result(string query, string queryArguments, IPublicAPI api)
}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1031:Do not catch general exception types", Justification = "Intentially keeping the process alive.")]
public List<ContextMenuResult> ContextMenus(IPublicAPI api)
public List<ContextMenuResult> ContextMenus(string queryArguments, IPublicAPI api)
{
if (api == null)
{
Expand All @@ -269,14 +264,7 @@ public List<ContextMenuResult> ContextMenus(IPublicAPI api)
AcceleratorModifiers = ModifierKeys.Control | ModifierKeys.Shift,
Action = _ =>
{
var info = new ProcessStartInfo
{
FileName = FullPath,
WorkingDirectory = ParentDirectory,
Verb = "runas",
UseShellExecute = true,
};

var info = GetProcessStartInfo(queryArguments, true);
Task.Run(() => Main.StartProcess(Process.Start, info));

return true;
Expand Down Expand Up @@ -327,6 +315,18 @@ public List<ContextMenuResult> ContextMenus(IPublicAPI api)
return contextMenus;
}

private ProcessStartInfo GetProcessStartInfo(string programArguments, bool runAsAdmin = false)
{
return new ProcessStartInfo
{
FileName = LnkResolvedPath ?? FullPath,
WorkingDirectory = ParentDirectory,
UseShellExecute = true,
Arguments = programArguments,
Verb = runAsAdmin ? "runas" : string.Empty,
};
}

public override string ToString()
{
return ExecutableName;
Expand Down
2 changes: 2 additions & 0 deletions src/modules/launcher/Wox.Plugin/Result.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public string Title

public string FontFamily { get; set; }

public string ProgramArguments { get; set; }

public Visibility ToolTipVisibility { get; set; } = Visibility.Collapsed;

public ToolTipData ToolTipData
Expand Down