diff --git a/Directory.Build.props b/Directory.Build.props
index 291e3b4f05..f8cec32fb2 100644
--- a/Directory.Build.props
+++ b/Directory.Build.props
@@ -34,6 +34,26 @@
net6.0;net7.0;net8.0
+
+
+
+
+
+
+
+
+ false
+ true
+ true
+ $(DefineConstants);IS_DATA_SOURCE_SUPPORTED
+
+
diff --git a/Directory.Packages.props b/Directory.Packages.props
index 45514197e9..fae29b7552 100644
--- a/Directory.Packages.props
+++ b/Directory.Packages.props
@@ -30,6 +30,9 @@
+
+
+
@@ -37,6 +40,7 @@
+
diff --git a/samples/FxExtensibility/FxExtensibility.csproj b/samples/FxExtensibility/FxExtensibility.csproj
index 13face57bd..152e1d9d23 100644
--- a/samples/FxExtensibility/FxExtensibility.csproj
+++ b/samples/FxExtensibility/FxExtensibility.csproj
@@ -8,7 +8,7 @@
MSTest.Extensibility.SamplesMSTest.Extensibility.Samples
- TRACE
+ $(DefineConstants);TRACEprompt4
diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj
index 7eda3660e6..ecf487a7c2 100644
--- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj
+++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj
@@ -41,7 +41,7 @@
Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapterMicrosoft.VisualStudio.TestPlatform.MSTest.TestAdapter
- TRACE
+ $(DefineConstants);TRACEtrue
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/CsvDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/CsvDataConnection.cs
index 759b8f9645..f2e8d14525 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/CsvDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/CsvDataConnection.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Collections;
using System.Data;
@@ -77,6 +77,13 @@ public override List GetDataTablesAndViews()
[SuppressMessage("Microsoft.Security", "CA2100:Review SQL queries for security", Justification = "Not passed in from user.")]
public DataTable ReadTable(string tableName, IEnumerable? columns, int maxRows)
{
+#if !NETFRAMEWORK
+ if (!OperatingSystem.IsWindows())
+ {
+ // TODO: It looks like the whole Csv logic can be refactored to work on all operating systems by not using OleDbConnection at all?
+ throw new NotSupportedException("CsvDataConnection is only supported on Windows.");
+ }
+#endif
// We specifically use OleDb to read a CSV file...
WriteDiagnostics("ReadTable: {0}", tableName);
WriteDiagnostics("Current Directory: {0}", Directory.GetCurrentDirectory());
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/OdbcDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/OdbcDataConnection.cs
index a02afb0351..753c15ea9d 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/OdbcDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/OdbcDataConnection.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Data.Odbc;
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/OleDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/OleDataConnection.cs
index afe551583c..06000a76f0 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/OleDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/OleDataConnection.cs
@@ -1,9 +1,12 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Data.OleDb;
+#if !NETFRAMEWORK
+using System.Runtime.Versioning;
+#endif
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -12,6 +15,9 @@ namespace Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Dat
///
/// Utility classes to access databases, and to handle quoted strings etc for OLE DB.
///
+#if !NETFRAMEWORK
+[SupportedOSPlatform("windows")]
+#endif
internal sealed class OleDataConnection : TestDataConnectionSql
{
private readonly bool _isMSSql;
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/SqlDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/SqlDataConnection.cs
index e0bb32fa1b..6f25bdd4df 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/SqlDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/SqlDataConnection.cs
@@ -1,9 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
+#if NETFRAMEWORK
using System.Data.SqlClient;
+#else
+using Microsoft.Data.SqlClient;
+#endif
using Microsoft.VisualStudio.TestTools.UnitTesting;
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnection.cs
index 799a4cfbfd..6c3f5a94ef 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnection.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Collections;
using System.Data;
using System.Data.Common;
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionFactory.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionFactory.cs
index aa332bbb2f..e85f181281 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionFactory.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionFactory.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using Microsoft.VisualStudio.TestTools.UnitTesting;
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionSql.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionSql.cs
index 1f9ca7507c..7f7f076bd0 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionSql.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/TestDataConnectionSql.cs
@@ -1,17 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Collections;
using System.Data;
using System.Data.Common;
using System.Data.Odbc;
using System.Data.OleDb;
+#if NETFRAMEWORK
using System.Data.SqlClient;
+#endif
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Text;
+#if !NETFRAMEWORK
+using Microsoft.Data.SqlClient;
+#endif
using Microsoft.VisualStudio.TestPlatform.ObjectModel;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -35,11 +40,11 @@ protected internal TestDataConnectionSql(string invariantProviderName, string co
DebugEx.Assert(Factory != null, "factory should not be null.");
WriteDiagnostics("DbProviderFactory {0}", Factory);
- _connection = Factory.CreateConnection();
+ _connection = Factory.CreateConnection()!;
DebugEx.Assert(_connection != null, "connection");
WriteDiagnostics("DbConnection {0}", _connection);
- CommandBuilder = Factory.CreateCommandBuilder();
+ CommandBuilder = Factory.CreateCommandBuilder()!;
DebugEx.Assert(CommandBuilder != null, "builder");
WriteDiagnostics("DbCommandBuilder {0}", CommandBuilder);
@@ -81,6 +86,13 @@ public static TestDataConnectionSql Create(string invariantProviderName, string
}
else if (string.Equals(invariantProviderName, "System.Data.OleDb", StringComparison.OrdinalIgnoreCase))
{
+#if !NETFRAMEWORK
+ if (!OperatingSystem.IsWindows())
+ {
+ throw new NotSupportedException($"Failed to create connection to '{connectionString}'. OleDbConnection is only supported on Windows.");
+ }
+#endif
+
return new OleDataConnection(invariantProviderName, connectionString, dataFolders);
}
else if (string.Equals(invariantProviderName, "System.Data.Odbc", StringComparison.OrdinalIgnoreCase))
@@ -485,7 +497,7 @@ public override List GetDataTablesAndViews()
try
{
WriteDiagnostics("Getting schema table {0}", metadata.SchemaTable);
- dataTable = Connection.GetSchema(metadata.SchemaTable);
+ dataTable = Connection.GetSchema(metadata.SchemaTable!);
}
catch (Exception ex)
{
@@ -521,9 +533,9 @@ public override List GetDataTablesAndViews()
}
// Get the schema name, and filter bad schemas
- if (row[metadata.SchemaColumn] != DBNull.Value)
+ if (row[metadata.SchemaColumn!] != DBNull.Value)
{
- tableSchema = row[metadata.SchemaColumn] as string;
+ tableSchema = row[metadata.SchemaColumn!] as string;
if (IsInArray(tableSchema, metadata.InvalidSchemas))
{
@@ -536,7 +548,7 @@ public override List GetDataTablesAndViews()
isDefaultSchema = string.Equals(tableSchema, defaultSchema, StringComparison.OrdinalIgnoreCase);
}
- string? tableName = row[metadata.NameColumn] as string;
+ string? tableName = row[metadata.NameColumn!] as string;
WriteDiagnostics("Table {0}{1} found", tableSchema != null ? tableSchema + "." : string.Empty, tableName);
// If schema is defined and is not equal to default, prepend table schema in front of the table.
@@ -599,7 +611,7 @@ public override List GetDataTablesAndViews()
foreach (DataRow columnRow in columns.Rows)
{
WriteDiagnostics("Column info: {0}", columnRow);
- result.Add(columnRow["COLUMN_NAME"].ToString());
+ result.Add(columnRow["COLUMN_NAME"].ToString()!);
}
// Now we are done, since for any particular table or view, all the columns
@@ -723,6 +735,12 @@ protected virtual bool IsUserSchema(string tableSchema) =>
{
var oleDbConnection = Connection as OleDbConnection;
var odbcConnection = Connection as OdbcConnection;
+#if !NETFRAMEWORK
+ if (oleDbConnection is not null && !OperatingSystem.IsWindows())
+ {
+ throw new NotSupportedException("OleDbConnection is only supported on Windows.");
+ }
+#endif
DebugEx.Assert(
Connection is SqlConnection ||
(oleDbConnection != null && IsMSSql(oleDbConnection.Provider)) ||
@@ -732,7 +750,11 @@ Connection is SqlConnection ||
DebugEx.Assert(IsOpen(), "The connection must already be open!");
DebugEx.Assert(!StringEx.IsNullOrEmpty(Connection.ServerVersion), "GetDefaultSchema: the ServerVersion is null or empty!");
+#if NETFRAMEWORK
int index = Connection.ServerVersion.IndexOf(".", StringComparison.Ordinal);
+#else
+ int index = Connection.ServerVersion.IndexOf('.');
+#endif
DebugEx.Assert(index > 0, "GetDefaultSchema: index should be 0");
string versionString = Connection.ServerVersion.Substring(0, index);
@@ -776,8 +798,8 @@ Connection is SqlConnection ||
public override DataTable ReadTable(string tableName, IEnumerable? columns)
#pragma warning restore SA1202 // Elements must be ordered by access
{
- using DbDataAdapter dataAdapter = Factory.CreateDataAdapter();
- using DbCommand command = Factory.CreateCommand();
+ using DbDataAdapter dataAdapter = Factory.CreateDataAdapter()!;
+ using DbCommand command = Factory.CreateCommand()!;
// We need to escape bad characters in table name like [Sheet1$] in Excel.
// But if table name is quoted in terms of provider, don't touch it to avoid e.g. [dbo.tables.etc].
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Data/XmlDataConnection.cs b/src/Adapter/MSTestAdapter.PlatformServices/Data/XmlDataConnection.cs
index dfa7b3be55..1ca1073195 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Data/XmlDataConnection.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Data/XmlDataConnection.cs
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Collections;
using System.Data;
using System.Diagnostics.CodeAnalysis;
@@ -87,6 +87,9 @@ public XmlDataConnection(string fileName, List dataFolders)
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Un-tested. Preserving behavior.")]
[SuppressMessage("Security", "CA3075:Insecure DTD processing in XML", Justification = "Not enough tests to understand if we would break")]
[SuppressMessage("Security", "CA5366:Use XmlReader for 'DataSet.ReadXml()'", Justification = "Not enough tests to understand if we would break")]
+ [UnconditionalSuppressMessage("ReflectionAnalysis", "IL2026:RequiresUnreferencedCode",
+ Justification = "While this can fail and the analysis is actually correct, it seems like if it happened it's likely to be user's fault?" +
+ "If the user is referencing type from XML, they should be confident that they are not trimmed.")]
private DataSet? LoadDataSet(bool schemaOnly)
{
try
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj
index 7752d8d63a..9d68017fa5 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj
+++ b/src/Adapter/MSTestAdapter.PlatformServices/MSTestAdapter.PlatformServices.csproj
@@ -13,7 +13,7 @@
Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServicesMicrosoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices
- TRACE
+ $(DefineConstants);TRACE
@@ -36,6 +36,10 @@
+
+
+
+
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net6.0/PublicAPI.Unshipped.txt
index 7dc5c58110..928bd1b127 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net6.0/PublicAPI.Unshipped.txt
+++ b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net6.0/PublicAPI.Unshipped.txt
@@ -1 +1,3 @@
#nullable enable
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataConnection.get -> System.Data.Common.DbConnection?
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataRow.get -> System.Data.DataRow?
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net7.0/PublicAPI.Unshipped.txt b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net7.0/PublicAPI.Unshipped.txt
index 7dc5c58110..928bd1b127 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net7.0/PublicAPI.Unshipped.txt
+++ b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net7.0/PublicAPI.Unshipped.txt
@@ -1 +1,3 @@
#nullable enable
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataConnection.get -> System.Data.Common.DbConnection?
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataRow.get -> System.Data.DataRow?
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net8.0/PublicAPI.Unshipped.txt b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net8.0/PublicAPI.Unshipped.txt
index 7dc5c58110..928bd1b127 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net8.0/PublicAPI.Unshipped.txt
+++ b/src/Adapter/MSTestAdapter.PlatformServices/PublicAPI/net8.0/PublicAPI.Unshipped.txt
@@ -1 +1,3 @@
#nullable enable
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataConnection.get -> System.Data.Common.DbConnection?
+override Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.TestContextImplementation.DataRow.get -> System.Data.DataRow?
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs
index b67fa921c0..824d12aea5 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestContextImplementation.cs
@@ -2,7 +2,7 @@
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.Collections;
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Data;
using System.Data.Common;
#endif
@@ -53,7 +53,7 @@ public class TestContextImplementation : TestContext, ITestContext
///
private UnitTestOutcome _outcome;
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
///
/// DB connection for test context.
///
@@ -100,7 +100,7 @@ public TestContextImplementation(ITestMethod testMethod, StringWriter stringWrit
///
public override UnitTestOutcome CurrentTestOutcome => _outcome;
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
///
public override DbConnection? DataConnection => _dbConnection;
@@ -266,7 +266,7 @@ public void SetException(Exception? exception)
/// data row.
public void SetDataRow(object? dataRow)
{
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
#pragma warning disable IDE0022 // Use expression body for method
_dataRow = dataRow as DataRow;
#pragma warning restore IDE0022 // Use expression body for method
@@ -282,7 +282,7 @@ public void SetDataRow(object? dataRow)
/// db Connection.
public void SetDataConnection(object? dbConnection)
{
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
#pragma warning disable IDE0022 // Use expression body for method
_dbConnection = dbConnection as DbConnection;
#pragma warning restore IDE0022 // Use expression body for method
diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestDataSource.cs b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestDataSource.cs
index 04b6218b9e..2a4c844be9 100644
--- a/src/Adapter/MSTestAdapter.PlatformServices/Services/TestDataSource.cs
+++ b/src/Adapter/MSTestAdapter.PlatformServices/Services/TestDataSource.cs
@@ -1,10 +1,13 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
-#if NETFRAMEWORK
+#if IS_DATA_SOURCE_SUPPORTED
using System.Configuration;
using System.Data;
using System.Diagnostics;
+#if NET6_0_OR_GREATER
+using System.Diagnostics.CodeAnalysis;
+#endif
using System.Globalization;
using Microsoft.VisualStudio.TestPlatform.MSTestAdapter.PlatformServices.Data;
@@ -31,15 +34,26 @@ public class TestDataSource : ITestDataSource
#if NETFRAMEWORK
public IEnumerable
+
diff --git a/src/TestFramework/TestFramework/TestFramework.csproj b/src/TestFramework/TestFramework/TestFramework.csproj
index 8f67b0069d..67da309e7f 100644
--- a/src/TestFramework/TestFramework/TestFramework.csproj
+++ b/src/TestFramework/TestFramework/TestFramework.csproj
@@ -8,7 +8,7 @@
Microsoft.VisualStudio.TestTools.UnitTestingMicrosoft.VisualStudio.TestPlatform.TestFramework
- TRACE
+ $(DefineConstants);TRACE
diff --git a/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj b/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj
index c243ff45e2..47fb7b1742 100644
--- a/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj
+++ b/test/IntegrationTests/TestAssets/TestProject/TestProjectForDiscovery.csproj
@@ -5,7 +5,7 @@
- TRACE
+ $(DefineConstants);TRACEprompt4False