Skip to content

Handle assemblies loaded via a stream #1443

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

Merged
Show file tree
Hide file tree
Changes from 3 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
9 changes: 7 additions & 2 deletions src/BenchmarkDotNet/Toolchains/CsProj/CsProjGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@ public CsProjGenerator(string targetFrameworkMoniker, string cliPath, string pac

protected override string GetBuildArtifactsDirectoryPath(BuildPartition buildPartition, string programName)
{
string directoryName = Path.GetDirectoryName(buildPartition.AssemblyLocation)
?? throw new DirectoryNotFoundException(buildPartition.AssemblyLocation);
string assemblyLocation = buildPartition.AssemblyLocation;

//Assembles loaded from a stream will have an empty location (https://docs.microsoft.com/en-us/dotnet/api/system.reflection.assembly.location).
string directoryName = assemblyLocation.IsEmpty() ?
Path.Combine(Directory.GetCurrentDirectory(), "BenchmarkDotNet.Bin") :
Path.GetDirectoryName(buildPartition.AssemblyLocation);

return Path.Combine(directoryName, programName);
}

Expand Down
66 changes: 66 additions & 0 deletions tests/BenchmarkDotNet.Tests/CsProjGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
using System.IO;
using System.Linq;
using System.Reflection;
using BenchmarkDotNet.Characteristics;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Tests.Mocks;
using BenchmarkDotNet.Toolchains.CsProj;
using JetBrains.Annotations;
using Xunit;
using BenchmarkDotNet.Extensions;

namespace BenchmarkDotNet.Tests
{
Expand Down Expand Up @@ -140,5 +148,63 @@ public void SettingsFromPropsFileImportedUsingRelativePathGetCopies()

File.Delete(propsFilePath);
}

[Fact]
public void TheDefaultFilePathShouldBeUsedWhenAnAssemblyLocationIsEmpty()
{
const string programName = "testProgram";
var config = ManualConfig.CreateEmpty().CreateImmutableConfig();
var asyncVoidMethod =
typeof(MockFactory.MockBenchmarkClass)
.GetTypeInfo()
.GetMethods()
.Single(method => method.Name == nameof(MockFactory.MockBenchmarkClass.Foo));


//Simulate loading an assembly from a stream
var benchmarkDotNetAssembly = typeof(MockFactory.MockBenchmarkClass).GetTypeInfo().Assembly;
var streamLoadedAssembly = Assembly.Load(File.ReadAllBytes(benchmarkDotNetAssembly.Location));
var assemblyType = streamLoadedAssembly.GetRunnableBenchmarks().Select(type => type).FirstOrDefault();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smart way to mock it! 👍


var target = new Descriptor(assemblyType, asyncVoidMethod);
var benchmarkCase = BenchmarkCase.Create(target, Job.Default, null, config);

var benchmarks = new[] { new BenchmarkBuildInfo(benchmarkCase, config.CreateImmutableConfig(), 999) };
var projectGenerator = new SteamLoadedBuildPartition("netcoreapp3.0", null, null, null);
string binariesPath = projectGenerator.ResolvePathForBinaries(new BuildPartition(benchmarks, new Resolver()), programName);

string expectedPath = Path.Combine(Path.Combine(Directory.GetCurrentDirectory(), "BenchmarkDotNet.Bin"), programName);
Assert.Equal(expectedPath, binariesPath);
}

[Fact]
public void TestAssemblyFilePathIsUsedWhenTheAssemblyLocationIsNotEmpty()
{
const string programName = "testProgram";
var asyncVoidMethod =
typeof(MockFactory.MockBenchmarkClass)
.GetTypeInfo()
.GetMethods()
.Single(method => method.Name == nameof(MockFactory.MockBenchmarkClass.Foo));
var target = new Descriptor(typeof(MockFactory.MockBenchmarkClass), asyncVoidMethod);
var benchmarkCase = BenchmarkCase.Create(target, Job.Default, null, ManualConfig.CreateEmpty().CreateImmutableConfig());
var benchmarks = new[] { new BenchmarkBuildInfo(benchmarkCase, ManualConfig.CreateEmpty().CreateImmutableConfig(), 0) };
var projectGenerator = new SteamLoadedBuildPartition("netcoreapp3.0", null, null, null);
var buildPartition = new BuildPartition(benchmarks, new Resolver());
string binariesPath = projectGenerator.ResolvePathForBinaries(buildPartition, programName);

string expectedPath = Path.Combine(Path.GetDirectoryName(buildPartition.AssemblyLocation), programName);
Assert.Equal(expectedPath, binariesPath);
}

private class SteamLoadedBuildPartition : CsProjGenerator
{
internal string ResolvePathForBinaries(BuildPartition buildPartition, string programName)
{
return base.GetBuildArtifactsDirectoryPath(buildPartition, programName);
}

public SteamLoadedBuildPartition(string targetFrameworkMoniker, string cliPath, string packagesPath, string runtimeFrameworkVersion) : base(targetFrameworkMoniker, cliPath, packagesPath, runtimeFrameworkVersion) { }
}
}
}