Commit 0270676
managed loader: refactor for testability, add unit tests (#7594)
## Summary of changes
Refactored the managed loader
(`Datadog.Trace.ClrProfiler.Managed.Loader`) to improve testability and
added comprehensive unit tests.
## Reason for change
The managed loader code was difficult to test due to tight coupling with
environment variables and filesystem access. This refactoring enables
better test coverage and makes the code more maintainable.
## Implementation details
- **New abstractions**:
- Added `IEnvironmentVariableProvider` and `EnvironmentVariableProvider`
to abstract environment variable access, enabling dependency injection
for tests
- Implemented as `readonly struct` with generic type parameters to avoid
heap allocations and boxing
- **Refactored startup logic**:
- Moved TFM directory computation into dedicated methods that can be
tested independently
- Separated directory existence checks from path computation
- Improved error messages and logging consistency
- **Code improvements**:
- Added nullable reference type annotations throughout
- Standardized Boolean environment variable parsing to match tracer
conventions
- Cleaned up comments and improved code clarity
## Test coverage
Added 5 new test files with coverage of:
- Environment variable reading and Boolean parsing
(`EnvironmentVariableProviderTests`)
- Log directory resolution across platforms (`StartupLoggerTests`)
- TFM directory computation for .NET Core (`StartupNetCoreTests`)
- TFM directory computation for .NET Framework
(`StartupNetFrameworkTests`)
- Edge cases and error conditions
- Added `MockEnvironmentVariableProvider` for test isolation
## Other details
This refactoring is preparatory work for #7568 which makes
`DD_DOTNET_TRACER_HOME` optional, but the changes were hard to test.
---------
Co-authored-by: Claude <[email protected]>
Co-authored-by: Andrew Lock <[email protected]>1 parent 7a823fe commit 0270676
File tree
16 files changed
+643
-206
lines changed- tracer
- build/_build
- src/Datadog.Trace.ClrProfiler.Managed.Loader
- test/Datadog.Trace.Tests
- ClrProfiler/Managed/Loader
- DistributedTracer
16 files changed
+643
-206
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1366 | 1366 | | |
1367 | 1367 | | |
1368 | 1368 | | |
| 1369 | + | |
1369 | 1370 | | |
1370 | 1371 | | |
1371 | 1372 | | |
| |||
Lines changed: 8 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
Lines changed: 41 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
Lines changed: 72 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
Lines changed: 47 additions & 40 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
| |||
16 | 19 | | |
17 | 20 | | |
18 | 21 | | |
19 | | - | |
| 22 | + | |
20 | 23 | | |
21 | | - | |
| 24 | + | |
22 | 25 | | |
23 | | - | |
| 26 | + | |
24 | 27 | | |
25 | | - | |
26 | | - | |
27 | 28 | | |
| 29 | + | |
28 | 30 | | |
29 | | - | |
30 | | - | |
| 31 | + | |
31 | 32 | | |
32 | | - | |
| 33 | + | |
| 34 | + | |
33 | 35 | | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
| 36 | + | |
39 | 37 | | |
40 | | - | |
41 | | - | |
| 38 | + | |
| 39 | + | |
42 | 40 | | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
| 41 | + | |
47 | 42 | | |
48 | | - | |
| 43 | + | |
| 44 | + | |
49 | 45 | | |
50 | 46 | | |
51 | | - | |
52 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
53 | 61 | | |
54 | 62 | | |
55 | 63 | | |
56 | 64 | | |
57 | | - | |
| 65 | + | |
58 | 66 | | |
59 | 67 | | |
60 | 68 | | |
61 | 69 | | |
62 | | - | |
| 70 | + | |
63 | 71 | | |
64 | 72 | | |
65 | 73 | | |
66 | 74 | | |
67 | 75 | | |
68 | 76 | | |
69 | 77 | | |
70 | | - | |
| 78 | + | |
71 | 79 | | |
72 | 80 | | |
73 | 81 | | |
| |||
76 | 84 | | |
77 | 85 | | |
78 | 86 | | |
79 | | - | |
| 87 | + | |
80 | 88 | | |
81 | | - | |
82 | 89 | | |
83 | 90 | | |
84 | 91 | | |
| |||
90 | 97 | | |
91 | 98 | | |
92 | 99 | | |
93 | | - | |
94 | | - | |
95 | | - | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
96 | 103 | | |
97 | | - | |
| 104 | + | |
98 | 105 | | |
99 | | - | |
100 | | - | |
101 | | - | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
102 | 109 | | |
103 | 110 | | |
104 | 111 | | |
| |||
108 | 115 | | |
109 | 116 | | |
110 | 117 | | |
111 | | - | |
| 118 | + | |
112 | 119 | | |
113 | | - | |
| 120 | + | |
114 | 121 | | |
115 | 122 | | |
116 | 123 | | |
| |||
126 | 133 | | |
127 | 134 | | |
128 | 135 | | |
129 | | - | |
| 136 | + | |
130 | 137 | | |
131 | 138 | | |
132 | 139 | | |
133 | 140 | | |
134 | | - | |
| 141 | + | |
135 | 142 | | |
136 | 143 | | |
137 | 144 | | |
138 | 145 | | |
139 | 146 | | |
140 | 147 | | |
141 | 148 | | |
142 | | - | |
| 149 | + | |
143 | 150 | | |
144 | | - | |
| 151 | + | |
145 | 152 | | |
146 | 153 | | |
147 | 154 | | |
| |||
Lines changed: 11 additions & 17 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
5 | 5 | | |
6 | 6 | | |
7 | 7 | | |
| 8 | + | |
| 9 | + | |
8 | 10 | | |
9 | 11 | | |
10 | 12 | | |
| |||
16 | 18 | | |
17 | 19 | | |
18 | 20 | | |
19 | | - | |
| 21 | + | |
20 | 22 | | |
21 | | - | |
22 | | - | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
29 | | - | |
| 23 | + | |
30 | 24 | | |
31 | 25 | | |
32 | | - | |
| 26 | + | |
33 | 27 | | |
34 | 28 | | |
35 | 29 | | |
| |||
43 | 37 | | |
44 | 38 | | |
45 | 39 | | |
46 | | - | |
| 40 | + | |
47 | 41 | | |
48 | 42 | | |
49 | 43 | | |
| |||
60 | 54 | | |
61 | 55 | | |
62 | 56 | | |
63 | | - | |
| 57 | + | |
64 | 58 | | |
65 | 59 | | |
66 | 60 | | |
67 | | - | |
| 61 | + | |
68 | 62 | | |
69 | 63 | | |
70 | 64 | | |
71 | 65 | | |
72 | 66 | | |
73 | | - | |
| 67 | + | |
74 | 68 | | |
75 | | - | |
| 69 | + | |
76 | 70 | | |
77 | 71 | | |
78 | 72 | | |
79 | | - | |
| 73 | + | |
80 | 74 | | |
81 | 75 | | |
82 | 76 | | |
| |||
0 commit comments