From 8d8edc8919ebf31cf19dc7e481ca1251a42970e6 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 18 Nov 2024 14:58:42 +0100 Subject: [PATCH 1/5] Support DataSourceAttribute on .NET 6 and later --- Directory.Build.props | 20 +++++++++ Directory.Packages.props | 4 ++ .../FxExtensibility/FxExtensibility.csproj | 2 +- .../MSTest.TestAdapter.csproj | 2 +- .../Data/CsvDataConnection.cs | 9 +++- .../Data/OdbcDataConnection.cs | 2 +- .../Data/OleDataConnection.cs | 8 +++- .../Data/SqlDataConnection.cs | 6 ++- .../Data/TestDataConnection.cs | 2 +- .../Data/TestDataConnectionFactory.cs | 2 +- .../Data/TestDataConnectionSql.cs | 42 ++++++++++++++----- .../Data/XmlDataConnection.cs | 5 ++- .../MSTestAdapter.PlatformServices.csproj | 6 ++- .../PublicAPI/net6.0/PublicAPI.Unshipped.txt | 2 + .../PublicAPI/net7.0/PublicAPI.Unshipped.txt | 2 + .../PublicAPI/net8.0/PublicAPI.Unshipped.txt | 2 + .../Services/TestContextImplementation.cs | 10 ++--- .../Services/TestDataSource.cs | 22 ++++++++-- .../Utilities/RandomIntPermutation.cs | 2 +- .../Utilities/SequentialIntPermutation.cs | 2 +- .../Microsoft.Testing.Platform.MSBuild.csproj | 2 +- .../ConfigurationNames.cs | 2 +- .../DataSourceElement.cs | 10 +++-- .../DataSourceElementCollection.cs | 9 +++- .../TestConfiguration.cs | 9 +++- .../TestConfigurationSection.cs | 9 +++- .../PublicAPI/net6.0/PublicAPI.Unshipped.txt | 4 +- .../PublicAPI/net7.0/PublicAPI.Unshipped.txt | 4 +- .../PublicAPI/net8.0/PublicAPI.Unshipped.txt | 4 +- .../TestFramework.Extensions/TestContext.cs | 4 +- .../TestFramework.Extensions.csproj | 3 +- .../TestFramework/TestFramework.csproj | 2 +- .../TestProjectForDiscovery.csproj | 2 +- 33 files changed, 166 insertions(+), 50 deletions(-) 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.Samples MSTest.Extensibility.Samples - TRACE + $(DefineConstants);TRACE prompt 4 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.TestAdapter Microsoft.VisualStudio.TestPlatform.MSTest.TestAdapter - TRACE + $(DefineConstants);TRACE true 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.PlatformServices Microsoft.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? GetData(UTF.ITestMethod testMethodInfo, ITestContext testContext) #else +#if NET6_0_OR_GREATER && IS_DATA_SOURCE_SUPPORTED + [UnconditionalSuppressMessage("SingleFile", "IL3000: Avoid accessing Assembly file path when publishing as a single file", + Justification = "The code handles the Assembly.Location equals null")] +#endif IEnumerable? ITestDataSource.GetData(UTF.ITestMethod testMethodInfo, ITestContext testContext) #endif { -#if NETFRAMEWORK +#if IS_DATA_SOURCE_SUPPORTED // Figure out where (as well as the current directory) we could look for data files // for unit tests this means looking at the location of the test itself + string location = +#if NETFRAMEWORK + testMethodInfo.MethodInfo.Module.Assembly.CodeBase; +#else + // TODO: Should we use AppContext.BaseDirectory when Assembly.Location is null? + testMethodInfo.MethodInfo.Module.Assembly.Location ?? throw new NotSupportedException("DataSourceAttribute is currently not supported when publishing as single file."); +#endif List dataFolders = [ - Path.GetDirectoryName(new Uri(testMethodInfo.MethodInfo.Module.Assembly.CodeBase).LocalPath), + Path.GetDirectoryName(new Uri(location).LocalPath), ]; List dataRowResults = []; @@ -89,7 +103,7 @@ public class TestDataSource : ITestDataSource #endif } -#if NETFRAMEWORK +#if IS_DATA_SOURCE_SUPPORTED /// /// Get permutations for data row access. /// diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/RandomIntPermutation.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/RandomIntPermutation.cs index 668da63a8a..665e58201b 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/RandomIntPermutation.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/RandomIntPermutation.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; diff --git a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/SequentialIntPermutation.cs b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/SequentialIntPermutation.cs index c77c8fe83d..e700c906cd 100644 --- a/src/Adapter/MSTestAdapter.PlatformServices/Utilities/SequentialIntPermutation.cs +++ b/src/Adapter/MSTestAdapter.PlatformServices/Utilities/SequentialIntPermutation.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; diff --git a/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj b/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj index 3af417a7eb..cb2f33b3b0 100644 --- a/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj +++ b/src/Platform/Microsoft.Testing.Platform.MSBuild/Microsoft.Testing.Platform.MSBuild.csproj @@ -1,7 +1,7 @@ $(MicrosoftTestingTargetFrameworks);netstandard2.0 - PLATFORM_MSBUILD + $(DefineConstants);PLATFORM_MSBUILD $(NoWarn);NU5100 diff --git a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/ConfigurationNames.cs b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/ConfigurationNames.cs index 7350f0802f..7999863010 100644 --- a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/ConfigurationNames.cs +++ b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/ConfigurationNames.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 namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// diff --git a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElement.cs b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElement.cs index 1d630e02b8..1ead013963 100644 --- a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElement.cs +++ b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElement.cs @@ -1,8 +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.Configuration; namespace Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,7 +9,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// /// The Data Source element. /// -public sealed class DataSourceElement : ConfigurationElement +#if NETFRAMEWORK +public +#else +internal +#endif +sealed class DataSourceElement : ConfigurationElement { private static readonly ConfigurationProperty NameValue = new(ConfigurationNames.NameAttributeName, typeof(string), string.Empty, ConfigurationPropertyOptions.IsKey | ConfigurationPropertyOptions.IsRequired); private static readonly ConfigurationProperty ConnectionStringValue = new(ConfigurationNames.ConnectionStringAttributeName, typeof(string), string.Empty, ConfigurationPropertyOptions.IsRequired); diff --git a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElementCollection.cs b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElementCollection.cs index b2bd44da6f..77ba4a58b0 100644 --- a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElementCollection.cs +++ b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/DataSourceElementCollection.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.Configuration; using System.Diagnostics.CodeAnalysis; @@ -12,7 +12,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// The Data source element collection. /// [SuppressMessage("Microsoft.Design", "CA1010:CollectionsShouldImplementGenericInterface", Justification = "Compat")] -public sealed class DataSourceElementCollection : ConfigurationElementCollection +#if NETFRAMEWORK +public +#else +internal +#endif +sealed class DataSourceElementCollection : ConfigurationElementCollection { /// /// Initializes a new instance of the class. diff --git a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfiguration.cs b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfiguration.cs index cadeb8f74b..dc33c2ca26 100644 --- a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfiguration.cs +++ b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfiguration.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.Configuration; namespace Microsoft.VisualStudio.TestTools.UnitTesting; @@ -9,7 +9,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// /// Support for configuration settings for Tests. /// -public static class TestConfiguration +#if NETFRAMEWORK +public +#else +internal +#endif +static class TestConfiguration { /// /// Gets the configuration section for tests. diff --git a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfigurationSection.cs b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfigurationSection.cs index 91fde548cd..0e6eb7ad12 100644 --- a/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfigurationSection.cs +++ b/src/TestFramework/TestFramework.Extensions/ConfigurationSettings/TestConfigurationSection.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.Configuration; @@ -10,7 +10,12 @@ namespace Microsoft.VisualStudio.TestTools.UnitTesting; /// /// The configuration section for tests. /// -public sealed class TestConfigurationSection : ConfigurationSection +#if NETFRAMEWORK +public +#else +internal +#endif +sealed class TestConfigurationSection : ConfigurationSection { private static readonly ConfigurationProperty DataSourcesValue = new(ConfigurationNames.DataSourcesSectionName, typeof(DataSourceElementCollection), null); diff --git a/src/TestFramework/TestFramework.Extensions/PublicAPI/net6.0/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework.Extensions/PublicAPI/net6.0/PublicAPI.Unshipped.txt index 815c92006a..0c1292171f 100644 --- a/src/TestFramework/TestFramework.Extensions/PublicAPI/net6.0/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework.Extensions/PublicAPI/net6.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ -#nullable enable \ No newline at end of file +#nullable enable +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataConnection.get -> System.Data.Common.DbConnection? +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataRow.get -> System.Data.DataRow? \ No newline at end of file diff --git a/src/TestFramework/TestFramework.Extensions/PublicAPI/net7.0/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework.Extensions/PublicAPI/net7.0/PublicAPI.Unshipped.txt index 815c92006a..0c1292171f 100644 --- a/src/TestFramework/TestFramework.Extensions/PublicAPI/net7.0/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework.Extensions/PublicAPI/net7.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ -#nullable enable \ No newline at end of file +#nullable enable +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataConnection.get -> System.Data.Common.DbConnection? +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataRow.get -> System.Data.DataRow? \ No newline at end of file diff --git a/src/TestFramework/TestFramework.Extensions/PublicAPI/net8.0/PublicAPI.Unshipped.txt b/src/TestFramework/TestFramework.Extensions/PublicAPI/net8.0/PublicAPI.Unshipped.txt index 815c92006a..0c1292171f 100644 --- a/src/TestFramework/TestFramework.Extensions/PublicAPI/net8.0/PublicAPI.Unshipped.txt +++ b/src/TestFramework/TestFramework.Extensions/PublicAPI/net8.0/PublicAPI.Unshipped.txt @@ -1 +1,3 @@ -#nullable enable \ No newline at end of file +#nullable enable +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataConnection.get -> System.Data.Common.DbConnection? +abstract Microsoft.VisualStudio.TestTools.UnitTesting.TestContext.DataRow.get -> System.Data.DataRow? \ No newline at end of file diff --git a/src/TestFramework/TestFramework.Extensions/TestContext.cs b/src/TestFramework/TestFramework.Extensions/TestContext.cs index 95f08dcde9..3f3b0ff059 100644 --- a/src/TestFramework/TestFramework.Extensions/TestContext.cs +++ b/src/TestFramework/TestFramework.Extensions/TestContext.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 @@ -57,7 +57,7 @@ public abstract class TestContext public string? TestDisplayName { get; protected set; } -#if NETFRAMEWORK +#if IS_DATA_SOURCE_SUPPORTED /// /// Gets the current data row when test is used for data driven testing. /// diff --git a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj index 5dcc9df4a5..67f823017c 100644 --- a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj +++ b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj @@ -34,7 +34,7 @@ Microsoft.VisualStudio.TestTools.UnitTesting Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions - TRACE + $(DefineConstants);TRACE @@ -56,6 +56,7 @@ + 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.UnitTesting Microsoft.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);TRACE prompt 4 False From 9818ef6432e709f7ba208cda1c950cda9e1436d7 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 18 Nov 2024 15:54:29 +0100 Subject: [PATCH 2/5] Adjust test --- .../DataSourceTests.cs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs index 375dedad5f..e58ec01a54 100644 --- a/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs +++ b/test/IntegrationTests/MSTest.Acceptance.IntegrationTests/DataSourceTests.cs @@ -14,7 +14,7 @@ public class DataSourceTests : AcceptanceTestBase #file DataSourceTests.csproj - net472 + $TargetFramework$ enable Exe preview @@ -88,7 +88,8 @@ public void MyTest() public DataSourceTests(ITestExecutionContext testExecutionContext, AcceptanceFixture acceptanceFixture) : base(testExecutionContext) => _acceptanceFixture = acceptanceFixture; - public async Task TestDataSourceFromAppConfig() + [ArgumentsProvider(nameof(TargetFrameworks.All), typeof(TargetFrameworks))] + public async Task TestDataSourceFromAppConfig(string tfm) { if (!OperatingSystem.IsWindows()) { @@ -100,7 +101,8 @@ public async Task TestDataSourceFromAppConfig() "DataSourceTests", SourceCode .PatchCodeWithReplace("$MSTestVersion$", MSTestVersion) - .PatchCodeWithReplace("$MicrosoftNETTestSdkVersion$", MicrosoftNETTestSdkVersion), + .PatchCodeWithReplace("$MicrosoftNETTestSdkVersion$", MicrosoftNETTestSdkVersion) + .PatchCodeWithReplace("$TargetFramework$", tfm), addPublicFeeds: true); await DotnetCli.RunAsync( @@ -108,7 +110,7 @@ await DotnetCli.RunAsync( _acceptanceFixture.NuGetGlobalPackagesFolder.Path, retryCount: 0); - var testHost = TestHost.LocateFrom(generator.TargetAssetPath, "DataSourceTests", "net472"); + var testHost = TestHost.LocateFrom(generator.TargetAssetPath, "DataSourceTests", tfm); TestHostResult result = await testHost.ExecuteAsync(); result.AssertExitCodeIs(ExitCodes.AtLeastOneTestFailed); From 9757e1d19af0de5030e04fa5745aa49a3fe3d0a9 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 18 Nov 2024 16:05:30 +0100 Subject: [PATCH 3/5] Adjust packaging for System.Configuration.ConfigurationManager dependency --- Directory.Packages.props | 3 ++- .../MSTest.TestFramework.Linux.nuspec | 12 +++++++++--- .../MSTest.TestFramework.nuspec | 12 +++++++++--- .../TestFramework.Extensions.csproj | 1 + 4 files changed, 21 insertions(+), 7 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index fae29b7552..11252777b3 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -4,6 +4,7 @@ true + 8.0.0 8.2.1 17.11.4 3.11.0-beta1.24508.2 @@ -30,7 +31,7 @@ - + diff --git a/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.Linux.nuspec b/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.Linux.nuspec index cda91f2b76..e580727562 100644 --- a/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.Linux.nuspec +++ b/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.Linux.nuspec @@ -5,9 +5,15 @@ - - - + + + + + + + + + PACKAGE.md diff --git a/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.nuspec b/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.nuspec index 3c7f9c9f87..0c7688e05a 100644 --- a/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.nuspec +++ b/src/TestFramework/TestFramework.Extensions/MSTest.TestFramework.nuspec @@ -7,9 +7,15 @@ - - - + + + + + + + + + PACKAGE.md diff --git a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj index 67f823017c..03bb751993 100644 --- a/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj +++ b/src/TestFramework/TestFramework.Extensions/TestFramework.Extensions.csproj @@ -76,6 +76,7 @@ + From 8b96b5ae96e63b8b33dba8ed1ccd288c44f0946a Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 18 Nov 2024 16:25:46 +0100 Subject: [PATCH 4/5] Adjust packaging for PlatformServices dependencies --- Directory.Packages.props | 9 ++++++--- .../MSTest.TestAdapter/MSTest.TestAdapter.Linux.nuspec | 9 +++++++++ src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj | 3 +++ src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec | 9 +++++++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 11252777b3..097f6cf4de 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -5,6 +5,8 @@ 8.0.0 + 8.0.0 + 8.0.0 8.2.1 17.11.4 3.11.0-beta1.24508.2 @@ -12,6 +14,7 @@ 4.8.0 $(MicrosoftCodeAnalysisAnalyzersVersion) $(MicrosoftCodeAnalysisPublicApiAnalyzersVersion) + 5.1.5 6.2.14 @@ -32,8 +35,8 @@ - - + + @@ -41,7 +44,7 @@ - + diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.Linux.nuspec b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.Linux.nuspec index 661698c9e9..ad78232203 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.Linux.nuspec +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.Linux.nuspec @@ -12,14 +12,23 @@ + + + + + + + + + diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj index ecf487a7c2..75704d0ff9 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.csproj @@ -80,6 +80,9 @@ + + + diff --git a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec index b9842b3993..fb12f8b457 100644 --- a/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec +++ b/src/Adapter/MSTest.TestAdapter/MSTest.TestAdapter.nuspec @@ -17,14 +17,23 @@ + + + + + + + + + From ec29fced5ae13fb9ea782531ae946974b0d78c74 Mon Sep 17 00:00:00 2001 From: Youssef1313 Date: Mon, 18 Nov 2024 16:44:01 +0100 Subject: [PATCH 5/5] Update version --- Directory.Packages.props | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Directory.Packages.props b/Directory.Packages.props index 097f6cf4de..4fe61e7847 100644 --- a/Directory.Packages.props +++ b/Directory.Packages.props @@ -14,7 +14,7 @@ 4.8.0 $(MicrosoftCodeAnalysisAnalyzersVersion) $(MicrosoftCodeAnalysisPublicApiAnalyzersVersion) - 5.1.5 + 5.2.2 6.2.14