1
1
using System . IO ;
2
+ using System . Linq ;
3
+ using System . Reflection ;
4
+ using BenchmarkDotNet . Characteristics ;
5
+ using BenchmarkDotNet . Configs ;
6
+ using BenchmarkDotNet . Jobs ;
7
+ using BenchmarkDotNet . Running ;
8
+ using BenchmarkDotNet . Tests . Mocks ;
2
9
using BenchmarkDotNet . Toolchains . CsProj ;
3
10
using JetBrains . Annotations ;
4
11
using Xunit ;
12
+ using BenchmarkDotNet . Extensions ;
5
13
6
14
namespace BenchmarkDotNet . Tests
7
15
{
@@ -140,5 +148,63 @@ public void SettingsFromPropsFileImportedUsingRelativePathGetCopies()
140
148
141
149
File . Delete ( propsFilePath ) ;
142
150
}
151
+
152
+ [ Fact ]
153
+ public void TheDefaultFilePathShouldBeUsedWhenAnAssemblyLocationIsEmpty ( )
154
+ {
155
+ const string programName = "testProgram" ;
156
+ var config = ManualConfig . CreateEmpty ( ) . CreateImmutableConfig ( ) ;
157
+ var benchmarkMethod =
158
+ typeof ( MockFactory . MockBenchmarkClass )
159
+ . GetTypeInfo ( )
160
+ . GetMethods ( )
161
+ . Single ( method => method . Name == nameof ( MockFactory . MockBenchmarkClass . Foo ) ) ;
162
+
163
+
164
+ //Simulate loading an assembly from a stream
165
+ var benchmarkDotNetAssembly = typeof ( MockFactory . MockBenchmarkClass ) . GetTypeInfo ( ) . Assembly ;
166
+ var streamLoadedAssembly = Assembly . Load ( File . ReadAllBytes ( benchmarkDotNetAssembly . Location ) ) ;
167
+ var assemblyType = streamLoadedAssembly . GetRunnableBenchmarks ( ) . Select ( type => type ) . FirstOrDefault ( ) ;
168
+
169
+ var target = new Descriptor ( assemblyType , benchmarkMethod ) ;
170
+ var benchmarkCase = BenchmarkCase . Create ( target , Job . Default , null , config ) ;
171
+
172
+ var benchmarks = new [ ] { new BenchmarkBuildInfo ( benchmarkCase , config . CreateImmutableConfig ( ) , 999 ) } ;
173
+ var projectGenerator = new SteamLoadedBuildPartition ( "netcoreapp3.0" , null , null , null ) ;
174
+ string binariesPath = projectGenerator . ResolvePathForBinaries ( new BuildPartition ( benchmarks , new Resolver ( ) ) , programName ) ;
175
+
176
+ string expectedPath = Path . Combine ( Path . Combine ( Directory . GetCurrentDirectory ( ) , "BenchmarkDotNet.Bin" ) , programName ) ;
177
+ Assert . Equal ( expectedPath , binariesPath ) ;
178
+ }
179
+
180
+ [ Fact ]
181
+ public void TestAssemblyFilePathIsUsedWhenTheAssemblyLocationIsNotEmpty ( )
182
+ {
183
+ const string programName = "testProgram" ;
184
+ var benchmarkMethod =
185
+ typeof ( MockFactory . MockBenchmarkClass )
186
+ . GetTypeInfo ( )
187
+ . GetMethods ( )
188
+ . Single ( method => method . Name == nameof ( MockFactory . MockBenchmarkClass . Foo ) ) ;
189
+ var target = new Descriptor ( typeof ( MockFactory . MockBenchmarkClass ) , benchmarkMethod ) ;
190
+ var benchmarkCase = BenchmarkCase . Create ( target , Job . Default , null , ManualConfig . CreateEmpty ( ) . CreateImmutableConfig ( ) ) ;
191
+ var benchmarks = new [ ] { new BenchmarkBuildInfo ( benchmarkCase , ManualConfig . CreateEmpty ( ) . CreateImmutableConfig ( ) , 0 ) } ;
192
+ var projectGenerator = new SteamLoadedBuildPartition ( "netcoreapp3.0" , null , null , null ) ;
193
+ var buildPartition = new BuildPartition ( benchmarks , new Resolver ( ) ) ;
194
+ string binariesPath = projectGenerator . ResolvePathForBinaries ( buildPartition , programName ) ;
195
+
196
+ string expectedPath = Path . Combine ( Path . GetDirectoryName ( buildPartition . AssemblyLocation ) , programName ) ;
197
+ Assert . Equal ( expectedPath , binariesPath ) ;
198
+ }
199
+
200
+ private class SteamLoadedBuildPartition : CsProjGenerator
201
+ {
202
+ internal string ResolvePathForBinaries ( BuildPartition buildPartition , string programName )
203
+ {
204
+ return base . GetBuildArtifactsDirectoryPath ( buildPartition , programName ) ;
205
+ }
206
+
207
+ public SteamLoadedBuildPartition ( string targetFrameworkMoniker , string cliPath , string packagesPath , string runtimeFrameworkVersion ) : base ( targetFrameworkMoniker , cliPath , packagesPath , runtimeFrameworkVersion ) { }
208
+ }
143
209
}
144
- }
210
+ }
0 commit comments