Skip to content

Commit

Permalink
Add ability to turn of xml docs (microsoft#4078)
Browse files Browse the repository at this point in the history
  • Loading branch information
m-nash authored Aug 2, 2024
1 parent 611667c commit 3540e63
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ private Configuration(
bool generateTestProject,
string libraryName,
bool useModelNamespace,
string libraryNamespace)
string libraryNamespace,
bool disableXmlDocs)
{
OutputDirectory = outputPath;
AdditionalConfigOptions = additionalConfigOptions;
Expand All @@ -46,6 +47,7 @@ private Configuration(
UseModelNamespace = useModelNamespace;
RootNamespace = libraryNamespace;
ModelNamespace = useModelNamespace ? $"{libraryNamespace}.Models" : libraryNamespace;
DisableXmlDocs = disableXmlDocs;
}

/// <summary>
Expand All @@ -60,8 +62,14 @@ private static class Options
public const string LibraryName = "library-name";
public const string Namespace = "namespace";
public const string UseModelNamespace = "use-model-namespace";
public const string DisableXmlDocs = "disable-xml-docs";
}

/// <summary>
/// Gets whether XML docs are disabled.
/// </summary>
public bool DisableXmlDocs { get; }

/// <summary> Gets the root namespace for the library. </summary>
public string RootNamespace { get; }

Expand Down Expand Up @@ -121,15 +129,16 @@ internal static Configuration Load(string outputPath, string? json = null)
: JsonDocument.Parse(json).RootElement;

return new Configuration(
outputPath.Equals(string.Empty) ? outputPath : Path.GetFullPath(outputPath),
Path.GetFullPath(outputPath),
ParseAdditionalConfigOptions(root),
ReadOption(root, Options.ClearOutputFolder),
ReadOption(root, Options.GenerateModelFactory),
ReadOption(root, Options.GenerateSampleProject),
ReadOption(root, Options.GenerateTestProject),
ReadRequiredStringOption(root, Options.LibraryName),
ReadOption(root, Options.UseModelNamespace),
ReadRequiredStringOption(root, Options.Namespace));
ReadRequiredStringOption(root, Options.Namespace),
ReadOption(root, Options.DisableXmlDocs));
}

/// <summary>
Expand All @@ -141,7 +150,8 @@ internal static Configuration Load(string outputPath, string? json = null)
{ Options.GenerateModelFactory, true },
{ Options.GenerateSampleProject, true },
{ Options.ClearOutputFolder, true },
{ Options.GenerateTestProject, false }
{ Options.GenerateTestProject, false },
{ Options.DisableXmlDocs, false },
};

/// <summary>
Expand All @@ -156,6 +166,7 @@ internal static Configuration Load(string outputPath, string? json = null)
Options.LibraryName,
Options.UseModelNamespace,
Options.Namespace,
Options.DisableXmlDocs,
};

private static bool ReadOption(JsonElement root, string option)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ public void WriteConstructor(ConstructorProvider ctor)

internal void WriteXmlDocs(XmlDocProvider? docs)
{
if (docs is null)
if (CodeModelPlugin.Instance.Configuration.DisableXmlDocs || docs is null)
return;

if (docs.Inherit is not null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.Generator.CSharp.Primitives;
using Microsoft.Generator.CSharp.Providers;
using NUnit.Framework;
using static Microsoft.Generator.CSharp.Snippets.Snippet;

namespace Microsoft.Generator.CSharp.Tests
{
Expand Down Expand Up @@ -67,15 +70,13 @@ public void TestParseConfig_OutputFolder(string mockJson, bool throwsError)
[TestCaseSource("ParseConfigLibraryNameTestCases")]
public void TestParseConfig_LibraryName(string mockJson, bool throwsError)
{

if (throwsError)
{
Assert.Throws<InvalidOperationException>(() => Configuration.Load(string.Empty, mockJson));
Assert.Throws<InvalidOperationException>(() => MockHelpers.LoadMockPlugin(configuration: mockJson));
return;
}

var configuration = Configuration.Load(string.Empty, mockJson);
var library = configuration.LibraryName;
var library = CodeModelPlugin.Instance.Configuration.LibraryName;
var expected = "libraryName";

Assert.AreEqual(expected, library);
Expand All @@ -87,12 +88,11 @@ public void TestParseConfig_Namespace(string mockJson, bool throwsError)
{
if (throwsError)
{
Assert.Throws<InvalidOperationException>(() => Configuration.Load(string.Empty, mockJson));
Assert.Throws<InvalidOperationException>(() => MockHelpers.LoadMockPlugin(configuration: mockJson));
return;
}

var configuration = Configuration.Load(string.Empty, mockJson);
var ns = configuration.RootNamespace;
var ns = CodeModelPlugin.Instance.Configuration.RootNamespace;
var expected = "namespace";

Assert.AreEqual(expected, ns);
Expand All @@ -102,8 +102,8 @@ public void TestParseConfig_Namespace(string mockJson, bool throwsError)
[TestCaseSource("ParseConfigUseModelNamespaceTestCases")]
public void TestParseConfig_UseModelNamespace(string mockJson, bool expected)
{
var configuration = Configuration.Load(string.Empty, mockJson);
var useModelNs = configuration.UseModelNamespace;
MockHelpers.LoadMockPlugin(configuration: mockJson);
var useModelNs = CodeModelPlugin.Instance.Configuration.UseModelNamespace;

Assert.AreEqual(expected, useModelNs);
}
Expand All @@ -120,9 +120,9 @@ public void TestParseConfig_AdditionalConfigOptions()
""unknown-bool-property"": true
}";

var configuration = Configuration.Load(string.Empty, mockJson);
MockHelpers.LoadMockPlugin(configuration: mockJson);

var additionalConfigOptions = configuration.AdditionalConfigOptions;
var additionalConfigOptions = CodeModelPlugin.Instance.Configuration.AdditionalConfigOptions;
Assert.IsNotNull(additionalConfigOptions);
Assert.IsTrue(additionalConfigOptions!.ContainsKey("unknown-string-property"));
Assert.IsTrue(additionalConfigOptions.ContainsKey("unknown-bool-property"));
Expand All @@ -134,6 +134,91 @@ public void TestParseConfig_AdditionalConfigOptions()
Assert.AreEqual(true, unknownBoolValue);
}

[Test]
public void DisableDocsForProperty()
{
var mockJson = @"{
""output-folder"": ""outputFolder"",
""library-name"": ""libraryName"",
""namespace"": ""namespace"",
""disable-xml-docs"": true
}";

MockHelpers.LoadMockPlugin(configuration: mockJson);

Assert.IsTrue(CodeModelPlugin.Instance.Configuration.DisableXmlDocs);

PropertyProvider property = new($"IntProperty description", MethodSignatureModifiers.Public, typeof(int), "IntProperty", new AutoPropertyBody(true));
using var writer = new CodeWriter();
writer.WriteProperty(property, true);
Assert.AreEqual("public int IntProperty { get; set; }\n", writer.ToString(false));
}

[Test]
public void DisableDocsForMethod()
{
var mockJson = @"{
""output-folder"": ""outputFolder"",
""library-name"": ""libraryName"",
""namespace"": ""namespace"",
""disable-xml-docs"": true
}";

MockHelpers.LoadMockPlugin(configuration: mockJson);

Assert.IsTrue(CodeModelPlugin.Instance.Configuration.DisableXmlDocs);
MethodProvider method = new(
new MethodSignature(
"Method",
$"Method Description",
MethodSignatureModifiers.Public,
null,
null,
[]),
ThrowExpression(Null),
new TestTypeProvider());
using var writer = new CodeWriter();
writer.WriteMethod(method);
Assert.AreEqual("public void Method() => throw null;\n", writer.ToString(false));
}

[Test]
public void DisableDocsForType()
{
var mockJson = @"{
""output-folder"": ""outputFolder"",
""library-name"": ""libraryName"",
""namespace"": ""Test"",
""disable-xml-docs"": true
}";

MockHelpers.LoadMockPlugin(configuration: mockJson);

Assert.IsTrue(CodeModelPlugin.Instance.Configuration.DisableXmlDocs);
TypeProvider type = new TestTypeProvider();
TypeProviderWriter writer = new(type);
Assert.AreEqual(Helpers.GetExpectedFromFile(), writer.Write().Content);
}

[Test]
public void DisableDocsForField()
{
var mockJson = @"{
""output-folder"": ""outputFolder"",
""library-name"": ""libraryName"",
""namespace"": ""namespace"",
""disable-xml-docs"": true
}";

MockHelpers.LoadMockPlugin(configuration: mockJson);

Assert.IsTrue(CodeModelPlugin.Instance.Configuration.DisableXmlDocs);
FieldProvider field = new(FieldModifiers.Public, typeof(int), "_field", $"Field Description");
using var writer = new CodeWriter();
writer.WriteField(field);
Assert.AreEqual("public int _field;\n", writer.ToString(false));
}

public static IEnumerable<TestCaseData> ParseConfigOutputFolderTestCases
{
get
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@ internal static class MockHelpers

public static void LoadMockPlugin(
Func<InputType, CSharpType>? createCSharpTypeCore = null,
Func<OutputLibrary>? createOutputLibrary = null)
Func<OutputLibrary>? createOutputLibrary = null,
string? configuration = null)
{
var configFilePath = Path.Combine(AppContext.BaseDirectory, TestHelpersFolder);
// initialize the singleton instance of the plugin
var mockPlugin = new Mock<CodeModelPlugin>(new GeneratorContext(Configuration.Load(configFilePath))) { CallBase = true };
var mockPlugin = new Mock<CodeModelPlugin>(new GeneratorContext(Configuration.Load(configFilePath, configuration))) { CallBase = true };

var mockTypeFactory = new Mock<TypeFactory>() { CallBase = true };

Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,16 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.Generator.CSharp.Providers;

namespace Microsoft.Generator.CSharp.Tests
{
internal class TestTypeProvider : TypeProvider
{
protected override string BuildRelativeFilePath()
{
throw new NotImplementedException();
}
protected override string BuildRelativeFilePath() => $"{Name}.cs";

protected override string BuildName() => "TestName";

protected override string BuildName()
{
throw new NotImplementedException();
}
public static readonly TypeProvider Empty = new TestTypeProvider();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// <auto-generated/>

#nullable disable

namespace Test
{
public partial class TestName
{
}
}

0 comments on commit 3540e63

Please sign in to comment.