Skip to content
This repository was archived by the owner on Jul 12, 2022. It is now read-only.

Commit 6612669

Browse files
committed
Add rules to convert tests to xUnit and add a command line option to specify type of rules to run
1 parent b747c89 commit 6612669

File tree

6 files changed

+325
-10
lines changed

6 files changed

+325
-10
lines changed

src/CodeFormatter/Program.cs

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
using System;
22
using System.ComponentModel.Composition.Hosting;
33
using System.IO;
4+
using System.Linq;
45
using System.Threading;
56
using System.Threading.Tasks;
67

78
using Microsoft.CodeAnalysis.MSBuild;
89
using Microsoft.DotNet.CodeFormatting;
10+
using System.Collections.Generic;
911

1012
namespace CodeFormatter
1113
{
1214
internal static class Program
1315
{
1416
private static int Main(string[] args)
1517
{
16-
if (args.Length != 1)
18+
if (args.Length < 1)
1719
{
18-
Console.Error.WriteLine("CodeFormatter <solution>");
20+
Console.Error.WriteLine("CodeFormatter <solution> [<rule types>]");
1921
return -1;
2022
}
2123

@@ -26,23 +28,33 @@ private static int Main(string[] args)
2628
return -1;
2729
}
2830

31+
var ruleTypes = args.Skip(1);
32+
2933
var cts = new CancellationTokenSource();
3034
var ct = cts.Token;
3135

3236
Console.CancelKeyPress += delegate { cts.Cancel(); };
3337

34-
RunAsync(solutionPath, ct).Wait(ct);
38+
RunAsync(solutionPath, ruleTypes, ct).Wait(ct);
3539
Console.WriteLine("Completed formatting.");
3640
return 0;
3741
}
3842

39-
private static async Task RunAsync(string solutionFilePath, CancellationToken cancellationToken)
43+
private static async Task RunAsync(string solutionFilePath, IEnumerable<string> ruleTypes, CancellationToken cancellationToken)
4044
{
41-
var workspace = MSBuildWorkspace.Create();
42-
await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken);
45+
try
46+
{
47+
var workspace = MSBuildWorkspace.Create();
48+
await workspace.OpenSolutionAsync(solutionFilePath, cancellationToken);
4349

44-
var engine = FormattingEngine.Create();
45-
await engine.RunAsync(workspace, cancellationToken);
50+
var engine = FormattingEngine.Create(ruleTypes);
51+
await engine.RunAsync(workspace, cancellationToken);
52+
}
53+
catch (Exception ex)
54+
{
55+
Console.WriteLine(ex.ToString());
56+
throw;
57+
}
4658
}
4759
}
4860
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.CodeAnalysis;
8+
9+
namespace Microsoft.DotNet.CodeFormatting.Filters
10+
{
11+
// Useful to restrict processing to a single file when testing code
12+
[Export(typeof(IFormattingFilter))]
13+
[PartMetadata(RuleTypeConstants.PartMetadataKey, "Test")]
14+
internal sealed class TestFilter : IFormattingFilter
15+
{
16+
public Task<bool> ShouldBeProcessedAsync(Document document)
17+
{
18+
//if (document.FilePath.EndsWith("ActivationEventOrderingTests.cs"))
19+
if (document.FilePath.EndsWith("PartBuilderOfTTests.cs"))
20+
{
21+
return Task.FromResult(true);
22+
}
23+
return Task.FromResult(false);
24+
}
25+
}
26+
}

src/Microsoft.DotNet.CodeFormatting/FormattingEngine.cs

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,45 @@
11
// Copyright (c) Microsoft Corporation. All rights reserved.
22
// Licensed under MIT. See LICENSE in the project root for license information.
33
using System;
4+
using System.Linq;
45
using System.ComponentModel.Composition.Hosting;
6+
using System.ComponentModel.Composition;
7+
using System.Collections.Generic;
58

69
namespace Microsoft.DotNet.CodeFormatting
710
{
811
public static class FormattingEngine
912
{
10-
public static IFormattingEngine Create()
13+
public static IFormattingEngine Create(IEnumerable<string> ruleTypes)
1114
{
1215
var catalog = new AssemblyCatalog(typeof(FormattingEngine).Assembly);
13-
var container = new CompositionContainer(catalog);
16+
17+
var ruleTypesHash = new HashSet<string>(ruleTypes);
18+
if (ruleTypesHash.Count == 0)
19+
{
20+
ruleTypesHash.Add(RuleTypeConstants.FormatCodeRuleType);
21+
}
22+
23+
var filteredCatalog = new FilteredCatalog(catalog, cpd =>
24+
{
25+
if (cpd.ExportDefinitions.Any(em =>
26+
em.ContractName == AttributedModelServices.GetContractName(typeof(IFormattingRule)) ||
27+
em.ContractName == AttributedModelServices.GetContractName(typeof(IFormattingFilter))))
28+
{
29+
object ruleType;
30+
if (cpd.Metadata.TryGetValue("RuleType", out ruleType))
31+
{
32+
if (ruleType is string && !ruleTypesHash.Contains((string)ruleType))
33+
{
34+
return false;
35+
}
36+
}
37+
}
38+
39+
return true;
40+
});
41+
42+
var container = new CompositionContainer(filteredCatalog);
1443
var engine = container.GetExportedValue<IFormattingEngine>();
1544
return engine;
1645
}

src/Microsoft.DotNet.CodeFormatting/Microsoft.DotNet.CodeFormatting.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
<Reference Include="System.Xml" />
7878
</ItemGroup>
7979
<ItemGroup>
80+
<Compile Include="Filters\TestFilter.cs" />
8081
<Compile Include="FormattingEngine.cs" />
8182
<Compile Include="FormattingEngineImplementation.cs" />
8283
<Compile Include="IFormattingEngine.cs" />
@@ -99,6 +100,8 @@
99100
<Compile Include="Rules\IsFormattedFormattingRule.cs" />
100101
<Compile Include="Rules\IsSimplifiedFormattingRule.cs" />
101102
<Compile Include="Rules\RuleExtensions.cs" />
103+
<Compile Include="Rules\UsesXunitForTests.cs" />
104+
<Compile Include="RuleTypeConstants.cs" />
102105
</ItemGroup>
103106
<ItemGroup>
104107
<None Include="packages.config" />
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.ComponentModel.Composition;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
8+
namespace Microsoft.DotNet.CodeFormatting
9+
{
10+
static class RuleTypeConstants
11+
{
12+
public const string PartMetadataKey = "RuleType";
13+
14+
public const string FormatCodeRuleType = "FormatCode";
15+
public const string ConvertTestsRuleType = "ConvertTests";
16+
}
17+
}

0 commit comments

Comments
 (0)