Skip to content

Commit

Permalink
expose ProjectName and SolutionName in AttributeReader (#1274)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonCropp authored Aug 30, 2024
1 parent 6c2df5a commit 7174979
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<Project>
<PropertyGroup>
<NoWarn>CS1591;CS0649;xUnit1026;xUnit1013;CS1573;VerifyTestsProjectDir;VerifySetParameters;PolyFillTargetsForNuget</NoWarn>
<Version>26.2.0</Version>
<Version>26.3.0</Version>
<ImplicitUsings>enable</ImplicitUsings>
<LangVersion>preview</LangVersion>
<AssemblyVersion>1.0.0</AssemblyVersion>
Expand Down
6 changes: 5 additions & 1 deletion src/Verify.Tests/AttributeReaderTests.Simple.verified.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
6 changes: 5 additions & 1 deletion src/Verify.Tests/AttributeReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
});
}
}
Expand Down
30 changes: 30 additions & 0 deletions src/Verify/DerivePaths/AttributeReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
11 changes: 10 additions & 1 deletion src/Verify/buildTransitive/Verify.props
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,20 @@
<_Parameter1>Verify.ProjectDirectory</_Parameter1>
<_Parameter2>$(ProjectDir)</_Parameter2>
</VerifyAttributes>
<VerifyAttributes Include="System.Reflection.AssemblyMetadata">
<_Parameter1>Verify.ProjectName</_Parameter1>
<_Parameter2>$(ProjectName)</_Parameter2>
</VerifyAttributes>
<VerifyAttributes Include="System.Reflection.AssemblyMetadata"
Condition="'$(SolutionDir)' != '' And '$(SolutionDir)' != '*Undefined*'">
Condition="'$(SolutionDir)' != '' And '$(SolutionDir)' != '*Undefined*'">
<_Parameter1>Verify.SolutionDirectory</_Parameter1>
<_Parameter2>$(SolutionDir)</_Parameter2>
</VerifyAttributes>
<VerifyAttributes Include="System.Reflection.AssemblyMetadata"
Condition="'$(SolutionName)' != '' And '$(SolutionName)' != '*Undefined*'">
<_Parameter1>Verify.SolutionName</_Parameter1>
<_Parameter2>$(SolutionName)</_Parameter2>
</VerifyAttributes>
<!-- Ensure not part of Compile, as a workaround for https://github.com/dotnet/sdk/issues/114 -->
<Compile Remove="$(VerifyAttributesFilePath)" />
</ItemGroup>
Expand Down

0 comments on commit 7174979

Please sign in to comment.