-
Notifications
You must be signed in to change notification settings - Fork 258
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
DataSource draft #4112
Draft
Youssef1313
wants to merge
11
commits into
microsoft:main
Choose a base branch
from
Youssef1313:datasource-new-packages
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
DataSource draft #4112
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ccb4a69
Implement CsvDataSourceAttribute
Youssef1313 70af843
Update verify-nupkgs for the new package
Youssef1313 d7cf953
Add TODO for moving away from OleDb and add test for the new attribute
Youssef1313 1b1d67d
Restrict unless it's clear we want otherwise :)
Youssef1313 8260bd6
TODO
Youssef1313 8f73a4a
Implement XmlDataSourceAttribute
Youssef1313 772c11d
XmlDataSource test
Youssef1313 cfdc63f
Update verify-nupkgs
Youssef1313 0cc1a4b
Fix xml test
Youssef1313 2b11863
Move things around
Youssef1313 9006f21
Address feedback
Youssef1313 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/TestFramework/TestFramework.Extensions.Csv/CsvDataSourceAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Data; | ||
using System.Data.OleDb; | ||
using System.Globalization; | ||
using System.Reflection; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting.Internal; | ||
|
||
namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// Attribute to define dynamic data from a CSV file for a test method. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public sealed class CsvDataSourceAttribute : Attribute, ITestDataSource | ||
{ | ||
// Template used to map from a filename to a DB connection string | ||
private const string CsvConnectionTemplate = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0};Persist Security Info=False;Extended Properties=\"text;HDR=YES;FMT=Delimited\""; | ||
private const string CsvConnectionTemplate64 = "Provider=Microsoft.Ace.OLEDB.12.0;Data Source={0};Persist Security Info=False;Extended Properties=\"text;HDR=YES;FMT=Delimited\""; | ||
|
||
public CsvDataSourceAttribute(string fileName) | ||
=> FileName = fileName; | ||
|
||
internal string FileName { get; } | ||
|
||
IEnumerable<object?[]> ITestDataSource.GetData(MethodInfo methodInfo) | ||
{ | ||
// TODO: Avoid using OleDb and instead parse Csv directly. Maybe use https://www.nuget.org/packages/CsvHelper? Write our own? | ||
// When that happens, the OleDb dependency in the csproj should be | ||
// removed, and relevant tests for CsvDataSource should run on Linux. | ||
|
||
// We better work with a full path, if nothing else, errors become easier to report | ||
string fullPath = Path.GetFullPath(FileName); | ||
if (!File.Exists(fullPath)) | ||
{ | ||
// TODO: Localize. | ||
throw new FileNotFoundException($"Csv file '{fullPath}' cannot be found.", fullPath); | ||
} | ||
|
||
using OleDbConnection connection = new(); | ||
|
||
// We have to use the name of the folder which contains the CSV file in the connection string | ||
// If target platform is x64, then use CsvConnectionTemplate64 connection string. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. will this work also on arm64? |
||
connection.ConnectionString = IntPtr.Size == 8 | ||
? string.Format(CultureInfo.InvariantCulture, CsvConnectionTemplate64, Path.GetDirectoryName(fullPath)) | ||
: string.Format(CultureInfo.InvariantCulture, CsvConnectionTemplate, Path.GetDirectoryName(fullPath)); | ||
|
||
// We have to open the connection now, before we try to quote | ||
// the table name, otherwise QuoteIdentifier fails (for OleDb, go figure!) | ||
// The connection will get closed when we dispose of it | ||
connection.Open(); | ||
|
||
using OleDbCommandBuilder commandBuilder = new(); | ||
string tableName = Path.GetFileName(fullPath).Replace('.', '#'); | ||
string quotedTableName = commandBuilder.QuoteIdentifier(tableName, connection); | ||
|
||
using OleDbCommand command = new() | ||
{ | ||
Connection = connection, | ||
CommandText = $"SELECT * FROM {quotedTableName}", | ||
}; | ||
|
||
using OleDbDataAdapter dataAdapter = new() | ||
{ | ||
SelectCommand = command, | ||
}; | ||
|
||
DataTable table = new() | ||
{ | ||
Locale = CultureInfo.InvariantCulture, | ||
}; | ||
|
||
dataAdapter.Fill(table); | ||
|
||
object?[][] dataRows = new object?[table.Rows.Count][]; | ||
for (int i = 0; i < dataRows.Length; i++) | ||
{ | ||
dataRows[i] = [table.Rows[i]]; | ||
} | ||
|
||
return dataRows; | ||
} | ||
|
||
string? ITestDataSource.GetDisplayName(MethodInfo methodInfo, object?[]? data) | ||
Youssef1313 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
=> TestDataSourceUtilities.ComputeDefaultDisplayName(methodInfo, data, DynamicDataAttribute.TestIdGenerationStrategy); | ||
} |
39 changes: 39 additions & 0 deletions
39
src/TestFramework/TestFramework.Extensions.Csv/MSTest.DataSource.Csv.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<!-- TODO: (before merge) Is this hack relevant for a new project? --> | ||
<UseAssemblyVersion14>true</UseAssemblyVersion14> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<IsPackable>true</IsPackable> | ||
<PackageId>MSTest.DataSource.Csv</PackageId> | ||
<PackageTags>MSTest TestFramework Unittest MSTestV2 Microsoft Test Testing TDD Framework</PackageTags> | ||
<PackageDescription> | ||
MSTest is Microsoft supported Test Framework. | ||
|
||
This package includes the functionality needed for writing Csv-based data source unit tests. | ||
|
||
Supported platforms: | ||
- .NET Standard 2.0 | ||
</PackageDescription> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework.Extensions\TestFramework.Extensions.csproj" /> | ||
|
||
<PackageReference Include="System.Data.OleDb" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- API that is common to all frameworks that we build for. --> | ||
<AdditionalFiles Include="PublicAPI\PublicAPI.Shipped.txt" /> | ||
<AdditionalFiles Include="PublicAPI\PublicAPI.Unshipped.txt" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# MSTest.TestFramework | ||
|
||
MSTest is Microsoft supported Test Framework. | ||
|
||
This package includes the functionality needed for writing Csv-based data source unit tests. | ||
|
||
Supported platforms: | ||
|
||
- .NET Standard 2.0 |
1 change: 1 addition & 0 deletions
1
src/TestFramework/TestFramework.Extensions.Csv/PublicAPI/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
3 changes: 3 additions & 0 deletions
3
src/TestFramework/TestFramework.Extensions.Csv/PublicAPI/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#nullable enable | ||
Microsoft.VisualStudio.TestTools.UnitTesting.CsvDataSourceAttribute | ||
Microsoft.VisualStudio.TestTools.UnitTesting.CsvDataSourceAttribute.CsvDataSourceAttribute(string! fileName) -> void |
37 changes: 37 additions & 0 deletions
37
src/TestFramework/TestFramework.Extensions.Xml/MSTest.DataSource.Xml.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<!-- TODO: (before merge) Is this hack relevant for a new project? --> | ||
<UseAssemblyVersion14>true</UseAssemblyVersion14> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<IsPackable>true</IsPackable> | ||
<PackageId>MSTest.DataSource.Xml</PackageId> | ||
<PackageTags>MSTest TestFramework Unittest MSTestV2 Microsoft Test Testing TDD Framework</PackageTags> | ||
<PackageDescription> | ||
MSTest is Microsoft supported Test Framework. | ||
|
||
This package includes the functionality needed for writing Xml-based data source unit tests. | ||
|
||
Supported platforms: | ||
- .NET Standard 2.0 | ||
</PackageDescription> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<RootNamespace>Microsoft.VisualStudio.TestTools.UnitTesting</RootNamespace> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework.Extensions\TestFramework.Extensions.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<!-- API that is common to all frameworks that we build for. --> | ||
<AdditionalFiles Include="PublicAPI\PublicAPI.Shipped.txt" /> | ||
<AdditionalFiles Include="PublicAPI\PublicAPI.Unshipped.txt" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# MSTest.TestFramework | ||
|
||
MSTest is Microsoft supported Test Framework. | ||
|
||
This package includes the functionality needed for writing Csv-based data source unit tests. | ||
|
||
Supported platforms: | ||
|
||
- .NET Standard 2.0 |
1 change: 1 addition & 0 deletions
1
src/TestFramework/TestFramework.Extensions.Xml/PublicAPI/PublicAPI.Shipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
#nullable enable |
3 changes: 3 additions & 0 deletions
3
src/TestFramework/TestFramework.Extensions.Xml/PublicAPI/PublicAPI.Unshipped.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
#nullable enable | ||
Microsoft.VisualStudio.TestTools.UnitTesting.XmlDataSourceAttribute | ||
Microsoft.VisualStudio.TestTools.UnitTesting.XmlDataSourceAttribute.XmlDataSourceAttribute(string! fileName, string! tableName) -> void |
59 changes: 59 additions & 0 deletions
59
src/TestFramework/TestFramework.Extensions.Xml/XmlDataSourceAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Data; | ||
using System.Globalization; | ||
using System.Reflection; | ||
using System.Xml; | ||
|
||
using Microsoft.VisualStudio.TestTools.UnitTesting.Internal; | ||
|
||
namespace Microsoft.VisualStudio.TestTools.UnitTesting; | ||
|
||
/// <summary> | ||
/// Attribute to define dynamic data from an XML file for a test method. | ||
/// </summary> | ||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)] | ||
public sealed class XmlDataSourceAttribute : Attribute, ITestDataSource | ||
{ | ||
public XmlDataSourceAttribute(string fileName, string tableName) | ||
{ | ||
FileName = fileName; | ||
TableName = tableName; | ||
} | ||
|
||
internal string FileName { get; } | ||
|
||
internal string TableName { get; } | ||
|
||
IEnumerable<object?[]> ITestDataSource.GetData(MethodInfo methodInfo) | ||
{ | ||
string fullPath = Path.GetFullPath(FileName); | ||
if (!File.Exists(fullPath)) | ||
{ | ||
// TODO: Localize. | ||
throw new FileNotFoundException($"Xml file '{fullPath}' cannot be found.", fullPath); | ||
} | ||
|
||
DataSet dataSet = new() | ||
{ | ||
Locale = CultureInfo.CurrentCulture, | ||
}; | ||
|
||
// ReadXml should use the overload with XmlReader to avoid DTD processing | ||
dataSet.ReadXml(new XmlTextReader(fullPath)); | ||
|
||
DataTable table = dataSet.Tables[TableName]; | ||
|
||
object?[][] dataRows = new object?[table.Rows.Count][]; | ||
for (int i = 0; i < dataRows.Length; i++) | ||
{ | ||
dataRows[i] = [table.Rows[i]]; | ||
} | ||
|
||
return dataRows; | ||
} | ||
|
||
string? ITestDataSource.GetDisplayName(MethodInfo methodInfo, object?[]? data) | ||
=> TestDataSourceUtilities.ComputeDefaultDisplayName(methodInfo, data, DynamicDataAttribute.TestIdGenerationStrategy); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would definitely avoid writing our own. 😁