8000 The default attributes to exclude list should only apply to the ApiDi… · dotnet/sdk@2f050e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 2f050e3

Browse files
committed
The default attributes to exclude list should only apply to the ApiDiff Tool, not to the library source code. Modify the existing test to consume the same list from the tool.
1 parent 37203a0 commit 2f050e3

File tree

8 files changed

+39
-22
lines changed

8 files changed

+39
-22
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
T:System.AttributeUsageAttribute
2+
T:System.ComponentModel.EditorBrowsableAttribute
3+
T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute
4+
T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff.Tool/Microsoft.DotNet.ApiDiff.Tool.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212
<ExcludeFromSourceOnlyBuild>true</ExcludeFromSourceOnlyBuild>
1313
</PropertyGroup>
1414

15+
<ItemGroup>
16+
<None Update="AttributesToExclude.txt">
17+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
18+
</None>
19+
</ItemGroup>
20+
1521
<ItemGroup>
1622
<ProjectReference Include="..\..\Microsoft.DotNet.ApiSymbolExtensions\Microsoft.DotNet.ApiSymbolExtensions.csproj" />
1723
<ProjectReference Include="..\Microsoft.DotNet.ApiDiff\Microsoft.DotNet.ApiDiff.csproj" />

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff.Tool/Program.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ namespace Microsoft.DotNet.ApiDiff.Tool;
1414
/// </summary>
1515
public static class Program
1616
{
17+
private static readonly string AttributesToExcludeDefaultFileName = "AttributesToExclude.txt";
18+
1719
public static async Task Main(string[] args)
1820
{
1921
RootCommand rootCommand = new("genapidiff");
@@ -85,10 +87,10 @@ public static async Task Main(string[] args)
8587

8688
Option<FileInfo[]?> optionFilesWithAttributesToExclude = new(name: "", aliases: ["--attributesToExclude", "-eattrs"])
8789
{
88-
Description = "An optional array of filepaths, each containing a list of attributes to exclude from the diff. Each file should contain one API full name per line.",
90+
Description = $"An optional array of filepaths, each containing a list of attributes to exclude from the diff. Each file should contain one API full name per line. You can either modify the default file '{AttributesToExcludeDefaultFileName}' to add your own attributes, or include additional files using this command line option.",
8991
Arity = ArgumentArity.ZeroOrMore,
9092
Required = false,
91-
DefaultValueFactory = _ => null
93+
DefaultValueFactory = _ => [new FileInfo(AttributesToExcludeDefaultFileName)]
9294
};
9395

9496
Option<FileInfo[]?> optionFilesWithApisToExclude = new(name: "", aliases: ["--apisToExclude", "-eapis"])

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff/DiffGeneratorFactory.cs

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,6 @@ namespace Microsoft.DotNet.ApiDiff;
99

1010
public static class DiffGeneratorFactory
1111
{
12-
/// <summary>
13-
/// The default attributes to exclude from the diff.
14-
/// </summary>
15-
public static readonly string[] DefaultAttributesToExclude = [
16-
"T:System.AttributeUsageAttribute",
17-
"T:System.ComponentModel.EditorBrowsableAttribute",
18-
"T:System.Diagnostics.CodeAnalysis.RequiresDynamicCodeAttribute",
19-
"T:System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute"
20-
];
21-
2212
/// <summary>
2313
/// The default diagnostic options to use when generating the diff.
2414
/// </summary>
@@ -43,7 +33,7 @@ public static class DiffGeneratorFactory
4333
/// <param name="afterFriendlyName">The friendly name for the assemblies after the change.</param>
4434
/// <param name="tableOfContentsTitle">The title for the table of contents in the generated diff.</param>
4535
/// <param name="filesWithAssembliesToExclude">An optional array of filepaths each containing a list of assemblies to avoid showing in the diff. If <see langword="null"/>, no assemblies are excluded.</param>
46-
/// <param name="filesWithAttributesToExclude">An optional array of filepaths each containing a list of attributes to avoid showing in the diff. If <see langword="null"/>, the default list of attributes to exclude <see cref="DiffGeneratorFactory.DefaultAttributesToExclude"/> is used. If an empty list, no attributes are excluded.</param>
36+
/// <param name="filesWithAttributesToExclude">An optional array of filepaths each containing a list of attributes to avoid showing in the diff.</param>
4737
/// <param name="filesWithApisToExclude">An optional array of filepaths each containing a list of APIs to avoid showing in the diff.</param>
4838
/// <param name="addPartialModifier">Indicates whether to add the partial modifier to types.</param>
4939
/// <param name="writeToDisk">If <see langword="true"/>, when calling <see cref="IDiffGenerator.RunAsync"/>, the generated markdown files get written to disk, and no item is added to the <see cref="IDiffGenerator.RunAsync"/> dictionary. If <see langword="false"/>, when calling <see cref="IDiffGenerator.RunAsync"/>, the generated markdown files get added to the <see cref="IDiffGenerator.RunAsync"/> dictionary (with the file path as the dictionary key) and none of them is written to disk. This is meant for testing purposes.</param>
@@ -91,7 +81,7 @@ public static IDiffGenerator Create(ILog log,
9181
/// <param name="afterLoader">The loader to use for loading the assemblies after the change.</param>
9282
/// <param name="beforeAssemblySymbols">The dictionary containing the assembly symbols before the change.</param>
9383
/// <param name="afterAssemblySymbols">The dictionary containing the assembly symbols after the change.</param>
94-
/// <param name="attributesToExclude">An optional list of attributes to avoid showing in the diff. If <see langword="null"/>, the default list of attributes to exclude <see cref="DiffGeneratorFactory.DefaultAttributesToExclude"/> is used. If an empty list, no attributes are excluded.</param>
84+
/// <param name="attributesToExclude">An optional 9E7A list of attributes to avoid showing in the diff.</param>
9585
/// <param name="apisToExclude">An optional list of APIs to avoid showing in the diff.</param>
9686
/// <param name="addPartialModifier">Indicates whether to add the partial modifier to types.</param>
9787
/// <param name="diagnosticOptions">An optional list of diagnostic options to use when generating the diff.</param>

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff/FileOutputDiffGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ internal sealed class FileOutputDiffGenerator : IDiffGenerator
4343
/// <param name="afterFriendlyName">The friendly name for the after version of the assemblies.</param>
4444
/// <param name="tableOfContentsTitle">The title for the table of contents.</param>
4545
/// <param name="filesWithAssembliesToExclude">An optional array of filepaths each containing a list of assemblies to avoid showing in the diff.</param>
46-
/// <param name="filesWithAttributesToExclude">An optional array of filepaths each containing a list of attributes to avoid showing in the diff. If <see langword="null"/>, the default list of attributes to exclude <see cref="DiffGeneratorFactory.DefaultAttributesToExclude"/> is used. If an empty list, no attributes are excluded.</param>
46+
/// <param name="filesWithAttributesToExclude">An optional array of filepaths each containing a list of attributes to avoid showing in the diff.</param>
4747
/// <param name="filesWithApisToExclude">An optional array of filepaths each containing a list of APIs to avoid showing in the diff.</param>
4848
/// <param name="addPartialModifier">A value indicating whether to add the partial modifier to types.</param>
4949
/// <param name="writeToDisk">If <see langword="true"/>, when calling <see cref="RunAsync"/>, the generated markdown files get written to disk, and no item is added to the <see cref="RunAsync"/> dictionary. If <see langword="false"/>, when calling <see cref="RunAsync"/>, the generated markdown files get added to the <see cref="RunAsync"/> dictionary (with the file path as the dictionary key) and none of them is written to disk. This is meant for testing purposes.</param>
@@ -75,7 +75,7 @@ internal FileOutputDiffGenerator(ILog log,
7575
_afterFriendlyName = afterFriendlyName;
7676
_tableOfContentsTitle = tableOfContentsTitle;
7777
_assembliesToExclude = CollectListsFromFiles(filesWithAssembliesToExclude);
78-
_attributesToExclude = filesWithAttributesToExclude != null ? CollectListsFromFiles(filesWithAttributesToExclude) : DiffGeneratorFactory.DefaultAttributesToExclude;
78+
_attributesToExclude = filesWithAttributesToExclude != null ? CollectListsFromFiles(filesWithAttributesToExclude) : [];
7979
_apisToExclude = CollectListsFromFiles(filesWithApisToExclude);
8080
_addPartialModifier = addPartialModifier;
8181
_writeToDisk = writeToDisk;

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff/MemoryOutputDiffGenerator.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public class MemoryOutputDiffGenerator : IDiffGenerator
4949
/// <param name="afterLoader">The loader for the "after" assembly symbols.</param>
5050
/// <param name="beforeAssemblySymbols">The dictionary of "before" assembly symbols.</param>
5151
/// <param name="afterAssemblySymbols">The dictionary of "after" assembly symbols.</param>
52-
/// <param name="attributesToExclude">An optional list of attributes to avoid showing in the diff. If <see langword="null"/>, the default list of attributes to exclude <see cref="DiffGeneratorFactory.DefaultAttributesToExclude"/> is used. If an empty list, no attributes are excluded.</param>
52+
/// <param name="attributesToExclude">An optional list of attributes to avoid showing in the diff.</param>
5353
/// <param name="apisToExclude">An optional list of APIs to avoid showing in the diff.</param>
5454
/// <param name="addPartialModifier">A boolean indicating whether to add the partial modifier to types.</param>
5555
/// <param name="diagnosticOptions">An optional dictionary of diagnostic options.</param>
@@ -71,7 +71,7 @@ internal MemoryOutputDiffGenerator(
7171
_afterAssemblySymbols = new ConcurrentDictionary<string, IAssemblySymbol>(afterAssemblySymbols);
7272
_addPartialModifier = addPartialModifier;
7373
_diagnosticOptions = diagnosticOptions ?? DiffGeneratorFactory.DefaultDiagnosticOptions;
74-
_attributeSymbolFilter = SymbolFilterFactory.GetFilterFromList(attributesToExclude ?? DiffGeneratorFactory.DefaultAttributesToExclude, includeExplicitInterfaceImplementationSymbols: true);
74+
_attributeSymbolFilter = SymbolFilterFactory.GetFilterFromList(attributesToExclude ?? [], includeExplicitInterfaceImplementationSymbols: true);
7575
_symbolFilter = SymbolFilterFactory.GetFilterFromList(apisToExclude ?? [], includeExplicitInterfaceImplementationSymbols: true);
7676
_twoSpacesTrivia = SyntaxFactory.TriviaList(SyntaxFactory.Space, SyntaxFactory.Space);
7777
_missingCloseBrace = SyntaxFactory.MissingToken(SyntaxKind.CloseBraceToken);

test/Microsoft.DotNet.ApiDiff.Tests/Diff.Attribute.Tests.cs

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,19 @@ public class MyClass
732732
#region Attribute exclusion
733733

734734
[Fact]
735-
public Task SuppressAllDefaultAttributes() =>
735+
public Task SuppressAllDefaultAttributesUsedByTool()
736+
{
736737
// The attributes that should get hidden in this test must all be part of
737-
// the DiffGeneratorFactory.DefaultAttributesToExclude list.
738-
RunTestAsync(
738+
// the AttributesToExclude.txt file that the ApiDiff tool uses by default.
739+
740+
FileInfo file = new FileInfo("AttributesToExclude.txt");
741+
if (!file.Exists)
742+
{
743+
throw new FileNotFoundException($"{file.FullName} file not found.");
744+
}
745+
string[] attributesToExclude = File.ReadAllLines(file.FullName);
746+
747+
return RunTestAsync(
739748
beforeCode: """
740749
namespace MyNamespace
741750
{
@@ -775,7 +784,8 @@ public class MyClass
775784
+ }
776785
}
777786
""",
778-
attributesToExclude: null); // null forces using the default list
787+
attributesToExclude: attributesToExclude);
788+
}
779789

780790
[Fact]
781791
public Task SuppressNone() => RunTestAsync(

test/Microsoft.DotNet.ApiDiff.Tests/Microsoft.DotNet.ApiDiff.Tests.csproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,11 @@
1414
<Compile Include="..\Microsoft.DotNet.GenAPI.Tests\TestAssemblyLoaderFactory.cs" />
1515
<Compile Include="..\Microsoft.DotNet.ApiSymbolExtensions.Tests\SymbolFactory.cs" />
1616
<Compile Include="..\Microsoft.DotNet.ApiSymbolExtensions.Tests\TempDirectory.cs" />
17+
<None
18+
Include="$(RepoRoot)src\Compatibility\ApiDiff\Microsoft.DotNet.ApiDiff.Tool\AttributesToExclude.txt"
19+
Link="AttributesToExclude.txt">
20+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
21+
</None>
1722
</ItemGroup>
1823

1924
<ItemGroup>

0 commit comments

Comments
 (0)
0