Skip to content
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
8 changes: 4 additions & 4 deletions BrowseRouter/Executable.cs → BrowseRouter/Args.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace BrowseRouter;

public static class Executable
public static class Args
{
/// <summary>
/// Return the quoted executable path and subsquent args i.e. <c>"chrome.exe --new-window"</c> → <c>("chrome.exe", "--new-window")</c>
/// Return the quoted executable path and subsquent args i.e. <c>"chrome.exe" --new-window</c> → <c>("chrome.exe", "--new-window")</c>
/// or the unquoted executable path and no args i.e. <c>"chrome.exe</c> → <c>("chrome.exe", "")</c>
/// </summary>
public static (string, string) GetPathAndArgs(string s)
public static (string, string) SplitPathAndArgs(string s)
{
int q1 = s.IndexOf('"', 0);
int q2 = s.IndexOf('"', q1 + 1);
Expand All @@ -33,7 +33,7 @@ public static (string, string) GetPathAndArgs(string s)
/// <param name="originalArgs">The unformatted arguments</param>
/// <param name="uri">The URI to format the arguments with</param>
/// <returns>The formatted arguments</returns>
public static string FormatArguments(string originalArgs, Uri uri)
public static string Format(string originalArgs, Uri uri)
{
int tagReplacedCount = 0;
StringBuilder args = new();
Expand Down
4 changes: 2 additions & 2 deletions BrowseRouter/BrowserService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ public async Task LaunchAsync(string url, string windowTitle)
return;
}

(string path, string args) = Executable.GetPathAndArgs(pref.Browser.Location);
(string path, string args) = Args.SplitPathAndArgs(pref.Browser.Location);

args = Executable.FormatArguments(args, uri);
args = Args.Format(args, uri);

Log.Write($"Launching {path} with args \"{args}\"");

Expand Down
9 changes: 0 additions & 9 deletions BrowseRouter/Interop/Win32/Comctl32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,3 @@ internal static class Comctl32
[DllImport("Comctl32.dll", CharSet = CharSet.Unicode)]
public static extern nint LoadIconWithScaleDown(nint hinst, string pszName, int cx, int cy, out nint phico);
}

public static class Icon
{
/// <summary>
/// Default application icon. Corresponds to IDI_APPLICATION.
/// https://learn.microsoft.com/en-us/windows/win32/menurc/about-icons
/// </summary>
public const string Application = "#32512";
}
10 changes: 10 additions & 0 deletions BrowseRouter/Interop/Win32/Icon.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace BrowseRouter.Interop.Win32;

public static class Icon
{
/// <summary>
/// Default application icon. Corresponds to IDI_APPLICATION.
/// https://learn.microsoft.com/en-us/windows/win32/menurc/about-icons
/// </summary>
public const string Application = "#32512";
}
14 changes: 12 additions & 2 deletions BrowseRouter/Interop/Win32/Kernel32.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@ namespace BrowseRouter.Interop.Win32;

public static class Kernel32
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern nint GetModuleHandle(string? lpModuleName);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern nint GetModuleHandle(string? lpModuleName);

private const uint ATTACH_PARENT_PROCESS = 0x0ffffffff;

[DllImport("kernel32.dll")]
private static extern bool AttachConsole(uint dwProcessId);

/// <summary>
/// This enables e.g. showing --help in Terminal / cmd text even though this is a WinExe app.
/// </summary>
public static void AttachToParentConsole() => AttachConsole(ATTACH_PARENT_PROCESS);
}
4 changes: 1 addition & 3 deletions BrowseRouter/Log.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Diagnostics;

namespace BrowseRouter;
namespace BrowseRouter;

public static class Log
{
Expand Down
8 changes: 6 additions & 2 deletions BrowseRouter/Program.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
using BrowseRouter.Interop.Win32;
using System.Runtime.InteropServices;
using BrowseRouter.Interop.Win32;

namespace BrowseRouter;

public static class Program
{

private static async Task Main(string[] args)
{
Kernel32.AttachToParentConsole();

if (args.Length == 0)
{
await new DefaultBrowserService(new NotifyService(false)).RegisterOrUnregisterAsync();
Expand Down Expand Up @@ -81,7 +85,7 @@ private static async Task LaunchUrlAsyc(string url)

private static void ShowHelp()
{
Log.Write
Console.WriteLine
(
$@"{nameof(BrowseRouter)}: In Windows, launch a different browser depending on the URL.

Expand Down
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>latest</LangVersion>
<Version>0.13.2</Version>
<Version>0.14.0</Version>
<Product>BrowseRouter</Product>
<Copyright>EnduraByte LLC 2024</Copyright>
<!-- Reduces flagging as malware -->
Expand Down
172 changes: 172 additions & 0 deletions Tests/BrowserRouter.Tests/ArgsTests/FormatMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
namespace BrowserRouter.Tests.ArgsTests;

public class FormatMethod
{
[Fact]
public void FormatsUrlTag()
{
// Arrange
string args = "--open-url {url}";
Uri uri = new Uri("https://example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--open-url https://example.com");
}

[Fact]
public void FormatsUserinfoTag()
{
// Arrange
string args = "--user-info {userinfo}";
Uri uri = new Uri("https://user:password@example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--user-info user:password");
}

[Fact]
public void FormatsHostTag()
{
// Arrange
string args = "--host {host}";
Uri uri = new Uri("https://example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--host example.com");
}

[Fact]
public void FormatsPortTag()
{
// Arrange
string args = "--port {port}";
Uri uri = new Uri("https://example.com:8080");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--port 8080");
}

[Fact]
public void FormatsAuthorityTag()
{
// Arrange
string args = "--authority {authority}";
Uri uri = new Uri("https://user:password@example.com:8080");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--authority example.com:8080");
}

[Fact]
public void FormatsPathTag()
{
// Arrange
string args = "--path {path}";
Uri uri = new Uri("https://example.com/some/path");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--path /some/path");
}

[Fact]
public void FormatsQueryTag()
{
// Arrange
string args = "--query {query}";
Uri uri = new Uri("https://example.com?query=1");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--query ?query=1");
}

[Fact]
public void FormatsFragmentTag()
{
// Arrange
string args = "--fragment {fragment}";
Uri uri = new Uri("https://example.com#fragment");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--fragment #fragment");
}

[Fact]
public void FormatsMultipleTags()
{
// Arrange
string args = "--host {host} --path {path}";
Uri uri = new Uri("https://example.com/some/path");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--host example.com --path /some/path");
}

[Fact]
public void HandlesNoTags()
{
// Arrange
string args = "--open-url";
Uri uri = new Uri("https://example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--open-url \"https://example.com\"");
}

[Fact]
public void HandlesNoArgs()
{
// Arrange
string args = "";
Uri uri = new Uri("https://example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be(" \"https://example.com\""); // Mind the leading space
}

[Fact]
public void IgnoresUnknownTags()
{
// Arrange
string args = "--unknown-tag {unknown}";
Uri uri = new Uri("https://example.com");

// Act
var result = Args.Format(args, uri);

// Assert
result.Should().Be("--unknown-tag {unknown} \"https://example.com\"");
}
}
56 changes: 56 additions & 0 deletions Tests/BrowserRouter.Tests/ArgsTests/GetPathAndArgsMethod.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
namespace BrowserRouter.Tests.ArgsTests;

public class GetPathAndArgsMethod
{
[Fact]
public void ReturnsQuotedPathAndArgs_WhenInputContainsQuotes()
{
// Arrange
string input = "\"chrome.exe\" --new-window";

// Act
var result = Args.SplitPathAndArgs(input);

// Assert
result.Should().Be(("chrome.exe", "--new-window"));
}

[Fact]
public void ReturnsPathOnly_WhenInputDoesNotContainQuotesOrArgs()
{
// Arrange
string input = "chrome.exe";

// Act
var result = Args.SplitPathAndArgs(input);

// Assert
result.Should().Be(("chrome.exe", ""));
}

[Fact]
public void ReturnsEmptyArgs_WhenInputHasOnlyQuotedPath()
{
// Arrange
string input = "\"chrome.exe\"";

// Act
var result = Args.SplitPathAndArgs(input);

// Assert
result.Should().Be(("chrome.exe", ""));
}

[Fact]
public void ReturnsInputAsPath_WhenInputContainsInvalidQuotes()
{
// Arrange
string input = "\"chrome.exe";

// Act
var result = Args.SplitPathAndArgs(input);

// Assert
result.Should().Be((input, ""));
}
}
11 changes: 7 additions & 4 deletions Tests/BrowserRouter.Tests/BrowserRouter.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />
<PackageReference Include="NUnit" Version="3.13.3" />
<PackageReference Include="NUnit3TestAdapter" Version="4.2.1" />
<PackageReference Include="NUnit.Analyzers" Version="3.3.0" />
<PackageReference Include="FluentAssertions" Version="6.12.2" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageReference Include="xunit" Version="2.9.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down
Loading
Loading