public void TestPackageCompatibilityResult()
        {
            var versions = new SortedSet <string>
            {
                "1.0.0",
                "1.0.1-beta",
                "2.0.0",
            };
            var packageVersionPair = new PackageVersionPair
            {
                PackageId         = "MyNugetPackage",
                PackageSourceType = PackageSourceType.NUGET,
                Version           = "1.0.0"
            };
            var packageDetails = new PackageDetails
            {
                Name     = "MyNugetPackage",
                Versions = versions,
                Targets  = new Dictionary <string, SortedSet <string> >
                {
                    { "netcoreapp3.1", versions }
                }
            };

            var compatResults  = PackageCompatibility.IsCompatibleAsync(Task.FromResult(packageDetails), packageVersionPair, NullLogger.Instance);
            var recommendation = PackageCompatibility.GetPackageAnalysisResult(compatResults, packageVersionPair, "netcoreapp3.1").Result;

            Assert.AreEqual(2, compatResults.Result.CompatibleVersions.Count);
            Assert.AreEqual(1, recommendation.CompatibilityResults["netcoreapp3.1"].CompatibleVersions.Count);
            Assert.AreEqual("2.0.0", recommendation.Recommendations.RecommendedActions[0].Description);
        }
        public void TestIsCompatibleAsync_ReturnsIncompatible_PackageVersionNotInTargetVersions()
        {
            var versions = new SortedSet <string>
            {
                "4.0.0",
                "4.5.0"
            };
            var packageVersionPair = new PackageVersionPair
            {
                PackageId         = "System.DirectoryServices",
                PackageSourceType = PackageSourceType.NUGET,
                Version           = "5.0.0"
            };
            var packageDetails = new PackageDetails
            {
                Name     = "System.DirectoryServices",
                Versions = versions,
                Targets  = new Dictionary <string, SortedSet <string> >
                {
                    { "net5.0", versions }
                }
            };

            var compatResults = PackageCompatibility.IsCompatibleAsync(Task.FromResult(packageDetails), packageVersionPair, NullLogger.Instance);

            Assert.AreEqual(0, compatResults.Result.CompatibleVersions.Count);
            Assert.AreEqual(Compatibility.INCOMPATIBLE, compatibilityResult.Compatibility);
        }
Esempio n. 3
0
        public void CompatibilityCheckOfMissingExternalPackageThrowsException()
        {
            SetMockHttpService(new PackageDetails());

            var externalPackagesCompatibilityChecker = GetExternalPackagesCompatibilityChecker();

            var packageVersionPair = new PackageVersionPair {
                PackageId = "Newtonsoft.Json", Version = "12.0.5", PackageSourceType = PackageSourceType.NUGET
            };
            var packages = new List <PackageVersionPair>()
            {
                packageVersionPair
            };

            var resultTasks = externalPackagesCompatibilityChecker.CheckAsync(packages, null);

            _loggerMock.Verify(_ => _.Log(
                                   LogLevel.Error,
                                   It.IsAny <EventId>(),
                                   It.IsAny <It.IsValueType>(),
                                   It.IsAny <Exception>(),
                                   (Func <It.IsValueType, Exception, string>)It.IsAny <object>()), Times.Exactly(2));

            Assert.Throws <AggregateException>(() =>
            {
                Task.WaitAll(resultTasks.Values.ToArray());
            });
        }
Esempio n. 4
0
        public void PackageDownloadRequestWithInvalidJsonResponseThrowsException()
        {
            _httpService.Reset();
            _httpService
            .Setup(transfer => transfer.DownloadS3FileAsync(It.IsAny <string>()))
            .Returns(async(string key) =>
            {
                await Task.Delay(1);
                MemoryStream stream = new MemoryStream();
                StreamWriter writer = new StreamWriter(stream);
                writer.Write("INVALID");
                writer.Flush();
                writer.BaseStream.Position = 0;
                return(writer.BaseStream);
            });

            var externalChecker = GetExternalPackagesCompatibilityChecker();

            var packageVersionPair = new PackageVersionPair {
                PackageId = "Newtonsoft.Json", Version = "12.0.5", PackageSourceType = PackageSourceType.NUGET
            };
            var packages = new List <PackageVersionPair>()
            {
                packageVersionPair
            };

            Assert.Throws <AggregateException>(() =>
            {
                var resultTasks = externalChecker.CheckAsync(packages, null);
                Task.WaitAll(resultTasks.Values.ToArray());
            });
        }
Esempio n. 5
0
 private static CodeEntityDetails CreateCodeEntity(string name,
                                                   string @namespace,
                                                   string signature,
                                                   PackageVersionPair package,
                                                   string originalDefinition,
                                                   CodeEntityType codeEntityType,
                                                   UstNode ustNode)
 {
     return(new CodeEntityDetails
     {
         Name = name,
         Namespace = @namespace ?? string.Empty,
         Signature = signature,
         OriginalDefinition = originalDefinition,
         CodeEntityType = codeEntityType,
         TextSpan = new TextSpan
         {
             StartCharPosition = ustNode.TextSpan?.StartCharPosition,
             EndCharPosition = ustNode.TextSpan?.EndCharPosition,
             StartLinePosition = ustNode.TextSpan?.StartLinePosition,
             EndLinePosition = ustNode.TextSpan?.EndLinePosition
         },
         // If we found an matching sdk assembly, assume the code is using the sdk.
         Package = package ?? new PackageVersionPair()
         {
             PackageId = "", Version = ""
         }
     });
 }
Esempio n. 6
0
        public void InternalPackagesLoggerLogsExceptions()
        {
            var checker      = _internalPackagesCompatibilityChecker.Object;
            var repositories = GetInternalRepositoryThrowsException(new OperationCanceledException());

            var packageVersionPair = new PackageVersionPair {
                PackageId = "Newtonsoft.Json", Version = "12.0.5"
            };
            var packages = new List <PackageVersionPair>()
            {
                packageVersionPair
            };
            var result = checker.GetInternalPackagesAsync(packages, repositories);

            _loggerMock.Verify(_ => _.Log(
                                   LogLevel.Information,
                                   It.IsAny <EventId>(),
                                   It.IsAny <It.IsValueType>(),
                                   It.IsAny <Exception>(),
                                   (Func <It.IsValueType, Exception, string>)It.IsAny <object>()), Times.Once);

            repositories = GetInternalRepositoryThrowsException(new AggregateException());
            result       = checker.GetInternalPackagesAsync(packages, repositories);

            _loggerMock.Verify(_ => _.Log(
                                   LogLevel.Error,
                                   It.IsAny <EventId>(),
                                   It.IsAny <It.IsValueType>(),
                                   It.IsAny <Exception>(),
                                   (Func <It.IsValueType, Exception, string>)It.IsAny <object>()), Times.Exactly(2));
        }
Esempio n. 7
0
        private static PackageVersionPair GetPackageVersionPair(Reference reference, ExternalReferences externalReferences)
        {
            var assemblyLength = reference?.Assembly?.Length;

            if (assemblyLength == null || assemblyLength == 0)
            {
                return(null);
            }

            // Check if code entity is from Nuget
            var potentialNugetPackage = externalReferences?.NugetReferences?.Find((n) =>
                                                                                  n.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || n.Identity.Equals(reference.Assembly));

            if (potentialNugetPackage == null)
            {
                potentialNugetPackage = externalReferences?.NugetDependencies?.Find((n) =>
                                                                                    n.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || n.Identity.Equals(reference.Assembly));
            }
            PackageVersionPair nugetPackage = ReferenceToPackageVersionPair(potentialNugetPackage);

            // Check if code entity is from SDK
            var potentialSdk = externalReferences?.SdkReferences?.Find((s) =>
                                                                       s.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || s.Identity.Equals(reference.Assembly));
            PackageVersionPair sdk = ReferenceToPackageVersionPair(potentialSdk, PackageSourceType.SDK);

            return(sdk ?? nugetPackage);
        }
        public void SetUp()
        {
            _handler.Reset();
            _handler.Setup(handler => handler.GetNugetPackages(It.IsAny <List <PackageVersionPair> >(), ""))
            .Returns((List <PackageVersionPair> packages, string path) =>
            {
                var task = new TaskCompletionSource <PackageDetails>();
                task.SetResult(_packageDetails);
                return(new Dictionary <PackageVersionPair, Task <PackageDetails> > {
                    { packages.First(), task.Task }
                });
            });
            var package = new PackageVersionPair
            {
                PackageId = "Newtonsoft.Json",
                Version   = "11.0.1"
            };

            var packageTask        = new TaskCompletionSource <PackageDetails>();
            var recommendationTask = new TaskCompletionSource <RecommendationDetails>();

            packageTask.SetResult(_packageDetails);
            recommendationTask.SetResult(new RecommendationDetails());

            packageResults = new Dictionary <PackageVersionPair, Task <PackageDetails> >
            {
                { package, packageTask.Task }
            };

            recommendationResults = new Dictionary <string, Task <RecommendationDetails> >
            {
                { "Newtonsoft.Json", recommendationTask.Task }
            };
        }
        public void ApiAnalysis_Returns_Compatible_When_ApiVersion_Matches_A_CompatibleVersion()
        {
            var sourceFileToInvocations = new Dictionary <string, List <CodeEntityDetails> >
            {
                {
                    "file1", new UstList <CodeEntityDetails>
                    {
                        new CodeEntityDetails
                        {
                            Name = "JsonConvert.SerializeObject",
                            OriginalDefinition = "Newtonsoft.Json.JsonConvert.SerializeObject(object)",
                            Namespace          = "Newtonsoft.Json",
                            Package            = new PackageVersionPair
                            {
                                PackageId = "Newtonsoft.Json",
                                Version   = "10.2.0"
                            },
                            TextSpan       = new TextSpan(),
                            CodeEntityType = CodeEntityType.Method
                        }
                    }
                }
            };

            var package = new PackageVersionPair
            {
                PackageId = "Newtonsoft.Json",
                Version   = "10.2.0"
            };

            var packageTask = new TaskCompletionSource <PackageDetails>();

            packageTask.SetResult(_packageDetails);
            _packageResults = new Dictionary <PackageVersionPair, Task <PackageDetails> >
            {
                { package, packageTask.Task }
            };

            var results = CodeEntityModelToCodeEntities.AnalyzeResults(
                sourceFileToInvocations, _packageResults, _recommendationResults, new Dictionary <string, List <RecommendedAction> >());

            var apiAnalysisResults = results.First().ApiAnalysisResults;

            Assert.AreEqual("10.2.0", apiAnalysisResults.First().CodeEntityDetails.Package.Version);
            Assert.AreEqual(Compatibility.COMPATIBLE, apiAnalysisResults.First().CompatibilityResults.GetValueOrDefault(DEFAULT_TARGET).Compatibility);
            Assert.AreEqual("12.0.3", apiAnalysisResults[0].Recommendations.RecommendedActions.First().Description);
        }
Esempio n. 10
0
        public void CompatibilityCheckOfMissingExternalPackageThrowsException()
        {
            SetMockHttpService(new PackageDetails());

            var externalPackagesCompatibilityChecker = GetExternalPackagesCompatibilityChecker();

            var packageVersionPair = new PackageVersionPair {
                PackageId = "Newtonsoft.Json", Version = "12.0.5", PackageSourceType = PackageSourceType.NUGET
            };
            var packages = new List <PackageVersionPair>()
            {
                packageVersionPair
            };

            var resultTasks = externalPackagesCompatibilityChecker.Check(packages, null);

            Assert.Throws <AggregateException>(() =>
            {
                Task.WaitAll(resultTasks.Values.ToArray());
            });
        }
Esempio n. 11
0
        private static PackageVersionPair GetPackageVersionPair(Reference reference, ExternalReferences externalReferences, string @namespace)
        {
            var assemblyLength = reference?.Assembly?.Length;

            if (assemblyLength == null || assemblyLength == 0)
            {
                return(null);
            }

            // Check if code entity is from Nuget
            var potentialNugetPackage = externalReferences?.NugetReferences?.Find((n) =>
                                                                                  n.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || n.Identity.Equals(reference.Assembly));

            if (potentialNugetPackage == null)
            {
                potentialNugetPackage = externalReferences?.NugetDependencies?.Find((n) =>
                                                                                    n.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || n.Identity.Equals(reference.Assembly));
            }
            PackageVersionPair nugetPackage = ReferenceToPackageVersionPair(potentialNugetPackage);

            // Check if code entity is from SDK
            var potentialSdk = externalReferences?.SdkReferences?.Find((s) =>
                                                                       s.AssemblyLocation?.EndsWith(reference.Assembly + ".dll") == true || s.Identity.Equals(reference.Assembly));
            PackageVersionPair sdk = ReferenceToPackageVersionPair(potentialSdk, PackageSourceType.SDK);

            // if mscorlib, try to match namespace to sdk
            if (string.Compare(reference.Assembly, "mscorlib", StringComparison.OrdinalIgnoreCase) == 0)
            {
                var potential = externalReferences?.SdkReferences?.Find(s =>
                                                                        s.AssemblyLocation?.EndsWith($"{@namespace}.dll") == true ||
                                                                        s.Identity.Equals(@namespace));
                if (potential != null)
                {
                    potential.Version = "0.0.0"; // SDK lookups will have no version number
                    sdk = ReferenceToPackageVersionPair(potential, PackageSourceType.SDK);
                }
            }

            return(sdk ?? nugetPackage);
        }
Esempio n. 12
0
        public void AnalyzeWellDefinedSolutionSucceeds()
        {
            var package = new PackageVersionPair
            {
                PackageId         = "Newtonsoft.Json",
                Version           = "11.0.1",
                PackageSourceType = PackageSourceType.NUGET
            };
            var result = _analysisHandler.AnalyzeSolution(_solutionFile, _projectPaths);

            Task.WaitAll(result);

            var projectAnalysisResult = result.Result.Values.First();

            Task.WaitAll(projectAnalysisResult.PackageAnalysisResults.Values.ToArray());
            var packageAnalysisResult = projectAnalysisResult.PackageAnalysisResults.First(p => p.Key.PackageId == "Newtonsoft.Json").Value.Result;

            Assert.AreEqual(package, packageAnalysisResult.PackageVersionPair);
            Assert.AreEqual(Compatibility.INCOMPATIBLE, packageAnalysisResult.CompatibilityResults.GetValueOrDefault(DEFAULT_TARGET).Compatibility);
            Assert.AreEqual("12.0.3", packageAnalysisResult.CompatibilityResults.GetValueOrDefault(DEFAULT_TARGET).CompatibleVersions.First());
            Assert.AreEqual("12.0.3", packageAnalysisResult.Recommendations.RecommendedActions.First().Description);
            Assert.AreEqual(RecommendedActionType.UpgradePackage, packageAnalysisResult.Recommendations.RecommendedActions.First().RecommendedActionType);

            var sourceFile = projectAnalysisResult.SourceFileAnalysisResults.Find(s => s.SourceFileName == "Program.cs");

            Assert.NotNull(sourceFile);
            Assert.NotNull(sourceFile.ApiAnalysisResults);

            var apiAnalysisResult = sourceFile.ApiAnalysisResults.Find(r => r.CodeEntityDetails.OriginalDefinition == "Newtonsoft.Json.JsonConvert.SerializeObject(object)");

            Assert.NotNull(apiAnalysisResult);

            Assert.AreEqual("Newtonsoft.Json", apiAnalysisResult.CodeEntityDetails.Package.PackageId);
            Assert.AreEqual("11.0.1", apiAnalysisResult.CodeEntityDetails.Package.Version);
            Assert.AreEqual("Newtonsoft.Json.JsonConvert.SerializeObject(object)",
                            apiAnalysisResult.CodeEntityDetails.OriginalDefinition);
            Assert.AreEqual(Compatibility.COMPATIBLE, apiAnalysisResult.CompatibilityResults.GetValueOrDefault(DEFAULT_TARGET).Compatibility);
            Assert.AreEqual("12.0.3", apiAnalysisResult.Recommendations.RecommendedActions.First().Description);
        }
Esempio n. 13
0
        public void CompatibilityCheckOfInternalPackageThrowsExceptionDoesNotRecordTargets()
        {
            _internalNuGetCompatibilityHandlerMock.Reset();
            _internalNuGetCompatibilityHandlerMock
            .Setup(checker => checker.CheckCompatibilityAsync(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <IEnumerable <SourceRepository> >()))
            .Throws(new OperationCanceledException());

            var packageVersionPair = new PackageVersionPair {
                PackageId = "Newtonsoft.Json", Version = "12.0.5"
            };
            var packages = new List <PackageVersionPair>()
            {
                packageVersionPair
            };
            var result = _internalPackagesCompatibilityChecker.Object.CheckAsync(packages, Path.Combine(_testSolutionDirectory, "SolutionWithNugetConfigFile.sln"));

            Task.WaitAll(result.Values.ToArray());
            Assert.AreEqual(0, result.Values.ToList().First().Result.Targets.GetValueOrDefault("netcoreapp3.1").Count);

            _internalNuGetCompatibilityHandlerMock.Reset();
            _internalNuGetCompatibilityHandlerMock
            .Setup(checker => checker.CheckCompatibilityAsync(
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <string>(),
                       It.IsAny <IEnumerable <SourceRepository> >()))
            .Throws(new AggregateException());

            result = _internalPackagesCompatibilityChecker.Object.CheckAsync(packages, Path.Combine(_testSolutionDirectory, "SolutionWithNugetConfigFile.sln"));

            Task.WaitAll(result.Values.ToArray());
            Assert.AreEqual(0, result.Values.First().Result.Targets.GetValueOrDefault("netcoreapp3.1").Count);
        }
Esempio n. 14
0
 private async Task <InternalNuGetCompatibilityResult> ProcessCompatibility(
     PackageVersionPair packageVersion,
     IEnumerable <SourceRepository> internalRepositories)
 {
     try
     {
         return(await _internalNuGetCompatibilityHandler.CheckCompatibilityAsync(
                    packageVersion.PackageId,
                    packageVersion.Version,
                    "netcoreapp3.1",
                    internalRepositories));
     }
     catch (Exception ex) when(ex is PortingAssistantClientException)
     {
         _logger.LogInformation($"Could not check compatibility for package {packageVersion} " +
                                $"using internal resources. Error: {ex.Message}");
     }
     catch (Exception ex)
     {
         _logger.LogError($"Unexpected error encountered when checking compatibility of package {packageVersion} " +
                          $"using internal source(s): {ex}");
     }
     return(null);
 }
        public static async Task <PackageAnalysisResult> GetPackageAnalysisResult(Task <CompatibilityResult> CompatibilityResult, PackageVersionPair packageVersionPair, string targetFramework)
        {
            var result = await CompatibilityResult;

            return(new PackageAnalysisResult
            {
                PackageVersionPair = packageVersionPair,
                CompatibilityResults = new Dictionary <string, CompatibilityResult>
                {
                    {
                        targetFramework, new CompatibilityResult
                        {
                            Compatibility = result.Compatibility,
                            CompatibleVersions = result.CompatibleVersions
                        }
                    }
                },
                Recommendations = new Recommendations
                {
                    RecommendedActions = new List <RecommendedAction>
                    {
                        new PackageRecommendation
                        {
                            PackageId = packageVersionPair.PackageId,
                            RecommendedActionType = RecommendedActionType.UpgradePackage,
                            Description = result.CompatibleVersions.Count != 0 ? result.CompatibleVersions.First() : null
                        }
                    }
                }
            });
        }
Esempio n. 16
0
        public static List <SourceFileAnalysisResult> AnalyzeResults(
            Dictionary <string, List <CodeEntityDetails> > sourceFileToCodeEntities,
            Dictionary <PackageVersionPair, Task <PackageDetails> > packageResults,
            Dictionary <string, Task <RecommendationDetails> > recommendationResults,
            Dictionary <string, List <RecommendedAction> > portingActionResults,
            string targetFramework = "net6.0",
            bool compatibleOnly    = false
            )
        {
            var packageDetailsWithIndicesResults = ApiCompatiblity.PreProcessPackageDetails(packageResults);

            return(sourceFileToCodeEntities.Select(sourceFile =>
            {
                return new SourceFileAnalysisResult
                {
                    SourceFileName = Path.GetFileName(sourceFile.Key),
                    SourceFilePath = sourceFile.Key,
                    RecommendedActions = portingActionResults?.GetValueOrDefault(sourceFile.Key, new List <RecommendedAction>()),
                    ApiAnalysisResults = sourceFile.Value.Select(codeEntity =>
                    {
                        var package = codeEntity.Package;

                        //A code entity with no reference data. This can be any error in the code
                        if (package == null)
                        {
                            return new ApiAnalysisResult
                            {
                                CodeEntityDetails = codeEntity,
                                CompatibilityResults = new Dictionary <string, CompatibilityResult>
                                {
                                    {
                                        targetFramework, new CompatibilityResult()
                                        {
                                            Compatibility = Compatibility.UNKNOWN
                                        }
                                    }
                                },
                                Recommendations = new Model.Recommendations
                                {
                                    RecommendedActions = new List <RecommendedAction>()
                                }
                            };
                        }
                        var sdkpackage = new PackageVersionPair {
                            PackageId = codeEntity.Namespace, Version = "0.0.0", PackageSourceType = PackageSourceType.SDK
                        };

                        // check result with nuget package
                        var packageDetails = packageDetailsWithIndicesResults.GetValueOrDefault(package, null);
                        var compatibilityResultWithPackage = ApiCompatiblity.GetCompatibilityResult(packageDetails, codeEntity, targetFramework);

                        // potential check with namespace
                        var sdkpackageDetails = packageDetailsWithIndicesResults.GetValueOrDefault(sdkpackage, null);
                        var compatibilityResultWithSdk = ApiCompatiblity.GetCompatibilityResult(sdkpackageDetails, codeEntity, targetFramework);
                        var compatibilityResult = GetCompatibilityResult(compatibilityResultWithPackage, compatibilityResultWithSdk);

                        if (compatibleOnly)
                        {
                            if (!(compatibilityResult.Compatibility == Compatibility.INCOMPATIBLE))
                            {
                                return null;
                            }
                        }

                        var recommendationDetails = recommendationResults.GetValueOrDefault(codeEntity.Namespace, null);
                        var apiRecommendation = ApiCompatiblity.UpgradeStrategy(compatibilityResult, codeEntity.OriginalDefinition, recommendationDetails, targetFramework);

                        return new ApiAnalysisResult
                        {
                            CodeEntityDetails = codeEntity,
                            CompatibilityResults = new Dictionary <string, CompatibilityResult>
                            {
                                { targetFramework, compatibilityResult }
                            },
                            Recommendations = new Model.Recommendations
                            {
                                RecommendedActions = new List <RecommendedAction>
                                {
                                    apiRecommendation
                                }
                            }
                        };
                    }).Where(codeEntity => codeEntity != null)
                                         .ToList()
                };
            }
                                                   ).ToList());
        }
Esempio n. 17
0
        public static async Task <PackageAnalysisResult> GetPackageAnalysisResult(Task <CompatibilityResult> CompatibilityResult, PackageVersionPair packageVersionPair, string targetFramework)
        {
            var result             = await CompatibilityResult;
            var compatibleVersions = result.GetCompatibleVersionsWithoutPreReleases();

            return(new PackageAnalysisResult
            {
                PackageVersionPair = packageVersionPair,
                CompatibilityResults = new Dictionary <string, CompatibilityResult>
                {
                    {
                        targetFramework, new CompatibilityResult
                        {
                            Compatibility = result.Compatibility,
                            CompatibleVersions = compatibleVersions
                        }
                    }
                },
                Recommendations = new PortingAssistant.Client.Model.Recommendations
                {
                    RecommendedActions = new List <PortingAssistant.Client.Model.RecommendedAction>
                    {
                        new PackageRecommendation
                        {
                            PackageId = packageVersionPair.PackageId,
                            RecommendedActionType = RecommendedActionType.UpgradePackage,
                            Description = compatibleVersions.Count != 0 ? compatibleVersions.First() : null,
                            TargetVersions = compatibleVersions
                        }
                    }
                }
            });
        }
Esempio n. 18
0
        public static async Task <CompatibilityResult> IsCompatibleAsync(Task <PackageDetails> packageDetails, PackageVersionPair packageVersionPair, ILogger _logger, string target = "netcoreapp3.1")
        {
            if (packageDetails == null || packageVersionPair == null)
            {
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }

            try
            {
                await packageDetails;
                if (!packageDetails.IsCompletedSuccessfully)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var foundTarget = packageDetails.Result.Targets.GetValueOrDefault(target, null);
                if (foundTarget == null)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.INCOMPATIBLE,
                        CompatibleVersions = new List <string>()
                    });
                }

                if (!NuGetVersion.TryParse(packageVersionPair.Version, out var version))
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersions = foundTarget.Where(v =>
                {
                    if (!NuGetVersion.TryParse(v, out var semversion))
                    {
                        return(false);
                    }
                    return(semversion.CompareTo(version) > 0);
                }).ToList();

                var compatibility = foundTarget.Any(v =>
                {
                    if (!NuGetVersion.TryParse(v, out var semversion))
                    {
                        return(false);
                    }

                    return(version.CompareTo(semversion) == 0);
                })
                    ? Compatibility.COMPATIBLE
                    : Compatibility.INCOMPATIBLE;

                compatibleVersions.Sort((a, b) => NuGetVersion.Parse(a).CompareTo(NuGetVersion.Parse(b)));
                return(new CompatibilityResult
                {
                    Compatibility = compatibility,
                    CompatibleVersions = compatibleVersions
                });
            }
            catch (OutOfMemoryException e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                MemoryUtils.LogMemoryConsumption(_logger);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
            catch (Exception e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
        }
Esempio n. 19
0
        public static List <SourceFileAnalysisResult> AnalyzeResults(
            Dictionary <string, List <CodeEntityDetails> > sourceFileToInvocations,
            Dictionary <PackageVersionPair, Task <PackageDetails> > packageResults,
            Dictionary <string, Task <RecommendationDetails> > recommendationResults
            )
        {
            var packageDetailsWithIndicesResults = ApiCompatiblity.PreProcessPackageDetails(packageResults);

            return(sourceFileToInvocations.Select(sourceFile =>
            {
                return new SourceFileAnalysisResult
                {
                    SourceFileName = Path.GetFileName(sourceFile.Key),
                    SourceFilePath = sourceFile.Key,
                    ApiAnalysisResults = sourceFile.Value.Select(invocation =>
                    {
                        var package = invocation.Package;
                        var sdkpackage = new PackageVersionPair {
                            PackageId = invocation.Namespace, Version = "0.0.0", PackageSourceType = PackageSourceType.SDK
                        };

                        // check result with nuget package
                        var packageDetails = packageDetailsWithIndicesResults.GetValueOrDefault(package, null);
                        var compatibilityResultWithPackage = ApiCompatiblity.GetCompatibilityResult(packageDetails,
                                                                                                    invocation.OriginalDefinition,
                                                                                                    invocation.Package.Version);

                        // potential check with namespace
                        var sdkpackageDetails = packageDetailsWithIndicesResults.GetValueOrDefault(sdkpackage, null);
                        var compatibilityResultWithSdk = ApiCompatiblity.GetCompatibilityResult(sdkpackageDetails,
                                                                                                invocation.OriginalDefinition,
                                                                                                invocation.Package.Version);

                        var compatibilityResult = GetCompatibilityResult(compatibilityResultWithPackage, compatibilityResultWithSdk);

                        var recommendationDetails = recommendationResults.GetValueOrDefault(invocation.Namespace, null);
                        var apiRecommendation = ApiCompatiblity.UpgradeStrategy(
                            compatibilityResult,
                            invocation.OriginalDefinition,
                            invocation.Namespace,
                            recommendationDetails);


                        return new ApiAnalysisResult
                        {
                            CodeEntityDetails = invocation,
                            CompatibilityResults = new Dictionary <string, CompatibilityResult>
                            {
                                { ApiCompatiblity.DEFAULT_TARGET, compatibilityResult }
                            },
                            Recommendations = new Recommendations
                            {
                                RecommendedActions = new List <RecommendedAction>
                                {
                                    apiRecommendation
                                }
                            }
                        };
                    }).Where(invocation => invocation != null)
                                         .ToList()
                };
            }
                                                  ).ToList());
        }
Esempio n. 20
0
        public void SetUp()
        {
            var solutionDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestXml", "TestPorting");

            _tmpDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestXml", "TmpDirectory");
            DirectoryCopy(solutionDirectory, _tmpDirectory, true);

            _tmpSolutionDirectory = Path.Combine(_tmpDirectory, "src");
            _tmpProjectPath       = Path.Combine(_tmpSolutionDirectory, "Libraries", "Nop.Core", "Nop.Core.csproj");

            _apiAnalysisHandlerMock.Reset();
            _apiAnalysisHandlerMock.Setup(analyzer => analyzer.AnalyzeSolution(It.IsAny <string>(), It.IsAny <List <string> >()))
            .Returns((string solutionFilePath, List <string> projects) =>
            {
                return(Task.Run(() => projects.Select(project =>
                {
                    var package = new PackageVersionPair
                    {
                        PackageId = "Newtonsoft.Json",
                        Version = "11.0.1"
                    };
                    var packageAnalysisResult = Task.Run(() => new PackageAnalysisResult
                    {
                        PackageVersionPair = package,
                        CompatibilityResults = new Dictionary <string, CompatibilityResult>
                        {
                            { "netcoreapp3.1", new CompatibilityResult {
                                  Compatibility = Compatibility.COMPATIBLE,
                                  CompatibleVersions = new List <string>
                                  {
                                      "12.0.3", "12.0.4"
                                  }
                              } }
                        },
                        Recommendations = new Recommendations
                        {
                            RecommendedActions = new List <RecommendedAction>
                            {
                                new RecommendedAction
                                {
                                    RecommendedActionType = RecommendedActionType.UpgradePackage,
                                    Description = "12.0.3"
                                }
                            }
                        }
                    });

                    var projectAnalysisResult = new ProjectAnalysisResult
                    {
                        ProjectName = Path.GetFileNameWithoutExtension(project),
                        ProjectFilePath = project,
                        PackageAnalysisResults = new Dictionary <PackageVersionPair, Task <PackageAnalysisResult> >
                        {
                            { package, packageAnalysisResult }
                        },
                        SourceFileAnalysisResults = new List <SourceFileAnalysisResult>
                        {
                            _sourceFileAnalysisResult
                        },
                        ProjectGuid = "xxx",
                        ProjectType = SolutionProjectType.KnownToBeMSBuildFormat.ToString()
                    };

                    return new KeyValuePair <string, ProjectAnalysisResult>(project, projectAnalysisResult);
                }).ToDictionary(k => k.Key, v => v.Value)));
            });
        }
Esempio n. 21
0
        public static Dictionary <string, List <CodeEntityDetails> > Convert(
            Dictionary <string, UstList <InvocationExpression> > sourceFileToInvocations,
            AnalyzerResult analyzer)
        {
            return(sourceFileToInvocations.Select(sourceFile =>
                                                  KeyValuePair.Create(
                                                      sourceFile.Key,
                                                      sourceFile.Value.Select(invocation =>
            {
                var assemblyLength = invocation.Reference?.Assembly?.Length;
                if (assemblyLength == null || assemblyLength == 0)
                {
                    return null;
                }

                // Check if invocation is from Nuget
                var potentialNugetPackage = analyzer?.ProjectResult?.ExternalReferences?.NugetReferences?.Find((n) =>
                                                                                                               n.AssemblyLocation != null && n.AssemblyLocation.EndsWith(invocation.Reference.Assembly + ".dll"));

                if (potentialNugetPackage == null)
                {
                    potentialNugetPackage = analyzer?.ProjectResult?.ExternalReferences?.NugetDependencies?.Find((n) =>
                                                                                                                 n.AssemblyLocation != null && n.AssemblyLocation.EndsWith(invocation.Reference.Assembly + ".dll"));
                }
                PackageVersionPair nugetPackage = ReferenceToPackageVersionPair(potentialNugetPackage);

                // Check if invocation is from SDK
                var potentialSdk = analyzer?.ProjectResult?.ExternalReferences?.SdkReferences?.Find((s) =>
                                                                                                    s.AssemblyLocation != null && s.AssemblyLocation.EndsWith(invocation.Reference.Assembly + ".dll"));
                PackageVersionPair sdk = ReferenceToPackageVersionPair(potentialSdk, PackageSourceType.SDK);

                // If both nuget package and sdk are null, this invocation is from an internal project. Skip it.
                if (nugetPackage == null && sdk == null)
                {
                    return null;
                }

                // Otherwise return the invocation
                return new CodeEntityDetails
                {
                    Name = invocation.MethodName,
                    Namespace = invocation.SemanticNamespace,
                    Signature = invocation.SemanticMethodSignature,
                    OriginalDefinition = invocation.SemanticOriginalDefinition,
                    TextSpan = new TextSpan
                    {
                        StartCharPosition = invocation.TextSpan?.StartCharPosition,
                        EndCharPosition = invocation.TextSpan?.EndCharPosition,
                        StartLinePosition = invocation.TextSpan?.StartLinePosition,
                        EndLinePosition = invocation.TextSpan?.EndLinePosition
                    },
                    // If we found an matching sdk assembly, assume the code is using the sdk.
                    Package = sdk ?? nugetPackage,
                };
            })
                                                      .Where(invocation => invocation != null)
                                                      .ToList()
                                                      )
                                                  )
                   .Where(p => p.Value.Count != 0)
                   .ToDictionary(p => p.Key, p => p.Value));
        }
        public static async Task <CompatibilityResult> IsCompatibleAsync(Task <PackageDetails> packageDetails, PackageVersionPair packageVersionPair, ILogger _logger, string target = "netcoreapp3.1")
        {
            if (packageDetails == null || packageVersionPair == null)
            {
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }

            try
            {
                await packageDetails;
                if (!packageDetails.IsCompletedSuccessfully)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var foundTarget = packageDetails.Result.Targets.GetValueOrDefault(target, null);
                if (foundTarget == null)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.INCOMPATIBLE,
                        CompatibleVersions = new List <string>()
                    });
                }

                if (!NuGetVersion.TryParse(packageVersionPair.Version, out var version))
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersions = foundTarget.Where(v =>
                {
                    if (!NuGetVersion.TryParse(v, out var semversion))
                    {
                        return(false);
                    }
                    return(semversion.CompareTo(version) > 0);
                }).ToList();

                compatibleVersions.Sort((a, b) => NuGetVersion.Parse(a).CompareTo(NuGetVersion.Parse(b)));
                return(new CompatibilityResult
                {
                    Compatibility = foundTarget.Any(v =>
                    {
                        if (!NuGetVersion.TryParse(v, out var semversion))
                        {
                            return false;
                        }

                        return version.CompareTo(semversion) >= 0;
                    }) ? Compatibility.COMPATIBLE : Compatibility.INCOMPATIBLE,
                    CompatibleVersions = compatibleVersions
                });
            }
Esempio n. 23
0
        public void SetUp()
        {
            var solutionDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestXml", "TestPorting");

            _tmpDirectory = Path.Combine(TestContext.CurrentContext.TestDirectory, "TestXml", "TmpDirectory");
            DirectoryCopy(solutionDirectory, _tmpDirectory, true);

            _tmpSolutionDirectory = Path.Combine(_tmpDirectory, "src");
            _tmpSolutionFileName  = Path.Combine(_tmpSolutionDirectory, "NopCommerce.sln");
            _tmpProjectPath       = Path.Combine(_tmpSolutionDirectory, "Libraries", "Nop.Core", "Nop.Core.csproj");

            _apiAnalysisHandlerMock.Reset();
            _apiAnalysisHandlerMock.Setup(analyzer => analyzer.AnalyzeSolution(It.IsAny <string>(), It.IsAny <List <string> >(), It.IsAny <string>()))
            .Returns((string solutionFilePath, List <string> projects, string targetFramework) =>
            {
                return(Task.Run(() => projects.Select(project =>
                {
                    var package = new PackageVersionPair
                    {
                        PackageId = "Newtonsoft.Json",
                        Version = "11.0.1"
                    };
                    var packageAnalysisResult = Task.Run(() => new PackageAnalysisResult
                    {
                        PackageVersionPair = package,
                        CompatibilityResults = new Dictionary <string, CompatibilityResult>
                        {
                            { targetFramework, new CompatibilityResult {
                                  Compatibility = Compatibility.COMPATIBLE,
                                  CompatibleVersions = new List <string>
                                  {
                                      "12.0.3", "12.0.4"
                                  }
                              } }
                        },
                        Recommendations = new PortingAssistant.Client.Model.Recommendations
                        {
                            RecommendedActions = new List <RecommendedAction>
                            {
                                new RecommendedAction
                                {
                                    RecommendedActionType = RecommendedActionType.UpgradePackage,
                                    Description = "12.0.3"
                                }
                            }
                        }
                    });

                    var projectAnalysisResult = new ProjectAnalysisResult
                    {
                        ProjectName = Path.GetFileNameWithoutExtension(project),
                        ProjectFilePath = project,
                        PackageAnalysisResults = new Dictionary <PackageVersionPair, Task <PackageAnalysisResult> >
                        {
                            { package, packageAnalysisResult }
                        },
                        SourceFileAnalysisResults = new List <SourceFileAnalysisResult>
                        {
                            _sourceFileAnalysisResult
                        },
                        ProjectGuid = "xxx",
                        ProjectType = nameof(SolutionProjectType.KnownToBeMSBuildFormat),
                        PreportMetaReferences = new List <string> {
                        },
                        MetaReferences = new List <string> {
                        },
                        ExternalReferences = null,
                        ProjectRules = null
                    };

                    return new KeyValuePair <string, ProjectAnalysisResult>(project, projectAnalysisResult);
                }).ToDictionary(k => k.Key, v => v.Value)));
            });
            _apiAnalysisHandlerMock.Setup(analyzer => analyzer.AnalyzeSolutionIncremental(It.IsAny <string>(), It.IsAny <List <string> >(), It.IsAny <string>()))
            .Returns((string solutionFilePath, List <string> projects, string targetFramework) =>
            {
                return(Task.Run(() =>
                {
                    return projects.Select(project =>
                    {
                        var package = new PackageVersionPair
                        {
                            PackageId = "Newtonsoft.Json",
                            Version = "11.0.1"
                        };
                        var packageAnalysisResult = Task.Run(() => new PackageAnalysisResult
                        {
                            PackageVersionPair = package,
                            CompatibilityResults = new Dictionary <string, CompatibilityResult>
                            {
                                { targetFramework, new CompatibilityResult {
                                      Compatibility = Compatibility.COMPATIBLE,
                                      CompatibleVersions = new List <string>
                                      {
                                          "12.0.3", "12.0.4"
                                      }
                                  } }
                            },
                            Recommendations = new PortingAssistant.Client.Model.Recommendations
                            {
                                RecommendedActions = new List <RecommendedAction>
                                {
                                    new RecommendedAction
                                    {
                                        RecommendedActionType = RecommendedActionType.UpgradePackage,
                                        Description = "12.0.3"
                                    }
                                }
                            }
                        });

                        var projectAnalysisResult = new ProjectAnalysisResult
                        {
                            ProjectName = Path.GetFileNameWithoutExtension(project),
                            ProjectFilePath = project,
                            PackageAnalysisResults = new Dictionary <PackageVersionPair, Task <PackageAnalysisResult> >
                            {
                                { package, packageAnalysisResult }
                            },
                            SourceFileAnalysisResults = new List <SourceFileAnalysisResult>
                            {
                                _sourceFileAnalysisResult
                            },
                            ProjectGuid = "xxx",
                            ProjectType = nameof(SolutionProjectType.KnownToBeMSBuildFormat)
                        };

                        return new KeyValuePair <string, ProjectAnalysisResult>(project, projectAnalysisResult);
                    }).ToDictionary(k => k.Key, v => v.Value);
                }));
            });
            _apiAnalysisHandlerMock.Setup(analyzer => analyzer.AnalyzeFileIncremental(It.IsAny <string>(), It.IsAny <string>(), It.IsAny <string>(), It.IsAny <List <string> >(),
                                                                                      It.IsAny <List <string> >(), It.IsAny <RootNodes>(), It.IsAny <Codelyzer.Analysis.Model.ExternalReferences>(),
                                                                                      It.IsAny <bool>(),
                                                                                      It.IsAny <bool>(),
                                                                                      It.IsAny <string>()))
            .Returns((string filePath, string project, string solutionPath, List <string> preportReferences,
                      List <string> metaReferences, RootNodes projectRules, Codelyzer.Analysis.Model.ExternalReferences externalReferences, bool actionsOnly, bool compatibleOnly, string targetFramework) =>
            {
                return(Task.Run(() =>
                {
                    return new List <SourceFileAnalysisResult> {
                        _sourceFileAnalysisResult
                    };
                }));
            });
            _apiAnalysisHandlerMock.Setup(analyzer => analyzer.AnalyzeFileIncremental(It.IsAny <string>(), It.IsAny <string>(),
                                                                                      It.IsAny <string>(), It.IsAny <string>(), It.IsAny <List <string> >(), It.IsAny <List <string> >(), It.IsAny <RootNodes>(), It.IsAny <Codelyzer.Analysis.Model.ExternalReferences>(),
                                                                                      It.IsAny <bool>(),
                                                                                      It.IsAny <bool>(),
                                                                                      It.IsAny <string>()))
            .Returns((string filePath, string fileContents, string project, string solutionPath, List <string> preportReferences,
                      List <string> metaReferences, RootNodes projectRules, Codelyzer.Analysis.Model.ExternalReferences externalReferences, bool actionsOnly, bool compatibleOnly, string targetFramework) =>
            {
                return(Task.Run(() =>
                {
                    return new List <SourceFileAnalysisResult> {
                        _sourceFileAnalysisResult
                    };
                }));
            });
        }
Esempio n. 24
0
        public static async Task <CompatibilityResult> IsCompatibleAsync(Task <PackageDetails> packageDetails, PackageVersionPair packageVersionPair, ILogger _logger, string target = "net6.0")
        {
            if (packageDetails == null || packageVersionPair == null)
            {
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }

            try
            {
                await packageDetails;
                if (!packageDetails.IsCompletedSuccessfully)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersionsForTargetFramework = packageDetails.Result.Targets.GetValueOrDefault(target, null);
                if (compatibleVersionsForTargetFramework == null)
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.INCOMPATIBLE,
                        CompatibleVersions = new List <string>()
                    });
                }

                if (!NuGetVersion.TryParse(packageVersionPair.Version, out var version))
                {
                    return(new CompatibilityResult
                    {
                        Compatibility = Compatibility.UNKNOWN,
                        CompatibleVersions = new List <string>()
                    });
                }

                var compatibleVersionsToRecommend = version.FindGreaterCompatibleVersions(compatibleVersionsForTargetFramework).ToList();
                compatibleVersionsToRecommend.Sort((a, b) => NuGetVersion.Parse(a).CompareTo(NuGetVersion.Parse(b)));

                Compatibility compatibility;
                var           maxCompatibleVersion = NugetVersionHelper.GetMaxVersion(compatibleVersionsForTargetFramework);
                if (maxCompatibleVersion != null &&
                    !maxCompatibleVersion.IsZeroVersion() &&
                    version.IsGreaterThan(maxCompatibleVersion))
                {
                    compatibility = version.HasSameMajorAs(maxCompatibleVersion)
                        ? Compatibility.COMPATIBLE
                        : Compatibility.INCOMPATIBLE;
                }
                else
                {
                    compatibility = version.HasLowerOrEqualCompatibleVersion(compatibleVersionsForTargetFramework)
                        ? Compatibility.COMPATIBLE
                        : Compatibility.INCOMPATIBLE;
                }

                return(new CompatibilityResult
                {
                    Compatibility = compatibility,
                    CompatibleVersions = compatibleVersionsToRecommend
                });
            }
            catch (OutOfMemoryException e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                MemoryUtils.LogMemoryConsumption(_logger);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
            catch (Exception e)
            {
                _logger.LogError("parse package version {0} {1} with error {2}", packageVersionPair.PackageId, packageVersionPair.Version, e);
                return(new CompatibilityResult
                {
                    Compatibility = Compatibility.UNKNOWN,
                    CompatibleVersions = new List <string>()
                });
            }
        }