-
Notifications
You must be signed in to change notification settings - Fork 358
[Infra] Add helper for managing test env vars #3412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Infra] Add helper for managing test env vars #3412
Conversation
Add `EnvironmentVariableScope` helper class for managing the scope of changes to environment variables in tests to ensure the environment is consistently reset between tests.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3412 +/- ##
==========================================
- Coverage 70.79% 70.78% -0.01%
==========================================
Files 440 440
Lines 17265 17265
==========================================
- Hits 12222 12221 -1
- Misses 5043 5044 +1 Flags with carried forward coverage won't be shown. Click here to find out more. 🚀 New features to boost your workflow:
|
Instead of `Span<T>`.
Add a `using` to ensure the environment variables are reset before another test runs, rather than outside the scope of the test when the instance is disposed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR introduces a shared utility class EnvironmentVariableScope to improve test isolation by managing environment variables with proper cleanup. The PR refactors multiple test files across the codebase to use this utility instead of manually managing environment variables.
- Adds
EnvironmentVariableScopeutility class for automatic environment variable cleanup - Refactors test classes to use the new utility, removing manual
IDisposableimplementations and cleanup code - Improves test isolation by ensuring environment variables are always restored to their original state
Reviewed Changes
Copilot reviewed 21 out of 21 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| test/Shared/EnvironmentVariableScope.cs | New shared utility class for managing scoped environment variables with automatic cleanup |
| test/OpenTelemetry.Resources.Azure.Tests/*.cs | Refactored to use EnvironmentVariableScope, removed manual IDisposable implementation |
| test/OpenTelemetry.Resources.AWS.Tests/*.cs | Refactored to use EnvironmentVariableScope, removed manual cleanup methods |
| test/OpenTelemetry.Instrumentation.StackExchangeRedis.Tests/*.cs | Simplified SemanticConventionScope to use EnvironmentVariableScope |
| test/OpenTelemetry.Instrumentation.SqlClient.Tests/*.cs | Refactored to use EnvironmentVariableScope for test setup and semantic convention scoping |
| test/OpenTelemetry.Instrumentation.Owin.Tests/*.cs | Replaced try-finally blocks with EnvironmentVariableScope |
| test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/*.cs | Simplified semantic convention scope management |
| test/OpenTelemetry.Instrumentation.AspNet.Tests/*.cs | Replaced try-finally blocks with EnvironmentVariableScope |
| test/OpenTelemetry.Instrumentation.AWSLambda.Tests/*.cs | Refactored constructor to use EnvironmentVariableScope for multiple environment variables |
| test/OpenTelemetry.Contrib.Shared.Tests/*.cs | Refactored to use EnvironmentVariableScope, removed manual IDisposable implementation |
| Multiple .csproj files | Added references to the shared EnvironmentVariableScope.cs file |
| opentelemetry-dotnet-contrib.sln | Added EnvironmentVariableScope.cs to solution and reordered docker-compose entries |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs
Outdated
Show resolved
Hide resolved
test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs
Outdated
Show resolved
Hide resolved
test/OpenTelemetry.Instrumentation.AWSLambda.Tests/AWSLambdaWrapperTests.cs
Outdated
Show resolved
Hide resolved
test/OpenTelemetry.Instrumentation.EntityFrameworkCore.Tests/EntityFrameworkIntegrationTests.cs
Show resolved
Hide resolved
Remove unused variables.
Only reset the environment once.
Changes
Add
EnvironmentVariableScopehelper class for managing the scope of changes to environment variables in tests to ensure the environment is consistently reset between tests.For example, #3411 sets multiple variables but does not reset them so state could easily bleed into other tests. With these changes the test could be trivially refactored to do so without requiring the test class to implement
IDisposableand manually revert the changes.Merge requirement checklist
AppropriateCHANGELOG.mdfiles updated for non-trivial changesChanges in public API reviewed (if applicable)