Skip to content

Commit eab2957

Browse files
authored
Make DerivePathInfo specific to test framework (VerifyTests#646)
1 parent 8e55db0 commit eab2957

File tree

20 files changed

+196
-35
lines changed

20 files changed

+196
-35
lines changed

docs/build-server.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ For example a possible implementation for [AppVeyor](https://www.appveyor.com/)
9393
if (BuildServerDetector.Detected)
9494
{
9595
var buildDirectory = Environment.GetEnvironmentVariable("APPVEYOR_BUILD_FOLDER")!;
96-
VerifierSettings.DerivePathInfo(
96+
Verifier.DerivePathInfo(
9797
(sourceFile, projectDirectory, typeName, methodName) =>
9898
{
9999
var testDirectory = Path.GetDirectoryName(sourceFile)!;

docs/naming.md

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -551,11 +551,11 @@ For example to place all `.verified.` files in a `{ProjectDirectory}\Snapshots`
551551
<!-- snippet: DerivePathInfo -->
552552
<a id='snippet-derivepathinfo'></a>
553553
```cs
554-
VerifierSettings.DerivePathInfo(
555-
(sourceFile, projectDirectory, typeName, methodName) => new(
554+
Verifier.DerivePathInfo(
555+
(sourceFile, projectDirectory, type, method) => new(
556556
directory: Path.Combine(projectDirectory, "Snapshots"),
557-
typeName: typeName,
558-
methodName: methodName));
557+
typeName: type.Name,
558+
methodName: method.Name));
559559
```
560560
<sup><a href='/src/Verify.Tests/Snippets/Snippets.cs#L63-L71' title='Snippet source file'>snippet source</a> | <a href='#snippet-derivepathinfo' title='Start of snippet'>anchor</a></sup>
561561
<!-- endSnippet -->
@@ -578,9 +578,36 @@ static DerivePathInfo derivePathInfo = (sourceFile, projectDirectory, type, meth
578578
typeName: type,
579579
methodName: method);
580580
```
581-
<sup><a href='/src/Verify/DerivePaths/VerifierSettings.cs#L7-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo' title='Start of snippet'>anchor</a></sup>
581+
<sup><a href='/src/Verify.Expecto/DerivePaths/VerifierSettings.cs#L7-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo' title='Start of snippet'>anchor</a></sup>
582582
<a id='snippet-defaultderivepathinfo-1'></a>
583583
```cs
584+
static DerivePathInfo derivePathInfo = (sourceFile, projectDirectory, type, method) =>
585+
new(
586+
directory: Path.GetDirectoryName(sourceFile)!,
587+
typeName: type.NameWithParent(),
588+
methodName: method.Name);
589+
```
590+
<sup><a href='/src/Verify.MSTest/DerivePaths/VerifierSettings.cs#L7-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-1' title='Start of snippet'>anchor</a></sup>
591+
<a id='snippet-defaultderivepathinfo-2'></a>
592+
```cs
593+
static DerivePathInfo derivePathInfo = (sourceFile, projectDirectory, type, method) =>
594+
new(
595+
directory: Path.GetDirectoryName(sourceFile)!,
596+
typeName: type.NameWithParent(),
597+
methodName: method.Name);
598+
```
599+
<sup><a href='/src/Verify.NUnit/DerivePaths/VerifierSettings.cs#L7-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-2' title='Start of snippet'>anchor</a></sup>
600+
<a id='snippet-defaultderivepathinfo-3'></a>
601+
```cs
602+
static DerivePathInfo derivePathInfo = (sourceFile, projectDirectory, type, method) =>
603+
new(
604+
directory: Path.GetDirectoryName(sourceFile)!,
605+
typeName: type.NameWithParent(),
606+
methodName: method.Name);
607+
```
608+
<sup><a href='/src/Verify.Xunit/DerivePaths/VerifierSettings.cs#L7-L15' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-3' title='Start of snippet'>anchor</a></sup>
609+
<a id='snippet-defaultderivepathinfo-4'></a>
610+
```cs
584611
public static string NameWithParent(this Type type)
585612
{
586613
if (type.IsNested)
@@ -591,7 +618,7 @@ public static string NameWithParent(this Type type)
591618
return type.Name;
592619
}
593620
```
594-
<sup><a href='/src/Verify/Extensions.cs#L27-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-1' title='Start of snippet'>anchor</a></sup>
621+
<sup><a href='/src/Verify/Extensions.cs#L27-L39' title='Snippet source file'>snippet source</a> | <a href='#snippet-defaultderivepathinfo-4' title='Start of snippet'>anchor</a></sup>
595622
<!-- endSnippet -->
596623

597624

src/StrictJsonTests/ModuleInit.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@ public static void Init()
88
VerifierSettings.UseStrictJson();
99

1010
#endregion
11-
VerifierSettings.DerivePathInfo(
12-
(_, _, typeName, methodName) => new(AttributeReader.GetProjectDirectory(), typeName: typeName, methodName));
11+
DerivePathInfo(
12+
(_, _, type, method) => new(AttributeReader.GetProjectDirectory(), typeName: type.Name, method.Name));
1313
}
14-
1514
}

src/Verify.DerivePaths.Tests/Tests.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ public class Tests
44
[Fact]
55
public Task Test()
66
{
7-
VerifierSettings.DerivePathInfo(
7+
DerivePathInfo(
88
(sourceFile, projectDirectory, methodName, typeName) =>
99
{
1010
Assert.True(File.Exists(sourceFile));
@@ -21,28 +21,28 @@ public Task Test()
2121
[Fact]
2222
public Task ReturnNulls()
2323
{
24-
VerifierSettings.DerivePathInfo((_, _, _, _) => new(null));
24+
DerivePathInfo((_, _, _, _) => new(null));
2525
return Verify("Value");
2626
}
2727

2828
[Fact]
2929
public Task InvalidMethod()
3030
{
31-
VerifierSettings.DerivePathInfo((_, _, _, _) => new(null, null, Path.GetInvalidFileNameChars().First().ToString()));
31+
DerivePathInfo((_, _, _, _) => new(null, null, Path.GetInvalidFileNameChars().First().ToString()));
3232
return Assert.ThrowsAsync<ArgumentException>(() => Verify("Value"));
3333
}
3434

3535
[Fact]
3636
public Task InvalidType()
3737
{
38-
VerifierSettings.DerivePathInfo((_, _, _, _) => new(null, Path.GetInvalidFileNameChars().First().ToString()));
38+
DerivePathInfo((_, _, _, _) => new(null, Path.GetInvalidFileNameChars().First().ToString()));
3939
return Assert.ThrowsAsync<ArgumentException>(() => Verify("Value"));
4040
}
4141

4242
[Fact]
4343
public Task InvalidDirectory()
4444
{
45-
VerifierSettings.DerivePathInfo((_, _, _, _) => new(Path.GetInvalidPathChars().First().ToString()));
45+
DerivePathInfo((_, _, _, _) => new(Path.GetInvalidPathChars().First().ToString()));
4646
return Assert.ThrowsAsync<ArgumentException>(() => Verify("Value"));
4747
}
4848
}

src/Verify/DerivePaths/DerivePathInfo.cs renamed to src/Verify.Expecto/DerivePaths/DerivePathInfo.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace VerifyTests;
1+
namespace VerifyExpecto;
22

33
/// <summary>
44
/// Signature for deriving a custom path information for `.verified.` files.

src/Verify/DerivePaths/VerifierSettings.cs renamed to src/Verify.Expecto/DerivePaths/VerifierSettings.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// ReSharper disable UnusedParameter.Local
22

3-
namespace VerifyTests;
3+
namespace VerifyExpecto;
44

5-
public static partial class VerifierSettings
5+
public partial class Verifier
66
{
77
#region defaultDerivePathInfo
88

@@ -26,5 +26,5 @@ internal static PathInfo GetPathInfo(string sourceFile, string typeName, string
2626
/// </remarks>
2727
/// <param name="derivePathInfo">Custom callback to control the behavior.</param>
2828
public static void DerivePathInfo(DerivePathInfo derivePathInfo) =>
29-
VerifierSettings.derivePathInfo = derivePathInfo;
29+
Verifier.derivePathInfo = derivePathInfo;
3030
}

src/Verify.Expecto/Verifier.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ static InnerVerifier GetVerifier(VerifySettings settings, string sourceFile, str
1616

1717
var fileName = Path.GetFileNameWithoutExtension(sourceFile);
1818

19-
return new(sourceFile, settings, fileName, methodName, new());
19+
var pathInfo = GetPathInfo(sourceFile, fileName, methodName);
20+
return new(sourceFile, settings, fileName, methodName, new(), pathInfo);
2021
}
2122

2223
[DoesNotReturn]
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace VerifyMSTest;
2+
3+
/// <summary>
4+
/// Signature for deriving a custom path information for `.verified.` files.
5+
/// </summary>
6+
/// <param name="sourceFile">The source file derived from <see cref="CallerFilePathAttribute" />.</param>
7+
/// <param name="projectDirectory">The directory of the project that the test was compile from.</param>
8+
/// <param name="type">The class the test method exists in.</param>
9+
/// <param name="method">The test method.</param>
10+
public delegate PathInfo DerivePathInfo(string sourceFile, string projectDirectory, Type type, MethodInfo method);
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ReSharper disable UnusedParameter.Local
2+
3+
namespace VerifyMSTest;
4+
5+
public partial class VerifyBase
6+
{
7+
#region defaultDerivePathInfo
8+
9+
static DerivePathInfo derivePathInfo = (sourceFile, projectDirectory, type, method) =>
10+
new(
11+
directory: Path.GetDirectoryName(sourceFile)!,
12+
typeName: type.NameWithParent(),
13+
methodName: method.Name);
14+
15+
#endregion
16+
17+
internal static PathInfo GetPathInfo(string sourceFile, Type type, MethodInfo method) =>
18+
derivePathInfo(sourceFile, TargetAssembly.ProjectDir, type, method);
19+
20+
/// <summary>
21+
/// Use custom path information for `.verified.` files.
22+
/// </summary>
23+
/// <remarks>
24+
/// This is sometimes needed on CI systems that move/remove the original source.
25+
/// To move to this approach, any existing `.verified.` files will need to be moved to the new directory
26+
/// </remarks>
27+
/// <param name="derivePathInfo">Custom callback to control the behavior.</param>
28+
public static void DerivePathInfo(DerivePathInfo derivePathInfo) =>
29+
VerifyBase.derivePathInfo = derivePathInfo;
30+
}

src/Verify.MSTest/VerifyBase.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,14 @@ InnerVerifier BuildVerifier(VerifySettings settings, string sourceFile)
3232
throw new($"Could not find method `{type.Name}.{testName}`.");
3333
}
3434

35+
var pathInfo = GetPathInfo(sourceFile, type, method);
3536
return new(
3637
sourceFile,
3738
settings,
3839
type.NameWithParent(),
3940
method.Name,
40-
method.ParameterNames());
41+
method.ParameterNames(),
42+
pathInfo);
4143
}
4244

4345
public SettingsTask Verify(

0 commit comments

Comments
 (0)