Ejemplo n.º 1
0
        public void ShouldThrowWhenAddingFileTwice()
        {
            var projectDetails = new ProjectDetails(@"c:\Project\", @"c:\Project\Project1.csproj");

            projectDetails.AddItems(@"c:\Project\File1.cs");

            var argumentException = Assert.Throws <ProjectDetailsException>(() => projectDetails.AddItems(@"c:\Project\File1.cs"));

            argumentException.Message.Should().Be(@"Item ""c:\Project\File1.cs"" already exists");
        }
        public void ShouldTestConsoleApp1Error()
        {
            var cloneRoot = @"C:\projects\testconsoleapp1\";

            var solutionDetails = new SolutionDetails(cloneRoot);

            var projectFile = @"C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj";
            var project     = new ProjectDetails(cloneRoot, projectFile);

            project.AddItems("Program.cs", @"Properties\AssemblyInfo.cs");
            solutionDetails.Add(project);

            var parsedBinaryLog = ParseLogs("testconsoleapp1-1error.binlog", cloneRoot);

            parsedBinaryLog.BuildMessages.ToArray().Should().BeEquivalentTo(new[]
            {
                new BuildMessage(BuildMessageLevel.Error, projectFile, "Program.cs", 13, 13, "; expected", "CS1002"),
            });

            parsedBinaryLog.SolutionDetails.Should().BeEquivalentTo(solutionDetails,
                                                                    options => options.IncludingNestedObjects().IncludingProperties());

            parsedBinaryLog.SolutionDetails.Keys.ToArray().Should().BeEquivalentTo(
                cloneRoot + @"TestConsoleApp1\TestConsoleApp1.csproj");

            parsedBinaryLog.BuildMessages.Any(message => Path.IsPathRooted(message.File)).Should().BeFalse();
        }
        public void ShouldThrowWhenBuildPathOutisdeCloneRoot()
        {
            var solutionDetails = new SolutionDetails(@"C:\projects\testconsoleapp1\");

            var project = new ProjectDetails(@"C:\projects\testconsoleapp1\", @"C:\projects\testconsoleapp1\TestConsoleApp1.sln");

            solutionDetails.Add(project);

            var projectFile = @"C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj";

            project = new ProjectDetails(@"C:\projects\testconsoleapp1\", projectFile);
            project.AddItems("Program.cs", @"Properties\AssemblyInfo.cs");
            solutionDetails.Add(project);

            var incorrectConsoleRoot = @"C:\projects\testconsoleapp2\";

            var binaryLogProcessingException = Assert.Throws <BinaryLogProcessingException>(() =>
            {
                var parsedBinaryLog = ParseLogs("testconsoleapp1-1warning.binlog", incorrectConsoleRoot);

                parsedBinaryLog.BuildMessages.ToArray().Should().BeEquivalentTo(new BuildMessage[0]);

                parsedBinaryLog.SolutionDetails.Should().BeEquivalentTo(solutionDetails, options => options.IncludingNestedObjects().IncludingProperties());
            });

            binaryLogProcessingException.Message.Should().MatchRegex($"^Error processing log. binLogPath:\'.*?testconsoleapp1-1warning.binlog\' cloneRoot:\'{incorrectConsoleRoot.Replace("\\", "\\\\")}\'$");
            binaryLogProcessingException.InnerException.Message.Should().Be($@"Project file path ""C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj"" is not a subpath of ""{incorrectConsoleRoot}""");
        }
        public void ShouldThrowWhenBuildPathOutisdeCloneRoot()
        {
            var solutionDetails = new SolutionDetails(@"C:\projects\testconsoleapp1\");

            var project = new ProjectDetails(@"C:\projects\testconsoleapp1\", @"C:\projects\testconsoleapp1\TestConsoleApp1.sln");

            solutionDetails.Add(project);

            var projectFile = @"C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj";

            project = new ProjectDetails(@"C:\projects\testconsoleapp1\", projectFile);
            project.AddItems("Program.cs", @"Properties\AssemblyInfo.cs");
            solutionDetails.Add(project);

            var projectDetailsException = Assert.Throws <ProjectDetailsException>(() =>
            {
                var parsedBinaryLog = ParseLogs("testconsoleapp1-1warning.binlog", @"C:\projects\testconsoleapp2\");

                parsedBinaryLog.BuildMessages.ToArray().Should().BeEquivalentTo(new BuildMessage[0]);

                parsedBinaryLog.SolutionDetails.Should().BeEquivalentTo(solutionDetails, options => options.IncludingNestedObjects().IncludingProperties());
            });

            projectDetailsException.Message.Should().Be(@"Project file path ""C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj"" is not a subpath of ""C:\projects\testconsoleapp2\""");
        }
        public void ShouldTestConsoleApp1CodeAnalysis()
        {
            var cloneRoot = @"C:\projects\testconsoleapp1\";

            var solutionDetails = new SolutionDetails(cloneRoot);

            var projectFile = @"C:\projects\testconsoleapp1\TestConsoleApp1\TestConsoleApp1.csproj";
            var project     = new ProjectDetails(cloneRoot, projectFile);

            project.AddItems("Program.cs", @"Properties\AssemblyInfo.cs");
            solutionDetails.Add(project);

            var parsedBinaryLog = ParseLogs("testconsoleapp1-codeanalysis.binlog", cloneRoot);

            parsedBinaryLog.BuildMessages.Any(message => Path.IsPathRooted(message.File)).Should().BeFalse();
        }
        /// <inheritdoc />
        public BuildDetails ProcessLog(string binLogPath, string cloneRoot)
        {
            try
            {
                var binLogReader = new StructuredLogger::Microsoft.Build.Logging.BinaryLogReplayEventSource();

                var solutionDetails = new SolutionDetails(cloneRoot);
                var buildDetails    = new BuildDetails(solutionDetails);
                var buildMessages   = new List <BuildMessage>();

                foreach (var record in binLogReader.ReadRecords(binLogPath))
                {
                    var buildEventArgs = record.Args;
                    if (buildEventArgs is ProjectStartedEventArgs startedEventArgs)
                    {
                        var notPresent = !solutionDetails.ContainsKey(startedEventArgs.ProjectFile);

                        var items = startedEventArgs.Items?.Cast <DictionaryEntry>()
                                    .Where(entry => (string)entry.Key == "Compile")
                                    .Select(entry => entry.Value)
                                    .Cast <ITaskItem>()
                                    .Select(item => item.ItemSpec)
                                    .ToArray();

                        if (notPresent && (items?.Any() ?? false))
                        {
                            var projectDetails = new ProjectDetails(cloneRoot, startedEventArgs.ProjectFile);
                            solutionDetails.Add(projectDetails);

                            if (items != null)
                            {
                                projectDetails.AddItems(items);
                            }
                        }
                    }

                    BuildMessage buildMessage = null;
                    if (buildEventArgs is BuildWarningEventArgs buildWarning)
                    {
                        buildMessage = CreateBuildMessage(
                            BuildMessageLevel.Warning,
                            buildWarning.ProjectFile,
                            buildWarning.File,
                            buildWarning.LineNumber,
                            buildWarning.EndLineNumber == 0 ? buildWarning.LineNumber : buildWarning.EndLineNumber,
                            buildWarning.Message,
                            buildWarning.Code,
                            buildDetails);
                    }

                    if (buildEventArgs is BuildErrorEventArgs buildError)
                    {
                        buildMessage = CreateBuildMessage(
                            BuildMessageLevel.Error,
                            buildError.ProjectFile,
                            buildError.File,
                            buildError.LineNumber,
                            buildError.EndLineNumber == 0 ? buildError.LineNumber : buildError.EndLineNumber,
                            buildError.Message,
                            buildError.Code,
                            buildDetails);
                    }

                    if (buildMessage != null)
                    {
                        buildMessages.Add(buildMessage);
                    }
                }

                var distinctBy = buildMessages
                                 .DistinctBy(message => (message.ProjectFile, message.MessageLevel, message.File, message.Code, message.Message, message.LineNumber, message.EndLineNumber))
                                 .ToArray();

                buildDetails.AddMessages(distinctBy);

                return(buildDetails);
            }
            catch (Exception ex)
            {
                throw new BinaryLogProcessingException($"Error processing log. binLogPath:'{binLogPath}' cloneRoot:'{cloneRoot}'", ex);
            }
        }
Ejemplo n.º 7
0
        public async Task Submit100To150BuildDetails()
        {
            var cloneRoot   = @"c:" + Faker.System.DirectoryPath().Replace("/", @"\");
            var projectPath = Path.Combine(cloneRoot, Faker.Lorem.Word());
            var projectFile = Path.Combine(projectPath, Faker.System.FileName("csproj"));

            var projectDetails = new ProjectDetails(cloneRoot, projectFile);

            var solutionDetails = new SolutionDetails(cloneRoot)
            {
                projectDetails
            };
            var buildDetails = new BuildDetails(solutionDetails);

            var projectCodeFiles = GenerateFileNames().Distinct().Take(Faker.Random.Int(200, 300)).ToArray();

            projectDetails.AddItems(projectCodeFiles);

            foreach (var projectCodeFile in Faker.PickRandom(projectCodeFiles, Faker.Random.Int(101, 150)))
            {
                buildDetails.AddMessage(new BuildMessage(BuildMessageLevel.Warning, projectFile, projectCodeFile,
                                                         Faker.Random.Int(2), Faker.Random.Int(2), Faker.Lorem.Sentence(), Faker.Lorem.Word()));
            }

            buildDetails.BuildMessages.Count.Should().BeGreaterThan(100);
            buildDetails.BuildMessages.Count.Should().BeLessOrEqualTo(150);

            _logger.LogInformation("Build Message Count: {0}", buildDetails.BuildMessages.Count);

            var owner      = Faker.Lorem.Word();
            var repository = Faker.Lorem.Word();
            var sha        = Faker.Random.String();

            var gitHubAppModelService = await SubmitBuild(buildDetails, owner, repository, sha);

            Received.InOrder(async() =>
            {
                await gitHubAppModelService.Received(1).GetLogAnalyzerConfigurationAsync(Arg.Is(owner),
                                                                                         Arg.Is(repository),
                                                                                         Arg.Is(sha));

                await gitHubAppModelService.Received(1).CreateCheckRunAsync(
                    Arg.Is(owner),
                    Arg.Is(repository),
                    Arg.Is(sha),
                    Arg.Is("MSBuildLog Analyzer"),
                    Arg.Is("MSBuildLog Analysis"),
                    Arg.Is(""),
                    Arg.Is(true),
                    Arg.Is <Annotation[]>(annotations => annotations.Length == 50),
                    Arg.Any <DateTimeOffset>(),
                    Arg.Any <DateTimeOffset>());

                await gitHubAppModelService.Received(1)
                .UpdateCheckRunAsync(
                    Arg.Any <long>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Is <Annotation[]>(annotations => annotations.Length == 50),
                    Arg.Any <DateTimeOffset?>(),
                    Arg.Any <DateTimeOffset?>());

                await gitHubAppModelService.Received(1)
                .UpdateCheckRunAsync(
                    Arg.Any <long>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Any <string>(),
                    Arg.Is <Annotation[]>(annotations => annotations.Length == buildDetails.BuildMessages.Count - 100),
                    Arg.Any <DateTimeOffset?>(),
                    Arg.Any <DateTimeOffset?>());
            });
        }
Ejemplo n.º 8
0
        public async Task SubmitBuildDetailsWithError()
        {
            var cloneRoot   = @"c:" + Faker.System.DirectoryPath().Replace("/", @"\");
            var projectPath = Path.Combine(cloneRoot, Faker.Lorem.Word());
            var projectFile = Path.Combine(projectPath, Faker.System.FileName("csproj"));

            var projectDetails  = new ProjectDetails(cloneRoot, projectFile);
            var projectCodeFile = Path.Combine(Faker.Lorem.Word(), Faker.System.FileName("cs"));

            projectDetails.AddItems(projectCodeFile);

            var solutionDetails = new SolutionDetails(cloneRoot)
            {
                projectDetails
            };

            var buildDetails  = new BuildDetails(solutionDetails);
            var lineNumber    = Faker.Random.Int(2);
            var endLineNumber = lineNumber + 1;
            var message       = Faker.Lorem.Sentence();
            var messageCode   = Faker.Lorem.Word();

            buildDetails.AddMessage(new BuildMessage(BuildMessageLevel.Error, projectFile, projectCodeFile,
                                                     lineNumber, endLineNumber, message, messageCode));

            var filename   = Path.Combine(projectPath, projectCodeFile).Substring(cloneRoot.Length).Replace(@"\", "/").TrimStart('/');
            var owner      = Faker.Lorem.Word();
            var repository = Faker.Lorem.Word();
            var sha        = Faker.Random.String();

            var gitHubAppModelService = await SubmitBuild(buildDetails, owner, repository, sha);

            Received.InOrder(async() =>
            {
                await gitHubAppModelService.Received(1).GetLogAnalyzerConfigurationAsync(Arg.Is(owner),
                                                                                         Arg.Is(repository),
                                                                                         Arg.Is(sha));

                await gitHubAppModelService.Received(1).CreateCheckRunAsync(
                    Arg.Is(owner),
                    Arg.Is(repository),
                    Arg.Is(sha),
                    Arg.Is("MSBuildLog Analyzer"),
                    Arg.Is("MSBuildLog Analysis"),
                    Arg.Is(""),
                    Arg.Is(false),
                    Arg.Any <Annotation[]>(),
                    Arg.Any <DateTimeOffset>(),
                    Arg.Any <DateTimeOffset>());
            });

            var arguments   = gitHubAppModelService.ReceivedCalls().Skip(1).First().GetArguments().ToArray();
            var annotations = (Annotation[])arguments[7];

            annotations.Should().BeEquivalentTo(new Annotation(
                                                    filename,
                                                    CheckWarningLevel.Failure,
                                                    messageCode,
                                                    messageCode + ": " + message,
                                                    lineNumber,
                                                    endLineNumber));
        }