public async Task ContractsCheckFix()
        {
            var test = new VerifyCS.Test
            {
                LanguageVersion = LanguageVersion.CSharp7_2,
                TestState       =
                {
                    Sources              =
                    {
                        SourcePreFix.Value,
                        SourceContracts.Value,
                        SourceArgAttr.Value,
                        SourceArgType.Value,
                        SourceBestAttr.Value,
                        SourceDefArgAttr.Value,
                    },
                    ExpectedDiagnostics  =
                    {
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(8,                  43).WithArguments("ExceptParam"),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(8,                      65).WithArguments("ExceptParam","paramName",  "\"yuck\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(12,                      39).WithArguments("CheckValue","paramName",  "\"str\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(13,                    66).WithArguments("CheckUserArg","name",       "\"Foo\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(14,                    57).WithArguments("CheckUserArg","name",       "\"Bar\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(15,                    57).WithArguments("CheckUserArg","name",       "\"A\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(16,                     52).WithArguments("ExceptParam","paramName",  "\"Bar\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(19,                      17).WithArguments("CheckParam","paramName",  "\"isFive\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(20,                      49).WithArguments("CheckValue","paramName",  "\"Y\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(21,                     52).WithArguments("ExceptParam","paramName",  "\"tom\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(22,                      39).WithArguments("CheckValue","paramName",  "\"noMatch\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(23,                    53).WithArguments("CheckUserArg","name",       "\"chumble\""),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(24,                    53).WithArguments("CheckUserArg","name",       "\"sp\""),
                        new DiagnosticResult("CS1503",                                                       DiagnosticSeverity.Error).WithLocation("Test1.cs",         748, 91).WithMessage("Argument 2: cannot convert from 'Microsoft.ML.IHostEnvironment' to 'Microsoft.ML.IExceptionContext'"),
                    },
                    AdditionalReferences = { AdditionalMetadataReferences.RefFromType <Memory <int> >() },
                },
                FixedState =
                {
                    Sources             =
                    {
                        SourcePostFix.Value,
                        SourceContracts.Value,
                        SourceArgAttr.Value,
                        SourceArgType.Value,
                        SourceBestAttr.Value,
                        SourceDefArgAttr.Value,
                    },
                    ExpectedDiagnostics =
                    {
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(8,                  43).WithArguments("ExceptParam"),
                        VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(22,                      39).WithArguments("CheckValue","paramName","\"noMatch\""),
                        new DiagnosticResult("CS1503",                                                       DiagnosticSeverity.Error).WithLocation("Test1.cs",     748, 91).WithMessage("Argument 2: cannot convert from 'Microsoft.ML.IHostEnvironment' to 'Microsoft.ML.IExceptionContext'"),
                    },
                },
            };

            await test.RunAsync();
        }
Exemple #2
0
        internal IReadOnlyList <MetadataReference> Resolve(Assembly assembly, DependencyContext dependencyContext)
        {
            var libraryPaths = new HashSet <string>(StringComparer.OrdinalIgnoreCase);
            IEnumerable <string> references;

            if (dependencyContext == null)
            {
                var context = new HashSet <string>();
                var x       = GetReferencedAssemblies(assembly, ExcludedAssemblies, context).Union(new[] { assembly }).ToArray();
                references = x.Select(p => _directoryFormatter.GetAssemblyDirectory(p)).ToList();
            }
            else
            {
                var excluded          = ExcludedAssemblies;
                var compiledLibraries = dependencyContext.CompileLibraries.Select(x => x.Name);
                var paths             = dependencyContext.CompileLibraries.SelectMany(x => x.ResolveReferencePaths()).ToList();

                references = dependencyContext.CompileLibraries.Where(x => !ExcludedAssemblies.Contains(x.Name)).SelectMany(library => library.ResolveReferencePaths()).ToList();

                if (!references.Any())
                {
                    throw new RazorLightException(
                              "Can't load metadata reference from the entry assembly. " +
                              "Make sure PreserveCompilationContext is set to true in *.csproj file");
                }
            }

            var metadataReferences = new List <MetadataReference>();

            foreach (var reference in references)
            {
                if (!libraryPaths.Add(reference))
                {
                    continue;
                }

                using (var stream = File.OpenRead(reference))
                {
                    var moduleMetadata   = ModuleMetadata.CreateFromStream(stream, PEStreamOptions.PrefetchMetadata);
                    var assemblyMetadata = AssemblyMetadata.Create(moduleMetadata);

                    metadataReferences.Add(assemblyMetadata.GetReference(filePath: reference));
                }
            }

            if (AdditionalMetadataReferences.Any())
            {
                metadataReferences.AddRange(AdditionalMetadataReferences);
            }

            return(metadataReferences);
        }
        public async Task ContractsCheck()
        {
            const int basis    = 10;
            var       expected = new DiagnosticResult[] {
                new DiagnosticResult("CS0051", DiagnosticSeverity.Error).WithLocation(14, 16).WithMessage("Inconsistent accessibility: parameter type 'IHostEnvironment' is less accessible than method 'TypeName.TypeName(IHostEnvironment, float, int)'"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 8, 46).WithArguments("CheckParam", "paramName", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 9, 46).WithArguments("CheckParam", "paramName", "nameof(p) + nameof(p)"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 11, 28).WithArguments("CheckValue", "paramName", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 13, 39).WithArguments("CheckUserArg", "name", "\"foo\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(basis + 15, 13).WithArguments("Except"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(basis + 16, 13).WithArguments("ExceptParam"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 22, 39).WithArguments("ExceptParam", "paramName", "\"env\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 24, 29).WithArguments("Check", "$\"Hello {foo} is cool\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 28, 29).WithArguments("Check", "string.Format(\"Hello {0} is cool\", foo)"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 32, 35).WithArguments("Check", "\"Less fine: \" + env.GetType().Name"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 34, 17).WithArguments("CheckUserArg", "name", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.DecodeMessageWithLoadContextDiagnostic.Rule).WithLocation(basis + 39, 41).WithArguments("CheckDecode", "\"This message is suspicious\""),
                new DiagnosticResult("CS0117", DiagnosticSeverity.Error).WithLocation("Test1.cs", 220, 70).WithMessage("'MessageSensitivity' does not contain a definition for 'UserData'"),
                new DiagnosticResult("CS0117", DiagnosticSeverity.Error).WithLocation("Test1.cs", 231, 70).WithMessage("'MessageSensitivity' does not contain a definition for 'Schema'"),
                new DiagnosticResult("CS1061", DiagnosticSeverity.Error).WithLocation("Test1.cs", 747, 21).WithMessage("'IHostEnvironment' does not contain a definition for 'IsCancelled' and no accessible extension method 'IsCancelled' accepting a first argument of type 'IHostEnvironment' could be found (are you missing a using directive or an assembly reference?)"),
            };

            var test = new VerifyCS.Test
            {
                LanguageVersion = LanguageVersion.CSharp7_2,
                TestState       =
                {
                    Sources              =
                    {
                        Source.Value,
                        SourceContracts.Value,
                        SourceFriend.Value,
                    },
                    AdditionalReferences = { AdditionalMetadataReferences.RefFromType <Memory <int> >() },
                }
            };

            test.ExpectedDiagnostics.AddRange(expected);
            await test.RunAsync();
        }
        public async Task ContractsCheck()
        {
            const int basis    = 11;
            var       expected = new DiagnosticResult[] {
                new DiagnosticResult("CS0051", DiagnosticSeverity.Error).WithLocation(15, 16).WithMessage("Inconsistent accessibility: parameter type 'IHostEnvironment' is less accessible than method 'TypeName.TypeName(IHostEnvironment, float, int)'"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 8, 46).WithArguments("CheckParam", "paramName", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 9, 46).WithArguments("CheckParam", "paramName", "nameof(p) + nameof(p)"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 11, 28).WithArguments("CheckValue", "paramName", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 13, 39).WithArguments("CheckUserArg", "name", "\"foo\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(basis + 15, 13).WithArguments("Except"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.ExceptionDiagnostic.Rule).WithLocation(basis + 16, 13).WithArguments("ExceptParam"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 22, 39).WithArguments("ExceptParam", "paramName", "\"env\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 24, 29).WithArguments("Check", "$\"Hello {foo} is cool\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 28, 29).WithArguments("Check", "string.Format(\"Hello {0} is cool\", foo)"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.SimpleMessageDiagnostic.Rule).WithLocation(basis + 32, 35).WithArguments("Check", "\"Less fine: \" + env.GetType().Name"),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.NameofDiagnostic.Rule).WithLocation(basis + 34, 17).WithArguments("CheckUserArg", "name", "\"p\""),
                VerifyCS.Diagnostic(ContractsCheckAnalyzer.DecodeMessageWithLoadContextDiagnostic.Rule).WithLocation(basis + 39, 41).WithArguments("CheckDecode", "\"This message is suspicious\""),
                new DiagnosticResult("CS0122", DiagnosticSeverity.Error).WithLocation("Test1.cs", 752, 24).WithMessage("'ICancelable' is inaccessible due to its protection level"),
                new DiagnosticResult("CS0122", DiagnosticSeverity.Error).WithLocation("Test1.cs", 752, 67).WithMessage("'ICancelable.IsCanceled' is inaccessible due to its protection level"),
            };

            var test = new VerifyCS.Test
            {
                LanguageVersion = LanguageVersion.CSharp7_2,
                TestState       =
                {
                    Sources              =
                    {
                        Source.Value,
                        SourceContracts.Value,
                        SourceFriend.Value,
                    },
                    AdditionalReferences = { AdditionalMetadataReferences.RefFromType <Memory <int> >() },
                }
            };

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