Skip to content

Commit 360833a

Browse files
committed
Fixed problems in TestData
1 parent e3d7dc0 commit 360833a

File tree

4 files changed

+59
-27
lines changed

4 files changed

+59
-27
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
This is the content of the dummy file

Test/UnitTests/TestSupport/TestFileData.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,18 @@ public void TestGetTestDataDummyFilePathSubDirectory()
7171
path.ShouldEndWith(@"SubDirWithOneFileInIt\One file.txt");
7272
}
7373

74+
[Fact]
75+
public void TestGetTestDataDummyFileAltTestDataDir()
76+
{
77+
//SETUP
78+
79+
//ATTEMPT
80+
var path = TestData.GetFilePath(@"\AltTestDataDir\Alt dummy file.txt");
81+
82+
//VERIFY
83+
path.ShouldEndWith(@"\AltTestDataDir\Alt dummy file.txt");
84+
}
85+
7486

7587
[Fact]
7688
public void TestGetTestDataAllFilesInDir()

TestSupport/Helpers/TestData.cs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using System;
55
using System.IO;
6+
using System.Linq;
67
using System.Reflection;
78
using System.Runtime.CompilerServices;
89

@@ -21,10 +22,12 @@ public static class TestData
2122
/// This returns the filepath of the file found by the searchPattern. If more than one file found that throws and exception
2223
/// </summary>
2324
/// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
25+
/// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
2426
/// <returns>The absolute filepath to the found file</returns>
25-
public static string GetFilePath(string searchPattern)
27+
public static string GetFilePath(string searchPattern, Assembly callingAssembly = null)
2628
{
27-
string[] fileList = GetFilePaths(searchPattern);
29+
callingAssembly = callingAssembly ?? Assembly.GetCallingAssembly();
30+
string[] fileList = GetFilePaths(searchPattern, callingAssembly);
2831

2932
if (fileList.Length != 1)
3033
throw new Exception(string.Format("GetTestDataFilePath: The searchString {0} found {1} file. Either not there or ambiguous",
@@ -36,17 +39,23 @@ public static string GetFilePath(string searchPattern)
3639
/// <summary>
3740
/// This returns all the filepaths of file that fit the search pattern
3841
/// </summary>
39-
/// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
42+
/// <param name="searchPattern">If the search pattern starts with a @"\", then it will look for an alternate testdata directory</param>
43+
/// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
4044
/// <returns>array of absolute filepaths that match the filepath</returns>
41-
public static string[] GetFilePaths(string searchPattern = "")
45+
public static string[] GetFilePaths(string searchPattern = "", Assembly callingAssembly = null)
4246
{
43-
var directory = GetTestDataDir();
44-
if (searchPattern.Contains(@"\"))
47+
callingAssembly = callingAssembly ?? Assembly.GetCallingAssembly();
48+
string testDir = null;
49+
if (searchPattern.Any() && searchPattern[0] == Path.DirectorySeparatorChar)
4550
{
46-
//Has subdirectory in search pattern, so change directory
47-
directory = Path.Combine(directory, searchPattern.Substring(0, searchPattern.LastIndexOf('\\')));
48-
searchPattern = searchPattern.Substring(searchPattern.LastIndexOf('\\') + 1);
51+
//has alternate test directory name, so set that and remove from search string
52+
var indexNextDirSep = searchPattern.Substring(1).IndexOf(Path.DirectorySeparatorChar);
53+
if (indexNextDirSep < 0 )
54+
throw new InvalidOperationException(@"Your search pattern started with an alternative directory, but did have a closing directory separator.");
55+
testDir = searchPattern.Substring(1, indexNextDirSep);
56+
searchPattern = searchPattern.Substring(indexNextDirSep + 2);
4957
}
58+
var directory = GetTestDataDir(testDir, callingAssembly);
5059

5160
string[] fileList = Directory.GetFiles(directory, searchPattern);
5261

@@ -57,21 +66,25 @@ public static string[] GetFilePaths(string searchPattern = "")
5766
/// This returns the content of the file found by the searchPattern. If more than one file found that throws and exception
5867
/// </summary>
5968
/// <param name="searchPattern">If the search pattern starts with a @"\", then it will look in a subdirectory for the file</param>
69+
/// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
6070
/// <returns>The content of the file as text of the found file</returns>
61-
public static string GetFileContent(string searchPattern)
71+
public static string GetFileContent(string searchPattern, Assembly callingAssembly = null)
6272
{
63-
var filePath = GetFilePath(searchPattern);
73+
callingAssembly = callingAssembly ?? Assembly.GetCallingAssembly();
74+
var filePath = GetFilePath(searchPattern, callingAssembly);
6475
return File.ReadAllText(filePath);
6576
}
6677

6778
/// <summary>
6879
/// This will ensure that a file in the TestData directory is deleted
6980
/// </summary>
7081
/// <param name="searchPattern"></param>
82+
/// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
7183
/// <returns></returns>
72-
public static bool EnsureFileDeleted(string searchPattern)
84+
public static bool EnsureFileDeleted(string searchPattern, Assembly callingAssembly = null)
7385
{
74-
var fileList = GetFilePaths(searchPattern);
86+
callingAssembly = callingAssembly ?? Assembly.GetCallingAssembly();
87+
var fileList = GetFilePaths(searchPattern, callingAssembly);
7588
if (fileList.Length == 0) return false;
7689
if (fileList.Length != 1)
7790
throw new Exception(string.Format("TestFileDeleteIfPresent: The searchString {0} found {1} files!",
@@ -119,13 +132,14 @@ public static void DeleteAllFilesAndSubDirsInDir(string topDirPath)
119132
/// <param name="callingAssembly">optional: provide the calling assembly. default is to use the current calling assembly</param>
120133
/// <returns></returns>
121134
[MethodImpl(MethodImplOptions.NoInlining)]
122-
public static string GetTestDataDir(string alternateTestDir = TestFileDirectoryName, Assembly callingAssembly = null)
135+
public static string GetTestDataDir(string alternateTestDir = null, Assembly callingAssembly = null)
123136
{
137+
alternateTestDir = alternateTestDir ?? TestFileDirectoryName;
124138
//see https://stackoverflow.com/questions/670566/path-combine-absolute-with-relative-path-strings
125139
return Path.Combine(
126140
Path.GetFullPath(
127141
GetCallingAssemblyTopLevelDir(callingAssembly ?? Assembly.GetCallingAssembly())
128-
+ "\\" + alternateTestDir));
142+
+ Path.DirectorySeparatorChar + alternateTestDir));
129143
}
130144

131145
/// <summary>
@@ -136,7 +150,7 @@ public static string GetTestDataDir(string alternateTestDir = TestFileDirectoryN
136150
[MethodImpl(MethodImplOptions.NoInlining)] //see https://docs.microsoft.com/en-gb/dotnet/api/system.reflection.assembly.getcallingassembly?view=netstandard-2.0#System_Reflection_Assembly_GetCallingAssembly
137151
public static string GetCallingAssemblyTopLevelDir(Assembly callingAssembly = null)
138152
{
139-
const string binDir = @"\bin\";
153+
var binDir = $"{Path.DirectorySeparatorChar}bin{Path.DirectorySeparatorChar}";
140154
var pathToManipulate = (callingAssembly ?? Assembly.GetCallingAssembly()).Location;
141155

142156
var indexOfPart = pathToManipulate.IndexOf(binDir, StringComparison.OrdinalIgnoreCase);

TestSupport/TestSupport.csproj

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,41 @@
99
</PropertyGroup>
1010

1111
<ItemGroup>
12-
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.0.0" />
13-
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.0.0" />
14-
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.0.0" />
15-
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.0.0" />
12+
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="2.*" />
13+
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="2.*" />
14+
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="2.*" />
15+
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="2.*" />
1616
<PackageReference Include="Microsoft.Extensions.Configuration" Version="2.0.0" />
1717
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="2.0.0" />
18-
<PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
19-
<PackageReference Include="xunit" Version="2.2.0" />
18+
<PackageReference Include="System.Data.SqlClient" Version="4.4.2" />
19+
<PackageReference Include="xunit.assert" Version="2.3.1" />
20+
<PackageReference Include="xunit.core" Version="2.3.1" />
2021
</ItemGroup>
2122

2223
<PropertyGroup>
2324
<TargetFramework>netstandard2.0</TargetFramework>
2425
<PackageId>EfCore.TestSupport</PackageId>
25-
<PackageVersion>1.2.0</PackageVersion>
26+
<PackageVersion>1.3.0</PackageVersion>
2627
<Authors>Jon P Smith</Authors>
2728
<Description>Useful tools when unit testing applications that use Entity Framework Core. See readme file on github.</Description>
2829
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
2930
<PackageReleaseNotes>
30-
- BREAKING CHANGE: The SqlServerHelpers, SqliteInMemory, and EfInMemory option setting extensions now default to throwing an exception if a Microsoft.EntityFrameworkCore.Query.QueryClientEvaluationWarning is logged
31+
- Feature: Allows any 2.* version of EF Core
32+
- Feature: Only loads the required parts of the xUnit libraries
33+
- Bug fix: Fixed problem with using TestData methods on non-windows systems (Path.DirectorySeparatorChar)
34+
- Bug fix: TestData didn't find the correct directory
3135
</PackageReleaseNotes>
3236
<Copyright>Copyright (c) 2017 Jon P Smith. Licenced under MIT licence</Copyright>
3337
<PackageTags>Entity Framework Core, xUnit</PackageTags>
38+
<IsPackable>true</IsPackable>
3439
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
3540
<PackageProjectUrl>https://github.com/JonPSmith/EfCore.TestSupport</PackageProjectUrl>
3641
<PackageIconUrl>https://raw.githubusercontent.com/JonPSmith/EfCore.TestSupport/master/EfCoreTestSupportNuGetIcon128.png</PackageIconUrl>
3742
<RepositoryUrl></RepositoryUrl>
3843
<PackageLicenseUrl>https://github.com/JonPSmith/EfCore.TestSupport/blob/master/LICENSE</PackageLicenseUrl>
39-
<Version>1.2.0</Version>
40-
<AssemblyVersion>1.2.0.0</AssemblyVersion>
41-
<FileVersion>1.2.0.0</FileVersion>
44+
<Version>1.3.0</Version>
45+
<AssemblyVersion>1.3.0.0</AssemblyVersion>
46+
<FileVersion>1.3.0.0</FileVersion>
4247
</PropertyGroup>
4348

4449
</Project>

0 commit comments

Comments
 (0)