Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow explicit definition of "entry" assembly #785

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/CommandLine/Infrastructure/ReflectionHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ static class ReflectionHelper
/// </summary>
[ThreadStatic] private static IDictionary<Type, Attribute> _overrides;

private static Assembly _programAssembly;

public static Assembly ProgramAssembly
{
get => _programAssembly ?? GetExecutingOrEntryAssembly();
set => _programAssembly = value;
}

/// <summary>
/// Assembly attribute overrides for testing.
/// </summary>
Expand Down Expand Up @@ -51,12 +59,10 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()
Maybe.Nothing<TAttribute>();
}

var assembly = GetExecutingOrEntryAssembly();

#if NET40
var attributes = assembly.GetCustomAttributes(typeof(TAttribute), false);
var attributes = ProgramAssembly.GetCustomAttributes(typeof(TAttribute), false);
#else
var attributes = assembly.GetCustomAttributes<TAttribute>().ToArray();
var attributes = ProgramAssembly.GetCustomAttributes<TAttribute>().ToArray();
#endif

return attributes.Length > 0
Expand All @@ -66,14 +72,12 @@ public static Maybe<TAttribute> GetAttribute<TAttribute>()

public static string GetAssemblyName()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Name;
return ProgramAssembly.GetName().Name;
}

public static string GetAssemblyVersion()
{
var assembly = GetExecutingOrEntryAssembly();
return assembly.GetName().Version.ToStringInvariant();
return ProgramAssembly.GetName().Version.ToStringInvariant();
}

public static bool IsFSharpOptionType(Type type)
Expand Down
11 changes: 11 additions & 0 deletions src/CommandLine/Text/HelpText.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,17 @@ public SentenceBuilder SentenceBuilder
get { return sentenceBuilder; }
}

/// <summary>
/// Gets or sets the <see cref="Assembly"/> that will be used to look for meta data (assembly attributes) to auto build the help.
/// By default the entry assembly is automatically selected, but this may not match what is really expected, in particular when
/// running unit tests.
/// </summary>
public static Assembly AutoBuildMetadataAssembly
{
get => ReflectionHelper.ProgramAssembly;
set => ReflectionHelper.ProgramAssembly = value;
}

/// <summary>
/// Creates a new instance of the <see cref="CommandLine.Text.HelpText"/> class using common defaults.
/// </summary>
Expand Down
16 changes: 16 additions & 0 deletions tests/CommandLine.Tests/Unit/Text/HelpTextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class HelpTextTests : IDisposable
public void Dispose()
{
ReflectionHelper.SetAttributeOverride(null);
HelpText.AutoBuildMetadataAssembly = null;
}

[Fact]
Expand Down Expand Up @@ -716,6 +717,21 @@ public void AutoBuild_with_assembly_company_attribute_only()
actualResult.Copyright.Should().Be(string.Format("Copyright (C) {0} {1}", DateTime.Now.Year, expectedCompany));
}

[Fact]
public void AutoBuild_with_AutoBuildMetadataAssembly_defined()
{
HelpText.AutoBuildMetadataAssembly = typeof(HelpText).Assembly;
string expectedHeading = "CommandLine 0.0.0";
string expectedCopyright = "Copyright (c) 2005 - 2020 Giacomo Stelluti Scala & Contributors";

ParserResult<Simple_Options> fakeResult = new NotParsed<Simple_Options>(
TypeInfo.Create(typeof(Simple_Options)), new Error[0]);
HelpText actualResult = HelpText.AutoBuild(fakeResult, ht => ht, ex => ex);

actualResult.Heading.Should().Be(expectedHeading);
actualResult.Copyright.Should().Be(expectedCopyright);
}

[Fact]
public void Add_line_with_two_empty_spaces_at_the_end()
{
Expand Down