Exemple #1
0
        public void CanRenderProblemsToHTMLFile(string relativeSlnPath)
        {
            var slnPath  = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);

            var problemDetector = DIRegistrar.GetInstance <ProblemDetectorService>();
            var problems        = problemDetector.DetectAllSolutionProblems(solution);

            var outFile = new FileInfo("testout.html");

            if (outFile.Exists)
            {
                outFile.Delete();
            }

            var renderer = new HTMLProblemRenderer(outFile);
            var output   = new CommandOutput();

            output.DetectedProblems.AddRange(problems);

            renderer.RenderOutput(output);

            var fileText = File.ReadAllText(outFile.FullName);

            Assert.That(fileText.Contains(@"<span class='Highlighted'>Newtonsoft.Json</span>"));
        }
Exemple #2
0
        public void CanReadProjectAssemblyFilename(string relativeCsProjPath, string assemblyName)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();

            Assert.AreEqual(assemblyName, service.ExtractAssemblyName(new DotNetXMLDoc(csProjPath)));
        }
        public void CanBuildProjectGraphFromSolution(string relativeSlnPath, int expectedProjects)
        {
            var slnFile  = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnFile);
            var graph    = DIRegistrar.GetInstance <IProjectGraphBuilder>().BuildGraph(solution);

            Assert.AreEqual(expectedProjects, graph.SolutionProjects.Count);
        }
Exemple #4
0
        public void CanReadDotNetVersionOfProject(string relativeCsProjPath, string expectedVersion)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();
            var proj       = service.ReadProject(csProjPath);

            Assert.AreEqual(expectedVersion, proj.DotNetVersion.ToString());
        }
        public static SolutionGraph GetSolutionGraph(string relativeSlnPath)
        {
            var solution     = GetSolution(relativeSlnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(solution);

            return(graph);
        }
Exemple #6
0
        public void CanReadProjectReferences(string relativeCsProjPath, string expectedReference)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();
            var proj       = service.ReadProject(csProjPath);
            var reference  = proj.ProjectReferences.FirstOrDefault(rf => rf.RelativePath == expectedReference);

            Assert.IsNotNull(reference);
        }
        public void CanAdjustPackesConfigPaths(string packagesFolder, string projectDirectory, string text, string expectedModifiedText)
        {
            var packagesDir  = TestPaths.GetFolderRelativeToProjectDir(packagesFolder);
            var projectDir   = TestPaths.GetFolderRelativeToProjectDir(projectDirectory);
            var modifier     = DIRegistrar.GetInstance <NugetPackageModifier>();
            var modifiedText = modifier.CorrectPackagesPath(projectDir, packagesDir, text);

            Assert.AreEqual(expectedModifiedText, modifiedText);
        }
        public void CanFindAllProblemsWithSolution(string relativeSlnPath, int expectedNumberOfProblems)
        {
            var slnPath  = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);

            var problemDetector = DIRegistrar.GetInstance <ProblemDetectorService>();
            var problems        = problemDetector.DetectAllSolutionProblems(solution);

            Assert.AreEqual(expectedNumberOfProblems, problems.Length);
        }
Exemple #9
0
        public void CanReadFileReferences(string relativeCsProjPath, string expectedReference)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();
            var proj       = service.ReadProject(csProjPath);

            var dependency = proj.FileReferences.FirstOrDefault(fr => fr.RelativePath.EndsWith(expectedReference));

            Assert.IsNotNull(dependency);
        }
Exemple #10
0
        public void CanDetectVersionOfFileReferenceFromPath(string relativeCsProjPath, string reference, string expectedFileVersion)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();
            var proj       = service.ReadProject(csProjPath);

            var fileRef = proj.FileReferences.FirstOrDefault(r => r.Include.ID == reference);

            Assert.AreEqual(expectedFileVersion, fileRef.Version.ToString());
        }
        public void CanReadNugetReference(string relativePackagesConfigPath, string expectedReference)
        {
            var projectFolder      = TestPaths.GetFolderRelativeToProjectDir(relativePackagesConfigPath);
            var service            = DIRegistrar.GetInstance <INugetReferenceReader>();
            var packagesConfigFile = service.TryReadPackagesConfig(projectFolder);

            var dependency = packagesConfigFile.Packages.FirstOrDefault(pk => pk.ID == expectedReference);

            Assert.IsNotNull(dependency);
        }
        public void CanGetLatestNugetPackageFromSolution(string slnPath, string nugetPackage, bool includePrerelease, string expectedVersion)
        {
            var solution     = TestHelpers.GetSolutionGraph(slnPath);
            var nugetService = DIRegistrar.GetInstance <NugetPackageModifier>();

            var latestRef = nugetService.GetLatestNugetReference(nugetPackage, includePrerelease, solution);

            Assert.AreEqual(latestRef.Package.Version, latestRef.Version);

            Assert.AreEqual(expectedVersion, latestRef.Package.Version.ToString());
        }
        public void CanIdentifyInconsistentNugetPackageVersions(string slnPath)
        {
            var sln          = TestHelpers.GetSolution(slnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(sln);

            var nugetMismatchDetector = new DifferentNugetVersionsDetector();
            var problem = nugetMismatchDetector.DetectProblems(graph).First();

            Assert.AreEqual(typeof(MultipleNugetVersions), problem.GetType());
        }
        public void CanIdentifyNugetVersionMismatch(string slnPath, string project)
        {
            var sln          = TestHelpers.GetSolution(slnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(sln);
            var node         = graph.SolutionProjects.First(p => p.Project.Name == project);

            var nugetMismatchDetector = new NugetVersionMismatchDetector();
            var problem = nugetMismatchDetector.DetectProblems(node.NugetPackageRequirements[0]).First();

            Assert.AreEqual(typeof(NugetVersionMismatch), problem.GetType());
        }
        public void CanDetectSameDLLReferencedInDifferentPaths(string relativeSlnPath)
        {
            var slnPath      = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution     = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(solution);

            var detector = new FileReferencedInMultipleWaysDetector();
            var problem  = detector.DetectProblems(graph).First();

            Assert.That(problem.Description.ToString().StartsWith("The file Newtonsoft.Json is referenced in multiple paths"));
        }
Exemple #16
0
        public void AllFileReferencesHavePaths(string relativeCsProjPath)
        {
            var csProjPath = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeCsProjPath);
            var service    = DIRegistrar.GetInstance <IProjectReader>();
            var proj       = service.ReadProject(csProjPath);


            foreach (var file in proj.FileReferences)
            {
                Assert.IsNotNull(file.GetFile());
            }
        }
        public void CanDetectProjectDependingOnHigherDotNetVersion(string relativeSlnPath, string project)
        {
            var slnPath      = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution     = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(solution);
            var projectNode  = graph.AllNodes.Values.Single(p => p.Project.Name == project);

            var dependsOnHigherFrameworkDetector = new DependsOnIncompatibleFrameworkDetector();
            var problem = dependsOnHigherFrameworkDetector.DetectProblems(projectNode).First();

            Assert.AreEqual(typeof(DependsOnIncompatibleFramework), problem.GetType());
        }
        public void CanDetectFileReferenceWhereProjectReferenceIsMoreAppropriate(string relativeSlnPath, string project)
        {
            var slnPath      = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution     = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);
            var graphBuilder = DIRegistrar.GetInstance <IProjectGraphBuilder>();
            var graph        = graphBuilder.BuildGraph(solution);
            var projectNode  = graph.AllNodes.Values.Single(p => p.Project.Name == project);

            var detector = new FileReferenceInsteadOfProjectReferenceDetector();
            var problem  = detector.DetectProblems(projectNode).First();

            Assert.AreEqual("Project FirstProject has a file reference instead of a project reference to AnotherProject", problem.Description.ToString());
        }
Exemple #19
0
        static void Main(string[] args)
        {
            DIRegistrar.RegisterTypes += (services => services
                                          .AddImplementationsOf <ICommandHandler>()
                                          .AddSingleton <ProblemRendererService>()
                                          .AddSingleton <CommandLineHandler>());

            var consoleArgs = new CommandLineParser().Parse(args);
            var handler     = DIRegistrar.GetInstance <CommandLineHandler>();
            var result      = handler.ExecuteCommand(consoleArgs);

            var renderService = DIRegistrar.GetInstance <ProblemRendererService>();

            renderService.RenderResults(result, consoleArgs);
        }
Exemple #20
0
        public void DetectProblemsFromCommandLine(string slnPath, int expectedProblems)
        {
            var sln  = TestPaths.GetFileRelativeToProjectDir(slnPath);
            var args = new CommandLineArgs
                       (
                command: "detect",
                solutionFile: sln.FullName,
                outputPath: string.Empty
                       );

            var handler = DIRegistrar.GetInstance <CommandLineHandler>();
            var result  = handler.ExecuteCommand(args);

            Assert.AreEqual(expectedProblems, result.DetectedProblems.Count);
        }
Exemple #21
0
        public void CorrectProblemsFromCommandLine(string slnPath)
        {
            var sln  = TestPaths.GetFileRelativeToProjectDir(slnPath);
            var args = new CommandLineArgs
                       (
                command: "correct",
                solutionFile: sln.FullName,
                outputPath: string.Empty
                       );

            var handler = DIRegistrar.GetInstance <CommandLineHandler>();
            var result  = handler.ExecuteCommand(args);

            Assert.That(result.CorrectedProblems.Any());
        }
        public void CanChangeVersionInPackagesConfig(string solutionPath, string projectName, string packageName, string dotNetVersion, string newVersion)
        {
            var solution = TestHelpers.GetSolution(solutionPath);
            var project  = solution.Projects.FirstOrDefault(p => p.Name == projectName);
            var modifier = DIRegistrar.GetInstance <NugetPackageModifier>();

            modifier.ChangeNugetPackageVersionInPackagesConfig(
                project,
                packageName,
                DotNetVersion.TryParse(dotNetVersion),
                VersionWithSuffix.TryParse(newVersion));

            var reader         = DIRegistrar.GetInstance <INugetReferenceReader>();
            var packagesConfig = reader.TryReadPackagesConfig(project.ProjectDirectory);
            var package        = packagesConfig.Packages.FirstOrDefault(p => p.ID == packageName);

            Assert.AreEqual(newVersion, package.Version.ToString());
        }
        public void CanCorrectSolutionProblems(string slnPath)
        {
            var correctorService = DIRegistrar.GetInstance <ProblemCorrectorService>();
            var detectorService  = DIRegistrar.GetInstance <ProblemDetectorService>();



            int remainingProblemCount         = int.MaxValue;
            int previousRemainingProblemCount = 0;

            while (remainingProblemCount > 0)
            {
                var sln = TestHelpers.GetSolution(slnPath);

                if (remainingProblemCount == previousRemainingProblemCount)
                {
                    Assert.Fail("Did not solve any problems in this loop");
                }

                previousRemainingProblemCount = remainingProblemCount;

                var problems = detectorService.DetectAllSolutionProblems(sln);
                remainingProblemCount = problems.OfType <MultipleNugetVersions>().Count() +
                                        problems.OfType <NugetVersionMismatch>().Count();

                var correction = problems.Select(p => correctorService.TryCorrectProblem(p))
                                 .FirstOrDefault(p => p.Resolution != Resolution.NoActionTaken);

                if (correction == null)
                {
                    if (remainingProblemCount > 0)
                    {
                        Assert.Fail("Did not solve all problems");
                    }
                    else
                    {
                        return;
                    }
                }

                Assert.AreNotEqual(Resolution.FailedToSolve, correction.Resolution);
            }
        }
        public void CanModifyNugetPackage(string slnPath, string projectPath, string packagesConfigPath, string nugetPackage, string expectedVersion)
        {
            var packagesConfigFile = TestPaths.GetFileRelativeToProjectDir(packagesConfigPath);

            var sln = TestHelpers.GetSolutionGraph(slnPath);

            var nugetService = DIRegistrar.GetInstance <NugetPackageModifier>();
            var package      = nugetService.GetLatestNugetReference(nugetPackage, false, sln);

            var project = sln.FindProject(p => p.Project.FullPath.EndsWith(projectPath)).First().Project;

            nugetService.AddOrModifyNugetReference(package, project, sln.Solution.PackagesDirectory);


            sln = TestHelpers.GetSolutionGraph(slnPath);
            var reloadedProject = sln.FindProject(p => p.Project.FullPath.EndsWith(projectPath)).First();

            package = reloadedProject.NugetPackageRequirements.FirstOrDefault(p => p.Package.ID == nugetPackage);
            Assert.AreEqual(expectedVersion, package.Version.ToString());
        }
Exemple #25
0
        public void CanRenderProblemsToConsole(string relativeSlnPath)
        {
            var slnPath  = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);
            var solution = DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(slnPath);

            var problemDetector = DIRegistrar.GetInstance <ProblemDetectorService>();
            var problems        = problemDetector.DetectAllSolutionProblems(solution);

            var renderer = new ConsoleProblemRenderer();

            var stringBuilder = new StringBuilder();

            var output = new CommandOutput();

            output.DetectedProblems.AddRange(problems);

            using (var writer = new StringWriter(stringBuilder))
            {
                Console.SetOut(writer);
                renderer.RenderOutput(output);

                Assert.That(stringBuilder.ToString().Contains("Multiple versions of Newtonsoft.Json exist"));
            }
        }
        public static Solution GetSolution(string relativeSlnPath)
        {
            var path = TestPaths.GetVerifiedFileRelativeToProjectDir(relativeSlnPath);

            return(DIRegistrar.GetInstance <ISolutionReader>().ReadSolution(path));
        }