Skip to content

Commit c8f729b

Browse files
committed
Fix hint name when root namespace is empty
1 parent bbcfedf commit c8f729b

3 files changed

Lines changed: 523 additions & 7 deletions

File tree

src/DocoptNet/CodeGeneration/SourceGenerator.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ public void Execute(GeneratorExecutionContext context)
166166
if (Generate(ns, name, parentNames, attribute?.HelpConstName, help, options) is { Length: > 0 } source)
167167
{
168168
hintNameBuilder.Clear();
169-
if (ns is { } someNamespace)
169+
if (ns?.Trim() is { Length: > 0 } someNamespace)
170170
hintNameBuilder.Append(someNamespace).Append('.');
171171
if (parentNames.Length > 0)
172172
{

tests/DocoptNet.Tests/CodeGeneration/SourceGeneratorTests.cs

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,17 @@ public void Generate_with_usage_in_external_file()
5454
});
5555
}
5656

57+
[Test]
58+
public void Generate_with_empty_root_namespace()
59+
{
60+
AssertMatchesSnapshot(
61+
analyzerConfigOptions: new(KeyValuePair.Create("build_property.RootNamespace", string.Empty)),
62+
sources: new[]
63+
{
64+
("Program.docopt.txt", SourceText.From(NavalFateUsage))
65+
});
66+
}
67+
5768
[Test]
5869
public void Generate_with_inline_usage()
5970
{
@@ -256,9 +267,10 @@ sealed partial class ProgramArguments
256267
}
257268

258269
void AssertMatchesSnapshot((string Path, SourceText Text)[] sources,
270+
AnalyzerConfigOptions? analyzerConfigOptions = null,
259271
[CallerMemberName]string? callerName = null)
260272
{
261-
var (driver, compilation) = PrepareForGeneration(sources);
273+
var (driver, compilation) = PrepareForGeneration(analyzerConfigOptions ?? new(), sources);
262274

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

343-
var actualFiles = EnumerateFiles(actualSourcesPath);
355+
var actualFiles = EnumerateFiles(actualSourcesPath).ToImmutableArray();
356+
357+
Assert.That(from gs in grr.GeneratedSources
358+
orderby gs.HintName
359+
select gs.HintName,
360+
Is.EqualTo(from af in actualFiles
361+
select Path.GetFileName(af) into af
362+
where af.EndsWith(".cs", StringComparison.OrdinalIgnoreCase)
363+
orderby af
364+
select af));
344365

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

555576
internal static (CSharpGeneratorDriver, CSharpCompilation)
556-
PrepareForGeneration(params (string Path, SourceText Text)[] sources)
577+
PrepareForGeneration(params (string Path, SourceText Text)[] sources) =>
578+
PrepareForGeneration(new AnalyzerConfigOptions(), sources);
579+
580+
internal static (CSharpGeneratorDriver, CSharpCompilation)
581+
PrepareForGeneration(AnalyzerConfigOptions analyzerConfigOptions,
582+
params (string Path, SourceText Text)[] sources)
557583
{
558584
var trees = new List<SyntaxTree>();
559585
var additionalTexts = new List<AdditionalText>();
@@ -579,11 +605,9 @@ internal static (CSharpGeneratorDriver, CSharpCompilation)
579605

580606
ISourceGenerator generator = new SourceGenerator();
581607

582-
var globalOptions = Enumerable.Empty<KeyValuePair<string, string>>();
583-
584608
var optionsProvider =
585609
new AnalyzerConfigOptionsProvider(
586-
new AnalyzerConfigOptions(globalOptions),
610+
analyzerConfigOptions,
587611
additionalTexts.Select(at => KeyValuePair.Create(at, DocoptSourceItemTypeConfigOption))
588612
.ToImmutableDictionary());
589613

0 commit comments

Comments
 (0)