Skip to content

Commit 31fb3bd

Browse files
[OpenTelemetry.Api.Tests] enable analysis (#6246)
Co-authored-by: Rajkumar Rangaraj <[email protected]>
1 parent ea70cd5 commit 31fb3bd

File tree

12 files changed

+96
-70
lines changed

12 files changed

+96
-70
lines changed

test/OpenTelemetry.Api.Tests/BaggageTests.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright The OpenTelemetry Authors
22
// SPDX-License-Identifier: Apache-2.0
33

4+
using System.Globalization;
45
using Xunit;
56

67
namespace OpenTelemetry.Tests;
@@ -39,8 +40,10 @@ public void SetAndGetTest()
3940
Assert.Equal(list, Baggage.GetBaggage(Baggage.Current));
4041

4142
Assert.Equal(V1, Baggage.GetBaggage(K1));
42-
Assert.Equal(V1, Baggage.GetBaggage(K1.ToLower()));
43-
Assert.Equal(V1, Baggage.GetBaggage(K1.ToUpper()));
43+
#pragma warning disable CA1308 // Normalize strings to uppercase
44+
Assert.Equal(V1, Baggage.GetBaggage(K1.ToLower(CultureInfo.InvariantCulture)));
45+
#pragma warning restore CA1308 // Normalize strings to uppercase
46+
Assert.Equal(V1, Baggage.GetBaggage(K1.ToUpper(CultureInfo.InvariantCulture)));
4447
Assert.Null(Baggage.GetBaggage("NO_KEY"));
4548
Assert.Equal(V2, Baggage.Current.GetBaggage(K2));
4649

@@ -166,7 +169,7 @@ public void EnumeratorTest()
166169
var tag2 = enumerator.Current;
167170
Assert.False(enumerator.MoveNext());
168171

169-
Assert.Equal(list, new List<KeyValuePair<string, string>> { tag1, tag2 });
172+
Assert.Equal(list, [tag1, tag2]);
170173

171174
Baggage.ClearBaggage();
172175

test/OpenTelemetry.Api.Tests/Context/Propagation/B3PropagatorTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public void Fields_list()
359359
{
360360
ContainsExactly(
361361
this.b3propagator.Fields,
362-
new List<string> { B3Propagator.XB3TraceId, B3Propagator.XB3SpanId, B3Propagator.XB3ParentSpanId, B3Propagator.XB3Sampled, B3Propagator.XB3Flags });
362+
[B3Propagator.XB3TraceId, B3Propagator.XB3SpanId, B3Propagator.XB3ParentSpanId, B3Propagator.XB3Sampled, B3Propagator.XB3Flags]);
363363
}
364364

365365
private static void ContainsExactly(ISet<string> list, List<string> items)
@@ -371,7 +371,7 @@ private static void ContainsExactly(ISet<string> list, List<string> items)
371371
}
372372
}
373373

374-
private void ContainsExactly(IDictionary<string, string> dict, IDictionary<string, string> items)
374+
private void ContainsExactly(Dictionary<string, string> dict, Dictionary<string, string> items)
375375
{
376376
foreach (var d in dict)
377377
{

test/OpenTelemetry.Api.Tests/Context/Propagation/CompositePropagatorTests.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace OpenTelemetry.Context.Propagation.Tests;
88

99
public class CompositePropagatorTests
1010
{
11-
private static readonly string[] Empty = Array.Empty<string>();
11+
private static readonly string[] Empty = [];
1212
private static readonly Func<IDictionary<string, string>, string, IEnumerable<string>> Getter = (headers, name) =>
1313
{
1414
count++;
@@ -25,7 +25,7 @@ public class CompositePropagatorTests
2525
carrier[name] = value;
2626
};
2727

28-
private static int count = 0;
28+
private static int count;
2929

3030
private readonly ActivityTraceId traceId = ActivityTraceId.CreateRandom();
3131
private readonly ActivitySpanId spanId = ActivitySpanId.CreateRandom();
@@ -141,11 +141,11 @@ public void CompositePropagator_UsingSameTag()
141141
[Fact]
142142
public void CompositePropagator_ActivityContext_Baggage()
143143
{
144-
var compositePropagator = new CompositeTextMapPropagator(new List<TextMapPropagator>
145-
{
144+
var compositePropagator = new CompositeTextMapPropagator(
145+
[
146146
new TraceContextPropagator(),
147147
new BaggagePropagator(),
148-
});
148+
]);
149149

150150
var activityContext = new ActivityContext(this.traceId, this.spanId, ActivityTraceFlags.Recorded, traceState: null, isRemote: true);
151151
var baggage = new Dictionary<string, string> { ["key1"] = "value1" };
@@ -159,12 +159,12 @@ public void CompositePropagator_ActivityContext_Baggage()
159159
var extractedContext = compositePropagator.Extract(default, carrier, Getter);
160160
Assert.Equal(propagationContextActivityOnly, extractedContext);
161161

162-
carrier = new Dictionary<string, string>();
162+
carrier = [];
163163
compositePropagator.Inject(propagationContextBaggageOnly, carrier, Setter);
164164
extractedContext = compositePropagator.Extract(default, carrier, Getter);
165165
Assert.Equal(propagationContextBaggageOnly, extractedContext);
166166

167-
carrier = new Dictionary<string, string>();
167+
carrier = [];
168168
compositePropagator.Inject(propagationContextBoth, carrier, Setter);
169169
extractedContext = compositePropagator.Extract(default, carrier, Getter);
170170
Assert.Equal(propagationContextBoth, extractedContext);

test/OpenTelemetry.Api.Tests/Context/Propagation/PropagatorsTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OpenTelemetry.Context.Propagation.Tests;
77

8-
public class PropagatorsTests : IDisposable
8+
public sealed class PropagatorsTests : IDisposable
99
{
1010
public PropagatorsTests()
1111
{

test/OpenTelemetry.Api.Tests/Context/Propagation/TestPropagator.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace OpenTelemetry.Context.Propagation.Tests;
77

8-
public class TestPropagator : TextMapPropagator
8+
internal sealed class TestPropagator : TextMapPropagator
99
{
1010
private readonly string idHeaderName;
1111
private readonly string stateHeaderName;
1212
private readonly bool defaultContext;
1313

14-
private int extractCount = 0;
15-
private int injectCount = 0;
14+
private int extractCount;
15+
private int injectCount;
1616

1717
public TestPropagator(string idHeaderName, string stateHeaderName, bool defaultContext = false)
1818
{

test/OpenTelemetry.Api.Tests/Context/RuntimeContextTests.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace OpenTelemetry.Context.Tests;
77

8-
public class RuntimeContextTests : IDisposable
8+
public sealed class RuntimeContextTests : IDisposable
99
{
1010
public RuntimeContextTests()
1111
{
@@ -168,17 +168,21 @@ public void Dispose()
168168
}
169169

170170
#if NETFRAMEWORK
171-
private class RemoteObject : ContextBoundObject
171+
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
172+
private sealed class RemoteObject : ContextBoundObject
173+
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
172174
{
175+
#pragma warning disable CA1822 // Mark members as static
173176
public int GetValueFromContextSlot(string slotName)
177+
#pragma warning restore CA1822 // Mark members as static
174178
{
175179
// Slot is not propagated across AppDomains, attempting to get
176180
// an existing slot here should throw an ArgumentException.
177181
try
178182
{
179183
RuntimeContext.GetSlot<int>(slotName);
180184

181-
throw new Exception("Should not have found an existing slot: " + slotName);
185+
throw new InvalidOperationException("Should not have found an existing slot: " + slotName);
182186
}
183187
catch (ArgumentException)
184188
{

test/OpenTelemetry.Api.Tests/Internal/GuardTests.cs

Lines changed: 45 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ public void NullTest()
2222
// Invalid
2323
object? potato = null;
2424
var ex1 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(potato));
25-
Assert.Contains("Must not be null", ex1.Message);
25+
Assert.Contains("Must not be null", ex1.Message, StringComparison.OrdinalIgnoreCase);
2626
Assert.Equal("potato", ex1.ParamName);
2727

2828
object? @event = null;
2929
var ex2 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(@event));
30-
Assert.Contains("Must not be null", ex2.Message);
30+
Assert.Contains("Must not be null", ex2.Message, StringComparison.OrdinalIgnoreCase);
3131
Assert.Equal("@event", ex2.ParamName);
3232

3333
Thing? thing = null;
3434
var ex3 = Assert.Throws<ArgumentNullException>(() => Guard.ThrowIfNull(thing?.Bar));
35-
Assert.Contains("Must not be null", ex3.Message);
35+
Assert.Contains("Must not be null", ex3.Message, StringComparison.OrdinalIgnoreCase);
3636
Assert.Equal("thing?.Bar", ex3.ParamName);
3737
}
3838

@@ -45,16 +45,16 @@ public void NullOrEmptyTest()
4545

4646
// Invalid
4747
var ex1 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(null));
48-
Assert.Contains("Must not be null or empty", ex1.Message);
48+
Assert.Contains("Must not be null or empty", ex1.Message, StringComparison.OrdinalIgnoreCase);
4949
Assert.Equal("null", ex1.ParamName);
5050

5151
var ex2 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(string.Empty));
52-
Assert.Contains("Must not be null or empty", ex2.Message);
52+
Assert.Contains("Must not be null or empty", ex2.Message, StringComparison.OrdinalIgnoreCase);
5353
Assert.Equal("string.Empty", ex2.ParamName);
5454

5555
var x = string.Empty;
5656
var ex3 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrEmpty(x));
57-
Assert.Contains("Must not be null or empty", ex3.Message);
57+
Assert.Contains("Must not be null or empty", ex3.Message, StringComparison.OrdinalIgnoreCase);
5858
Assert.Equal("x", ex3.ParamName);
5959
}
6060

@@ -66,15 +66,15 @@ public void NullOrWhitespaceTest()
6666

6767
// Invalid
6868
var ex1 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrWhitespace(null));
69-
Assert.Contains("Must not be null or whitespace", ex1.Message);
69+
Assert.Contains("Must not be null or whitespace", ex1.Message, StringComparison.OrdinalIgnoreCase);
7070
Assert.Equal("null", ex1.ParamName);
7171

7272
var ex2 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrWhitespace(string.Empty));
73-
Assert.Contains("Must not be null or whitespace", ex2.Message);
73+
Assert.Contains("Must not be null or whitespace", ex2.Message, StringComparison.OrdinalIgnoreCase);
7474
Assert.Equal("string.Empty", ex2.ParamName);
7575

7676
var ex3 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfNullOrWhitespace(" \t\n\r"));
77-
Assert.Contains("Must not be null or whitespace", ex3.Message);
77+
Assert.Contains("Must not be null or whitespace", ex3.Message, StringComparison.OrdinalIgnoreCase);
7878
Assert.Equal("\" \\t\\n\\r\"", ex3.ParamName);
7979
}
8080

@@ -88,7 +88,7 @@ public void InvalidTimeoutTest()
8888

8989
// Invalid
9090
var ex1 = Assert.Throws<ArgumentOutOfRangeException>(() => Guard.ThrowIfInvalidTimeout(-100));
91-
Assert.Contains("Must be non-negative or 'Timeout.Infinite'", ex1.Message);
91+
Assert.Contains("Must be non-negative or 'Timeout.Infinite'", ex1.Message, StringComparison.OrdinalIgnoreCase);
9292
Assert.Equal("-100", ex1.ParamName);
9393
}
9494

@@ -104,10 +104,10 @@ public void RangeIntTest()
104104

105105
// Invalid
106106
var ex1 = Assert.Throws<ArgumentOutOfRangeException>(() => Guard.ThrowIfOutOfRange(-1, min: 0, max: 100, minName: "empty", maxName: "full"));
107-
Assert.Contains("Must be in the range: [0: empty, 100: full]", ex1.Message);
107+
Assert.Contains("Must be in the range: [0: empty, 100: full]", ex1.Message, StringComparison.OrdinalIgnoreCase);
108108

109109
var ex2 = Assert.Throws<ArgumentOutOfRangeException>(() => Guard.ThrowIfOutOfRange(-1, min: 0, max: 100, message: "error"));
110-
Assert.Contains("error", ex2.Message);
110+
Assert.Contains("error", ex2.Message, StringComparison.OrdinalIgnoreCase);
111111
}
112112

113113
[Fact]
@@ -120,10 +120,10 @@ public void RangeDoubleTest()
120120

121121
// Invalid
122122
var ex3 = Assert.Throws<ArgumentOutOfRangeException>(() => Guard.ThrowIfOutOfRange(-1.1, min: 0.1, max: 99.9, minName: "empty", maxName: "full"));
123-
Assert.Contains("Must be in the range: [0.1: empty, 99.9: full]", ex3.Message);
123+
Assert.Contains("Must be in the range: [0.1: empty, 99.9: full]", ex3.Message, StringComparison.OrdinalIgnoreCase);
124124

125125
var ex4 = Assert.Throws<ArgumentOutOfRangeException>(() => Guard.ThrowIfOutOfRange(-1.1, min: 0.0, max: 100.0));
126-
Assert.Contains("Must be in the range: [0, 100]", ex4.Message);
126+
Assert.Contains("Must be in the range: [0, 100]", ex4.Message, StringComparison.OrdinalIgnoreCase);
127127
}
128128

129129
[Fact]
@@ -147,41 +147,45 @@ public void ZeroTest()
147147

148148
// Invalid
149149
var ex1 = Assert.Throws<ArgumentException>(() => Guard.ThrowIfZero(0));
150-
Assert.Contains("Must not be zero", ex1.Message);
150+
Assert.Contains("Must not be zero", ex1.Message, StringComparison.OrdinalIgnoreCase);
151151
Assert.Equal("0", ex1.ParamName);
152152
}
153153

154-
public class Thing
154+
#pragma warning disable CA1812 // Avoid uninstantiated internal classes
155+
internal sealed class Thing
156+
#pragma warning restore CA1812 // Avoid uninstantiated internal classes
155157
{
156158
public string? Bar { get; set; }
157159
}
160+
}
158161

159162
#if !NET
160-
/// <summary>
161-
/// Borrowed from: <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/CallerArgumentExpressionAttributeTests.cs"/>.
162-
/// </summary>
163-
public class CallerArgumentExpressionAttributeTests
163+
/// <summary>
164+
/// Borrowed from: <see href="https://github.com/dotnet/runtime/blob/main/src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/CallerArgumentExpressionAttributeTests.cs"/>.
165+
/// </summary>
166+
#pragma warning disable SA1402 // File may only contain a single type
167+
public class CallerArgumentExpressionAttributeTests
168+
#pragma warning restore SA1402 // File may only contain a single type
169+
{
170+
[Theory]
171+
[InlineData(null)]
172+
[InlineData("")]
173+
[InlineData("paramName")]
174+
public void Ctor_ParameterName_Roundtrip(string? value)
164175
{
165-
[Theory]
166-
[InlineData(null)]
167-
[InlineData("")]
168-
[InlineData("paramName")]
169-
public static void Ctor_ParameterName_Roundtrip(string? value)
170-
{
171-
var caea = new CallerArgumentExpressionAttribute(value);
172-
Assert.Equal(value, caea.ParameterName);
173-
}
174-
175-
[Fact]
176-
public static void BasicTest()
177-
{
178-
Assert.Equal("null", GetValue(null));
179-
Assert.Equal("\"hello\"", GetValue("hello"));
180-
Assert.Equal("3 + 2", GetValue(3 + 2));
181-
Assert.Equal("new object()", GetValue(new object()));
182-
}
183-
184-
private static string? GetValue(object? argument, [CallerArgumentExpression(nameof(argument))] string? expr = null) => expr;
176+
var caea = new CallerArgumentExpressionAttribute(value);
177+
Assert.Equal(value, caea.ParameterName);
185178
}
186-
#endif
179+
180+
[Fact]
181+
public void BasicTest()
182+
{
183+
Assert.Equal("null", GetValue(null));
184+
Assert.Equal("\"hello\"", GetValue("hello"));
185+
Assert.Equal("3 + 2", GetValue(3 + 2));
186+
Assert.Equal("new object()", GetValue(new object()));
187+
}
188+
189+
private static string? GetValue(object? argument, [CallerArgumentExpression(nameof(argument))] string? expr = null) => expr;
187190
}
191+
#endif

test/OpenTelemetry.Api.Tests/OpenTelemetry.Api.Tests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>$(TargetFrameworksForTests)</TargetFrameworks>
66
<NoWarn>$(NoWarn),CS0618</NoWarn>
77
<ReferenceCoyotePackages>true</ReferenceCoyotePackages>
8+
<AnalysisLevel>latest-all</AnalysisLevel>
89
</PropertyGroup>
910

1011
<ItemGroup>

test/OpenTelemetry.Api.Tests/Trace/ActivityExtensionsTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ public void LastSetStatusWins()
134134
public void CheckRecordException()
135135
{
136136
var message = "message";
137-
var exception = new ArgumentNullException(message, new Exception(message));
137+
var exception = new ArgumentNullException(message, new InvalidOperationException(message));
138138
using var activity = new Activity("test-activity");
139139
activity.RecordException(exception);
140140

@@ -147,7 +147,7 @@ public void CheckRecordException()
147147
public void RecordExceptionWithAdditionalTags()
148148
{
149149
var message = "message";
150-
var exception = new ArgumentNullException(message, new Exception(message));
150+
var exception = new ArgumentNullException(message, new InvalidOperationException(message));
151151
using var activity = new Activity("test-activity");
152152

153153
var tags = new TagList

test/OpenTelemetry.Api.Tests/Trace/TelemetrySpanTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public void CheckRecordExceptionData()
1515

1616
using Activity activity = new Activity("exception-test");
1717
using TelemetrySpan telemetrySpan = new TelemetrySpan(activity);
18-
telemetrySpan.RecordException(new ArgumentNullException(message, new Exception("new-exception")));
18+
telemetrySpan.RecordException(new ArgumentNullException(message, new InvalidOperationException("new-exception")));
1919
Assert.Single(activity.Events);
2020

2121
Assert.NotNull(telemetrySpan.Activity);
@@ -66,7 +66,8 @@ public void ParentIds()
6666
Assert.Equal(default, parentSpan.ParentSpanId);
6767
Assert.NotNull(parentActivity.Id);
6868

69-
using var childActivity = new Activity("childOperation").SetParentId(parentActivity.Id);
69+
using var childActivity = new Activity("childOperation");
70+
childActivity.SetParentId(parentActivity.Id);
7071
using var childSpan = new TelemetrySpan(childActivity);
7172

7273
Assert.Equal(parentSpan.Context.SpanId, childSpan.ParentSpanId);

0 commit comments

Comments
 (0)