Skip to content

Commit bd9ac69

Browse files
committed
Improve reliability and logging for MSBuild tests
Instead of directory probing, just use assembly metadata to persist the project directory where the test project files are. Quicker and also more reliable. Also, always generate binlogs for MSBuild tests, for troubleshooting purposes.
1 parent 66c3405 commit bd9ac69

File tree

3 files changed

+27
-20
lines changed

3 files changed

+27
-20
lines changed

src/ReportGenerator.Core.Test/FileManager.cs

+7-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System.IO;
22
using System.Linq;
3+
using System.Reflection;
34
using Xunit;
45

56
namespace Palmmedia.ReportGenerator.Core.Test
@@ -88,21 +89,12 @@ internal static string GetFilesDirectory()
8889
return filesDirectory;
8990
}
9091

91-
internal static string GetTestDirectory()
92-
{
93-
var currentDirectory = new DirectoryInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
94-
95-
while (true)
96-
{
97-
currentDirectory = currentDirectory.Parent;
98-
string directory = Path.Combine(currentDirectory.FullName, "ReportGenerator.Core.Test");
99-
100-
if (Directory.Exists(directory))
101-
{
102-
return directory;
103-
}
104-
}
105-
}
92+
internal static string GetTestDirectory() => Assembly
93+
.GetExecutingAssembly()
94+
.GetCustomAttributes<AssemblyMetadataAttribute>()
95+
.Where(attr => attr.Key == "ProjectDirectory")
96+
.Select(attr => attr.Value)
97+
.FirstOrDefault() ?? Directory.GetCurrentDirectory() + @"..\..";
10698
}
10799

108100
[CollectionDefinition("FileManager")]

src/ReportGenerator.Core.Test/MsBuildTest.cs

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Diagnostics;
1+
using System;
2+
using System.Diagnostics;
23
using System.IO;
34
using Xunit;
45

@@ -30,16 +31,19 @@ public void ExecuteMSBuildScript_NetFull()
3031
{
3132
found = true;
3233

34+
var log = Path.Combine(
35+
Environment.GetEnvironmentVariable("BUILD_ARTIFACTSTAGINGDIRECTORY") ?? FileManager.GetTestDirectory(),
36+
nameof(ExecuteMSBuildScript_NetFull) + ".binlog");
3337
var processStartInfo = new ProcessStartInfo(
3438
paths[i],
35-
$"MsBuildTestScript.proj /p:Configuration={configuration}")
39+
$"MsBuildTestScript.proj /p:Configuration={configuration} /bl:{log}")
3640
{
3741
WorkingDirectory = FileManager.GetTestDirectory(),
3842
RedirectStandardOutput = true
3943
};
4044

4145
var process = Process.Start(processStartInfo);
42-
process.WaitForExit();
46+
Assert.True(process.WaitForExit(5000));
4347

4448
Assert.True(0 == process.ExitCode, process.StandardOutput.ReadToEnd());
4549

@@ -59,16 +63,19 @@ public void ExecuteMSBuildScript_NetCore()
5963
configuration = "Debug";
6064
#endif
6165

66+
var log = Path.Combine(
67+
Environment.GetEnvironmentVariable("BUILD_ARTIFACTSTAGINGDIRECTORY") ?? FileManager.GetTestDirectory(),
68+
nameof(ExecuteMSBuildScript_NetCore) + ".binlog");
6269
var processStartInfo = new ProcessStartInfo(
6370
"dotnet",
64-
$"msbuild MsBuildTestScript_NetCore.proj /p:Configuration={configuration}")
71+
$"msbuild MsBuildTestScript_NetCore.proj /p:Configuration={configuration} /bl:{log}")
6572
{
6673
WorkingDirectory = FileManager.GetTestDirectory(),
6774
RedirectStandardOutput = true
6875
};
6976

7077
var process = Process.Start(processStartInfo);
71-
process.WaitForExit();
78+
Assert.True(process.WaitForExit(5000));
7279

7380
Assert.True(0 == process.ExitCode, process.StandardOutput.ReadToEnd());
7481
}

src/ReportGenerator.Core.Test/ReportGenerator.Core.Test.csproj

+8
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
<RootNamespace>Palmmedia.ReportGenerator.Core.Test</RootNamespace>
77
<AssemblyVersion>4.6.5.0</AssemblyVersion>
88
<FileVersion>4.6.5.0</FileVersion>
9+
<AppendTargetFrameworkToOutputPath>false</AppendTargetFrameworkToOutputPath>
910
</PropertyGroup>
1011

1112
<ItemGroup>
@@ -53,4 +54,11 @@
5354
</None>
5455
</ItemGroup>
5556

57+
<ItemGroup>
58+
<AssemblyAttribute Include="System.Reflection.AssemblyMetadataAttribute">
59+
<_Parameter1>ProjectDirectory</_Parameter1>
60+
<_Parameter2>$(MSBuildProjectDirectory)</_Parameter2>
61+
</AssemblyAttribute>
62+
</ItemGroup>
63+
5664
</Project>

0 commit comments

Comments
 (0)