Skip to content

Commit

Permalink
Update OpenTelemetry.AotCompatibility.Tests to use filescoped namespa…
Browse files Browse the repository at this point in the history
…ces (#4708)
  • Loading branch information
jarosal authored Jul 28, 2023
1 parent 06b0a06 commit 8c7173f
Showing 1 changed file with 55 additions and 56 deletions.
111 changes: 55 additions & 56 deletions test/OpenTelemetry.AotCompatibility.Tests/AotCompatibilityTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,74 +18,73 @@
using Xunit;
using Xunit.Abstractions;

namespace OpenTelemetry.AotCompatibility.Tests
namespace OpenTelemetry.AotCompatibility.Tests;

public class AotCompatibilityTests
{
public class AotCompatibilityTests
private readonly ITestOutputHelper testOutputHelper;

public AotCompatibilityTests(ITestOutputHelper testOutputHelper)
{
this.testOutputHelper = testOutputHelper;
}

/// <summary>
/// This test ensures that the intended APIs of the OpenTelemetry.AotCompatibility.TestApp libraries are
/// trimming and NativeAOT compatible.
///
/// This test follows the instructions in https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#show-all-warnings-with-sample-application
///
/// If this test fails, it is due to adding trimming and/or AOT incompatible changes
/// to code that is supposed to be compatible.
///
/// To diagnose the problem, inspect the test output which will contain the trimming and AOT errors. For example:
///
/// error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors'.
/// </summary>
[Fact]
public void EnsureAotCompatibility()
{
private readonly ITestOutputHelper testOutputHelper;
string[] paths = { @"..", "..", "..", "..", "OpenTelemetry.AotCompatibility.TestApp" };
string testAppPath = Path.Combine(paths);
string testAppProject = "OpenTelemetry.AotCompatibility.TestApp.csproj";

public AotCompatibilityTests(ITestOutputHelper testOutputHelper)
// ensure we run a clean publish every time
DirectoryInfo testObjDir = new DirectoryInfo(Path.Combine(testAppPath, "obj"));
if (testObjDir.Exists)
{
this.testOutputHelper = testOutputHelper;
testObjDir.Delete(recursive: true);
}

/// <summary>
/// This test ensures that the intended APIs of the OpenTelemetry.AotCompatibility.TestApp libraries are
/// trimming and NativeAOT compatible.
///
/// This test follows the instructions in https://learn.microsoft.com/dotnet/core/deploying/trimming/prepare-libraries-for-trimming#show-all-warnings-with-sample-application
///
/// If this test fails, it is due to adding trimming and/or AOT incompatible changes
/// to code that is supposed to be compatible.
///
/// To diagnose the problem, inspect the test output which will contain the trimming and AOT errors. For example:
///
/// error IL2091: 'T' generic argument does not satisfy 'DynamicallyAccessedMemberTypes.PublicConstructors'.
/// </summary>
[Fact]
public void EnsureAotCompatibility()
var process = new Process
{
string[] paths = { @"..", "..", "..", "..", "OpenTelemetry.AotCompatibility.TestApp" };
string testAppPath = Path.Combine(paths);
string testAppProject = "OpenTelemetry.AotCompatibility.TestApp.csproj";

// ensure we run a clean publish every time
DirectoryInfo testObjDir = new DirectoryInfo(Path.Combine(testAppPath, "obj"));
if (testObjDir.Exists)
// set '-nodereuse:false /p:UseSharedCompilation=false' so the MSBuild and Roslyn server processes don't hang around, which may hang the test in CI
StartInfo = new ProcessStartInfo("dotnet", $"publish {testAppProject} --self-contained -nodereuse:false /p:UseSharedCompilation=false")
{
testObjDir.Delete(recursive: true);
}

var process = new Process
{
// set '-nodereuse:false /p:UseSharedCompilation=false' so the MSBuild and Roslyn server processes don't hang around, which may hang the test in CI
StartInfo = new ProcessStartInfo("dotnet", $"publish {testAppProject} --self-contained -nodereuse:false /p:UseSharedCompilation=false")
{
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = testAppPath,
},
};
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true,
WorkingDirectory = testAppPath,
},
};

var expectedOutput = new System.Text.StringBuilder();
process.OutputDataReceived += (sender, e) =>
var expectedOutput = new System.Text.StringBuilder();
process.OutputDataReceived += (sender, e) =>
{
if (!string.IsNullOrEmpty(e.Data))
{
if (!string.IsNullOrEmpty(e.Data))
{
this.testOutputHelper.WriteLine(e.Data);
expectedOutput.AppendLine(e.Data);
}
};
this.testOutputHelper.WriteLine(e.Data);
expectedOutput.AppendLine(e.Data);
}
};

process.Start();
process.BeginOutputReadLine();
process.Start();
process.BeginOutputReadLine();

Assert.True(process.WaitForExit(milliseconds: 240_000), "dotnet publish command timed out after 240 seconds.");
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");
Assert.True(process.WaitForExit(milliseconds: 240_000), "dotnet publish command timed out after 240 seconds.");
Assert.True(process.ExitCode == 0, "Publishing the AotCompatibility app failed. See test output for more details.");

var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
Assert.Equal(30, warnings.Count());
}
var warnings = expectedOutput.ToString().Split('\n', '\r').Where(line => line.Contains("warning IL"));
Assert.Equal(30, warnings.Count());
}
}

0 comments on commit 8c7173f

Please sign in to comment.