-
Notifications
You must be signed in to change notification settings - Fork 259
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
153e889
commit ccb4a69
Showing
7 changed files
with
147 additions
and
0 deletions.
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
83 changes: 83 additions & 0 deletions
83
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,83 @@ | ||
// 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; | ||
|
||
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) | ||
{ | ||
// We specifically use OleDb to read a CSV file... | ||
// 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); | ||
} | ||
|
||
string tableName = Path.GetFileName(fullPath).Replace('.', '#'); | ||
|
||
// We can map simplified CSVs to an OLEDB/Text connection, then proceed as normal | ||
using OleDbConnection connection = new(); | ||
using OleDbDataAdapter dataAdapter = new(); | ||
using OleDbCommandBuilder commandBuilder = new(); | ||
using OleDbCommand command = 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. | ||
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(); | ||
|
||
string quotedTableName = commandBuilder.QuoteIdentifier(tableName, connection); | ||
|
||
command.Connection = connection; | ||
|
||
command.CommandText = $"SELECT * FROM {quotedTableName}"; | ||
|
||
dataAdapter.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) | ||
// TODO | ||
=> null; | ||
} |
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 |
42 changes: 42 additions & 0 deletions
42
src/TestFramework/TestFramework.Extensions.Csv/TestFramework.Extensions.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,42 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>netstandard2.0</TargetFramework> | ||
<UseAssemblyVersion14>true</UseAssemblyVersion14> | ||
</PropertyGroup> | ||
|
||
<PropertyGroup> | ||
<IsPackable>true</IsPackable> | ||
<PackageId>MSTest.TestFramework.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> | ||
<!-- TODO: Discuss the assembly name before shipping/merging --> | ||
<AssemblyName>Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions.Csv</AssemblyName> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="$(RepoRoot)src\TestFramework\TestFramework.Extensions\TestFramework.Extensions.csproj" /> | ||
|
||
<!--<PackageReference Include="System.Data.Odbc" />--> | ||
<PackageReference Include="System.Data.OleDb" /> | ||
<!--<PackageReference Include="Microsoft.Data.SqlClient" />--> | ||
</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> |