Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update analyzer test library #4740

Merged
merged 1 commit into from
Jan 30, 2020
Merged
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
2 changes: 1 addition & 1 deletion build/Dependencies.props
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
<!-- Test-only Dependencies -->
<PropertyGroup>
<BenchmarkDotNetVersion>0.11.3</BenchmarkDotNetVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.0-beta1-63812-02</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftCodeAnalysisTestingVersion>1.0.1-beta1.20080.1</MicrosoftCodeAnalysisTestingVersion>
<MicrosoftMLTestDatabasesPackageVersion>0.0.5-test</MicrosoftMLTestDatabasesPackageVersion>
<MicrosoftMLTestModelsPackageVersion>0.0.6-test</MicrosoftMLTestModelsPackageVersion>
<MicrosoftMLTensorFlowTestModelsVersion>0.0.11-test</MicrosoftMLTensorFlowTestModelsVersion>
Expand Down
11 changes: 11 additions & 0 deletions test/Microsoft.ML.CodeAnalyzer.Tests/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-10.0.0.0" newVersion="10.0.0.0" />
</dependentAssembly>
Comment on lines +5 to +8
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have been applied automatically. I do not know why it failed, but this addresses the problem.

</assemblyBinding>
</runtime>
</configuration>
9 changes: 4 additions & 5 deletions test/Microsoft.ML.CodeAnalyzer.Tests/Code/BestFriendTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ public async Task BestFriend()
TestState =
{
Sources = { SourceUser.Value },
AdditionalReferences = { MetadataReference.CreateFromFile(typeof(Console).Assembly.Location) },
},
SolutionTransforms =
{
Expand All @@ -70,7 +69,7 @@ public async Task BestFriend()
projectA = projectA.AddDocument("BestFriendDeclaration.cs", SourceDeclaration.Value).Project;
projectA = projectA.WithParseOptions(((CSharpParseOptions)projectA.ParseOptions).WithLanguageVersion(LanguageVersion.CSharp7_2));
projectA = projectA.WithCompilationOptions(projectA.CompilationOptions.WithOutputKind(OutputKind.DynamicallyLinkedLibrary));
projectA = projectA.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences.Concat(test.TestState.AdditionalReferences));
projectA = projectA.WithMetadataReferences(solution.GetProject(projectId).MetadataReferences);
solution = projectA.Solution;

solution = solution.AddProjectReference(projectId, new ProjectReference(projectA.Id));
Expand All @@ -83,10 +82,10 @@ public async Task BestFriend()

// Remove these assemblies, or an additional definition of BestFriendAttribute could exist and the
// compilation will not be able to locate a single definition for use in the analyzer.
test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetCoreReference);
test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetDataReference);
Assert.True(test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetCoreReference));
Assert.True(test.TestState.AdditionalReferences.Remove(AdditionalMetadataReferences.MLNetDataReference));

test.Exclusions &= ~AnalysisExclusions.GeneratedCode;
test.TestBehaviors |= TestBehaviors.SkipGeneratedCodeCheck;
test.ExpectedDiagnostics.AddRange(expected);
await test.RunAsync();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public async Task ContractsCheck()
SourceContracts.Value,
SourceFriend.Value,
},
AdditionalReferences = { AdditionalMetadataReferences.RefFromType<Memory<int>>() },
}
};

Expand Down Expand Up @@ -131,7 +130,6 @@ public async Task ContractsCheckFix()
new DiagnosticResult("CS0122", DiagnosticSeverity.Error).WithLocation("Test1.cs", 752, 67).WithMessage("'ICancelable.IsCanceled' is inaccessible due to its protection level"),
new DiagnosticResult("CS1503", DiagnosticSeverity.Error).WithLocation("Test1.cs", 753, 91).WithMessage("Argument 2: cannot convert from 'Microsoft.ML.Runtime.IHostEnvironment' to 'Microsoft.ML.Runtime.IExceptionContext'"),
},
AdditionalReferences = { AdditionalMetadataReferences.RefFromType<Memory<int>>() },
},
FixedState =
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,35 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Reflection;
#nullable enable

using System.Collections.Immutable;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.Testing;
using Microsoft.ML.Runtime;

namespace Microsoft.ML.CodeAnalyzer.Tests.Helpers
{
internal static class AdditionalMetadataReferences
{
internal static readonly MetadataReference StandardReference = MetadataReference.CreateFromFile(Assembly.Load("netstandard, Version=2.0.0.0").Location);
internal static readonly MetadataReference RuntimeReference = MetadataReference.CreateFromFile(Assembly.Load("System.Runtime, Version=0.0.0.0").Location);
internal static readonly MetadataReference CSharpSymbolsReference = RefFromType<CSharpCompilation>();
#if NETCOREAPP
internal static readonly ReferenceAssemblies DefaultReferenceAssemblies = ReferenceAssemblies.Default
.AddPackages(ImmutableArray.Create(new PackageIdentity("System.Memory", "4.5.1")));
#else
internal static readonly ReferenceAssemblies DefaultReferenceAssemblies = ReferenceAssemblies.NetFramework.Net472.Default
.AddPackages(ImmutableArray.Create(new PackageIdentity("System.Memory", "4.5.1")));
Comment on lines +21 to +22
Copy link
Member Author

@sharwell sharwell Jan 30, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is required due to incompatible targets used in the netfx build of Microsoft.ML. #4741

#endif

internal static readonly MetadataReference MSDataDataViewReference = RefFromType<IDataView>();
internal static readonly MetadataReference MLNetCoreReference = RefFromType<IHostEnvironment>();
internal static readonly MetadataReference MLNetDataReference = RefFromType<MLContext>();

internal static MetadataReference RefFromType<TType>()
=> MetadataReference.CreateFromFile(typeof(TType).Assembly.Location);
{
var location = typeof(TType).Assembly.Location;
var documentationProvider = XmlDocumentationProvider.CreateFromFile(Path.ChangeExtension(location, ".pdb"));
return MetadataReference.CreateFromFile(location, documentation: documentationProvider);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,8 @@ internal class Test : CSharpCodeFixTest<TAnalyzer, TCodeFix, XUnitVerifier>
{
public Test()
{
TestState.AdditionalReferences.Add(AdditionalMetadataReferences.StandardReference);
TestState.AdditionalReferences.Add(AdditionalMetadataReferences.RuntimeReference);
TestState.AdditionalReferences.Add(AdditionalMetadataReferences.CSharpSymbolsReference);
ReferenceAssemblies = AdditionalMetadataReferences.DefaultReferenceAssemblies;

TestState.AdditionalReferences.Add(AdditionalMetadataReferences.MSDataDataViewReference);
TestState.AdditionalReferences.Add(AdditionalMetadataReferences.MLNetCoreReference);
TestState.AdditionalReferences.Add(AdditionalMetadataReferences.MLNetDataReference);
Expand Down