diff --git a/eng/Versions.props b/eng/Versions.props index d2cfc79fd0..1a2b3b64e8 100644 --- a/eng/Versions.props +++ b/eng/Versions.props @@ -39,7 +39,7 @@ $(DogfoodAnalyzersVersion) 2.0.0-pre-20160714 - 1.0.1-beta1.20521.1 + 1.0.1-beta1.20623.3 1.3.1 4.7.0 diff --git a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/InternalImplementationOnlyTests.cs b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/InternalImplementationOnlyTests.cs index 931840ebd0..9377a92146 100644 --- a/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/InternalImplementationOnlyTests.cs +++ b/src/Microsoft.CodeAnalysis.Analyzers/UnitTests/InternalImplementationOnlyTests.cs @@ -59,6 +59,14 @@ class SomeOtherClass : IMyOtherInterface { }"; TestState = { Sources = { source2 }, + AdditionalProjects = + { + ["DependencyProject"] = + { + Sources = { source1 }, + }, + }, + AdditionalProjectReferences = { "DependencyProject" }, ExpectedDiagnostics = { // Test0.cs(2,7): error RS1009: Type SomeClass cannot implement interface IMyInterface because IMyInterface is not available for public implementation. @@ -67,19 +75,6 @@ class SomeOtherClass : IMyOtherInterface { }"; VerifyCS.Diagnostic().WithSpan(4, 7, 4, 21).WithArguments("SomeOtherClass", "IMyInterface"), }, }, - SolutionTransforms = - { - (solution, projectId) => - { - var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp) - .WithCompilationOptions(solution.GetProject(projectId).CompilationOptions) - .WithParseOptions(solution.GetProject(projectId).ParseOptions) - .WithMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .AddDocument("Test0.cs", source1, filePath: "Test0.cs").Project; - - return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id)); - }, - }, }.RunAsync(); } @@ -105,19 +100,17 @@ class SomeOtherClass : IMyOtherInterface { }"; await new VerifyCS.Test { ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithoutRoslynSymbols, - TestState = { Sources = { source2 } }, - SolutionTransforms = + TestState = { - (solution, projectId) => + Sources = { source2 }, + AdditionalProjects = { - var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp) - .WithCompilationOptions(solution.GetProject(projectId).CompilationOptions) - .WithParseOptions(solution.GetProject(projectId).ParseOptions) - .WithMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .AddDocument("Test0.cs", source1, filePath: "Test0.cs").Project; - - return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id)); + ["DependencyProject"] = + { + Sources = { source1 }, + }, }, + AdditionalProjectReferences = { "DependencyProject" }, }, }.RunAsync(); } @@ -441,6 +434,14 @@ End Class TestState = { Sources = { source2 }, + AdditionalProjects = + { + ["DependencyProject"] = + { + Sources = { source1 }, + }, + }, + AdditionalProjectReferences = { "DependencyProject" }, ExpectedDiagnostics = { // Test0.vb(2,7): error RS1009: Type SomeClass cannot implement interface IMyInterface because IMyInterface is not available for public implementation. @@ -449,19 +450,6 @@ End Class VerifyVB.Diagnostic().WithSpan(6, 7, 6, 21).WithArguments("SomeOtherClass", "IMyInterface"), }, }, - SolutionTransforms = - { - (solution, projectId) => - { - var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.VisualBasic) - .WithCompilationOptions(solution.GetProject(projectId).CompilationOptions) - .WithParseOptions(solution.GetProject(projectId).ParseOptions) - .WithMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .AddDocument("Test0.vb", source1, filePath: "Test0.vb").Project; - - return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id)); - }, - }, }.RunAsync(); } @@ -495,19 +483,17 @@ End Class await new VerifyVB.Test { ReferenceAssemblies = AdditionalMetadataReferences.DefaultWithoutRoslynSymbols, - TestState = { Sources = { source2 } }, - SolutionTransforms = + TestState = { - (solution, projectId) => + Sources = { source2 }, + AdditionalProjects = { - var dependencyProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.VisualBasic) - .WithCompilationOptions(solution.GetProject(projectId).CompilationOptions) - .WithParseOptions(solution.GetProject(projectId).ParseOptions) - .WithMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .AddDocument("Test0.vb", source1, filePath: "Test0.vb").Project; - - return dependencyProject.Solution.AddProjectReference(projectId, new ProjectReference(dependencyProject.Id)); + ["DependencyProject"] = + { + Sources = { source1 }, + }, }, + AdditionalProjectReferences = { "DependencyProject" }, }, }.RunAsync(); } diff --git a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs index 1105d666b3..aefea61a52 100644 --- a/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs +++ b/src/Microsoft.CodeAnalysis.BannedApiAnalyzers/UnitTests/RestrictedInternalsVisibleToAnalyzerTests.cs @@ -1,10 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. -using System.Linq; using System.Threading.Tasks; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Testing; -using Microsoft.CodeAnalysis.VisualBasic; using Test.Utilities; using Xunit; @@ -41,9 +38,6 @@ private static DiagnosticResult GetBasicResultAt(int startLine, int startColumn, private const string CSharpApiProviderFileName = "ApiProviderFileName.cs"; private const string VisualBasicApiProviderFileName = "ApiProviderFileName.vb"; - private static readonly CompilationOptions s_csharpCompilationOptions = new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary); - private static readonly CompilationOptions s_visualBasicCompilationOptions = new VisualBasicCompilationOptions(OutputKind.DynamicallyLinkedLibrary); - private const string CSharpRestrictedInternalsVisibleToAttribute = @" namespace System.Runtime.CompilerServices { @@ -73,10 +67,21 @@ private async Task VerifyCSharpAsync(string apiProviderSource, string apiConsume TestState = { Sources = { apiConsumerSource }, + AdditionalProjects = + { + [ApiProviderProjectName] = + { + Sources = + { + (CSharpApiProviderFileName, apiProviderSource + CSharpRestrictedInternalsVisibleToAttribute), + }, + }, + }, + AdditionalProjectReferences = { ApiProviderProjectName }, }, SolutionTransforms = { - (solution, projectId) => ApplySolutionTransforms(solution, projectId, apiProviderSource, LanguageNames.CSharp), + (solution, projectId) => ApplySolutionTransforms(solution, projectId), }, }; @@ -91,10 +96,21 @@ private async Task VerifyBasicAsync(string apiProviderSource, string apiConsumer TestState = { Sources = { apiConsumerSource }, + AdditionalProjects = + { + [ApiProviderProjectName] = + { + Sources = + { + (VisualBasicApiProviderFileName, apiProviderSource + VisualBasicRestrictedInternalsVisibleToAttribute), + }, + }, + }, + AdditionalProjectReferences = { ApiProviderProjectName }, }, SolutionTransforms = { - (solution, projectId) => ApplySolutionTransforms(solution, projectId, apiProviderSource, LanguageNames.VisualBasic), + (solution, projectId) => ApplySolutionTransforms(solution, projectId), }, }; @@ -102,31 +118,9 @@ private async Task VerifyBasicAsync(string apiProviderSource, string apiConsumer await test.RunAsync(); } - private static Solution ApplySolutionTransforms( - Solution solution, - ProjectId apiConsumerProjectId, - string apiProviderSource, - string language) - { - var restrictedInternalsVisibleToAttribute = language == LanguageNames.CSharp - ? CSharpRestrictedInternalsVisibleToAttribute - : VisualBasicRestrictedInternalsVisibleToAttribute; - var compilationOptions = language == LanguageNames.CSharp - ? s_csharpCompilationOptions - : s_visualBasicCompilationOptions; - var fileName = language == LanguageNames.CSharp - ? CSharpApiProviderFileName - : VisualBasicApiProviderFileName; - - var metadataReferences = solution.Projects.Single().MetadataReferences; - solution = solution.WithProjectAssemblyName(apiConsumerProjectId, ApiConsumerProjectName); - var apiProducerProject = solution - .AddProject(ApiProviderProjectName, ApiProviderProjectName, language) - .WithCompilationOptions(compilationOptions) - .WithMetadataReferences(metadataReferences) - .AddDocument(fileName, apiProviderSource + restrictedInternalsVisibleToAttribute) - .Project; - return apiProducerProject.Solution.AddProjectReference(apiConsumerProjectId, new ProjectReference(apiProducerProject.Id)); + private static Solution ApplySolutionTransforms(Solution solution, ProjectId apiConsumerProjectId) + { + return solution.WithProjectAssemblyName(apiConsumerProjectId, ApiConsumerProjectName); } [Fact] diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/IdentifiersShouldNotContainTypeNamesTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/IdentifiersShouldNotContainTypeNamesTests.cs index 5cbf6ac436..c092a6fe72 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/IdentifiersShouldNotContainTypeNamesTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/ApiDesignGuidelines/IdentifiersShouldNotContainTypeNamesTests.cs @@ -1,6 +1,7 @@ // Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using System.Threading.Tasks; +using Microsoft.CodeAnalysis; using Microsoft.CodeAnalysis.Testing; using Test.Utilities; using Xunit; @@ -464,17 +465,12 @@ public async Task CA1720_TopLevelStatements_NoDiagnostic() { await new VerifyCS.Test() { - TestCode = @"int x = 0;", - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - SolutionTransforms = + TestState = { - (solution, projectId) => - { - var project = solution.GetProject(projectId); - project = project.WithCompilationOptions(project.CompilationOptions.WithOutputKind(CodeAnalysis.OutputKind.ConsoleApplication)); - return project.Solution; - }, - } + Sources = { @"int x = 0;" }, + OutputKind = OutputKind.ConsoleApplication, + }, + LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, }.RunAsync(); } diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs index cc239c7e41..9d36db631f 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/AvoidUninstantiatedInternalClassesTests.cs @@ -229,19 +229,17 @@ public async Task CA1812_CSharp_NoDiagnostic_TypeContainingAssemblyEntryPointRet { await new VerifyCS.Test { - TestCode = + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @"internal class C { private static void Main() {} }", - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -250,19 +248,17 @@ public async Task CA1812_Basic_NoDiagnostic_TypeContainingAssemblyEntryPointRetu { await new VerifyVB.Test { - TestCode = + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @"Friend Class C Public Shared Sub Main() End Sub End Class", - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -271,19 +267,17 @@ public async Task CA1812_CSharp_NoDiagnostic_TypeContainingAssemblyEntryPointRet { await new VerifyCS.Test { - TestCode = + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @"internal class C { private static int Main() { return 1; } }", - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -292,20 +286,18 @@ public async Task CA1812_Basic_NoDiagnostic_TypeContainingAssemblyEntryPointRetu { await new VerifyVB.Test { - TestCode = + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @"Friend Class C Public Shared Function Main() As Integer Return 1 End Function End Class", - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -314,21 +306,19 @@ public async Task CA1812_CSharp_NoDiagnostic_TypeContainingAssemblyEntryPointRet { await new VerifyCS.Test { - TestCode = + LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp7_1, + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @" using System.Threading.Tasks; internal static class C { private static async Task Main() { await Task.Delay(1); } }", - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp7_1, - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -337,21 +327,19 @@ public async Task CA1812_CSharp_NoDiagnostic_TypeContainingAssemblyEntryPointRet { await new VerifyCS.Test { - TestCode = + LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp7_1, + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @" using System.Threading.Tasks; internal static class C { private static async Task Main() { await Task.Delay(1); return 1; } }", - LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp7_1, - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } - } + }, + }, }.RunAsync(); } @@ -382,18 +370,16 @@ public async Task CA1812_Basic_NoDiagnostic_MainMethodIsDifferentlyCased() { await new VerifyVB.Test { - TestCode = + TestState = + { + OutputKind = OutputKind.ConsoleApplication, + Sources = + { @"Friend Class C Private Shared Sub mAiN() End Sub End Class", - SolutionTransforms = - { - (solution, projectId) => - { - var compilationOptions = solution.GetProject(projectId).CompilationOptions; - return solution.WithProjectCompilationOptions(projectId, compilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - } + }, }, ExpectedDiagnostics = { @@ -1615,17 +1601,15 @@ public async Task CA1812_CSharp_TopLevelStatements_NoDiagnostic() { await new VerifyCS.Test() { - TestCode = @"int x = 0;", LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - SolutionTransforms = + TestState = { - (solution, projectId) => + OutputKind = OutputKind.ConsoleApplication, + Sources = { - var project = solution.GetProject(projectId); - project = project.WithCompilationOptions(project.CompilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - return project.Solution; + @"int x = 0;", }, - } + }, }.RunAsync(); } diff --git a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs index 4501e9474d..aea83ee7d1 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.CodeQuality.Analyzers/Maintainability/ReviewUnusedParametersTests.cs @@ -1186,17 +1186,15 @@ public async Task CA1801_TopLevelStatements_NoDiagnostic() { await new VerifyCS.Test() { - TestCode = @"int x = 0;", LanguageVersion = CodeAnalysis.CSharp.LanguageVersion.CSharp9, - SolutionTransforms = + TestState = { - (solution, projectId) => + OutputKind = OutputKind.ConsoleApplication, + Sources = { - var project = solution.GetProject(projectId); - project = project.WithCompilationOptions(project.CompilationOptions.WithOutputKind(OutputKind.ConsoleApplication)); - return project.Solution; + @"int x = 0;", }, - } + }, }.RunAsync(); } diff --git a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Security/DoNotDisableCertificateValidationTests.cs b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Security/DoNotDisableCertificateValidationTests.cs index 4acd6ee6e3..341217a762 100644 --- a/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Security/DoNotDisableCertificateValidationTests.cs +++ b/src/NetAnalyzers/UnitTests/Microsoft.NetCore.Analyzers/Security/DoNotDisableCertificateValidationTests.cs @@ -1,8 +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. using System.Threading.Tasks; -using Microsoft.CodeAnalysis; -using Microsoft.CodeAnalysis.CSharp; using Microsoft.CodeAnalysis.Testing; using Xunit; using VerifyCS = Test.Utilities.CSharpSecurityCodeFixVerifier< @@ -201,21 +199,15 @@ public void TestMethod() TestState = { Sources = { source2 }, - }, - SolutionTransforms = - { - (solution, projectId) => + AdditionalProjects = { - var sideProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp) - .AddDocument("Dependency.cs", source1).Project - .AddMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - - return sideProject.Solution.GetProject(projectId) - .AddProjectReference(new ProjectReference(sideProject.Id)) - .Solution; - } - } + ["DependencyProject"] = + { + Sources = { ("Dependency.cs", source1) }, + }, + }, + AdditionalProjectReferences = { "DependencyProject" }, + }, }.RunAsync(); } @@ -271,21 +263,15 @@ public void TestMethod() TestState = { Sources = { source2 }, - }, - SolutionTransforms = - { - (solution, projectId) => + AdditionalProjects = { - var sideProject = solution.AddProject("DependencyProject", "DependencyProject", LanguageNames.CSharp) - .AddDocument("Dependency.cs", source1).Project - .AddMetadataReferences(solution.GetProject(projectId).MetadataReferences) - .WithCompilationOptions(new CSharpCompilationOptions(OutputKind.DynamicallyLinkedLibrary)); - - return sideProject.Solution.GetProject(projectId) - .AddProjectReference(new ProjectReference(sideProject.Id)) - .Solution; - } - } + ["DependencyProject"] = + { + Sources = { ("Dependency.cs", source1) }, + }, + }, + AdditionalProjectReferences = { "DependencyProject" }, + }, }.RunAsync(); }