From 7174979b70e12caa3c08fea87a63dc6e68505be6 Mon Sep 17 00:00:00 2001 From: Simon Cropp Date: Fri, 30 Aug 2024 22:53:16 +1000 Subject: [PATCH] expose ProjectName and SolutionName in AttributeReader (#1274) --- src/Directory.Build.props | 2 +- .../AttributeReaderTests.Simple.verified.txt | 6 +++- src/Verify.Tests/AttributeReaderTests.cs | 6 +++- src/Verify/DerivePaths/AttributeReader.cs | 30 +++++++++++++++++++ src/Verify/buildTransitive/Verify.props | 11 ++++++- 5 files changed, 51 insertions(+), 4 deletions(-) diff --git a/src/Directory.Build.props b/src/Directory.Build.props index 9a2060037b..9eb5e3d717 100644 --- a/src/Directory.Build.props +++ b/src/Directory.Build.props @@ -2,7 +2,7 @@ CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget - 26.2.0 + 26.3.0 enable preview 1.0.0 diff --git a/src/Verify.Tests/AttributeReaderTests.Simple.verified.txt b/src/Verify.Tests/AttributeReaderTests.Simple.verified.txt index 2bcd474dfe..c5fd9827e0 100644 --- a/src/Verify.Tests/AttributeReaderTests.Simple.verified.txt +++ b/src/Verify.Tests/AttributeReaderTests.Simple.verified.txt @@ -3,6 +3,10 @@ TargetFrameworksInferred: net9.0;net8.0;net48, ProjectDirectory: {ProjectDirectory}, ProjectDirectoryInferred: {ProjectDirectory}, + ProjectName: Verify.Tests, + ProjectNameInferred: Verify.Tests, SolutionDirectory: {SolutionDirectory}, - SolutionDirectoryInferred: {SolutionDirectory} + SolutionDirectoryInferred: {SolutionDirectory}, + SolutionName: Verify, + SolutionNameInferred: Verify } \ No newline at end of file diff --git a/src/Verify.Tests/AttributeReaderTests.cs b/src/Verify.Tests/AttributeReaderTests.cs index b07e932a0d..0354d4741a 100644 --- a/src/Verify.Tests/AttributeReaderTests.cs +++ b/src/Verify.Tests/AttributeReaderTests.cs @@ -13,8 +13,12 @@ public Task Simple() TargetFrameworksInferred = AttributeReader.GetTargetFrameworks(), ProjectDirectory = AttributeReader.GetProjectDirectory(assembly), ProjectDirectoryInferred = AttributeReader.GetProjectDirectory(), + ProjectName = AttributeReader.GetProjectName(assembly), + ProjectNameInferred = AttributeReader.GetProjectName(), SolutionDirectory = AttributeReader.GetSolutionDirectory(assembly), - SolutionDirectoryInferred = AttributeReader.GetSolutionDirectory() + SolutionDirectoryInferred = AttributeReader.GetSolutionDirectory(), + SolutionName = AttributeReader.GetSolutionName(assembly), + SolutionNameInferred = AttributeReader.GetSolutionName() }); } } diff --git a/src/Verify/DerivePaths/AttributeReader.cs b/src/Verify/DerivePaths/AttributeReader.cs index e20cc9e64e..705c8cdf75 100644 --- a/src/Verify/DerivePaths/AttributeReader.cs +++ b/src/Verify/DerivePaths/AttributeReader.cs @@ -17,36 +17,66 @@ public static bool TryGetTargetFrameworks(Assembly assembly, [NotNullWhen(true)] public static string GetProjectDirectory() => GetProjectDirectory(Assembly.GetCallingAssembly()); + public static string GetProjectName() => + GetProjectName(Assembly.GetCallingAssembly()); + public static string GetProjectDirectory(Assembly assembly) => GetValue(assembly, "Verify.ProjectDirectory", true); + public static string GetProjectName(Assembly assembly) => + GetValue(assembly, "Verify.ProjectName", true); + public static bool TryGetProjectDirectory([NotNullWhen(true)] out string? projectDirectory) => TryGetProjectDirectory(Assembly.GetCallingAssembly(), true, out projectDirectory); + public static bool TryGetProjectName([NotNullWhen(true)] out string? projectName) => + TryGetProjectName(Assembly.GetCallingAssembly(), true, out projectName); + public static bool TryGetProjectDirectory(Assembly assembly, [NotNullWhen(true)] out string? projectDirectory) => TryGetProjectDirectory(assembly, true, out projectDirectory); + public static bool TryGetProjectName(Assembly assembly, [NotNullWhen(true)] out string? projectName) => + TryGetProjectName(assembly, true, out projectName); + public static string GetSolutionDirectory() => GetSolutionDirectory(Assembly.GetCallingAssembly()); + public static string GetSolutionName() => + GetSolutionName(Assembly.GetCallingAssembly()); + public static string GetSolutionDirectory(Assembly assembly) => GetValue(assembly, "Verify.SolutionDirectory", true); + public static string GetSolutionName(Assembly assembly) => + GetValue(assembly, "Verify.SolutionName", true); + public static bool TryGetSolutionDirectory([NotNullWhen(true)] out string? solutionDirectory) => TryGetSolutionDirectory(Assembly.GetCallingAssembly(), true, out solutionDirectory); + public static bool TryGetSolutionName([NotNullWhen(true)] out string? solutionName) => + TryGetSolutionName(Assembly.GetCallingAssembly(), true, out solutionName); + public static bool TryGetSolutionDirectory(Assembly assembly, [NotNullWhen(true)] out string? solutionDirectory) => TryGetSolutionDirectory(assembly, true, out solutionDirectory); + public static bool TryGetSolutionName(Assembly assembly, [NotNullWhen(true)] out string? solutionName) => + TryGetSolutionName(assembly, true, out solutionName); + internal static bool TryGetSolutionDirectory(bool mapPathForVirtualizedRun, [NotNullWhen(true)] out string? solutionDirectory) => TryGetSolutionDirectory(Assembly.GetCallingAssembly(), mapPathForVirtualizedRun, out solutionDirectory); internal static bool TryGetSolutionDirectory(Assembly assembly, bool mapPathForVirtualizedRun, [NotNullWhen(true)] out string? solutionDirectory) => TryGetValue(assembly, "Verify.SolutionDirectory", out solutionDirectory, mapPathForVirtualizedRun); + internal static bool TryGetSolutionName(Assembly assembly, bool mapPathForVirtualizedRun, [NotNullWhen(true)] out string? solutionName) => + TryGetValue(assembly, "Verify.SolutionName", out solutionName, mapPathForVirtualizedRun); + internal static bool TryGetProjectDirectory(Assembly assembly, bool mapPathForVirtualizedRun, [NotNullWhen(true)] out string? projectDirectory) => TryGetValue(assembly, "Verify.ProjectDirectory", out projectDirectory, mapPathForVirtualizedRun); + internal static bool TryGetProjectName(Assembly assembly, bool mapPathForVirtualizedRun, [NotNullWhen(true)] out string? projectName) => + TryGetValue(assembly, "Verify.ProjectName", out projectName, mapPathForVirtualizedRun); + static bool TryGetValue(Assembly assembly, string key, [NotNullWhen(true)] out string? value, bool isSourcePath = false) { foreach (var attribute in Attribute.GetCustomAttributes(assembly, typeof(AssemblyMetadataAttribute), false)) diff --git a/src/Verify/buildTransitive/Verify.props b/src/Verify/buildTransitive/Verify.props index 67ced62f45..3ee9798be2 100644 --- a/src/Verify/buildTransitive/Verify.props +++ b/src/Verify/buildTransitive/Verify.props @@ -23,11 +23,20 @@ <_Parameter1>Verify.ProjectDirectory <_Parameter2>$(ProjectDir) + + <_Parameter1>Verify.ProjectName + <_Parameter2>$(ProjectName) + + Condition="'$(SolutionDir)' != '' And '$(SolutionDir)' != '*Undefined*'"> <_Parameter1>Verify.SolutionDirectory <_Parameter2>$(SolutionDir) + + <_Parameter1>Verify.SolutionName + <_Parameter2>$(SolutionName) +