Skip to content

Commit de2b8fa

Browse files
andrewlockbouwkast
andauthored
Include the target framework in our InstrumentationTests (#7347)
## Summary of changes Make sure we always specify the target framework when creating a test console app ## Reason for change We have some tests where we explicitly create, publish, and run a .NET Console app. The behaviour we're looking for is dependent on the target framework, however previously we _weren't_ specifying the TFM to use. That meant that it was always using the SDK version, even though that is not expected by the tests (and breaks in some scenarios, e.g. when using preview SDK builds). ## Implementation details Explicitly set the target framework to use when creating the console app ## Test coverage If the tests pass, we're fine. ## Other details This is based on a bunch of workarounds @bouwkast did in the .NET 10 branch, but they should work standalone too. As for why we can't just do `dotnet new console -f net6.0`? Who knows😅 for some reason you can't specify an earlier tfm in the .NET template in our Linux docker images (even though it works on Windows). I'm guessing it's somehow related to how we install the SDKs, but for now, this is the hacky workaround --------- Co-authored-by: Steven Bouwkamp <[email protected]>
1 parent 01ca7d0 commit de2b8fa

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

tracer/test/Datadog.Trace.ClrProfiler.IntegrationTests/InstrumentationTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using System.Linq;
1010
using System.Runtime.CompilerServices;
1111
using System.Text;
12+
using System.Text.RegularExpressions;
1213
using System.Threading.Tasks;
1314
using Datadog.Trace.Configuration;
1415
using Datadog.Trace.TestHelpers;
@@ -83,6 +84,7 @@ public async Task DoesNotInstrumentDotnetBuild()
8384
using var agent = EnvironmentHelper.GetMockAgent(useTelemetry: true);
8485

8586
var logDir = await RunDotnet("new console -n instrumentation_test -o . --no-restore");
87+
FixTfm(workingDir);
8688
AssertNotInstrumented(agent, logDir);
8789

8890
logDir = await RunDotnet("restore");
@@ -115,6 +117,7 @@ public async Task DoesNotInstrumentExcludedNames(string excludedProcess)
115117
using var agent = EnvironmentHelper.GetMockAgent(useTelemetry: true);
116118

117119
var logDir = await RunDotnet($"new console -n {excludedProcess} -o . --no-restore");
120+
FixTfm(workingDir);
118121
AssertNotInstrumented(agent, logDir);
119122

120123
var programCs = GetProgramCSThatMakesSpans();
@@ -153,7 +156,9 @@ public async Task DoesInstrumentAllowedProcesses(string allowedProcess)
153156
using var agent = EnvironmentHelper.GetMockAgent(useTelemetry: true);
154157

155158
var logDir = await RunDotnet($"new console -n {allowedProcess} -o . --no-restore");
159+
FixTfm(workingDir);
156160
AssertNotInstrumented(agent, logDir);
161+
157162
var programCs = GetProgramCSThatMakesSpans();
158163

159164
File.WriteAllText(Path.Combine(workingDir, "Program.cs"), programCs);
@@ -594,7 +599,7 @@ private Task<string> RunDotnetCommand(string workingDirectory, MockTracerAgent m
594599
{
595600
// Disable .NET CLI telemetry to prevent extra HTTP spans
596601
SetEnvironmentVariable("DOTNET_CLI_TELEMETRY_OPTOUT", "1");
597-
return RunCommand(workingDirectory, mockTracerAgent, EnvironmentHelper.GetDotnetExe(), arguments);
602+
return RunCommand(workingDirectory, mockTracerAgent, "dotnet", arguments);
598603
}
599604

600605
private async Task<string> RunCommand(string workingDirectory, MockTracerAgent mockTracerAgent, string exe, string arguments = null)
@@ -758,6 +763,28 @@ private bool IsAllLoggingDisabledForBailout()
758763
return loggingDisabled;
759764
}
760765

766+
private void FixTfm(string workingDir)
767+
{
768+
// This is a hack because for _some_ reason, we can't set the -f flag in the Linux CI.
769+
// Force the project to target .NET 8 instead of whatever the SDK defaults to
770+
foreach (var projectFile in Directory.GetFiles(workingDir, "*.csproj"))
771+
{
772+
var projectContent = File.ReadAllText(projectFile);
773+
774+
// Replace any target framework with the updated version
775+
// and add a langversion update to ensure we still compile
776+
var replacement = $"""
777+
<TargetFramework>{EnvironmentHelper.GetTargetFramework()}</TargetFramework>
778+
<LangVersion>latest</LangVersion>
779+
""";
780+
var updatedContent = Regex.Replace(
781+
projectContent,
782+
@"<TargetFramework>.+</TargetFramework>",
783+
replacement);
784+
File.WriteAllText(projectFile, updatedContent);
785+
}
786+
}
787+
761788
public class TelemetryReporterFixture : IDisposable
762789
{
763790
private readonly string _workingDir = Path.Combine(Path.GetTempPath(), Path.GetFileNameWithoutExtension(Path.GetRandomFileName()));

0 commit comments

Comments
 (0)