Skip to content

Commit

Permalink
Fix System.UriFormatException in DefaultAssemblyDirectoryFormatter (#…
Browse files Browse the repository at this point in the history
…489)

* Fix System.UriFormatException in `DefaultAssemblyDirectoryFormatter`.

Rename to `IAssemblyPathFormatter` as it returns the path to the assembly, not its directory.

Fixes #481

* Fix assertion on Windows

* SkippableFact -> Fact
  • Loading branch information
Xtansia authored Jul 13, 2022
1 parent eb2c557 commit 3e954d5
Show file tree
Hide file tree
Showing 11 changed files with 74 additions and 80 deletions.
15 changes: 0 additions & 15 deletions src/RazorLight/Compilation/DefaultAssemblyDirectoryFormatter.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/RazorLight/Compilation/DefaultAssemblyPathFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace RazorLight.Compilation
{
public class DefaultAssemblyPathFormatter : IAssemblyPathFormatter
{
public string GetAssemblyPath(Assembly assembly) => assembly.Location;
}
}
8 changes: 4 additions & 4 deletions src/RazorLight/Compilation/DefaultMetadataReferenceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ namespace RazorLight.Compilation
{
public class DefaultMetadataReferenceManager : IMetadataReferenceManager
{
private readonly IAssemblyDirectoryFormatter _directoryFormatter = new DefaultAssemblyDirectoryFormatter();
private readonly IAssemblyPathFormatter _pathFormatter = new DefaultAssemblyPathFormatter();
public HashSet<MetadataReference> AdditionalMetadataReferences { get; }
public HashSet<string> ExcludedAssemblies { get; }

Expand All @@ -22,9 +22,9 @@ public DefaultMetadataReferenceManager()
ExcludedAssemblies = new HashSet<string>();
}

public DefaultMetadataReferenceManager(IOptions<RazorLightOptions> options, IAssemblyDirectoryFormatter directoryFormatter) : this(options.Value.AdditionalMetadataReferences, options.Value.ExcludedAssemblies)
public DefaultMetadataReferenceManager(IOptions<RazorLightOptions> options, IAssemblyPathFormatter pathFormatter) : this(options.Value.AdditionalMetadataReferences, options.Value.ExcludedAssemblies)
{
_directoryFormatter = directoryFormatter;
_pathFormatter = pathFormatter;
}

public DefaultMetadataReferenceManager(HashSet<MetadataReference> metadataReferences)
Expand Down Expand Up @@ -54,7 +54,7 @@ internal IReadOnlyList<MetadataReference> Resolve(Assembly assembly, DependencyC
{
var context = new HashSet<string>();
var x = GetReferencedAssemblies(assembly, ExcludedAssemblies, context).Union(new[] { assembly }).ToArray();
references = x.Select(p => _directoryFormatter.GetAssemblyDirectory(p)).ToList();
references = x.Select(p => _pathFormatter.GetAssemblyPath(p)).ToList();
}
else
{
Expand Down
9 changes: 0 additions & 9 deletions src/RazorLight/Compilation/IAssemblyDirectoryFormatter.cs

This file was deleted.

9 changes: 9 additions & 0 deletions src/RazorLight/Compilation/IAssemblyPathFormatter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using System.Reflection;

namespace RazorLight.Compilation
{
public interface IAssemblyPathFormatter
{
string GetAssemblyPath(Assembly assembly);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

namespace RazorLight.Compilation
{
public class LegacyFixAssemblyDirectoryFormatter : IAssemblyDirectoryFormatter
public class LegacyFixAssemblyPathFormatter : IAssemblyPathFormatter
{
public string GetAssemblyDirectory(Assembly assembly)
public string GetAssemblyPath(Assembly assembly)
{
string codeBase = assembly.CodeBase;
UriBuilder uriBuilder = new UriBuilder(codeBase);
Expand Down

This file was deleted.

6 changes: 3 additions & 3 deletions src/RazorLight/RazorLightDependencyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ public RazorLightDependencyBuilder UseEmbeddedResourcesProject(Type rootType)

public RazorLightDependencyBuilder UseNetFrameworkLegacyFix()
{
_services.RemoveAll<IAssemblyDirectoryFormatter>();
IAssemblyDirectoryFormatter formatter = new LegacyFixAssemblyDirectoryFormatter();
_services.RemoveAll<IAssemblyPathFormatter>();
IAssemblyPathFormatter formatter = new LegacyFixAssemblyPathFormatter();
// ReSharper disable once RedundantTypeArgumentsOfMethod
_services.AddSingleton<IAssemblyDirectoryFormatter>(formatter);
_services.AddSingleton<IAssemblyPathFormatter>(formatter);
return this;
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using RazorLight.Compilation;
using System;
using System.Runtime.InteropServices;
using Xunit;
using Xunit.Abstractions;

namespace RazorLight.Tests.Compilation
{
public class DefaultAssemblyPathFormatterTest
{
private readonly ITestOutputHelper _testOutputHelper;

public DefaultAssemblyPathFormatterTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper ?? throw new ArgumentNullException(nameof(testOutputHelper));
}

[Fact]
public void Ensure_GetAssemblyPath_Works()
{
var assembly = typeof(DefaultAssemblyPathFormatterTest).Assembly;
_testOutputHelper.WriteLine(assembly.Location);
var directory = new DefaultAssemblyPathFormatter().GetAssemblyPath(assembly);
Assert.NotNull(directory);
}

[Fact]
public void Ensure_GetAssemblyPath_MatchesLegacy()
{
var assembly = typeof(DefaultAssemblyPathFormatterTest).Assembly;
_testOutputHelper.WriteLine(assembly.Location);
var directory = new DefaultAssemblyPathFormatter().GetAssemblyPath(assembly);
var legacyDir = new LegacyFixAssemblyPathFormatter().GetAssemblyPath(assembly);
Assert.NotNull(directory);
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
{
// On Windows, legacy formatter returns forward-slash as separator due to UriBuilder.
// So "normalise" the default one for comparison.
directory = directory.Replace('\\', '/');
}

Assert.Equal(legacyDir, directory);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -276,8 +276,8 @@ public void Ensure_DI_Extension_Can_Inject()
}));

var provider = services.BuildServiceProvider();
var directoryFormatter = provider.GetService<IAssemblyDirectoryFormatter>();
Assert.IsType<LegacyFixAssemblyDirectoryFormatter>(directoryFormatter);
var directoryFormatter = provider.GetService<IAssemblyPathFormatter>();
Assert.IsType<LegacyFixAssemblyPathFormatter>(directoryFormatter);

var project = provider.GetService<RazorLightProject>();
Assert.IsType<FileSystemRazorProject>(project);
Expand Down

0 comments on commit 3e954d5

Please sign in to comment.