Skip to content

Commit dadd19c

Browse files
committed
Enable Roslyn analysis - NuGetPackagesTests
1 parent 43fec71 commit dadd19c

35 files changed

+302
-180
lines changed

test/IntegrationTests/Helpers/CollectorRequestHelper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public static async Task<MemoryStream> ReadBodyToMemoryAsync(this HttpContext ct
2121
ctx.Request.Body.Position = 0;
2222

2323
var inMemory = new MemoryStream();
24-
await ctx.Request.Body.CopyToAsync(inMemory);
24+
await ctx.Request.Body.CopyToAsync(inMemory).ConfigureAwait(false);
2525

2626
inMemory.Position = 0;
2727

test/IntegrationTests/Helpers/CollectorResponseHelper.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ public static async Task GenerateEmptyProtobufResponseAsync<T>(this HttpContext
5353
using var outMemory = new MemoryStream();
5454
responseMessage.WriteTo(outMemory);
5555

56-
await ctx.Response.Body.WriteAsync(outMemory.GetBuffer(), 0, (int)outMemory.Length);
57-
await ctx.Response.CompleteAsync();
56+
await ctx.Response.Body.WriteAsync(outMemory.GetBuffer().AsMemory(0, (int)outMemory.Length)).ConfigureAwait(false);
57+
await ctx.Response.CompleteAsync().ConfigureAwait(false);
5858
}
5959

6060
public static async Task GenerateEmptyJsonResponseAsync(this HttpContext ctx)
6161
{
6262
ctx.Response.ContentType = "application/json";
63-
await ctx.Response.WriteAsync("{}");
63+
await ctx.Response.WriteAsync("{}").ConfigureAwait(false);
6464
}
6565
#endif
6666
}

test/IntegrationTests/Helpers/Compatibility/DictionaryExtensions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace IntegrationTests.Helpers.Compatibility;
99

10-
public static class DictionaryExtensions
10+
internal static class DictionaryExtensions
1111
{
1212
public static TValue? GetValueOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key)
1313
{
@@ -48,7 +48,9 @@ public static bool TryGetValue<TValue>(this IDictionary dictionary, object key,
4848
// if a key is not found, but let's use try/catch to be defensive against misbehaving implementations
4949
valueObj = dictionary[key];
5050
}
51-
catch
51+
#pragma warning disable CA1031 // Do not catch general exception types
52+
catch (Exception)
53+
#pragma warning restore CA1031 // Do not catch general exception types
5254
{
5355
valueObj = null;
5456
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// Copyright The OpenTelemetry Authors
2+
// SPDX-License-Identifier: Apache-2.0
3+
4+
#if NETFRAMEWORK
5+
using System.Globalization;
6+
7+
namespace System.Text;
8+
9+
internal static class StringBuilderExtensions
10+
{
11+
public static StringBuilder AppendLine(this StringBuilder stringBuilder, CultureInfo cultureInfo, string value)
12+
{
13+
return stringBuilder.AppendLine(value);
14+
}
15+
16+
public static StringBuilder Append(this StringBuilder stringBuilder, CultureInfo cultureInfo, string value)
17+
{
18+
return stringBuilder.AppendLine(value);
19+
}
20+
}
21+
#endif

test/IntegrationTests/Helpers/DockerNetworkHelper.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ internal static class DockerNetworkHelper
2121
/// <returns>Docker network name</returns>
2222
internal static async Task<string> SetupIntegrationTestsNetworkAsync()
2323
{
24-
var client = new DockerClientConfiguration().CreateClient();
25-
var networks = await client.Networks.ListNetworksAsync();
24+
using var clientConfiguration = new DockerClientConfiguration();
25+
var client = clientConfiguration.CreateClient();
26+
var networks = await client.Networks.ListNetworksAsync().ConfigureAwait(false);
2627
var network = networks.FirstOrDefault(x => x.Name == IntegrationTestsNetworkName);
2728

2829
if (network != null)
@@ -33,7 +34,7 @@ internal static async Task<string> SetupIntegrationTestsNetworkAsync()
3334
}
3435
else
3536
{
36-
await client.Networks.DeleteNetworkAsync(network.ID);
37+
await client.Networks.DeleteNetworkAsync(network.ID).ConfigureAwait(false);
3738
}
3839
}
3940

@@ -53,10 +54,10 @@ internal static async Task<string> SetupIntegrationTestsNetworkAsync()
5354
Subnet = "10.1.1.0/24"
5455
});
5556

56-
var result = await client.Networks.CreateNetworkAsync(networkParams);
57+
var result = await client.Networks.CreateNetworkAsync(networkParams).ConfigureAwait(false);
5758
if (string.IsNullOrWhiteSpace(result.ID))
5859
{
59-
throw new Exception("Could not create docker network");
60+
throw new InvalidOperationException("Could not create docker network");
6061
}
6162

6263
return IntegrationTestsNetworkName;

test/IntegrationTests/Helpers/DockerSystemHelper.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@
77

88
namespace IntegrationTests.Helpers;
99

10-
internal class DockerSystemHelper
10+
internal sealed class DockerSystemHelper
1111
{
1212
public static async Task<bool> GetIsWindowsEngineEnabled()
1313
{
1414
using var client = GetDockerClient();
1515

16-
var version = await client.System.GetVersionAsync();
16+
var version = await client.System.GetVersionAsync().ConfigureAwait(false);
1717
return version.Os.IndexOf("Windows", StringComparison.OrdinalIgnoreCase) > -1;
1818
}
1919

test/IntegrationTests/Helpers/EnvironmentHelper.cs

Lines changed: 37 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,19 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
using System.Collections.Specialized;
5+
using System.Globalization;
56
using System.Reflection;
67
using System.Runtime.InteropServices;
78
using System.Runtime.Versioning;
89
using Xunit.Abstractions;
910

1011
namespace IntegrationTests.Helpers;
1112

12-
public class EnvironmentHelper
13+
internal sealed class EnvironmentHelper
1314
{
14-
private static readonly string RuntimeFrameworkDescription = RuntimeInformation.FrameworkDescription.ToLower();
15+
#pragma warning disable CA1308 // Normalize strings to uppercase
16+
private static readonly string RuntimeFrameworkDescription = RuntimeInformation.FrameworkDescription.ToLowerInvariant();
17+
#pragma warning restore CA1308 // Normalize strings to uppercase
1518

1619
private readonly ITestOutputHelper _output;
1720
private readonly int _major;
@@ -41,11 +44,15 @@ public EnvironmentHelper(
4144

4245
var parts = _targetFramework.FrameworkName.Split(',');
4346
_runtime = parts[0];
44-
_isCoreClr = _runtime.Equals(EnvironmentTools.CoreFramework);
47+
_isCoreClr = _runtime.Equals(EnvironmentTools.CoreFramework, StringComparison.Ordinal);
4548

49+
#if NET
50+
var versionParts = parts[1].Replace("Version=v", string.Empty, StringComparison.Ordinal).Split('.');
51+
#else
4652
var versionParts = parts[1].Replace("Version=v", string.Empty).Split('.');
47-
_major = int.Parse(versionParts[0]);
48-
_minor = int.Parse(versionParts[1]);
53+
#endif
54+
_major = int.Parse(versionParts[0], CultureInfo.InvariantCulture);
55+
_minor = int.Parse(versionParts[1], CultureInfo.InvariantCulture);
4956

5057
if (versionParts.Length == 3)
5158
{
@@ -77,7 +84,13 @@ public EnvironmentHelper(
7784

7885
public static bool IsCoreClr()
7986
{
80-
return RuntimeFrameworkDescription.Contains("core") || Environment.Version.Major >= 5;
87+
return
88+
#if NET
89+
RuntimeFrameworkDescription.Contains("core", StringComparison.Ordinal)
90+
#else
91+
RuntimeFrameworkDescription.Contains("core")
92+
#endif
93+
|| Environment.Version.Major >= 5;
8194
}
8295

8396
public static string GetNukeBuildOutput()
@@ -92,7 +105,7 @@ public static string GetNukeBuildOutput()
92105
return nukeOutputPath;
93106
}
94107

95-
throw new Exception($"Unable to find Nuke output at: {nukeOutputPath}. Ensure Nuke has run first.");
108+
throw new InvalidOperationException($"Unable to find Nuke output at: {nukeOutputPath}. Ensure Nuke has run first.");
96109
}
97110

98111
public static bool IsRunningOnCI()
@@ -138,14 +151,20 @@ public string GetProfilerPath()
138151
return _profilerFileLocation;
139152
}
140153

141-
throw new Exception($"Unable to find profiler at: {profilerPath}");
154+
throw new InvalidOperationException($"Unable to find profiler at: {profilerPath}");
142155
}
143156

144157
public string GetTestApplicationPath(string packageVersion = "", string framework = "", TestAppStartupMode startupMode = TestAppStartupMode.Auto)
145158
{
146159
var extension = startupMode switch
147160
{
148-
TestAppStartupMode.Auto => IsCoreClr() || _testApplicationDirectory.Contains("aspnet") ? ".dll" : GetExecutableExtension(),
161+
TestAppStartupMode.Auto => IsCoreClr() ||
162+
#if NET
163+
_testApplicationDirectory.Contains("aspnet", StringComparison.Ordinal)
164+
#else
165+
_testApplicationDirectory.Contains("aspnet")
166+
#endif
167+
? ".dll" : GetExecutableExtension(),
149168
TestAppStartupMode.DotnetCLI => ".dll",
150169
TestAppStartupMode.Exe => GetExecutableExtension(),
151170
_ => throw new InvalidOperationException($"Unknown startup mode '{startupMode}'")
@@ -165,7 +184,11 @@ public string GetTestApplicationExecutionSource()
165184
{
166185
string executor;
167186

187+
#if NET
188+
if (_testApplicationDirectory.Contains("aspnet", StringComparison.Ordinal))
189+
#else
168190
if (_testApplicationDirectory.Contains("aspnet"))
191+
#endif
169192
{
170193
executor = $"C:\\Program Files{(Environment.Is64BitProcess ? string.Empty : " (x86)")}\\IIS Express\\iisexpress.exe";
171194
}
@@ -180,7 +203,7 @@ public string GetTestApplicationExecutionSource()
180203

181204
if (!File.Exists(executor))
182205
{
183-
throw new Exception($"Unable to find executing assembly at {executor}");
206+
throw new InvalidOperationException($"Unable to find executing assembly at {executor}");
184207
}
185208
}
186209

@@ -203,7 +226,11 @@ public string GetTestApplicationApplicationOutputDirectory(string packageVersion
203226
var targetFramework = string.IsNullOrEmpty(framework) ? GetTargetFramework() : framework;
204227
var baseBinDirectory = GetTestApplicationBaseBinDirectory();
205228

229+
#if NET
230+
if (_testApplicationDirectory.Contains("aspnet", StringComparison.Ordinal))
231+
#else
206232
if (_testApplicationDirectory.Contains("aspnet"))
233+
#endif
207234
{
208235
return Path.Combine(
209236
baseBinDirectory,

test/IntegrationTests/Helpers/EnvironmentTools.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace IntegrationTests.Helpers;
99
/// <summary>
1010
/// General use utility methods for all tests and tools.
1111
/// </summary>
12-
public static class EnvironmentTools
12+
internal static class EnvironmentTools
1313
{
1414
public const string ProfilerClsId = "{918728DD-259F-4A6A-AC2B-B85E1B658318}";
1515
public const string DotNetFramework = ".NETFramework";
@@ -34,7 +34,7 @@ public static class EnvironmentTools
3434

3535
if (currentDirectory == null || !currentDirectory.Exists)
3636
{
37-
throw new Exception($"Unable to find solution directory from: {startDirectory}");
37+
throw new InvalidOperationException($"Unable to find solution directory from: {startDirectory}");
3838
}
3939
}
4040

@@ -90,7 +90,9 @@ public static bool IsMacOS()
9090

9191
public static string GetPlatform()
9292
{
93+
#pragma warning disable CA1308 // Normalize strings to uppercase
9394
return RuntimeInformation.ProcessArchitecture.ToString().ToLowerInvariant();
95+
#pragma warning restore CA1308 // Normalize strings to uppercase
9496
}
9597

9698
public static bool IsX64()
@@ -125,7 +127,9 @@ public static string GetBuildConfiguration()
125127

126128
public static string GetClrProfilerDirectoryName()
127129
{
130+
#pragma warning disable CA1308 // Normalize strings to uppercase
128131
return $"{GetClrProfilerOSDirectoryName()}-{GetPlatformDir().ToLowerInvariant()}";
132+
#pragma warning restore CA1308 // Normalize strings to uppercase
129133
}
130134

131135
private static string? GetClrProfilerOSDirectoryName()

test/IntegrationTests/Helpers/FirewallHelper.cs

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

66
namespace IntegrationTests.Helpers;
77

8-
public static class FirewallHelper
8+
internal static class FirewallHelper
99
{
1010
public static FirewallPort OpenWinPort(int port, ITestOutputHelper output)
1111
{
12-
string ruleName = $"TraceAgent-{port}";
13-
string psCommand = $"New-NetFirewallRule -DisplayName '{ruleName}' -Direction Inbound -LocalPort {port} -Protocol TCP -Action Allow -Profile Any";
12+
var ruleName = $"TraceAgent-{port}";
13+
var psCommand = $"New-NetFirewallRule -DisplayName '{ruleName}' -Direction Inbound -LocalPort {port} -Protocol TCP -Action Allow -Profile Any";
1414

1515
PowershellHelper.RunCommand(psCommand, output);
1616

@@ -19,7 +19,7 @@ public static FirewallPort OpenWinPort(int port, ITestOutputHelper output)
1919

2020
public static void CloseWinPort(string ruleName, ITestOutputHelper output)
2121
{
22-
string psCommand = $"Remove-NetFirewallRule -DisplayName {ruleName}";
22+
var psCommand = $"Remove-NetFirewallRule -DisplayName {ruleName}";
2323

2424
PowershellHelper.RunCommand(psCommand, output);
2525
}

test/IntegrationTests/Helpers/FirewallPort.cs

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

66
namespace IntegrationTests.Helpers;
77

8-
public class FirewallPort : IDisposable
8+
internal sealed class FirewallPort : IDisposable
99
{
1010
private readonly ITestOutputHelper _output;
1111

0 commit comments

Comments
 (0)