Example #1
0
        public void NoPackageResultsTest()
        {
            var sourceFileToInvocations = new Dictionary <string, List <CodeEntityDetails> >
            {
                {
                    "file1", new List <CodeEntityDetails>
                    {
                        _codeEntityDetails
                    }
                }
            };

            var project = new ProjectDetails
            {
                PackageReferences = new List <PackageVersionPair>
                {
                    new PackageVersionPair {
                        PackageId = "namespace",
                        Version   = "11.2"
                    }
                }
            };

            var result = InvocationExpressionModelToInvocations.AnalyzeResults(
                sourceFileToInvocations, new Dictionary <PackageVersionPair, Task <PackageDetails> >(), recommendationResults);

            Assert.AreEqual(1, result.First().ApiAnalysisResults.Count);
            Assert.AreEqual("11.0.1", result.First().ApiAnalysisResults.First().CodeEntityDetails.Package.Version);
            Assert.AreEqual(Compatibility.UNKNOWN, result.First().ApiAnalysisResults.First().CompatibilityResults.GetValueOrDefault(ApiCompatiblity.DEFAULT_TARGET).Compatibility);
            Assert.IsNull(result[0].ApiAnalysisResults[0].Recommendations.RecommendedActions.First().Description);;
        }
        public void NormalAnalyzeCaseTest()
        {
            var sourceFileToInvocations = new Dictionary <string, List <CodeEntityDetails> >
            {
                {
                    "file1", new UstList <CodeEntityDetails>
                    {
                        _codeEntityDetails
                    }
                }
            };

            var result = InvocationExpressionModelToInvocations.AnalyzeResults(
                sourceFileToInvocations, packageResults, recommendationResults, new Dictionary <string, List <RecommendedAction> >());

            Assert.AreEqual(1, result.First().ApiAnalysisResults.Count);
            Assert.AreEqual("11.0.1", result.First().ApiAnalysisResults.First().CodeEntityDetails.Package.Version);
            Assert.AreEqual(Compatibility.COMPATIBLE, result.First().ApiAnalysisResults.First().CompatibilityResults.GetValueOrDefault(DEFAULT_TARGET).Compatibility);
            Assert.AreEqual("12.0.3", result[0].ApiAnalysisResults[0].Recommendations.RecommendedActions.First().Description);
        }
Example #3
0
        private ProjectAnalysisResult AnalyzeProject(
            string project, List <AnalyzerResult> analyzers, Dictionary <string, ProjectActions> analysisActions, string targetFramework = "netcoreapp3.1")
        {
            try
            {
                using var analyzer = analyzers.Find((a) => a.ProjectResult?.ProjectFilePath != null &&
                                                    a.ProjectResult.ProjectFilePath.Equals(project));

                var projectActions = new ProjectActions();
                analysisActions.TryGetValue(project, out projectActions);

                if (analyzer == null || analyzer.ProjectResult == null)
                {
                    _logger.LogError("Unable to build {0}.", project);
                    return(null);
                }

                var sourceFileToInvocations = analyzer.ProjectResult.SourceFileResults.Select((sourceFile) =>
                {
                    var invocationsInSourceFile = sourceFile.AllInvocationExpressions();
                    _logger.LogInformation("API: SourceFile {0} has {1} invocations pre-filter", sourceFile.FileFullPath, invocationsInSourceFile.Count());
                    return(KeyValuePair.Create(sourceFile.FileFullPath, invocationsInSourceFile));
                }).ToDictionary(p => p.Key, p => p.Value);

                var sourceFileToCodeEntityDetails = InvocationExpressionModelToInvocations.Convert(sourceFileToInvocations, analyzer);

                var namespaces = sourceFileToCodeEntityDetails.Aggregate(new HashSet <string>(), (agg, cur) =>
                {
                    agg.UnionWith(cur.Value.Select(i => i.Namespace).Where(i => i != null));
                    return(agg);
                });

                var targetframeworks = analyzer.ProjectResult.TargetFrameworks.Count == 0 ?
                                       new List <string> {
                    analyzer.ProjectResult.TargetFramework
                } : analyzer.ProjectResult.TargetFrameworks;

                var nugetPackages = analyzer.ProjectResult.ExternalReferences.NugetReferences
                                    .Select(r => InvocationExpressionModelToInvocations.ReferenceToPackageVersionPair(r))
                                    .ToHashSet();

                var subDependencies = analyzer.ProjectResult.ExternalReferences.NugetDependencies
                                      .Select(r => InvocationExpressionModelToInvocations.ReferenceToPackageVersionPair(r))
                                      .ToHashSet();

                var sdkPackages = namespaces.Select(n => new PackageVersionPair {
                    PackageId = n, Version = "0.0.0", PackageSourceType = PackageSourceType.SDK
                });

                var allPackages = nugetPackages
                                  .Union(subDependencies)
                                  .Union(sdkPackages)
                                  .ToList();

                var packageResults        = _handler.GetNugetPackages(allPackages, null);
                var recommendationResults = _recommendationHandler.GetApiRecommendation(namespaces.ToList());

                var packageAnalysisResults = nugetPackages.Select(package =>
                {
                    var result = PackageCompatibility.IsCompatibleAsync(packageResults.GetValueOrDefault(package, null), package, _logger, targetFramework);
                    var packageAnalysisResult = PackageCompatibility.GetPackageAnalysisResult(result, package, targetFramework);
                    return(new Tuple <PackageVersionPair, Task <PackageAnalysisResult> >(package, packageAnalysisResult));
                }).ToDictionary(t => t.Item1, t => t.Item2);

                var portingActionResults = ProjectActionsToRecommendedActions.Convert(projectActions);

                var SourceFileAnalysisResults = InvocationExpressionModelToInvocations.AnalyzeResults(
                    sourceFileToCodeEntityDetails, packageResults, recommendationResults, portingActionResults, targetFramework);



                return(new ProjectAnalysisResult
                {
                    ProjectName = analyzer.ProjectResult.ProjectName,
                    ProjectFilePath = analyzer.ProjectResult.ProjectFilePath,
                    TargetFrameworks = targetframeworks,
                    PackageReferences = nugetPackages.ToList(),
                    ProjectReferences = analyzer.ProjectResult.ExternalReferences.ProjectReferences.ConvertAll(p => new ProjectReference {
                        ReferencePath = p.AssemblyLocation
                    }),
                    PackageAnalysisResults = packageAnalysisResults,
                    IsBuildFailed = analyzer.ProjectResult.IsBuildFailed(),
                    Errors = analyzer.ProjectResult.BuildErrors,
                    ProjectGuid = analyzer.ProjectResult.ProjectGuid,
                    ProjectType = analyzer.ProjectResult.ProjectType,
                    SourceFileAnalysisResults = SourceFileAnalysisResults
                });
            }
            catch (Exception ex)
            {
                _logger.LogError("Error while analyzing {0}, {1}", project, ex);
                return(new ProjectAnalysisResult
                {
                    ProjectName = Path.GetFileNameWithoutExtension(project),
                    ProjectFilePath = project,
                    TargetFrameworks = new List <string>(),
                    PackageReferences = new List <PackageVersionPair>(),
                    ProjectReferences = new List <ProjectReference>(),
                    PackageAnalysisResults = new Dictionary <PackageVersionPair, Task <PackageAnalysisResult> >(),
                    IsBuildFailed = true,
                    Errors = new List <string> {
                        string.Format("Error while analyzing {0}, {1}", project, ex)
                    },
                    ProjectGuid = null,
                    ProjectType = null,
                    SourceFileAnalysisResults = new List <SourceFileAnalysisResult>()
                });
            }
        }