Skip to content

Commit

Permalink
Merge branch 'main' into RemoveLinqExp
Browse files Browse the repository at this point in the history
  • Loading branch information
vitek-karas authored Jul 28, 2023
2 parents 0a2a105 + 8c7173f commit 4985d8d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 84 deletions.
55 changes: 27 additions & 28 deletions docs/logs/redaction/MyClassWithRedactionEnumerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,45 +16,44 @@

using System.Collections;

namespace Redaction
namespace Redaction;

internal class MyClassWithRedactionEnumerator : IReadOnlyList<KeyValuePair<string, object>>
{
internal class MyClassWithRedactionEnumerator : IReadOnlyList<KeyValuePair<string, object>>
{
private readonly IReadOnlyList<KeyValuePair<string, object>> state;
private readonly IReadOnlyList<KeyValuePair<string, object>> state;

public MyClassWithRedactionEnumerator(IReadOnlyList<KeyValuePair<string, object>> state)
{
this.state = state;
}
public MyClassWithRedactionEnumerator(IReadOnlyList<KeyValuePair<string, object>> state)
{
this.state = state;
}

public int Count => this.state.Count;
public int Count => this.state.Count;

public KeyValuePair<string, object> this[int index]
public KeyValuePair<string, object> this[int index]
{
get
{
get
var item = this.state[index];
var entryVal = item.Value;
if (entryVal != null && entryVal.ToString() != null && entryVal.ToString().Contains("<secret>"))
{
var item = this.state[index];
var entryVal = item.Value;
if (entryVal != null && entryVal.ToString() != null && entryVal.ToString().Contains("<secret>"))
{
return new KeyValuePair<string, object>(item.Key, "newRedactedValueHere");
}

return item;
return new KeyValuePair<string, object>(item.Key, "newRedactedValueHere");
}
}

public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (var i = 0; i < this.Count; i++)
{
yield return this[i];
}
return item;
}
}

IEnumerator IEnumerable.GetEnumerator()
public IEnumerator<KeyValuePair<string, object>> GetEnumerator()
{
for (var i = 0; i < this.Count; i++)
{
return this.GetEnumerator();
yield return this[i];
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return this.GetEnumerator();
}
}
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 4985d8d

Please sign in to comment.