Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/DocoptNet/CodeGeneration/SourceGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ public void Execute(GeneratorExecutionContext context)
if (Generate(ns, name, parentNames, attribute?.HelpConstName, help, options) is { Length: > 0 } source)
{
hintNameBuilder.Clear();
if (ns is { } someNamespace)
if (ns?.Trim() is { Length: > 0 } someNamespace)
hintNameBuilder.Append(someNamespace).Append('.');
if (parentNames.Length > 0)
{
Expand Down
36 changes: 30 additions & 6 deletions tests/DocoptNet.Tests/CodeGeneration/SourceGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,17 @@ public void Generate_with_usage_in_external_file()
});
}

[Test]
public void Generate_with_empty_root_namespace()
{
AssertMatchesSnapshot(
analyzerConfigOptions: new(KeyValuePair.Create("build_property.RootNamespace", string.Empty)),
sources: new[]
{
("Program.docopt.txt", SourceText.From(NavalFateUsage))
});
}

[Test]
public void Generate_with_inline_usage()
{
Expand Down Expand Up @@ -256,9 +267,10 @@ sealed partial class ProgramArguments
}

void AssertMatchesSnapshot((string Path, SourceText Text)[] sources,
AnalyzerConfigOptions? analyzerConfigOptions = null,
[CallerMemberName]string? callerName = null)
{
var (driver, compilation) = PrepareForGeneration(sources);
var (driver, compilation) = PrepareForGeneration(analyzerConfigOptions ?? new(), sources);

var grr = driver.RunGeneratorsAndUpdateCompilation(compilation, out var outputCompilation, out _)
.GetRunResult().Results.Single();
Expand Down Expand Up @@ -340,7 +352,16 @@ where Path.GetFileName(fp) is { } fn
|| fn.EndsWith(".json", StringComparison.OrdinalIgnoreCase))
select Path.GetRelativePath(solutionDirPath, fp);

var actualFiles = EnumerateFiles(actualSourcesPath);
var actualFiles = EnumerateFiles(actualSourcesPath).ToImmutableArray();

Assert.That(from gs in grr.GeneratedSources
orderby gs.HintName
select gs.HintName,
Is.EqualTo(from af in actualFiles
select Path.GetFileName(af) into af
where af.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)
orderby af
select af));

var expectedFiles = Directory.Exists(expectedSourcesPath)
? EnumerateFiles(expectedSourcesPath)
Expand Down Expand Up @@ -553,7 +574,12 @@ public partial class " + ProgramArgumentsClassName + @" { }
new AnalyzerConfigOptions(KeyValuePair.Create("build_metadata.AdditionalFiles.SourceItemType", "Docopt"));

internal static (CSharpGeneratorDriver, CSharpCompilation)
PrepareForGeneration(params (string Path, SourceText Text)[] sources)
PrepareForGeneration(params (string Path, SourceText Text)[] sources) =>
PrepareForGeneration(new AnalyzerConfigOptions(), sources);

internal static (CSharpGeneratorDriver, CSharpCompilation)
PrepareForGeneration(AnalyzerConfigOptions analyzerConfigOptions,
params (string Path, SourceText Text)[] sources)
{
var trees = new List<SyntaxTree>();
var additionalTexts = new List<AdditionalText>();
Expand All @@ -579,11 +605,9 @@ internal static (CSharpGeneratorDriver, CSharpCompilation)

ISourceGenerator generator = new SourceGenerator();

var globalOptions = Enumerable.Empty<KeyValuePair<string, string>>();

var optionsProvider =
new AnalyzerConfigOptionsProvider(
new AnalyzerConfigOptions(globalOptions),
analyzerConfigOptions,
additionalTexts.Select(at => KeyValuePair.Create(at, DocoptSourceItemTypeConfigOption))
.ToImmutableDictionary());

Expand Down
Loading