Skip to content
Closed
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
1 change: 1 addition & 0 deletions nuget.config
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@
<add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" protocolVersion="3" />
<add key="myget.org/F/aarnott" value="https://www.myget.org/F/aarnott/api/v3/index.json" protocolVersion="3" />
<add key="vs-devcore" value="https://www.myget.org/F/vs-devcore/api/v3/index.json" />
<add key="roslyn-analyzers" value="https://dotnet.myget.org/F/roslyn-analyzers/api/v3/index.json" />
</packageSources>
</configuration>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
{
using Microsoft.CodeAnalysis.Diagnostics;

public class CSharpAnalyzerTest<TAnalyzer> : CSharpCodeFixTest<TAnalyzer, EmptyCodeFixProvider>
where TAnalyzer : DiagnosticAnalyzer, new()
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
{
using System;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

public static class CSharpAnalyzerVerifier<TAnalyzer>
where TAnalyzer : DiagnosticAnalyzer, new()
{
public static DiagnosticResult[] EmptyDiagnosticResults { get; } = Array.Empty<DiagnosticResult>();

public static DiagnosticResult Diagnostic(string diagnosticId = null)
=> CSharpAnalyzerVerifier<TAnalyzer, XUnitVerifier>.Diagnostic(diagnosticId);

public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
=> new DiagnosticResult(descriptor);

public static DiagnosticResult CompilerError(string errorIdentifier)
=> new DiagnosticResult(errorIdentifier, DiagnosticSeverity.Error);

public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
{
var test = new Test
{
TestCode = source,
};

test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}

public class Test : CSharpAnalyzerTest<TAnalyzer>
{
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
{
using System;
using System.Collections.Immutable;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Windows.Threading;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing.Verifiers;
using Microsoft.CodeAnalysis.Text;
using IOleServiceProvider = Microsoft.VisualStudio.OLE.Interop.IServiceProvider;

public class CSharpCodeFixTest<TAnalyzer, TCodeFix> : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
{
private static readonly ImmutableArray<string> VSSDKPackageReferences = ImmutableArray.Create(new string[] {
Path.Combine("Microsoft.VisualStudio.Shell.Interop", "7.10.6071", "lib", "Microsoft.VisualStudio.Shell.Interop.dll"),
Path.Combine("Microsoft.VisualStudio.Shell.Interop.11.0", "11.0.61030", "lib", "Microsoft.VisualStudio.Shell.Interop.11.0.dll"),
Path.Combine("Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime", "14.3.25407", "lib", "Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime.dll"),
Path.Combine("Microsoft.VisualStudio.Shell.Immutable.14.0", "14.3.25407", "lib\\net45", "Microsoft.VisualStudio.Shell.Immutable.14.0.dll"),
Path.Combine("Microsoft.VisualStudio.Shell.14.0", "14.3.25407", "lib", "Microsoft.VisualStudio.Shell.14.0.dll"),
});

public CSharpCodeFixTest()
{
this.SolutionTransforms.Add((solution, projectId) =>
{
var parseOptions = (CSharpParseOptions)solution.GetProject(projectId).ParseOptions;
solution = solution.WithProjectParseOptions(projectId, parseOptions.WithLanguageVersion(LanguageVersion.CSharp7_1));

if (this.HasEntryPoint)
{
var compilationOptions = solution.GetProject(projectId).CompilationOptions;
solution = solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication));
}

if (this.IncludeMicrosoftVisualStudioThreading)
{
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(JoinableTaskFactory).Assembly.Location));
}

if (this.IncludeWindowsBase)
{
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(Dispatcher).Assembly.Location));
}

if (this.IncludeVisualStudioSdk)
{
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(typeof(IOleServiceProvider).Assembly.Location));

var nugetPackagesFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), ".nuget", "packages");
foreach (var reference in VSSDKPackageReferences)
{
solution = solution.AddMetadataReference(projectId, MetadataReference.CreateFromFile(Path.Combine(nugetPackagesFolder, reference)));
}
}

return solution;
});

this.AdditionalFilesFactories.Add(() =>
{
const string additionalFilePrefix = "AdditionalFiles.";
return from resourceName in Assembly.GetExecutingAssembly().GetManifestResourceNames()
where resourceName.StartsWith(additionalFilePrefix, StringComparison.Ordinal)
let content = ReadManifestResource(Assembly.GetExecutingAssembly(), resourceName)
select (filename: resourceName.Substring(additionalFilePrefix.Length), SourceText.From(content));
});
}

public bool HasEntryPoint { get; set; } = false;

public bool IncludeMicrosoftVisualStudioThreading { get; set; } = true;

public bool IncludeWindowsBase { get; set; } = true;

public bool IncludeVisualStudioSdk { get; set; } = true;

private static string ReadManifestResource(Assembly assembly, string resourceName)
{
using (var reader = new StreamReader(assembly.GetManifestResourceStream(resourceName)))
{
return reader.ReadToEnd();
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
{
using System;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CodeFixes;
using Microsoft.CodeAnalysis.CSharp.Testing;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.CodeAnalysis.Testing.Verifiers;

public static class CSharpCodeFixVerifier<TAnalyzer, TCodeFix>
where TAnalyzer : DiagnosticAnalyzer, new()
where TCodeFix : CodeFixProvider, new()
{
public static DiagnosticResult[] EmptyDiagnosticResults { get; } = Array.Empty<DiagnosticResult>();

public static DiagnosticResult Diagnostic(string diagnosticId = null)
=> CSharpCodeFixVerifier<TAnalyzer, TCodeFix, XUnitVerifier>.Diagnostic(diagnosticId);

public static DiagnosticResult Diagnostic(DiagnosticDescriptor descriptor)
=> new DiagnosticResult(descriptor);

public static DiagnosticResult CompilerError(string errorIdentifier)
=> new DiagnosticResult(errorIdentifier, DiagnosticSeverity.Error);

public static Task VerifyAnalyzerAsync(string source, params DiagnosticResult[] expected)
{
var test = new CSharpAnalyzerTest<TAnalyzer>
{
TestCode = source,
};

test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}

public static Task VerifyCodeFixAsync(string source, string fixedSource)
=> VerifyCodeFixAsync(source, EmptyDiagnosticResults, fixedSource);

public static Task VerifyCodeFixAsync(string source, DiagnosticResult expected, string fixedSource)
=> VerifyCodeFixAsync(source, new[] { expected }, fixedSource);

public static Task VerifyCodeFixAsync(string source, DiagnosticResult[] expected, string fixedSource)
{
var test = new Test
{
TestCode = source,
FixedCode = fixedSource,
};

test.ExpectedDiagnostics.AddRange(expected);
return test.RunAsync();
}

public class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix>
{
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System.Collections.Generic;
using System.Linq;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System;
using Microsoft.CodeAnalysis;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System;
using System.Collections.Generic;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
namespace Microsoft.VisualStudio.Threading.Analyzers.Tests.Legacy
{
using System.Collections.Generic;
using System.Collections.Immutable;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.

namespace Microsoft.VisualStudio.Threading.Analyzers.Tests
{
using System.Collections.Immutable;
using System.Threading.Tasks;
using Microsoft.CodeAnalysis.CodeFixes;

public sealed class EmptyCodeFixProvider : CodeFixProvider
{
public override ImmutableArray<string> FixableDiagnosticIds
=> ImmutableArray<string>.Empty;

public override Task RegisterCodeFixesAsync(CodeFixContext context)
=> Task.CompletedTask;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@
<ProjectReference Include="..\Microsoft.VisualStudio.Threading\Microsoft.VisualStudio.Threading.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Workspaces" Version="2.3.2" />
<PackageReference Include="MicroBuild.VisualStudio" Version="$(MicroBuildVersion)" PrivateAssets="all" />
<PackageReference Include="Microsoft.CodeAnalysis" Version="2.3.2" />
<PackageReference Include="Microsoft.Build.Utilities.Core" Version="14.3.0" />
<PackageReference Include="Microsoft.VisualStudio.OLE.Interop" Version="7.10.6070" />
<PackageReference Include="Microsoft.VisualStudio.Shell.14.0" Version="14.3.25407" IncludeAssets="none" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.11.0" Version="11.0.61030" IncludeAssets="none" />
<PackageReference Include="Microsoft.VisualStudio.Shell.Interop.14.0.DesignTime" Version="14.3.25407" IncludeAssets="none" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.Analyzer.Testing.XUnit" Version="1.0.0-beta1-63229-02" />
<PackageReference Include="Microsoft.CodeAnalysis.CSharp.CodeFix.Testing.XUnit" Version="1.0.0-beta1-63229-02" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.Analyzer.Testing.XUnit" Version="1.0.0-beta1-63229-02" />
<PackageReference Include="Microsoft.CodeAnalysis.VisualBasic.CodeFix.Testing.XUnit" Version="1.0.0-beta1-63229-02" />
<PackageReference Include="xunit" Version="2.2.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.1" />
<PackageReference Include="MicroBuild.Nonshipping" Version="$(MicroBuildVersion)" />
Expand Down
Loading